1
2
3
4
5
6 from rdkit import Chem
7 from rdkit.Chem import rdDepictor
8 from rdkit import Geometry
9
10 -def AlignDepict(mol,core,corePattern=None,acceptFailure=False):
11 """
12
13 Arguments:
14 - mol: the molecule to be aligned, this will come back
15 with a single conformer.
16 - core: a molecule with the core atoms to align to;
17 this should have a depiction.
18 - corePattern: (optional) an optional molecule to be used to
19 generate the atom mapping between the molecule
20 and the core.
21 """
22 if core and corePattern:
23 if not core.GetNumAtoms(onlyHeavy=True)==corePattern.GetNumAtoms(onlyHeavy=True):
24 raise ValueError,'When a pattern is provided, it must have the same number of atoms as the core'
25 coreMatch = core.GetSubstructMatch(corePattern)
26 if not coreMatch:
27 raise ValueError,"Core does not map to itself"
28 else:
29 coreMatch = range(core.GetNumAtoms(onlyHeavy=True))
30 if corePattern:
31 match = mol.GetSubstructMatch(corePattern)
32 else:
33 match = mol.GetSubstructMatch(core)
34
35 if not match:
36 if not acceptFailure:
37 raise ValueError,'Substructure match with core not found.'
38 else:
39 coordMap={}
40 else:
41 conf = core.GetConformer()
42 coordMap={}
43 for i,idx in enumerate(match):
44 pt3 = conf.GetAtomPosition(coreMatch[i])
45 pt2 = Geometry.Point2D(pt3.x,pt3.y)
46 coordMap[idx] = pt2
47 rdDepictor.Compute2DCoords(mol,clearConfs=True,coordMap=coordMap)
48
49 if __name__=='__main__':
50 import sys,getopt
51
54
55 args,extras = getopt.getopt(sys.argv[1:],'p:ho:',['smiles','pattern='])
56 if len(extras)!=2:
57 print >>sys.stderr,'ERROR: Not enough arguments'
58 Usage()
59 sys.exit(1)
60 patt = None
61 useSmiles = False
62 outF=None
63 for arg,val in args:
64 if arg=='-h':
65 Usage()
66 sys.exit(0)
67 elif arg=='-p' or arg=='--pattern':
68 patt = Chem.MolFromSmarts(val)
69 elif arg=='--smiles':
70 useSmiles = True
71 elif arg=='-o':
72 outF = val
73
74 if not useSmiles:
75 core = Chem.MolFromMolFile(extras[0])
76 else:
77 core = Chem.MolFromSmiles(extras[0])
78 rdDepictor.Compute2DCoords(core)
79
80 if not useSmiles:
81 mol = Chem.MolFromMolFile(extras[1])
82 else:
83 mol = Chem.MolFromSmiles(extras[1])
84
85 AlignDepict(mol,core,patt)
86
87 if outF:
88 outF = open(outF,'w+')
89 else:
90 outF = sys.stdout
91
92 print >>outF,Chem.MolToMolBlock(mol)
93