1
2
3
4
5
6
7
8
9
10
11 """ functions to match a bunch of fragment descriptors from a file
12
13 No user-servicable parts inside. ;-)
14
15 """
16 import os
17 from rdkit import RDConfig
18 from rdkit import Chem
19
20
21 defaultPatternFileName = os.path.join(RDConfig.RDDataDir,'FragmentDescriptors.csv')
22
24 return len(mol.GetSubstructMatches(patt,uniquify=unique))
25
26 fns = []
28 if fileName is None:
29 fileName = defaultPatternFileName
30 try:
31 with open(fileName,'r') as inF:
32 for line in inF.readlines():
33 if len(line) and line[0] != '#':
34 splitL = line.split('\t')
35 if len(splitL)>=3:
36 name = splitL[0]
37 descr = splitL[1]
38 sma = splitL[2]
39 descr=descr.replace('"','')
40 ok=1
41 try:
42 patt = Chem.MolFromSmarts(sma)
43 except:
44 ok=0
45 else:
46 if not patt or patt.GetNumAtoms()==0: ok=0
47 if not ok: raise ImportError('Smarts %s could not be parsed'%(repr(sma)))
48 fn = lambda mol,countUnique=True,pattern=patt:_CountMatches(mol,pattern,unique=countUnique)
49 fn.__doc__ = descr
50 name = name.replace('=','_')
51 name = name.replace('-','_')
52 fns.append((name,fn))
53 except IOError:
54 pass
55
56 _LoadPatterns()
57 for name,fn in fns:
58 exec('%s=fn'%(name))
59 fn=None
60