def test2(): smiles, id = "OC1=C(Cl)C=CC=C1[N+]", 1 mol = Smiles.smilin(smiles) print mol.cansmiles() for atom in mol.atoms: print atom.index
def cansmiles(self) : """ affiche en canonical smiles une brique """ # sys.stderr.write("Brique.cansmiles: will call molecule.cansmiles() ...\n") a = self.molecule.cansmiles() m = Smiles.smilin("".join(re.split("[\[\]+-]","".join(re.split("H[0-9]*",a))))) return m.cansmiles()
def checkSmiles(fname): from frowns import Smiles try: fSmiles = open(fname, "r") lines = fSmiles.readlines() fSmiles.close() asmiles = Smiles.smilin(lines[0].split()[0]) return True except: return False
def test(): from frowns import Smiles import time smiles, id = "OC1=C(Cl)C=CC=C1[N+]C2CCC3CC3CCC2", 1 mol = Smiles.smilin(smiles, transforms=[]) t1 = time.time() for i in range(100): atoms, bonds = getAtomsBondsInRings(mol) print (time.time() - t1)/100
def test(): from frowns import Smiles import time smiles, id = "OC1=C(Cl)C=CC=C1[N+]C2CCC3CC3CCC2", 1 mol = Smiles.smilin(smiles, transforms=[]) assert frerejaqueNumber(mol) == 3 t1 = time.time() for i in range(100): frerejaqueNumber(mol) print (time.time() - t1)/100
def next(self): if not self.smiles: return None smile = self.smiles.pop() m = Smiles.smilin(smile) #addCoords(m) #toplevel = Toplevel(self.tk) if self.drawer: self.drawer.frame.pack_forget() drawer = self.drawer=TkMoleculeDrawer.MoleculeDrawer(self.tk, m, name=smile) drawer.pack(expand=1, fill=BOTH)
def test(): from frowns import Smiles strings = ["Cl/C(F)=C(Br)/I", #"Cl\C)(F)=C(Br)\I", <- fix parsing bug #"Cl\C(\F)=C(/Br)\I", "Cl\C(F)=C(Br)\I", "Cl\C(F)=C(Br)/I", "Cl/C(F)=C(Br)\I" ] for s in strings: print s m = Smiles.smilin(s) assignStereo(m)
def testPath(): from frowns import Smiles mol = Smiles.smilin("C1CCCC1CCCC2CCCC3CCC3CCC2") rings = {} spans = [Path(mol.atoms[0], rings)] while spans: new = [] for span in spans: new += span.next() if len(rings) == 1: break if len(rings) == 1: break spans = new print rings
def test(): from frowns import Smiles import time h = BuildMatcher() for smi in ["C", "CC", "c", "C(N)O", "[O]", "c1ccccc1", ]: matcher = compile(smi) print matcher.dump() smiles = Smiles.smilin(smi) pathset = matcher.match(smiles)
atom.hybrid = SP2 continue symbol = atom.symbol hcount = atom.valences[0] - atom.sumBondOrders() connections = len(atom.bonds) + hcount charge = atom.charge if atom.symbol in ["C", "N"] and connections == 2: charge = ANY atom.hybrid = _hybrid.get((symbol, connections, charge), "") #print atom, `atom`, hcount, atom.hybrid if __name__ == "__main__": from frowns import Smiles from frowns.perception import TrustedSmiles, sssr smiles = "CC(=O)OC1C(N(c2c(CC1c1ccc(OC)cc1)cc(cc2)Oc1ccccc1)CCN(C)C)=O" smiles = "O=C1NC(N)=NC2=C1N=CNC2" #smiles = "O=C1C=CC(=O)C=C1" print smiles mol = Smiles.smilin(smiles, [sssr.sssr]) getHybridAtoms(mol) for cycle in mol.cycles: print [(atom, atom.symbol, atom.hybrid) for atom in cycle.atoms]
atoms, bonds = toposort(atoms) rings.append((atoms, bonds)) cycles.append(Cycle(atoms, bonds)) molecule.rings = rings molecule.cycles = cycles return molecule if __name__ == "__main__": from frowns import Smiles import time data = open("../Working/frowns/test/data/NCI_small").read() count = 0 i = 0 for line in data.split("\n"): i += 1 smiles = line.split()[0] m = Smiles.smilin(smiles) t1 = time.time() m = sssr(m) t2 = time.time() count += (t2-t1) assert len(m.cycles) == len(m.rings), "failed %s in %f average"%(float(count)/i) print "average sssr", float(count)/i
print len(molecule.atoms) print connections raise rings = potentialCycles #checkRings(potentialCycles) molecule.rings = rings molecule.cycles = [Cycle(atoms, bonds) for atoms, bonds in rings] return molecule if __name__ == "__main__": from frowns import Smiles import time data = open("../Working/frowns/test/data/NCI_small").read() count = 0 i = 0 for line in data.split("\n"): i += 1 smiles = line.split()[0] m = Smiles.smilin(smiles) t1 = time.time() m = sssr(m) t2 = time.time() count += (t2 - t1) assert len(m.cycles) == len( m.rings), "failed %s in %f average" % (float(count) / i) print "average sssr", float(count) / i
from frowns import Smiles print "*"*33 print "benzene with default transforms" mol = Smiles.smilin("c1ccccc1") print mol.cansmiles() print "atoms" for atom in mol.atoms: print "\tsymbol %s hcount %s aromatic %s"%( atom.symbol, atom.hcount, atom.aromatic) print "bonds" for bond in mol.bonds: print "\tbondorder %s, bondtype %s fixed %s"%( bond.bondorder, bond.bondtype, bond.fixed) print "*"*33 print "benzene with no transforms" mol = Smiles.smilin("c1ccccc1", transforms=[]) print mol.cansmiles() print "atoms" for atom in mol.atoms: print "\tsymbol %s hcount %s aromatic %s"%( atom.symbol, atom.hcount, atom.aromatic) print "bonds" for bond in mol.bonds: print "\tbondorder %s, bondtype %s fixed %s"%( bond.bondorder, bond.bondtype, bond.fixed)
from frowns import Smiles m = Smiles.smilin("N[C@](C)(F)C(=O)O") print "initial smiles:", m.arbsmiles(isomeric=1) print "canonical smiles:", m.cansmiles(isomeric=1) print m = Smiles.smilin("[C@H](C)(F)(O)") print "initial smiles:", m.arbsmiles(isomeric=1) print "canonical smiles:", m.cansmiles(isomeric=1)
from frowns import Smiles mol = Smiles.smilin("c1ccccc1") print mol.cansmiles() print "atoms" for atom in mol.atoms: print atom.symbol, atom.hcount, atom.aromatic print "bonds" for bond in mol.bonds: print bond.bondorder, bond.bondtype
#is the coordinates of hydrogen stored somewhere in the #atom the hydrogen is attached to? ## hatom.x = ## hatom.y = ## hatom.z = #does the hcount need to be changed? ## atom.hcount -= 1 mol.add_atom(hatom) bond = Bond() mol.add_bond(bond, atom, hatom) #reset atom indices index = 0 for atom in mol.atoms: atom.index = index index += 1 mol.explicitHydrogens = 1 #change flag to let know that hydrogens are explicit in this mol return mol from frowns import Smiles m = Smiles.smilin("CCCCc1ccccc1") m.explicitHydrogens = 0 addHydrogens(m) print m.cansmiles()
def nanotube(width, height): N = width * 2 first, middle, last = make_lines(width * 2) lines = [first] for i in range(height - 1): lines.append(middle) lines.append(last) return ".".join(lines) tube = nanotube(4, 100) print tube from frowns import Smiles import time #try: # import psyco # psyco.full() #except: # print "nope" # pass Smiles.smilin("c1cccc1") t1 = time.time() mol = Smiles.smilin(tube) t2 = time.time() print mol.cansmiles() print t2 - t1
from frowns import Smiles mol = Smiles.smilin("C(\C)(C)=C(/C)(C)") print mol.cansmiles() mol = Smiles.smilin("F/C=C/F") print mol.cansmiles() for bond in mol.bonds: print bond.symbol print bond.equiv_class
from Tkinter import * from frowns.Depict.MoleculeDock import MoleculeDock from frowns import Smiles # read in a molecule smiles = [ "c1ccccc1C=O", "c1ccc2cc1cccc2", "CCN", "CCCC(=O)NNCC", "CCC(CC)([CH](OC(N)=O)C1=CC=CC=C1)C2=CC=CC=C2", "ON=C(CC1=CC=CC=C1)[CH](C#N)C2=CC=CC=C2", "C1=CC=C(C=C1)C(N=C(C2=CC=CC=C2)C3=CC=CC=C3)C4=CC=CC=C4", ] # create the moleculedock widget and place it # into a tk window tk = top = Tk() m = MoleculeDock(top, cols=4) m.pack(fill=BOTH, expand=1) for smile in smiles: mol = Smiles.smilin(smile) mol.name = smile m.addMolecule(mol) mainloop()
from frowns.perception import sssr, TrustedSmiles from frowns import Smiles s = 'c12[nH]c(c3ccc(CC)cc3)[nH0]c2[nH0]ccc1' m = Smiles.smilin(s, transforms=[sssr.sssr, TrustedSmiles.trusted_smiles]) print m.arbsmarts() for atom in m.atoms: print atom.symbol, atom.hcount, atom.explicit_hcount
_recurse(next, visited) def components(molecule): atoms = {} for atom in molecule.atoms: atoms[atom] = 1 components = [] while atoms: start = atoms.keys()[0] visited = {start:1} _recurse(start, visited) bonds = {} for atom in visited.keys(): del atoms[atom] for bond in atom.bonds: bonds[bond] = 1 components.append((visited.keys(), bonds.keys())) return components if __name__ == "__main__": from frowns import Smiles mol = Smiles.smilin("CCC.NNN") print components(mol) print mol.cansmiles()
from frowns import Smiles from frowns.perception import figueras badsmi = "C1=CC(=C2C=CC=C(N=CC34C5=C6C7=C3[Fe]456789%10%11C%12C8=C9C%10=C%11%12)C2=C1)N=CC%13%14C%15=C%16C%17=C%13[Fe]%14%15%16%17%18%19%20%21C%22C%18=C%19C%20=C%21%22" m1 = Smiles.smilin(badsmi, transforms=[figueras.sssr]) m2 = Smiles.smilin(badsmi) print len(m1.cycles) print len(m2.cycles)
from Tkinter import * from frowns.Depict.MoleculeDock import MoleculeDock from frowns import Smiles # read in a molecule smiles = ["c1ccccc1C=O", "c1ccc2cc1cccc2", "CCN", "CCCC(=O)NNCC"] # create the moleculedock widget and place it # into a tk window tk = top = Tk() m = MoleculeDock(top) m.pack(fill=BOTH, expand=1) for smile in smiles: mol = Smiles.smilin(smile) m.addMolecule(mol) mainloop()
import Components def computeMW(mol): """mol -> mw of largest fragment, mw of total molecule""" fragments = [] total = 0.0 if not mol.atoms: return 0.0, 0.0 for atoms, bonds in Components.components(mol): mw = 0.0 for atom in atoms: mw += atom.mass + atom.hcount fragments.append(mw) total += mw largest = max(fragments) return largest, total if __name__ == "__main__": from frowns import Smiles mol = Smiles.smilin("CCC.NNN.Br") print computeMW(mol)
def createFP(self, molecule): p = SplitFingerprint(self.maxdepth, self.integersPerAtoms) paths = generatePaths(molecule, maxdepth=self.maxdepth) paths.sort() for length, s in paths: p.addPath(length, s) return p if __name__ == "__main__": from frowns import Smiles mol = Smiles.smilin("CCCc1cc[nH]c1") mol2 = Smiles.smilin("c1cc[nH]c1") paths = generatePaths(mol) pathLengths = {} for p in paths: l, s = p pathLengths[l] = pathLengths.get(l, []) + [s] generator = SplitFingerprintGenerator() sp = generator.createFP(mol) sp2 = generator.createFP(mol2) assert sp in sp assert sp2 in sp
from frowns import Smiles mol = Smiles.smilin("c1ccccc1CCC1CC1") index = 0 for cycle in mol.cycles: print "cycle", index print "\t", cycle.atoms print "\t", cycle.bonds index = index + 1
from frowns.perception import sssr, TrustedSmiles from frowns import Smiles s = "c12[nH]c(c3ccc(CC)cc3)[nH0]c2[nH0]ccc1" m = Smiles.smilin(s, transforms=[sssr.sssr, TrustedSmiles.trusted_smiles]) print m.arbsmarts() for atom in m.atoms: print atom.symbol, atom.hcount, atom.explicit_hcount
def generatePaths(molecule, maxdepth=5, name_atom=name_atom, name_bond=name_bond, name_ring_atom=name_ring_atom, name_ring_bond=name_ring_bond, make_rpath=0): """(molecule, maxdepth, *name_atom, *name_bond) -> linear paths Generate all linear paths through a molecule up to maxdepth change name_atom and name_bond to name the atoms and bonds in the molecule name_atom and name_bond must return a stringable value""" paths = {} for atom in molecule.atoms: _bfswalk(atom, {atom:1}, [name_atom(atom)], [name_ring_atom(atom)], paths, 1, maxdepth, name_atom, name_bond, name_ring_atom, name_ring_bond, make_rpath) return paths.keys() if __name__ == "__main__": from frowns import Smiles mol = Smiles.smilin("CCNc1cccc1") paths = generatePaths(mol) print len(paths) print paths ONE_STRING_PER_PATH = 1 paths = generatePaths(mol) print len(paths) print paths
from frowns import Smiles print "*" * 33 print "benzene with default transforms" mol = Smiles.smilin("c1ccccc1") print mol.cansmiles() print "atoms" for atom in mol.atoms: print "\tsymbol %s hcount %s aromatic %s" % (atom.symbol, atom.hcount, atom.aromatic) print "bonds" for bond in mol.bonds: print "\tbondorder %s, bondtype %s fixed %s" % (bond.bondorder, bond.bondtype, bond.fixed) print "*" * 33 print "benzene with no transforms" mol = Smiles.smilin("c1ccccc1", transforms=[]) print mol.cansmiles() print "atoms" for atom in mol.atoms: print "\tsymbol %s hcount %s aromatic %s" % (atom.symbol, atom.hcount, atom.aromatic) print "bonds" for bond in mol.bonds: print "\tbondorder %s, bondtype %s fixed %s" % (bond.bondorder, bond.bondtype, bond.fixed)
import time from frowns import Smiles from frowns import Smarts mol = Smiles.smilin("CCCCC(C)CCCC(C)CCCCCC(C)C(C)CCCCCC(C)CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCN") pat = Smarts.compile("C*") print len(pat.match(mol)) pat2 = Smarts.compile("[#6]") t1 = time.time() print len(pat2.match(mol)) t2 = time.time() print t2-t1 t1 = time.time() print len(pat2.match(mol)) t2 = time.time() print t2-t1 print mol.vfgraph
_dfswalk(oatom, visitedAtoms, visitedBonds, nonPathBonds, path, paths, depth+1, mindepth, maxdepth) while len(path) != mark: path.pop() while len(nonPathBonds) != markNonPathBonds: nonPathBonds.pop() del visitedAtoms[oatom] del visitedBonds[bond] def generatePaths(molecule, mindepth=6, maxdepth=7): """(molecule, mindepth maxdepth) -> linear paths Generate all linear paths through a molecule from mindepth atoms to maxdepth atoms """ paths = {} for atom in molecule.atoms: _dfswalk(atom, {atom:1}, {}, [], [atom], paths, 1, mindepth, maxdepth) return paths.keys() if __name__ == "__main__": from frowns import Smiles m = Smiles.smilin("CCCNc1ccccc1") for paths in generatePaths(m): print paths
from frowns import Smiles mol = Smiles.smilin("C1CC=1") print mol.arbsmiles()
# Just some simple postive testing for smarts # negative testing is a little harder to come by from frowns import Smiles from frowns import Smarts mol = Smiles.smilin("C1CCCCC1CCN(=O)OCCN=C") # search for ring atoms matcher = Smarts.compile("[R1]") print matcher.dump() pathset = matcher.match(mol) assert pathset print pathset.atoms print pathset.bonds # search for CCN=O or CCN=C matcher = Smarts.compile("CCN=[O,C]") print matcher.dump() pathset = matcher.match(mol) assert pathset for path in pathset: print path.atoms, path.bonds # search for a wildcard matcher = Smarts.compile("*=O") print matcher.dump() pathset = matcher.match(mol) assert pathset for path in pathset:
molecule = fixBonds(molecule, pyroleLike) # add implicit hydrogens return addHydrogens(molecule, usedPyroles) if __name__ == "__main__": smiles = 'C1=CC(NC=C2)=C2C=C1' smiles = 'C1=CC(NC=C2)=C2C=C1' smiles = 'C1C=C[NH]2C=1C=CC=C2' smiles = 'C1[NH]C=CC=1CCC2C=CC=CC=2' #smiles = 'C1NC=CC=1' smiles = 'c1ccccc1' smiles = 'CC1=CC=CC2=NC=C(C)C(=C12)Cl' smiles = 'O=n1ccccc1' #smiles = 'O=N1=CC=CC=C1' from frowns import Smiles import RingDetection print smiles, mol = Smiles.smilin(smiles, transforms=[RingDetection.sssr, aromatize]) print '->', mol.cansmiles() print "atoms" for atom in mol.atoms: print "\t", atom.symbol, atom.valences, atom.hcount, atom.sumBondOrders(), atom.charge, atom.aromatic print "bonds" for bond in mol.bonds: print "\t", bond.bondtype, bond.bondorder, bond.aromatic
def test(): from frowns import Smiles import RingDetection import sssr as sssr2 import time from frowns.Canonicalization import Disambiguate, Traverse ##mol = Smiles.smilin("C12C3C4C1C5C4C3C25CCCCCC12C3C4C1C5C4C3C25") ##mol1 = Smiles.smilin("C1O[CH]1C2=CC=CC=C2", ## transforms = []) ##print mol1.cansmiles() smiles = "COC(=O)[CH]1C2CCC(CC2)[CH]1C(=O)OCC" smiles = "CC1(C)[CH]2CC[CH](CC(O)=O)[CH]1C2" smiles = "C[CH]1C(=O)O[CH]2CCN3CC=C(COC(=O)[C](C)(O)[C]1(C)O)[CH]23" #smiles = "CC1=CC(=O)C=CC1=O" smiles = "NC1=CC2=C(C=C1)C(=O)C3=C(C=CC=C3)C2=O" smiles = "COC1=CC2=C(C=C1OC)[C]34CCN5CC6=CCO[CH]7CC(=O)N2[CH]3[CH]7[CH]6C[CH]45.OS(O)(=O)=O" mol1 = Smiles.smilin(smiles, transforms = []) print mol1.cansmiles() NUM = 1 t1 = time.time() for i in range(NUM): sssr(mol1) t2 = time.time() print (t2-t1)/NUM symbols = [] for atom in mol1.atoms: symbols.append(atom.symbol) for atom in mol1.atoms: atom.symbol += ".%s."%atom.index atom.symorder = mol1.atoms.index(atom) index = 0 for cycle in mol1.cycles: print cycle.atoms print cycle.bonds for atom in cycle.atoms: atom.symbol += "<%s>"%index index += 1 print Traverse.draw(mol1)[0] for atom, symbol in zip(mol1.atoms, symbols): atom.symbol = symbol for atom in mol1.atoms: atom.symbol += ".%s."%atom.index atom.symorder = mol1.atoms.index(atom) t1 = time.time() for i in range(NUM): RingDetection.sssr(mol1) t2 = time.time() print (t2-t1)/NUM index = 0 for cycle in mol1.cycles: print cycle.atoms print cycle.bonds for atom in cycle.atoms: atom.symbol += "<%s>"%index index += 1 print Traverse.draw(mol1)[0]
from frowns import Smiles m = Smiles.smilin("[CH0]") assert m.cansmiles() == "[C]" m = Smiles.smilin("C") assert m.cansmiles() == "C"
from frowns import Smiles from frowns import Smarts from frowns.perception import sssr from frowns.perception import RingDetection ## '*' is ok mol=Smiles.smilin("SCCCCSCCCC",transforms=[RingDetection.sssr]) #pattern=Smarts.compile("S*CC") ##!! '~' has problem pattern=Smarts.compile("S~C~C") ##!! pattern can't match itself mol=Smiles.smilin("C[CH](C1=CC=CC=C1)[C](C)(C#N)C2=CC=CC=C2") pattern = Smarts.compile(mol.arbsmarts()) print mol.arbsmarts() pattern=Smarts.compile("CC(C1=CC=CC=C1)C(C)(C#N)C2=CC=CC=C2") match=pattern.match(mol) assert match index=1 for path in match: print "match",index print "\tatoms",path.atoms print "\tbond",path.bonds index=index+1
from frowns import Smiles, Smarts for smiles in ['OC1C=CC(=O)C=C1', 'c1ccccc1']: mol = Smiles.smilin(smiles) print mol.cansmiles() print "with square brackets" pattern = Smarts.compile('OC(C)[A]') match = pattern.match(mol) if match: for path in match: print path.atoms print print "without square brackets" pattern2 = Smarts.compile('O=C(C)A') match2 = pattern2.match(mol) if match2: for path2 in match2: print path2.atoms m = Smiles.smilin("c1ccccc1") p = Smarts.compile("c1ccccc1") assert p.match(m)
import sys sys.path.append("/Users/stockwel/Working") from frowns import Smiles import time from frowns.perception import RingDetection, BasicAromaticity, figueras, pysssr transforms = [figueras.sssr, BasicAromaticity.aromatize] tests = [x.split()[0] for x in open("data/NCI_small")][0:100] t1 = time.time() for smiles in tests: m = Smiles.smilin(smiles, transforms) t2 = time.time() time1 = (t2-t1)/len(tests) print "start", t1 print "end", t2 print "elapsed", t2-t1 print "number of smiles", len(tests) print "average molecule generation", time1 transforms = [pysssr.sssr, BasicAromaticity.aromatize] t1 = time.time() for smiles in tests:
import frowns.Fingerprint from frowns import Smiles patterns_and_targets = ( ("CCN", ("CCN", "CCNCC", "c1cccc1CCN")), ) for pattern, targets in patterns_and_targets: pattern_molecule = Smiles.smilin(pattern) pfp = frowns.Fingerprint.generateFingerprint(pattern_molecule) for target in targets: mol = Smiles.smilin(target) molfp = \ frowns.Fingerprint.generateFingerprint(mol) # pfp must be "in" molfp for test to pass assert pfp in molfp, "match %s is not in target %s!"%(pattern, target)
from frowns import Smiles from frowns import Smarts from frowns.perception import sssr from frowns.perception import RingDetection ## '*' is ok mol = Smiles.smilin("SCCCCSCCCC", transforms=[RingDetection.sssr]) #pattern=Smarts.compile("S*CC") ##!! '~' has problem pattern = Smarts.compile("S~C~C") ##!! pattern can't match itself mol = Smiles.smilin("C[CH](C1=CC=CC=C1)[C](C)(C#N)C2=CC=CC=C2") pattern = Smarts.compile(mol.arbsmarts()) print mol.arbsmarts() pattern = Smarts.compile("CC(C1=CC=CC=C1)C(C)(C#N)C2=CC=CC=C2") match = pattern.match(mol) assert match index = 1 for path in match: print "match", index print "\tatoms", path.atoms print "\tbond", path.bonds index = index + 1