In [1]:
Copied!
%%time
from IPython.core.display import HTML
from retropaths.molecules.molecule import Molecule
HTML('<script src="//d3js.org/d3.v3.min.js"></script>')
%%time
from IPython.core.display import HTML
from retropaths.molecules.molecule import Molecule
HTML('')
CPU times: user 343 ms, sys: 129 ms, total: 472 ms Wall time: 577 ms
Out[1]:
MOLECULE¶
Creation and drawing:¶
In [2]:
Copied!
# a molecule can be created from a smiles string
# mol = Molecule.from_smiles('Cn1cnc2n(C)c(=O)n(C)c(=O)c12')
mol = Molecule.from_smiles('CCCCCC')
mol.draw() # <- it draws the molecule
# a molecule can be created from a smiles string
# mol = Molecule.from_smiles('Cn1cnc2n(C)c(=O)n(C)c(=O)c12')
mol = Molecule.from_smiles('CCCCCC')
mol.draw() # <- it draws the molecule
Out[2]:
In [3]:
Copied!
# different drawing method, so you can see the internal graph representation.
print(mol) # <- printing the molecule shows the nodes and edges with the various attributes.
mol.draw(mode='d3')
# different drawing method, so you can see the internal graph representation.
print(mol) # <- printing the molecule shows the nodes and edges with the various attributes.
mol.draw(mode='d3')
nodes = [(0, {'neighbors': 4, 'element': 'C', 'charge': 0}), (1, {'neighbors': 4, 'element': 'C', 'charge': 0}), (2, {'neighbors': 4, 'element': 'C', 'charge': 0}), (3, {'neighbors': 4, 'element': 'C', 'charge': 0}), (4, {'neighbors': 4, 'element': 'C', 'charge': 0}), (5, {'neighbors': 4, 'element': 'C', 'charge': 0}), (6, {'neighbors': 1, 'element': 'H', 'charge': 0}), (7, {'neighbors': 1, 'element': 'H', 'charge': 0}), (8, {'neighbors': 1, 'element': 'H', 'charge': 0}), (9, {'neighbors': 1, 'element': 'H', 'charge': 0}), (10, {'neighbors': 1, 'element': 'H', 'charge': 0}), (11, {'neighbors': 1, 'element': 'H', 'charge': 0}), (12, {'neighbors': 1, 'element': 'H', 'charge': 0}), (13, {'neighbors': 1, 'element': 'H', 'charge': 0}), (14, {'neighbors': 1, 'element': 'H', 'charge': 0}), (15, {'neighbors': 1, 'element': 'H', 'charge': 0}), (16, {'neighbors': 1, 'element': 'H', 'charge': 0}), (17, {'neighbors': 1, 'element': 'H', 'charge': 0}), (18, {'neighbors': 1, 'element': 'H', 'charge': 0}), (19, {'neighbors': 1, 'element': 'H', 'charge': 0})] edges = [(0, 1, {'bond_order': 'single'}), (0, 6, {'bond_order': 'single'}), (0, 7, {'bond_order': 'single'}), (0, 8, {'bond_order': 'single'}), (1, 2, {'bond_order': 'single'}), (1, 9, {'bond_order': 'single'}), (1, 10, {'bond_order': 'single'}), (2, 3, {'bond_order': 'single'}), (2, 11, {'bond_order': 'single'}), (2, 12, {'bond_order': 'single'}), (3, 4, {'bond_order': 'single'}), (3, 13, {'bond_order': 'single'}), (3, 14, {'bond_order': 'single'}), (4, 5, {'bond_order': 'single'}), (4, 15, {'bond_order': 'single'}), (4, 16, {'bond_order': 'single'}), (5, 17, {'bond_order': 'single'}), (5, 18, {'bond_order': 'single'}), (5, 19, {'bond_order': 'single'})]
Out[3]:
In [4]:
Copied!
# molecules can be added together
mol2 = mol + mol
mol2.draw(mode='d3', percentage=0.6)
# molecules can be added together
mol2 = mol + mol
mol2.draw(mode='d3', percentage=0.6)
Out[4]:
In [5]:
Copied!
# multiplication also works
mol10 = mol * 10
mol10.draw(mode='d3', size=(1200, 1200), percentage=1) # we increase the canvas size so 10 molecules fit better
# multiplication also works
mol10 = mol * 10
mol10.draw(mode='d3', size=(1200, 1200), percentage=1) # we increase the canvas size so 10 molecules fit better
Out[5]:
List of molecules. Separation, Deduplication, Addition¶
In [6]:
Copied!
# this creates a single molecule object from a list of smiles
mol_from_list = Molecule.from_list_of_smiles_to_single_graph(['C', 'CC', 'CCC', 'C'])
mol_from_list.draw()
# this creates a single molecule object from a list of smiles
mol_from_list = Molecule.from_list_of_smiles_to_single_graph(['C', 'CC', 'CCC', 'C'])
mol_from_list.draw()
Out[6]:
In [7]:
Copied!
list_of_molecules = mol_from_list.separate_graph_in_pieces() # <- You can separate the molecule back in a list of molecules
Molecule.draw_list(list_of_molecules) # <- this staticmethod draws a list of molecules in a HTML table.
list_of_molecules = mol_from_list.separate_graph_in_pieces() # <- You can separate the molecule back in a list of molecules
Molecule.draw_list(list_of_molecules) # <- this staticmethod draws a list of molecules in a HTML table.
Out[7]:
In [8]:
Copied!
dedup_molecules = Molecule.delete_duplicates(list_of_molecules) # <- this eliminates duplicates from the list, giving unique molecules.
Molecule.draw_list(dedup_molecules) # <- draw deduplicated list
dedup_molecules = Molecule.delete_duplicates(list_of_molecules) # <- this eliminates duplicates from the list, giving unique molecules.
Molecule.draw_list(dedup_molecules) # <- draw deduplicated list
Out[8]:
In [9]:
Copied!
back_in_a_single_graph = Molecule.add_list_of_molecules(dedup_molecules) # <- this adds a list of molecule into a single graph
print(f'smiles = {back_in_a_single_graph.smiles}') # <- the smiles method works with multiple molecule graphs, too!!
back_in_a_single_graph.draw()
back_in_a_single_graph = Molecule.add_list_of_molecules(dedup_molecules) # <- this adds a list of molecule into a single graph
print(f'smiles = {back_in_a_single_graph.smiles}') # <- the smiles method works with multiple molecule graphs, too!!
back_in_a_single_graph.draw()
smiles = C.CC.CCC
Out[9]:
Molecule properties¶
In [10]:
Copied!
mol.molecular_formula
mol.molecular_formula
Out[10]:
C6H14
In [11]:
Copied!
# take a look at other various properties of the molecule object!
print(f'{mol.weight=}\n{mol.charge=}\n{mol.num_heavy_atoms=}\n{mol.bond_order=}\n{mol.atom_types=}\n{mol.neighbors_number=}\n{mol.is_template=}\n')
# take a look at other various properties of the molecule object!
print(f'{mol.weight=}\n{mol.charge=}\n{mol.num_heavy_atoms=}\n{mol.bond_order=}\n{mol.atom_types=}\n{mol.neighbors_number=}\n{mol.is_template=}\n')
mol.weight=50 mol.charge=0 mol.num_heavy_atoms=6 mol.bond_order={(0, 1): 'single', (0, 6): 'single', (0, 7): 'single', (0, 8): 'single', (1, 2): 'single', (1, 9): 'single', (1, 10): 'single', (2, 3): 'single', (2, 11): 'single', (2, 12): 'single', (3, 4): 'single', (3, 13): 'single', (3, 14): 'single', (4, 5): 'single', (4, 15): 'single', (4, 16): 'single', (5, 17): 'single', (5, 18): 'single', (5, 19): 'single'} mol.atom_types={0: 'C', 1: 'C', 2: 'C', 3: 'C', 4: 'C', 5: 'C', 6: 'H', 7: 'H', 8: 'H', 9: 'H', 10: 'H', 11: 'H', 12: 'H', 13: 'H', 14: 'H', 15: 'H', 16: 'H', 17: 'H', 18: 'H', 19: 'H'} mol.neighbors_number={0: 4, 1: 4, 2: 4, 3: 4, 4: 4, 5: 4, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 19: 1} mol.is_template=False
In [12]:
Copied!
# properties functional form
print(f'{mol.which_atoms_are_in()=}\n{mol.list_of_elements()=}\n{mol.force_smiles()=}\n{mol.is_empty()=}\n')
# properties functional form
print(f'{mol.which_atoms_are_in()=}\n{mol.list_of_elements()=}\n{mol.force_smiles()=}\n{mol.is_empty()=}\n')
mol.which_atoms_are_in()={'H', 'C'} mol.list_of_elements()=['C', 'C', 'C', 'C', 'C', 'C', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'] mol.force_smiles()='CCCCCC' mol.is_empty()=False
Debug¶
In [13]:
Copied!
# printing a molecule will give you graph internals
print(mol)
# printing a molecule will give you graph internals
print(mol)
nodes = [(0, {'neighbors': 4, 'element': 'C', 'charge': 0}), (1, {'neighbors': 4, 'element': 'C', 'charge': 0}), (2, {'neighbors': 4, 'element': 'C', 'charge': 0}), (3, {'neighbors': 4, 'element': 'C', 'charge': 0}), (4, {'neighbors': 4, 'element': 'C', 'charge': 0}), (5, {'neighbors': 4, 'element': 'C', 'charge': 0}), (6, {'neighbors': 1, 'element': 'H', 'charge': 0}), (7, {'neighbors': 1, 'element': 'H', 'charge': 0}), (8, {'neighbors': 1, 'element': 'H', 'charge': 0}), (9, {'neighbors': 1, 'element': 'H', 'charge': 0}), (10, {'neighbors': 1, 'element': 'H', 'charge': 0}), (11, {'neighbors': 1, 'element': 'H', 'charge': 0}), (12, {'neighbors': 1, 'element': 'H', 'charge': 0}), (13, {'neighbors': 1, 'element': 'H', 'charge': 0}), (14, {'neighbors': 1, 'element': 'H', 'charge': 0}), (15, {'neighbors': 1, 'element': 'H', 'charge': 0}), (16, {'neighbors': 1, 'element': 'H', 'charge': 0}), (17, {'neighbors': 1, 'element': 'H', 'charge': 0}), (18, {'neighbors': 1, 'element': 'H', 'charge': 0}), (19, {'neighbors': 1, 'element': 'H', 'charge': 0})] edges = [(0, 1, {'bond_order': 'single'}), (0, 6, {'bond_order': 'single'}), (0, 7, {'bond_order': 'single'}), (0, 8, {'bond_order': 'single'}), (1, 2, {'bond_order': 'single'}), (1, 9, {'bond_order': 'single'}), (1, 10, {'bond_order': 'single'}), (2, 3, {'bond_order': 'single'}), (2, 11, {'bond_order': 'single'}), (2, 12, {'bond_order': 'single'}), (3, 4, {'bond_order': 'single'}), (3, 13, {'bond_order': 'single'}), (3, 14, {'bond_order': 'single'}), (4, 5, {'bond_order': 'single'}), (4, 15, {'bond_order': 'single'}), (4, 16, {'bond_order': 'single'}), (5, 17, {'bond_order': 'single'}), (5, 18, {'bond_order': 'single'}), (5, 19, {'bond_order': 'single'})]
In [14]:
Copied!
# you can create the molecule object back from the printed output (copy paste here the new string if you changed smiles above)
nodes = [(0, {'neighbors': 4, 'element': 'C', 'charge': 0}), (1, {'neighbors': 1, 'element': 'H', 'charge': 0}), (2, {'neighbors': 1, 'element': 'H', 'charge': 0}), (3, {'neighbors': 1, 'element': 'H', 'charge': 0}), (4, {'neighbors': 1, 'element': 'H', 'charge': 0})]
edges = [(0, 1, {'bond_order': 'single'}), (0, 2, {'bond_order': 'single'}), (0, 3, {'bond_order': 'single'}), (0, 4, {'bond_order': 'single'})]
molx = Molecule.from_debug_string(nodes, edges)
molx.draw()
# you can create the molecule object back from the printed output (copy paste here the new string if you changed smiles above)
nodes = [(0, {'neighbors': 4, 'element': 'C', 'charge': 0}), (1, {'neighbors': 1, 'element': 'H', 'charge': 0}), (2, {'neighbors': 1, 'element': 'H', 'charge': 0}), (3, {'neighbors': 1, 'element': 'H', 'charge': 0}), (4, {'neighbors': 1, 'element': 'H', 'charge': 0})]
edges = [(0, 1, {'bond_order': 'single'}), (0, 2, {'bond_order': 'single'}), (0, 3, {'bond_order': 'single'}), (0, 4, {'bond_order': 'single'})]
molx = Molecule.from_debug_string(nodes, edges)
molx.draw()
Out[14]:
Serialization¶
In [15]:
Copied!
# This will create a SMILES.svg file in the folder
mol.to_svg()
# This will create a SMILES.svg file in the folder
mol.to_svg()