-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfp.py
More file actions
executable file
·60 lines (52 loc) · 1.99 KB
/
fp.py
File metadata and controls
executable file
·60 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
import numpy as np
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import MolFromSmiles
from rdkit.DataStructs import ConvertToNumpyArray
def Rdkit1DFp():
fp1dnames = []
fp1dfuncnames = []
for funcname in sorted(dir(AllChem), key=lambda x: x.endswith("AsBitVect"), reverse=True):
fpname = funcname.replace("AsBitVect", "").replace("Get", "").replace("Hashed", "").replace("Fingerprint", "")
if fpname not in fp1dnames:
if funcname.endswith("AsBitVect") and "Fingerprint" in funcname and callable(getattr(AllChem, funcname)):
fp1dnames.append(fpname)
fp1dfuncnames.append(funcname)
elif funcname.endswith("Fingerprint") and fpname not in fp1dnames:
if "erg" in funcname.lower(): # skip 2d
continue
fp1dnames.append(fpname)
fp1dfuncnames.append(funcname)
return dict(zip(fp1dnames, fp1dfuncnames))
def GetFp(funcname, smiles: str, nbits=512):
mol = MolFromSmiles(smiles)
mol = Chem.AddHs(mol)
# rdkit1d = Rdkit1DFp()
func = getattr(AllChem, funcname)
if funcname.endswith("AsBitVect"):
try:
fp = func(mol, nBits=nbits)
except:
fp = func(mol, nBits=nbits, radius=2) # morgan needs radius
else:
try:
fp = func(mol, fpSize=nbits)
except:
fp = func(mol) # MACC is fixed
res = np.array(0)
ConvertToNumpyArray(fp, res)
res = res.astype(np.uint8)
return res
def GetFpArray(smiles: [str], nbits: int, funcname: str):
if "maccs" in funcname.lower():
nbits = 167
fparray = np.zeros((len(smiles), nbits), dtype=np.uint8)
ismi = 0
for smi in smiles:
fparray[ismi] = GetFp(funcname, smi, nbits)
ismi += 1
logging.info("GetFpArray output nbits: {}".format(nbits))
# for ismi in range(len(smiles)):
# fparray[ismi] = GetFp(funcname, smiles[ismi], nbits)
return fparray