def read_smiles( self, smiles_string, atoms_to_ignore=None): self.smiles_string = smiles_string.strip() self.structure = smiles.text_to_mol( smiles_string, calc_coords=False) if atoms_to_ignore: self.atoms_to_ignore = [self.structure.vertices[x-1] for x in atoms_to_ignore] else: self.atoms_to_ignore = []
def test_rings_in_disconnected_mol(): import smiles sc = smiles.converter() mol = smiles.text_to_mol( "c1ccccc1.c1ccccc1.c1ccccc1CC=C") mol.remove_zero_order_bonds() print mol, mol.is_connected() print [len( c) for c in mol.get_smallest_independent_cycles_e()]
def test_CIP_2(): import smiles m = smiles.text_to_mol("HC(CC)(O)C") from atom import atom a = m.atoms[1] dg = m.create_CIP_digraph(a) print dg
def test_CIP_2(): import smiles m = smiles.text_to_mol( "HC(CC)(O)C") from atom import atom a = m.atoms[1] dg = m.create_CIP_digraph( a) print dg
def test_rings_in_disconnected_mol(): import smiles sc = smiles.converter() mol = smiles.text_to_mol("c1ccccc1.c1ccccc1.c1ccccc1CC=C") mol.remove_zero_order_bonds() print mol, mol.is_connected() print[len(c) for c in mol.get_smallest_independent_cycles_e()]
def test_CIP(): import smiles m = smiles.text_to_mol( "HC(C(CCC)CCC)(C1CC1C)C2CC2") from atom import atom a = m.atoms[1] print a.is_chiral() print [m.atoms.index( x) for x in a.get_neighbors_CIP_sorted()]
def test_CIP(): import smiles m = smiles.text_to_mol("HC(C(CCC)CCC)(C1CC1C)C2CC2") from atom import atom a = m.atoms[1] print a.is_chiral() print[m.atoms.index(x) for x in a.get_neighbors_CIP_sorted()]
def test_sssr2(): import smiles string = "C1CCCC1C2CCC2" string = "C36C1C5C4C2C6C7CCC2CCC4CCC5CCC1CCC3CC7" string = "c1=%31c%25c8c%23c%19c%18c7c8c%17c%25c%29C=%21C=%31c%12c2-c%11c=%21c%20c%24c=4c%11C=6c2c9c(c5%28)c%12c1c%23c5c%19c3c%16c%18c%14c7c(c%17c%10c%20%29)c%22c%10c%24c%26c-%13c%22c%14c(c-%13c%15=C%27C=4%26)c%16c(c%15=%30)=c3c%28=C9C=%30C=6%27" string = "C12C34C5C24C135" mol = smiles.text_to_mol( string, calc_coords=False, localize_aromatic_bonds=False) print map( len, mol.get_smallest_independent_cycles())
def test_new_ring_perception(): import smiles mol = smiles.text_to_mol( "C2369C478C56C179C34C1258", calc_coords=False, localize_aromatic_bonds=False) t = time() for i, ring in enumerate( mol.get_all_cycles()): print len( ring), print print i, time()-t
def test1(self): mol = smiles.text_to_mol( "CCCO") rc = reaction.reaction_component( mol, 2) self.assertEqual( rc.stoichiometry, 2) self.assertRaises( Exception, reaction.reaction_component, mol, "x") self.assertRaises( Exception, rc._set_stoichiometry, "x") self.assertRaises( Exception, reaction.reaction_component, 2, 2) self.assertRaises( Exception, rc._set_molecule, "x")
def test1(self): mol = smiles.text_to_mol("CCCO") rc = reaction.reaction_component(mol, 2) self.assertEqual(rc.stoichiometry, 2) self.assertRaises(Exception, reaction.reaction_component, mol, "x") self.assertRaises(Exception, rc._set_stoichiometry, "x") self.assertRaises(Exception, reaction.reaction_component, 2, 2) self.assertRaises(Exception, rc._set_molecule, "x")
def _testformula(self, num): l = linear_formula.linear_formula() linear, smile, start_valency, end_valency = self.formulas[num] m1 = l.parse_text(linear, start_valency=start_valency, end_valency=end_valency) m2 = smiles.text_to_mol(smile) self.assert_(molecule.equals(m1, m2, level=3))
def read_cdml(text): """returns the last molecule for now""" doc = dom.parseString(text) #if doc.childNodes()[0].nodeName == 'svg': # path = "/svg/cdml/molecule" #else: # path = "/cdml/molecule" path = "//molecule" do_not_continue_this_mol = 0 for mol_el in dom_ext.simpleXPathSearch(doc, path): atom_id_remap = {} mol = molecule() groups = [] for atom_el in dom_ext.simpleXPathSearch(mol_el, "atom"): name = atom_el.getAttribute('name') if not name: #print "this molecule has an invalid symbol" do_not_continue_this_mol = 1 break pos = dom_ext.simpleXPathSearch(atom_el, 'point')[0] x = cm_to_float_coord(pos.getAttribute('x')) y = cm_to_float_coord(pos.getAttribute('y')) z = cm_to_float_coord(pos.getAttribute('z')) if name in PT: # its really an atom a = atom(symbol=name, charge=atom_el.getAttribute('charge') and int(atom_el.getAttribute('charge')) or 0, coords=(x, y, z)) mol.add_vertex(v=a) elif name in cdml_to_smiles: # its a known group group = smiles.text_to_mol(cdml_to_smiles[name], calc_coords=0) a = group.vertices[0] a.x = x a.y = y a.z = z mol.insert_a_graph(group) atom_id_remap[atom_el.getAttribute('id')] = a if do_not_continue_this_mol: break for bond_el in dom_ext.simpleXPathSearch(mol_el, "bond"): type = bond_el.getAttribute('type') if type[1] == u'0': # we ignore bonds with order 0 continue v1 = atom_id_remap[bond_el.getAttribute('start')] v2 = atom_id_remap[bond_el.getAttribute('end')] e = bond(order=int(type[1]), type=type[0]) mol.add_edge(v1, v2, e=e) if mol.is_connected(): # this is here to handle diborane and similar weird things yield mol else: for comp in mol.get_disconnected_subgraphs(): yield comp
def read_smiles(self, smiles_string, atoms_to_ignore=None): self.smiles_string = smiles_string.strip() self.structure = smiles.text_to_mol(smiles_string, calc_coords=False) if atoms_to_ignore: self.atoms_to_ignore = [ self.structure.vertices[x - 1] for x in atoms_to_ignore ] else: self.atoms_to_ignore = []
def is_edge_a_bridge_speed(): mol = smiles.text_to_mol("CCCC(CCC)CCCC1CC2CC1C2") t1 = time() for i in range(100): b = 0 for e in mol.edges: b += mol.is_edge_a_bridge(e) print b return time() - t1
def is_edge_a_bridge_speed(): mol = smiles.text_to_mol( "CCCC(CCC)CCCC1CC2CC1C2") t1 = time() for i in range( 100): b = 0 for e in mol.edges: b += mol.is_edge_a_bridge( e) print b return time() - t1
def read_cdml( text): """returns the last molecule for now""" doc = dom.parseString( text) #if doc.childNodes()[0].nodeName == 'svg': # path = "/svg/cdml/molecule" #else: # path = "/cdml/molecule" path = "//molecule" do_not_continue_this_mol = 0 for mol_el in dom_ext.simpleXPathSearch( doc, path): atom_id_remap = {} mol = molecule() groups = [] for atom_el in dom_ext.simpleXPathSearch( mol_el, "atom"): name = atom_el.getAttribute( 'name') if not name: #print "this molecule has an invalid symbol" do_not_continue_this_mol = 1 break pos = dom_ext.simpleXPathSearch( atom_el, 'point')[0] x = cm_to_float_coord( pos.getAttribute('x')) y = cm_to_float_coord( pos.getAttribute('y')) z = cm_to_float_coord( pos.getAttribute('z')) if name in PT: # its really an atom a = atom( symbol=name, charge=atom_el.getAttribute( 'charge') and int( atom_el.getAttribute( 'charge')) or 0, coords=( x, y, z)) mol.add_vertex( v=a) elif name in cdml_to_smiles: # its a known group group = smiles.text_to_mol( cdml_to_smiles[ name], calc_coords=0) a = group.vertices[0] a.x = x a.y = y a.z = z mol.insert_a_graph( group) atom_id_remap[ atom_el.getAttribute( 'id')] = a if do_not_continue_this_mol: break for bond_el in dom_ext.simpleXPathSearch( mol_el, "bond"): type = bond_el.getAttribute( 'type') if type[1] == u'0': # we ignore bonds with order 0 continue v1 = atom_id_remap[ bond_el.getAttribute( 'start')] v2 = atom_id_remap[ bond_el.getAttribute( 'end')] e = bond( order=int( type[1]), type=type[0]) mol.add_edge( v1, v2, e=e) if mol.is_connected(): # this is here to handle diborane and similar weird things yield mol else: for comp in mol.get_disconnected_subgraphs(): yield comp
def test_new_ring_perception(): import smiles mol = smiles.text_to_mol("C2369C478C56C179C34C1258", calc_coords=False, localize_aromatic_bonds=False) t = time() for i, ring in enumerate(mol.get_all_cycles()): print len(ring), print print i, time() - t
def test_sssr2(): import smiles string = "C1CCCC1C2CCC2" string = "C36C1C5C4C2C6C7CCC2CCC4CCC5CCC1CCC3CC7" string = "c1=%31c%25c8c%23c%19c%18c7c8c%17c%25c%29C=%21C=%31c%12c2-c%11c=%21c%20c%24c=4c%11C=6c2c9c(c5%28)c%12c1c%23c5c%19c3c%16c%18c%14c7c(c%17c%10c%20%29)c%22c%10c%24c%26c-%13c%22c%14c(c-%13c%15=C%27C=4%26)c%16c(c%15=%30)=c3c%28=C9C=%30C=6%27" string = "C12C34C5C24C135" mol = smiles.text_to_mol(string, calc_coords=False, localize_aromatic_bonds=False) print map(len, mol.get_smallest_independent_cycles())
def test_multimol_molfile2(): import molfile, smiles mols = [smiles.text_to_mol( t) for t in ["CCC(=O)[O-].[K+]"]] for mol in mols: mol.remove_zero_order_bonds() m = molfile.converter() text = m.mols_to_text( mols) f = file("multi2.mol","w") f.write( text) f.close() f = file("prase.sdf","r") for mol in m.read_text( text): print smiles.mol_to_text( mol) f.close()
def test_sssr(): import smiles string = "C1CCCC1C2CCC2" string = "C36C1C5C4C2C6C7CCC2CCC4CCC5CCC1CCC3CC7" string = "c1=%31c%25c8c%23c%19c%18c7c8c%17c%25c%29C=%21C=%31c%12c2-c%11c=%21c%20c%24c=4c%11C=6c2c9c(c5%28)c%12c1c%23c5c%19c3c%16c%18c%14c7c(c%17c%10c%20%29)c%22c%10c%24c%26c-%13c%22c%14c(c-%13c%15=C%27C=4%26)c%16c(c%15=%30)=c3c%28=C9C=%30C=6%27" string = "C12C3C2C13" string = "C12C34C5C24C135" mol = smiles.text_to_mol( string, calc_coords=False, localize_aromatic_bonds=False) print "AAA" for i,v in enumerate( mol.vertices): for rings in mol._get_smallest_cycles_for_vertex( v, to_reach=v): if rings: print map( len, rings) break
def test_multimol_molfile2(): import molfile, smiles mols = [smiles.text_to_mol(t) for t in ["CCC(=O)[O-].[K+]"]] for mol in mols: mol.remove_zero_order_bonds() m = molfile.converter() text = m.mols_to_text(mols) f = file("multi2.mol", "w") f.write(text) f.close() f = file("prase.sdf", "r") for mol in m.read_text(text): print smiles.mol_to_text(mol) f.close()
def cairo_out_test2(): import smiles #mol = smiles.text_to_mol( "COC(=O)CNC(C1=CC=CC=C1)C2=C(C=CC(=C2)Br)NC(=O)C3=CC(=CC=C3)Cl") #mol = smiles.text_to_mol( "c1nccc2c1cncn2") mol = smiles.text_to_mol( "CCC=CC(=O)C.CCCC") import cairo_out mol.normalize_bond_length( 30) mol.remove_unimportant_hydrogens() c = cairo_out.cairo_out( color_bonds=True, color_atoms=True) #, background_color=(0.4,1,0.5,0.8)) c.show_hydrogens_on_hetero = True c.font_size = 20 mols = list( mol.get_disconnected_subgraphs()) c.mols_to_cairo( mols, "test.pdf", format="pdf") c.mols_to_cairo( mols, "test.png") c.mols_to_cairo( mols, "test.svg", format="svg")
def test_sssr(): import smiles string = "C1CCCC1C2CCC2" string = "C36C1C5C4C2C6C7CCC2CCC4CCC5CCC1CCC3CC7" string = "c1=%31c%25c8c%23c%19c%18c7c8c%17c%25c%29C=%21C=%31c%12c2-c%11c=%21c%20c%24c=4c%11C=6c2c9c(c5%28)c%12c1c%23c5c%19c3c%16c%18c%14c7c(c%17c%10c%20%29)c%22c%10c%24c%26c-%13c%22c%14c(c-%13c%15=C%27C=4%26)c%16c(c%15=%30)=c3c%28=C9C=%30C=6%27" string = "C12C3C2C13" string = "C12C34C5C24C135" mol = smiles.text_to_mol(string, calc_coords=False, localize_aromatic_bonds=False) print "AAA" for i, v in enumerate(mol.vertices): for rings in mol._get_smallest_cycles_for_vertex(v, to_reach=v): if rings: print map(len, rings) break
def test_fullerene_lockdown(): import inchi import smiles from time import time t = time() #mol = inchi.text_to_mol( "InChI=1/C60/c1-2-5-6-3(1)8-12-10-4(1)9-11-7(2)17-21-13(5)23-24-14(6)22-18(8)28-20(12)30-26-16(10)15(9)25-29-19(11)27(17)37-41-31(21)33(23)43-44-34(24)32(22)42-38(28)48-40(30)46-36(26)35(25)45-39(29)47(37)55-49(41)51(43)57-52(44)50(42)56(48)59-54(46)53(45)58(55)60(57)59") string = "C12=C3C4=C5C6=C1C7=C8C9=C1C%10=C%11C(=C29)C3=C2C3=C4C4=C5C5=C9C6=C7C6=C7C8=C1C1=C8C%10=C%10C%11=C2C2=C3C3=C4C4=C5C5=C%11C%12=C(C6=C95)C7=C1C1=C%12C5=C%11C4=C3C3=C5C(=C81)C%10=C23" #string = "C12C3C2C13" string = "c1=%31c%25c8c%23c%19c%18c7c8c%17c%25c%29C=%21C=%31c%12c2-c%11c=%21c%20c%24c=4c%11C=6c2c9c(c5%28)c%12c1c%23c5c%19c3c%16c%18c%14c7c(c%17c%10c%20%29)c%22c%10c%24c%26c-%13c%22c%14c(c-%13c%15=C%27C=4%26)c%16c(c%15=%30)=c3c%28=C9C=%30C=6%27" mol = smiles.text_to_mol(string, calc_coords=True) print time() - t t = time() #print mol.get_all_cycles() print smiles.mol_to_text(mol) print time() - t print mol
def test_fullerene_lockdown(): import inchi import smiles from time import time t = time() #mol = inchi.text_to_mol( "InChI=1/C60/c1-2-5-6-3(1)8-12-10-4(1)9-11-7(2)17-21-13(5)23-24-14(6)22-18(8)28-20(12)30-26-16(10)15(9)25-29-19(11)27(17)37-41-31(21)33(23)43-44-34(24)32(22)42-38(28)48-40(30)46-36(26)35(25)45-39(29)47(37)55-49(41)51(43)57-52(44)50(42)56(48)59-54(46)53(45)58(55)60(57)59") string = "C12=C3C4=C5C6=C1C7=C8C9=C1C%10=C%11C(=C29)C3=C2C3=C4C4=C5C5=C9C6=C7C6=C7C8=C1C1=C8C%10=C%10C%11=C2C2=C3C3=C4C4=C5C5=C%11C%12=C(C6=C95)C7=C1C1=C%12C5=C%11C4=C3C3=C5C(=C81)C%10=C23" #string = "C12C3C2C13" string = "c1=%31c%25c8c%23c%19c%18c7c8c%17c%25c%29C=%21C=%31c%12c2-c%11c=%21c%20c%24c=4c%11C=6c2c9c(c5%28)c%12c1c%23c5c%19c3c%16c%18c%14c7c(c%17c%10c%20%29)c%22c%10c%24c%26c-%13c%22c%14c(c-%13c%15=C%27C=4%26)c%16c(c%15=%30)=c3c%28=C9C=%30C=6%27" mol = smiles.text_to_mol( string, calc_coords=True) print time()-t t = time() #print mol.get_all_cycles() print smiles.mol_to_text( mol) print time()-t print mol
def cairo_out_test2(): import smiles #mol = smiles.text_to_mol( "COC(=O)CNC(C1=CC=CC=C1)C2=C(C=CC(=C2)Br)NC(=O)C3=CC(=CC=C3)Cl") #mol = smiles.text_to_mol( "c1nccc2c1cncn2") mol = smiles.text_to_mol("CCC=CC(=O)C.CCCC") import cairo_out mol.normalize_bond_length(30) mol.remove_unimportant_hydrogens() c = cairo_out.cairo_out( color_bonds=True, color_atoms=True) #, background_color=(0.4,1,0.5,0.8)) c.show_hydrogens_on_hetero = True c.font_size = 20 mols = list(mol.get_disconnected_subgraphs()) c.mols_to_cairo(mols, "test.pdf", format="pdf") c.mols_to_cairo(mols, "test.png") c.mols_to_cairo(mols, "test.svg", format="svg")
def sssr(): mol = smiles.text_to_mol( sssr_smiles) t1 = time() cs = mol.get_smallest_independent_cycles_e_new() print len( cs) return time() - t1
dump = ssm.structures.get_graphviz_text_dump() f = file("dump.dot", "w") f.write(dump) f.close() print "Graphviz dump: %.1fms" % (1000 * (time.time() - t)) t = time.time() def print_tree(x, l): print l * " ", x #, x.children for ch in x.children: print_tree(ch, l + 2) #for tree in ssm._compute_search_trees(): # print_tree( tree, 0) #"c1cccc2c1cccc2 #text = "COc5ccc4c2sc(cc2nc4c5)-c(cc1nc3c6)sc1c3ccc6OC" text = 'C(=O)OCC' #text2 = 'C(=O)[O-]' mol = smiles.text_to_mol(text, calc_coords=False) #m2 = smiles.text_to_mol( text2, calc_coords=False) #print "XXXX", mol.contains_substructure( m2) subs = ssm.find_substructures_in_mol(mol) for sub in subs: print sub print "Substructure search: %.1fms" % (1000 * (time.time() - t)) t = time.time()
def _testformula(self, num): smile1, smile2, result = self.formulas[num] m1 = smiles.text_to_mol( smile1) m2 = smiles.text_to_mol( smile2) self.assert_(m1.contains_substructure(m2)==result)
def _testformula(self, num): l = linear_formula.linear_formula() linear, smile, start_valency, end_valency = self.formulas[num] m1 = l.parse_text( linear, start_valency=start_valency, end_valency=end_valency) m2 = smiles.text_to_mol( smile) self.assert_(molecule.equals(m1,m2,level=3))
def aromaticity(): mol = smiles.text_to_mol("C1=C[CH][CH]C=C1") #print [v.multiplicity for v in mol.vertices] mol.mark_aromatic_bonds() print[e.aromatic for e in mol.edges]
def _testformula(self, num): smile1, smile2, result = self.formulas[num] m1 = smiles.text_to_mol( smile1) m2 = smiles.text_to_mol( smile2) self.assert_(molecule.equals(m1,m2,level=3)==result)
#sm = "CP(c1ccccc1)(c2ccccc2)c3ccccc3" #sm = 'C1CC2C1CCCC3C2CC(CCC4)C4C3' #sm = "C\C=C/CC#CCCCC\C=C=C=C/CC" sm = "C/C(Cl)=C(\O)C" #sm = "C1CCC1C/C=C\CCCCC" #sm = "C25C1C3C5C4C2C1C34" #sm = 'C1CC2CCC1CC2' #sm = 'C1CC5(CC(C)CC5)CC(C)C12CC(CC(C(C)(C)CCCC)CCC)CC23C(CC)CC3' #sm = 'CCCC(C)(CCC)CCC(Cl)C(CCCCC)CCC(C)CCC' print "oasa::coords_generator DEMO" print "generating coords for following smiles" print " %s" % sm mol = smiles.text_to_mol( sm, calc_coords=False) import time #cg = coords_generator() t = time.time() calculate_coords( mol, force=1) print "generation time: %.3f ms" % ((time.time()-t)*1000) show_mol( mol) ################################################## # TODO # when an atom with degree 4 is created not as part of the backbone the neighbors are
def mols_to_png(mols, filename, **kw): c = cairo_out(**kw) c.mols_to_cairo(mols, filename) def mol_to_cairo(mol, filename, format, **kw): c = cairo_out(**kw) c.mol_to_cairo(mol, filename, format=format) def mols_to_cairo(mols, filename, format, **kw): c = cairo_out(**kw) c.mols_to_cairo(mols, filename, format=format) if __name__ == "__main__": import smiles mol = smiles.text_to_mol("FCCSCl", calc_coords=30) #mol.vertices[0].properties_['show_hydrogens'] = False #mol.vertices[1].properties_['show_symbol'] = False #mol.vertices[2].properties_['show_symbol'] = True mol_to_png(mol, "output.png", show_hydrogens_on_hetero=True, scaling=2) ## import inchi ## mol = inchi.text_to_mol( "1/C7H6O2/c8-7(9)6-4-2-1-3-5-6/h1-5H,(H,8,9)", include_hydrogens=False, calc_coords=30) ## mol_to_png( mol, "output.png")
def _testformula(self, num): smile1, smile2, result = self.formulas[num] m1 = smiles.text_to_mol(smile1) m2 = smiles.text_to_mol(smile2) self.assert_(m1.contains_substructure(m2) == result)
print "f**k", rad_to_deg( ang1), rad_to_deg( ang2), rad_to_deg( dang) else: #rint "good", rad_to_deg( ang1), rad_to_deg( ang2), rad_to_deg( dang) pass return gx1, gy1, gx2, gy2 if __name__ == "__main__": import smiles import time mol = smiles.text_to_mol( 'CC(C)C(C)CCCC') print [(v.x,v.y) for v in mol.vertices] opt = coords_optimizer() t = time.time() ok = opt.optimize_coords( mol, bond_length=0.9) print "converged:", ok print "%d iterations, RMS grad %f, max grad %f" % (opt.i, opt.rms_grad, opt.max_grad) print "time %.1f ms" % ((time.time() - t) * 1000) print [(v.x,v.y) for v in mol.vertices] import coords_generator
def _testformula(self, num): smile1, smile2, result = self.formulas[num] m1 = smiles.text_to_mol(smile1) m2 = smiles.text_to_mol(smile2) self.assert_(molecule.equals(m1, m2, level=3) == result)
from optparse import OptionParser op = OptionParser(usage="python %prog -c command [options] [structure file]") op.add_option( "-c", "--command", action="store", dest="command", default="test", help="command - what to do; one of 'test','update','rebuild','synonyms'") op.add_option( "-s", "--synonyms", action="store", dest="synonyms", default="", help="file containing synonyms") (options, args) = op.parse_args() if options.command == "test": print get_compounds_from_database( inchi="1/C4H10/c1-3-4-2/h3-4H2,1-2H3") print get_compounds_from_database( smiles="O") import smiles print find_molecule_in_database( smiles.text_to_mol("C1CCC=CC1")) print find_molecule_in_database( smiles.text_to_mol("c1ccccc1C")) print get_compounds_from_database( synonym="toluene") elif options.command in ('update','rebuild','synonyms'): if not options.command == 'synonyms': if len( args) >= 1: fname = args[0] else: print >> sys.stderr, "You must supply a valid filename of a file containing structures to be read." sys.exit() if options.command == 'rebuild': if os.path.exists( Config.database_file): try: os.remove( Config.database_file) except Exception, e: print >> sys.stderr, "Old database file could not be removed. Reason: %s" % e
def parse_form( self, text, start_valency=0, mol=None, reverse=False): form = text # the code itself if not mol: mol = Config.create_molecule() # create the dummy atom if start_valency: last_atom = mol.create_vertex() last_atom.valency = start_valency mol.add_vertex( last_atom) else: last_atom = None # check if there are branches in the formula if "(" not in form: # there are no subbranches chunks = re.split( "([A-Z][a-z]?[0-9]?[+-]?)", form) if reverse: chunks = reverse_chunks( chunks) for chunk in chunks: if chunk: atms = self.chunk_to_atoms( chunk, mol) if atms == None: return None last_atom = self.get_last_free_atom( mol) for a in atms: mol.add_vertex( a) if last_atom: max_val = min( last_atom.free_valency, a.free_valency, 3) if max_val <= 0 and last_atom.free_valency <= 0: if last_atom.raise_valency(): max_val = min( last_atom.free_valency, a.free_valency, 3) b = mol.create_edge() b.order = max_val mol.add_edge( last_atom, a, b) else: last_atom = a else: for chunk, count in gen_formula_fragments( form, reverse=reverse): if chunk: last_atom = self.get_last_free_atom( mol) do_linear = False # should we string the fragments rather than adding them all to the last atom if last_atom and last_atom.free_valency < count: do_linear = True for j in range( count): if chunk[0] == "!": # the form should be a smiles m = smiles.text_to_mol( chunk[1:], calc_coords=0) m.add_missing_hydrogens() hs = [v for v in m.vertices[0].neighbors if v.symbol == 'H'] m.disconnect( hs[0], m.vertices[0]) m.remove_vertex( hs[0]) smile = True else: val = last_atom and 1 or 0 m = self.parse_form( chunk, start_valency=val, mol=mol.create_graph()) smile = False if not m: return None if not last_atom: # !!! this should not happen in here mol.insert_a_graph( m) # if there are multiple chunks without a previous atom, we should create a linear fragment #last_atom = [v for v in mol.vertices if v.free_valency > 0][-1] else: if not smile: m.remove_vertex( m.vertices[0]) # remove the dummy mol.insert_a_graph( m) b = mol.create_edge() mol.add_edge( last_atom, m.vertices[0], b) if do_linear: last_atom = m.vertices[0] return mol
def sssr(): mol = smiles.text_to_mol(sssr_smiles) t1 = time() cs = mol.get_smallest_independent_cycles_e_new() print len(cs) return time() - t1
c.mol_to_cairo( mol, filename) def mols_to_png( mols, filename, **kw): c = cairo_out( **kw) c.mols_to_cairo( mols, filename) def mol_to_cairo( mol, filename, format, **kw): c = cairo_out( **kw) c.mol_to_cairo( mol, filename, format=format) def mols_to_cairo( mols, filename, format, **kw): c = cairo_out( **kw) c.mols_to_cairo( mols, filename, format=format) if __name__ == "__main__": import smiles mol = smiles.text_to_mol( "FCCSCl", calc_coords=30) #mol.vertices[0].properties_['show_hydrogens'] = False #mol.vertices[1].properties_['show_symbol'] = False #mol.vertices[2].properties_['show_symbol'] = True mol_to_png( mol, "output.png", show_hydrogens_on_hetero=True, scaling=2) ## import inchi ## mol = inchi.text_to_mol( "1/C7H6O2/c8-7(9)6-4-2-1-3-5-6/h1-5H,(H,8,9)", include_hydrogens=False, calc_coords=30) ## mol_to_png( mol, "output.png")
def aromaticity(): mol = smiles.text_to_mol( "C1=C[CH][CH]C=C1") #print [v.multiplicity for v in mol.vertices] mol.mark_aromatic_bonds() print [e.aromatic for e in mol.edges]