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
25
26 fns = []
28 if fileName is None:
29 fileName = defaultPatternFileName
30 try:
31 inF = open(fileName,'r')
32 except IOError:
33 pass
34 else:
35 for line in inF.readlines():
36 if len(line) and line[0] != '#':
37 splitL = line.split('\t')
38 if len(splitL)>=3:
39 name = splitL[0]
40 descr = splitL[1]
41 sma = splitL[2]
42 descr=descr.replace('"','')
43 ok=1
44 try:
45 patt = Chem.MolFromSmarts(sma)
46 except:
47 ok=0
48 else:
49 if not patt or patt.GetNumAtoms()==0: ok=0
50 if not ok: raise ImportError,'Smarts %s could not be parsed'%(repr(sma))
51 fn = lambda mol,countUnique=True,pattern=patt:_CountMatches(mol,pattern,unique=countUnique)
52 fn.__doc__ = descr
53 name = name.replace('=','_')
54 name = name.replace('-','_')
55 fns.append((name,fn))
56
57 _LoadPatterns()
58 for name,fn in fns:
59 exec('%s=fn'%(name))
60 fn=None
61