1
2
3
4
5 from __future__ import print_function
6
7 import argparse
8 import sys
9
10 from rdkit import Chem
11 from rdkit import Geometry
12 from rdkit.Chem import rdDepictor
13
14
15 -def AlignDepict(mol, core, corePattern=None, acceptFailure=False):
16 """
17 Arguments:
18 - mol: the molecule to be aligned, this will come back
19 with a single conformer.
20 - core: a molecule with the core atoms to align to;
21 this should have a depiction.
22 - corePattern: (optional) an optional molecule to be used to
23 generate the atom mapping between the molecule
24 and the core.
25 """
26 if core and corePattern:
27 if not core.GetNumAtoms(onlyExplicit=True) == corePattern.GetNumAtoms(onlyExplicit=True):
28 raise ValueError(
29 'When a pattern is provided, it must have the same number of atoms as the core')
30 coreMatch = core.GetSubstructMatch(corePattern)
31 if not coreMatch:
32 raise ValueError("Core does not map to itself")
33 else:
34 coreMatch = list(range(core.GetNumAtoms(onlyExplicit=True)))
35 if corePattern:
36 match = mol.GetSubstructMatch(corePattern)
37 else:
38 match = mol.GetSubstructMatch(core)
39
40 if not match:
41 if not acceptFailure:
42 raise ValueError('Substructure match with core not found.')
43 else:
44 coordMap = {}
45 else:
46 conf = core.GetConformer()
47 coordMap = {}
48 for i, idx in enumerate(match):
49 pt3 = conf.GetAtomPosition(coreMatch[i])
50 pt2 = Geometry.Point2D(pt3.x, pt3.y)
51 coordMap[idx] = pt2
52 rdDepictor.Compute2DCoords(mol, clearConfs=True, coordMap=coordMap, canonOrient=False)
53
54
56 """ Initialize the parser """
57 parser = argparse.ArgumentParser(description='Create aligned depiction')
58 parser.add_argument('--pattern', '-p', metavar='SMARTS', default=None, dest='patt')
59 parser.add_argument('--smiles', default=False, action='store_true', dest='useSmiles',
60 help='Set if core and input are SMILES strings')
61 parser.add_argument('-o', dest='outF', type=argparse.FileType('w'), default=sys.stdout,
62 metavar='OUTFILE',
63 help='Specify a file to take the output. If missing, uses stdout.')
64 parser.add_argument('core', metavar='core')
65 parser.add_argument('mol', metavar='molecule', help='')
66 return parser
67
68
70 patt = args.patt
71 if patt:
72 patt = Chem.MolFromSmarts(patt)
73
74 if args.useSmiles:
75 core = Chem.MolFromSmiles(args.core)
76 mol = Chem.MolFromSmiles(args.mol)
77 rdDepictor.Compute2DCoords(core)
78 else:
79 core = Chem.MolFromMolFile(args.core)
80 mol = Chem.MolFromMolFile(args.mol)
81
82 AlignDepict(mol, core, patt)
83 print(Chem.MolToMolBlock(mol), file=args.outF)
84
85
87 """ Main application """
88 parser = initParser()
89 args = parser.parse_args()
90 processArgs(args)
91
92
93 if __name__ == '__main__':
94 main()
95