import os
import sys
import csv
from rdkit import Chem
from rdkit.Chem import RDConfig
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import Draw
print(Chem.rdBase.rdkitVersion)
2021.09.11
fwpath = os.path.join(RDConfig.RDContribDir, 'FreeWilson')
datapath = os.path.join(fwpath, 'data')
sys.path.append(fwpath)
smifile = os.path.join(datapath, "CHEMBL2321810.smi")
scaffoldfile = os.path.join(datapath, "CHEMBL2321810_scaffold.mol")
csvfile = os.path.join(datapath, "CHEMBL2321810_act.csv")
mols = []
for line in open(smifile):
    smiles, name = line.strip().split()
    m = Chem.MolFromSmiles(smiles)
    m.SetProp("_Name", name)
    Draw.rdDepictor.Compute2DCoords(m)
    mols.append(m)
print(len(mols))
1017
Draw.MolsToGridImage(mols[:10], molsPerRow=5)
scaffold = Chem.MolFromMolFile(scaffoldfile)
scaffold
data = {k:float(v) for k,v in list(csv.reader(open(csvfile)))[1:]}
scores = [data[m.GetProp("_Name")] for m in mols]
print(scores[:10])
[5.48, 5.48, 5.5, 5.5, 5.5, 5.51, 5.51, 5.51, 5.51, 5.51]
from freewilson import FWDecompose, FWBuild, predictions_to_csv
decomp = FWDecompose(scaffold, mols, scores)
0it [00:00, ?it/s]RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
159it [00:00, 1588.98it/s]RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
325it [00:00, 1629.85it/s]RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:50] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
503it [00:00, 1697.94it/s]RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
695it [00:00, 1784.21it/s]RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
907it [00:00, 1903.96it/s]RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
RDKit DEBUG: [20:24:51] No core matches
1017it [00:00, 1756.09it/s]
RDKit INFO: [20:24:51] GA RNG seed 5489
RDKit WARNING: [20:24:51] WARNING: not removing hydrogen atom with dummy atom neighbors
print(f"Training R^2 is {decomp.r2:0.2f}")
Training R^2 is 0.81
from IPython.display import display
pred_mols = []
pred_vals = []
for pred in FWBuild(decomp):
    m = Chem.MolFromSmiles(pred.smiles)
    pred_v = pred.prediction
    pred_vals.append(pred_v)
    pred_mols.append(m)
100%|██████████| 64970/64970 [00:41<00:00, 1563.33it/s]
Draw.MolsToGridImage(pred_mols[:10],
                     legends=[f"{v:.2f}" for v in pred_vals][:10], 
                    molsPerRow=5)
print(list(decomp.rgroups.keys()))
['Core', 'R1', 'R10', 'R3']
display(Chem.MolFromSmiles(decomp.rgroups['Core'][0].smiles))
Draw.MolsToGridImage([Chem.MolFromSmiles(data.smiles) for data in decomp.rgroups['R1']][:10], molsPerRow=5)
Draw.MolsToGridImage([Chem.MolFromSmiles(data.smiles) for data in decomp.rgroups['R3']][:10], molsPerRow=5)
Draw.MolsToGridImage([Chem.MolFromSmiles(data.smiles) for data in decomp.rgroups['R10']][:10], molsPerRow=5)
RDKit WARNING: [20:33:12] WARNING: not removing hydrogen atom with dummy atom neighbors