def saveimg(smile, pltnm): atorvastatin = Chem.MolFromSmiles(smile) mh = Chem.AddHs(atorvastatin) rdDistGeom.EmbedMolecule(mh) _, res = rdEHTTools.RunMol(mh) static_chgs = res.GetAtomicCharges()[:atorvastatin.GetNumAtoms()] d = Draw.MolDraw2DCairo(400, 400) SimilarityMaps.GetSimilarityMapFromWeights(atorvastatin, list(static_chgs), draw2d=d) d.FinishDrawing() thing = show_png(d.GetDrawingText()) name = "http://localhost:5006/fol/static/" + pltnm + ".png" thing.save( "C:\\Users\\patil.py\\Documents\\11F-Drive\\PFastWebLocalApp\\FlavorTool\\fol\\static\\" + pltnm + ".png") p = figure(x_range=(0, 1), y_range=(0, 1), toolbar_location=None, plot_width=200, plot_height=200) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.axis.visible = False thing = WheelZoomTool() p.add_tools(thing) p.toolbar.active_scroll = thing p.image_url(url=[name], x=0, y=1, w=1, h=1) return p
def shapeParameters(): """ This function calculates the appropriate shape of im_tensor so that we can appropriately concat all image matrices to it. We need to be able to preassign the shape correctly """ # margin and other parameters. I first generate and image and get the # parameters from there. The only user parameter required is the trim # width. i = 10 smiles = data['SMILES'][i] category = data['Solubility'][i] mol = Chem.MolFromSmiles(smiles) Chem.ComputeGasteigerCharges(mol) contribs = ([float(mol.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) for i in range(mol.GetNumAtoms())]) filename = dir_path + '/figs/mol' + str(i) + '.png' fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, contribs, scale=150, colorMap='bwr', contourLines=1, size=(250, 250)) fig.savefig(filename, bbox_inches='tight') im = imageio.imread(filename) height, width, channels = im.shape trimmed_height, trimmed_width = (height - 2*trim_margin, width - 2*trim_margin) return trimmed_width, trimmed_height
def _similarityMap(ms, width=100, height=100, radius=2, fingerprint = 'morgan'): if matplotlib is None: raise ValueError('matplotlib not useable') if fingerprint=='morgan': fn = lambda x,i:SimilarityMaps.GetMorganFingerprint(x,i,radius=radius) elif fingerprint=='tt': fn = SimilarityMaps.GetAPFingerprint elif fingerprint=='ap': fn = SimilarityMaps.GetTTFingerprint fig, maxv = SimilarityMaps.GetSimilarityMapForFingerprint(ms[0], ms[1], fn, size=(width, height)) sio = StringIO.StringIO() pyplot.savefig(sio, format='png', bbox_inches='tight', dpi=100) return sio.getvalue()
def visualize_atom_attention(viz_dir: str, smiles: str, attention_weights, heads): """ Saves figures of attention maps between atoms. Note: works on a single molecule, not in batch :param viz_dir: Directory in which to save attention map figures. :param smiles: Smiles string for molecule. :param num_atoms: The number of atoms in this molecule. :param attention_weights: A num_atoms x num_atoms PyTorch FloatTensor containing attention weights. """ mol = Chem.MolFromSmiles(smiles) os.makedirs(viz_dir, exist_ok=True) # atomSum_weights=np.zeros(num_atoms) # for a in range(num_atoms): # a_weights = attention_weights[a].cpu().data.numpy() # atomSum_weights+=a_weights # Amean_weight=atomSum_weights/num_atoms # nanMean=np.nanmean(Amean_weight) for head in range(heads): attention_weights_this_head = attention_weights[:, head] mean_attention = np.mean(attention_weights_this_head) fig = SimilarityMaps.GetSimilarityMapFromWeights( mol, attention_weights_this_head - mean_attention, colorMap=matplotlib.cm.bwr) # save_path = os.path.join(smiles_viz_dir, f'atom_{a}.png') save_path = os.path.join(viz_dir, f'head_{head}.png') fig.savefig(save_path, bbox_inches='tight') plt.close(fig)
def sim_map_mf(results, query, fp_func, metric, out_file): file_counter = 1 for m in query: mol_keys = [] for i in results: if results[i]["q_num"] == m: Chem.Compute2DCoords(results[i]["mol"]) try: fig, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( query[results[i]["q_num"]]["mol"], results[i]["mol"], fp_func, metric=fpsearch.sim_dict[metric]) fig.set_figwidth(3.255) fig.set_figheight(3.255) filename = f"{i}.png" fig.savefig(filename, bbox_inches="tight") mol_keys.append(i) except: d = rdMolDraw2D.MolDraw2DCairo(600, 600) d.DrawMolecule(results[i]["mol"]) d.FinishDrawing() d.WriteDrawingText(f"{i}.png") mol_keys.append(i) pages = gen_pdf_pages(mol_keys, results) export_pdf(pages, f"{out_file}_{file_counter}.pdf") file_counter += 1
def testSimilarityMapsMolDraw2D(self): # nothing really sensible to test here, just make sure things run mol = Chem.MolFromSmiles( 'COc1cccc2cc(C(=O)NCCCCN3CCN(c4cccc5nccnc54)CC3)oc21') refmol = Chem.MolFromSmiles( 'CCCN(CCCCN1CCN(c2ccccc2OC)CC1)Cc1ccc2ccccc2c1') d = Draw.MolDraw2DSVG(400, 400) d.ClearDrawing() _, maxWeight = sm.GetSimilarityMapForFingerprint( refmol, mol, lambda m, i: sm.GetMorganFingerprint(m, i, radius=2, fpType='bv'), draw2d=d) d.FinishDrawing() with open('similarityMap1_out.svg', 'w+') as outf: outf.write(d.GetDrawingText())
def do_map(mol, fig_name=None, lab_atom=False, text=False, MapMin=0, MapMax=1): # settings import matplotlib import matplotlib.pyplot as plt from rdkit.Chem.Draw import SimilarityMaps scale = -1 # size of dots coordscale = 1 # coordinate scaling colmap = 'bwr' mol, charge, err = get_charge(mol, property_name='_GasteigerCharge', do_charge=True) if err == 1: print('Error in charge calculation') n_at = mol.GetNumAtoms() # num atoms charge = np.zeros((n_at, 1)) # init weights # coordinates and property for atom in range(n_at): charge[atom] = mol.GetAtomWithIdx(atom).GetProp('_GasteigerCharge') opts = Chem.Draw.DrawingOptions() opts.clearBackground = True opts.bgColor = (1, 1, 1) fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, charge, coordScale=coordscale, colorMap=colmap, colors='w', alpha=0, scale=scale) SimilarityMaps.Draw.MolDrawOptions.clearBackground if lab_atom is False: for elem in fig.axes[0].get_children(): if isinstance(elem, matplotlib.text.Text): elem.set_visible(False) plt.axis("off") if text is True: import matplotlib.patheffects as PathEffects for at in range(mol.GetNumAtoms()): x = mol._atomPs[at][0] y = mol._atomPs[at][1] plt.text(x, y, '%.2f' % charge[at], path_effects=[ PathEffects.withStroke(linewidth=1, foreground="blue") ]) if fig_name is not None: fig.savefig(fig_name, bbox_inches='tight') return plt.show()
def predict_smi(smi, model_name, save_fig_name): from help_tools import get_mol_feature, get_mol_A_ x = get_mol_feature(smi) A = get_mol_A_(smi) fn = model_name model.load_state_dict(torch.load(fn)) model.eval() #print(model) parm = {} for name, parameters in model.named_parameters(): #print(name) parm[name] = parameters.detach().numpy() #print(parm["W.0.weight"].shape,parm["W.0.bias"].shape,parm["embedding.weight"].shape,x.shape,parm["fc.weight"].shape) x = x.dot(parm["embedding.weight"].T) + parm["embedding.bias"] x = A.dot(x.dot(parm["W.0.weight"].T) + parm["W.0.bias"]) x = (abs(x) + x) / 2 x = A.dot(x.dot(parm["W.1.weight"].T) + parm["W.1.bias"]) x = (abs(x) + x) / 2 x = A.dot(x.dot(parm["W.2.weight"].T) + parm["W.2.bias"]) x = (abs(x) + x) / 2 x = A.dot(x.dot(parm["W.3.weight"].T) + parm["W.3.bias"]) x = (abs(x) + x) / 2 x = A.dot(x.dot(parm["W.4.weight"].T) + parm["W.4.bias"]) x = (abs(x) + x) / 2 #x = np.mean(x,0) x_f = x * parm["fc.weight"][0] + parm["fc.bias"][0] #80_128*128 + 1 x_t = x * parm["fc.weight"][1] + parm["fc.bias"][1] #80_128*128 + 1 x_f_with_w = np.sum(x_f, 1) #80 x_t_with_w = np.sum(x_t, 1) #80 x_with_w = x_t_with_w - x_f_with_w x_with_w_norm = (x_with_w - x_with_w.min(0)) / (x_with_w.max(0) - x_with_w.min(0)) - 0.5 #print("x_with_w_norm",x_with_w_norm) #from help_tools import plot_mol_with_color #plot_mol_with_color(smi,x_with_w_norm,"heat_map/"+save_fig_name+".png") print(smi, x_with_w_norm) mol = Chem.MolFromSmiles(smi) fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, x_with_w_norm) fig.savefig('descriptor_visulize2.png', bbox_inches='tight') input() #x = x.dot(parm["fc.weight"].T)+parm["fc.bias"] #print(x.shape,x) with torch.no_grad(): x = get_mol_feature(smi) x = torch.from_numpy(x).float().view(1, 80, 28) A = torch.from_numpy(A).float().view(1, 80, 80) score = model(x, A).squeeze(-1).numpy() #print(score) return score, np.where(score == np.max(score))[1]
def get_atom_pair_similarity(correct_ligand, mol_to_fix, type_fp='normal', use_features=False): type_fp = type_fp correct_ligand_fingerprint = SimilarityMaps.GetAPFingerprint( correct_ligand, nBits=4096, fpType=type_fp, ) mol_to_fix_fingerprint = SimilarityMaps.GetAPFingerprint( mol_to_fix, nBits=4096, fpType=type_fp, ) test = 1 sim_func = DataStructs.FingerprintSimilarity temp_autoinducer_TanimotoSimilarty = sim_func( correct_ligand_fingerprint, mol_to_fix_fingerprint, metric=DataStructs.TanimotoSimilarity) test = 1 curr_metric = DataStructs.TanimotoSimilarity fig, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( correct_ligand, mol_to_fix, lambda m, idx: SimilarityMaps.GetAPFingerprint( m, atomId=idx, fpType='normal'), metric=curr_metric) # print(maxweight) # fig.suptitle('test title', fontsize=20) ax = fig.gca() # ax.title plt.title('test', fontsize=30) fig.set_size_inches(7, 7) fig.set_dpi(600) fig.savefig('test_ap.png', bbox_inches='tight') test = 1
def plot_vals(mol, vals, colorMap='jet', contourLines=10): """ Plot values on a SimilarityMap :param mol: an RDKit.mol :return: SimilarityMap """ return SimilarityMaps.GetSimilarityMapFromWeights( mol, vals, colorMap=colorMap, contourLines=contourLines)
def weight_vis(smiles, weights, cm='jet', lines=10): m = Chem.MolFromSmiles(smiles) try: smi = Chem.MolToSmiles(m, kekuleSmiles=True, isomericSmiles=True, rootedAtAtom=int(n)) except: smi = Chem.MolToSmiles(m, kekuleSmiles=True, isomericSmiles=True) smi = Chem.MolToSmiles(m) aod = ast.literal_eval(m.GetProp('_smilesAtomOutputOrder')) flg = atom_flag(smi,150) exwt = [weights[i] for i in range(len(weights)) if flg[i]] fig = SimilarityMaps.GetSimilarityMapFromWeights(m, exwt, colorMap=cm, contourLines=lines) return fig
def testGithub4763(self): mol = Chem.MolFromSmiles( 'COc1cccc2cc(C(=O)NCCCCN3CCN(c4cccc5nccnc54)CC3)oc21') refmol = Chem.MolFromSmiles( 'CCCN(CCCCN1CCN(c2ccccc2OC)CC1)Cc1ccc2ccccc2c1') d = Draw.MolDraw2DSVG(400, 400) d.ClearDrawing() _, maxWeight = sm.GetSimilarityMapForFingerprint( refmol, mol, lambda m, i: sm.GetMorganFingerprint(m, i, radius=2, fpType='bv'), draw2d=d, colorMap="coolwarm") d.FinishDrawing() svg = d.GetDrawingText() with open('github4763.svg', 'w+') as outf: outf.write(svg) self.assertFalse('fill:#FBFCFB7F' in svg) self.assertTrue('fill:#DDDCDB' in svg)
def mapperfunc(mol): fig, weight = SimilarityMaps.GetSimilarityMapForModel( mol, SimilarityMaps.GetMorganFingerprint, lambda x: getProba(x, model.predict_proba), colorMap=cm.bwr) cls_result = model.predict(calc_fp_arr(mol)) fname = uuid.uuid1().hex + ".png" fig.savefig("static/tempfig/" + fname, bbox_inches="tight") return cls_result, fname
def draw_mol_comparison(self, refmol, mol): ''' Compare the structure of two molecules. The function takes the molecule names as input. ''' fig, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( refMol=self.molecules(refmol), probeMol=self.molecules(mol), fpFunction=SimilarityMaps.GetMorganFingerprint, metric=DataStructs.TanimotoSimilarity) return fig, maxweight
def _map(x): mol = molf.mol_method(x) fig, _ = SimilarityMaps.GetSimilarityMapForModel( mol, SimilarityMaps.GetMorganFingerprint, lambda y: _get_proba(y, model.predict_proba), linewidths=0, ) b64 = b64_fig(fig, dpi=72) img_src = '<img src="data:image/png;base64,{}" alt="Map" />'.format( b64) return img_src
def weighted_highlight_known(smiles, atom_list, atom_predictions, molecule_prediction, molecule_experiment, Number,\ molSize=(128,128)): mol = Chem.MolFromSmiles(smiles) note = '('+ str(Number) +') y-y\' : '+ str(round(molecule_experiment,2)) + '-' + str(round(molecule_prediction,2)) contribs = [atom_predictions[m] for m in np.argsort(atom_list)] fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, contribs, colorMap='bwr', contourLines=5, size=molSize) fig.axes[0].set_title(note) sio = StringIO() fig.savefig(sio, format="svg", bbox_inches='tight') svg = sio.getvalue() return svg
def fp_maps(results, query, fingerprint, fpradius, nbits, features, metric, out_file, multfile): if fingerprint == "ecfp" or fingerprint == "fcfp": fp_func = lambda m, idx: SimilarityMaps.GetMorganFingerprint( m, atomId=idx, radius=fpradius, fpType='bv', nBits=nbits, useFeatures=features) elif fingerprint == "rdkit": fp_func = lambda m, idx: SimilarityMaps.GetRDKFingerprint( m, atomId=idx, fpType="bv", nBits=nbits) elif fingerprint == "ap": fp_func = lambda m, idx: SimilarityMaps.GetAPFingerprint( m, atomId=idx, fpType="bv", nBits=nbits) else: fp_func = lambda m, idx: SimilarityMaps.GetTTFingerprint( m, atomId=idx, fpType="bv", nBits=nbits) if multfile: sim_map_mf(results, query, fp_func, metric, out_file) else: sim_map(results, query, fp_func, metric, out_file)
def draw_map(): """ Creates similarity maps using the provided molecules (SMILES). """ m1 = Chem.MolFromSmiles('c1ccccc1O') m2 = Chem.MolFromSmiles('c1ccccc1N') # Morgan Fingerprint (with normalization) # Can also be used with APFingerprint or TTFingerprint fig1, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( m1, m2, SimilarityMaps.GetMorganFingerprint) fig1.savefig('/path/to/similaritymap.png', bbox_inches='tight') # TT Fingerprint (with normalization) fig2, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( m1, m2, SimilarityMaps.GetTTFingerprint) fig2.savefig('/path/to/similaritymap.png', bbox_inches='tight') # Morgan Fingerprint (without normalization) weights = SimilarityMaps.GetAtomicWeightsForFingerprint( m1, m2, SimilarityMaps.GetMorganFingerprint) fig3 = SimilarityMaps.GetSimilarityMapFromWeights(m2, weights, size=(150, 150)) fig3.savefig('/path/to/similaritymap.png', bbox_inches='tight') # the degree of partial charge by using atomic charge AllChem.ComputeGasteigerCharges(m1) charges = [ float(atom.GetProp('_GasteigerCharge')) for atom in m1.GetAtoms() ] fig4 = SimilarityMaps.GetSimilarityMapFromWeights(m2, charges, size=(150, 150), scale=10) fig4.savefig('/path/to/molcharge_similaritymap.png', bbox_inches='tight')
def mapperfunc( mol ): fig, weight = SimilarityMaps.GetSimilarityMapForModel( mol, SimilarityMaps.GetMorganFingerprint, lambda x: getProba( x, cls.predict_proba), colorMap=cm.bwr ) fp = AllChem.GetMorganFingerprintAsBitVect( mol, 2 ) print(fp) arr = np.zeros((1,)) DataStructs.ConvertToNumpyArray( fp, arr ) print(arr) res = cls.predict( arr ) smi = Chem.MolToSmiles( mol ) print(smi) if res[0] == 1: fig.savefig( "res/act_"+smi+"_.png", bbox_inches = "tight" ) else: fig.savefig("res/nonact_"+smi+"_.png", bbox_inches = "tight" )
def _similarityMap(ms, params): if matplotlib is None: raise ValueError('matplotlib not useable') fp = params.get('fingerprint', 'morgan') if fp == 'morgan': rad = int(params.get('radius', 2)) fn = lambda x, i: SimilarityMaps.GetMorganFingerprint(x, i, radius=rad) elif fp == 'tt': fn = SimilarityMaps.GetAPFingerprint elif fp == 'ap': fn = SimilarityMaps.GetTTFingerprint w = int(params.get('w', 100)) h = int(params.get('h', 100)) fig, maxv = SimilarityMaps.GetSimilarityMapForFingerprint(ms[0], ms[1], fn, size=(w, h)) sio = StringIO.StringIO() pyplot.savefig(sio, format='png', bbox_inches='tight', dpi=100) return sio.getvalue()
def visualize_bond_attention(viz_dir: str, mol_graph: BatchMolGraph, attention_weights: torch.FloatTensor, depth: int): """ Saves figures of attention maps between bonds. :param viz_dir: Directory in which to save attention map figures. :param mol_graph: BatchMolGraph containing a batch of molecular graphs. :param attention_weights: A num_bonds x num_bonds PyTorch FloatTensor containing attention weights. :param depth: The current depth (i.e. message passing step). """ for i in trange(mol_graph.n_mols): smiles = mol_graph.smiles_batch[i] mol = Chem.MolFromSmiles(smiles) smiles_viz_dir = os.path.join(viz_dir, smiles) os.makedirs(smiles_viz_dir, exist_ok=True) a_start, a_size = mol_graph.a_scope[i] b_start, b_size = mol_graph.b_scope[i] for b in trange(b_start, b_start + b_size): # b = a1 --> a2 a1, a2 = mol_graph.b2a[b].item() - a_start, mol_graph.b2a[ mol_graph.b2revb[b]].item() - a_start # Convert weights from bond weights to atom weights b_weights = attention_weights[b] # num_bonds a2b = mol_graph.a2b[a_start:a_start + a_size] # restrict a2b to this molecule a_weights = index_select_ND(b_weights, a2b) # num_atoms x max_num_bonds a_weights = a_weights.sum(dim=1) # num_atoms a_weights = a_weights.cpu().data.numpy() # Plot attention weights on molecule fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, a_weights, highlightMap={ a1: (1, 1, 0), a2: (0, 1, 0) }) save_path = os.path.join( smiles_viz_dir, 'bond_{}_depth_{}.png'.format(b - b_start, depth)) fig.savefig(save_path, bbox_inches='tight') plt.close(fig)
def pred_frag(mol, model, radius=3, nBits=2048): def getProba(fp, probabilityfunc): return probabilityfunc(fp)[0][1] def fpFunction(mol,atomId=-1): fp = SimilarityMaps.GetMorganFingerprint(mol, atomId=atomId, radius=radius, nBits=nBits) return fp fig = SimilarityMaps.GetSimilarityMapForModel(mol, fpFunction, lambda x: getProba((x,), model.predict_proba), colorMap=cm.PiYG_r)
def molsToImgsToTensor(): """ This function takes all the molecules in the dataset, generates images of these molecules and daves these as png images """ # We need to preallocate the tensor in memory because append/concat is very # slow. We will have a bunch of elements at the end which we will delete # before returning the result by maintaining a counter. # %matplotlib agg trimmed_height, trimmed_width = shapeParameters() n_mols = data.shape[0] im_tensor = torch.zeros((n_mols, trimmed_height, trimmed_width, 4), dtype=torch.uint8) category_tensor = torch.zeros((n_mols, 1)) counter = 0 for i in tqdm(range(data.shape[0])): try: smiles = data['SMILES'][i] category = torch.Tensor([data['Solubility'][i]]).view(1,-1) mol = Chem.MolFromSmiles(smiles) Chem.ComputeGasteigerCharges(mol) contribs = ([float(mol.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) for i in range(mol.GetNumAtoms())]) filename = dir_path + '/figs/mol' + str(i) + '.png' fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, contribs, scale=150, colorMap='bwr', contourLines=1, size=(250, 250)) fig.savefig(filename, bbox_inches='tight') im = imageio.imread(filename) height, width, channels = im.shape im = im[trim_margin:-trim_margin, trim_margin:-trim_margin, :] im = torch.from_numpy(im).view(1, trimmed_width, trimmed_height, 4) im_tensor[counter] = im # im_tensor = torch.cat((im_tensor, im), dim=0) category_tensor[counter] = category # category_tensor = torch.cat((category_tensor, category), # dim=0) counter += 1 except: pass return (counter, im_tensor.numpy()[:counter], category_tensor.int().numpy()[:counter])
def main(): if len(sys.argv) == 3: #mol = Chem.MolFromMolFile(sys.argv[1]) mol = Chem.MolFromMol2File(sys.argv[1]) if mol is not None: Chem.SanitizeMol(mol) AllChem.Compute2DCoords(mol) contribs = [] fw = open(sys.argv[2], "r") for line in fw: v = str.split(line.strip(), " ") contribs.append(float(v[2])) fw.close() fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, contribs, colorMap='jet', contourLines=10) fig.savefig("%s.png" % sys.argv[1].replace(".mol2",""), bbox_inches='tight') else: print("Usage: %s file.mol2 file_atomcontrib.txt")
def mol_with_partial_charge(mol, supress_output=False): ''' Label each atom in mol with partial charge ''' mol = mol_with_atom_index(mol) Chem.AllChem.ComputeGasteigerCharges(mol) contribs = [ mol.GetAtomWithIdx(i).GetDoubleProp('_GasteigerCharge') for i in range(mol.GetNumAtoms()) ] if not supress_output: for ind, i in enumerate(contribs): print('Charge of atom %s is %s' % (ind, i)) fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, contribs, colorMap='jet', contourLines=10)
def sim_map(results, query, fp_func, metric, out_file): for i in results: Chem.Compute2DCoords(results[i]["mol"]) try: fig, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( query[results[i]["q_num"]]["mol"], results[i]["mol"], fp_func, metric=fpsearch.sim_dict[metric]) fig.set_figwidth(3.255) fig.set_figheight(3.255) filename = f"{i}.png" fig.savefig(filename, bbox_inches="tight") except: d = rdMolDraw2D.MolDraw2DCairo(600, 600) d.DrawMolecule(results[i]["mol"]) d.FinishDrawing() d.WriteDrawingText(f"{i}.png") pages = gen_pdf_pages(list(results.keys()), results) export_pdf(pages, out_file)
def testSimilarityMapsMolDraw2D(self): # nothing really sensible to test here, just make sure things run mol = Chem.MolFromSmiles( 'COc1cccc2cc(C(=O)NCCCCN3CCN(c4cccc5nccnc54)CC3)oc21') refmol = Chem.MolFromSmiles( 'CCCN(CCCCN1CCN(c2ccccc2OC)CC1)Cc1ccc2ccccc2c1') d = Draw.MolDraw2DSVG(400, 400) d.ClearDrawing() _, maxWeight = sm.GetSimilarityMapForFingerprint( refmol, mol, lambda m, i: sm.GetMorganFingerprint(m, i, radius=2, fpType='bv'), draw2d=d) d.FinishDrawing() with open('similarityMap1_out.svg', 'w+') as outf: outf.write(d.GetDrawingText()) # Github #2904: make sure we can provide our own colormap as a list: colors = [(0, 1, 0, 0.5), (1, 1, 1), (0, 0, 1, 0.5)] d = Draw.MolDraw2DSVG(400, 400) d.ClearDrawing() _, maxWeight = sm.GetSimilarityMapForFingerprint( refmol, mol, lambda m, i: sm.GetMorganFingerprint(m, i, radius=2, fpType='bv'), draw2d=d, colorMap=colors) d.FinishDrawing() with open('similarityMap1_out2.svg', 'w+') as outf: outf.write(d.GetDrawingText()) # Github #2904: make sure we can provide our own colormap as a matplotlib colormap: try: from matplotlib import cm d = Draw.MolDraw2DSVG(400, 400) d.ClearDrawing() _, maxWeight = sm.GetSimilarityMapForFingerprint( refmol, mol, lambda m, i: sm.GetMorganFingerprint( m, i, radius=2, fpType='bv'), draw2d=d, colorMap=cm.PiYG) d.FinishDrawing() with open('similarityMap1_out3.svg', 'w+') as outf: outf.write(d.GetDrawingText()) except ImportError: pass
def visualize_bond_attention(viz_dir: str, mol_graph: None, attention_weights: torch.FloatTensor, depth: int): """ Saves figures of attention maps between bonds. :param viz_dir: Directory in which to save attention map figures. :param mol_graph: BatchMolGraph containing a batch of molecular graphs. :param attention_weights: A num_bonds x num_bonds PyTorch FloatTensor containing attention weights. :param depth: The current depth (i.e. message passing step). """ for i in trange(mol_graph.n_mols): smiles = mol_graph.smiles_batch[i] mol = Chem.MolFromSmiles(smiles) smiles_viz_dir = os.path.join(viz_dir, smiles) os.makedirs(smiles_viz_dir, exist_ok=True) a_start, a_size = mol_graph.a_scope[i] b_start, b_size = mol_graph.b_scope[i] atomSum_weights = np.zeros(a_size) for b in trange(b_start, b_start + b_size): a1, a2 = mol_graph.b2a[b].item() - a_start, mol_graph.b2a[ mol_graph.b2revb[b]].item() - a_start b_weights = attention_weights[b] a2b = mol_graph.a2b[a_start:a_start + a_size] a_weights = index_select_ND(b_weights, a2b) a_weights = a_weights.sum(dim=1) a_weights = a_weights.cpu().data.numpy() atomSum_weights += a_weights Amean_weight = atomSum_weights / a_size nanMean = np.nanmean(Amean_weight) fig = SimilarityMaps.GetSimilarityMapFromWeights( mol, Amean_weight - nanMean, colorMap=matplotlib.cm.bwr) save_path = os.path.join(smiles_viz_dir, f'bond_{b - b_start}_depth_{depth}.png') fig.savefig(save_path, bbox_inches='tight') plt.close(fig)
def visualize_atom_attention(viz_dir: str, smiles: str, num_atoms: int, attention_weights: torch.FloatTensor): """ Saves figures of attention maps between atoms. Note: works on a single molecule, not in batch :param viz_dir: Directory in which to save attention map figures. :param smiles: Smiles string for molecule. :param num_atoms: The number of atoms in this molecule. :param attention_weights: A num_atoms x num_atoms PyTorch FloatTensor containing attention weights. """ mol = Chem.MolFromSmiles(smiles) smiles_viz_dir = os.path.join(viz_dir, smiles) os.makedirs(smiles_viz_dir, exist_ok=True) for a in trange(num_atoms): a_weights = attention_weights[a].cpu().data.numpy() # num_atoms # Plot attention weights on molecule fig = SimilarityMaps.GetSimilarityMapFromWeights( mol, a_weights, highlightMap={a: (0, 1, 0)}) save_path = os.path.join(smiles_viz_dir, f'atom_{a}.png') fig.savefig(save_path, bbox_inches='tight') plt.close(fig)
def testSimilarityMap(self): # Morgan2 BV refWeights = [0.5, 0.5, 0.5, -0.5, 0.5, 0.5] weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetMorganFingerprint(m, i, radius=2, fpType='bv')) for w, r in zip(weights, refWeights): self.assertEqual(w, r) _, maxWeight = sm.GetSimilarityMapForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetMorganFingerprint(m, i, radius=2, fpType='bv')) self.assertEqual(maxWeight, 0.5) weights, maxWeight = sm.GetStandardizedWeights(weights) self.assertEqual(maxWeight, 0.5) refWeights = [1.0, 1.0, 1.0, -1.0, 1.0, 1.0] for w, r in zip(weights, refWeights): self.assertEqual(w, r) weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetMorganFingerprint(m, i, fpType='count')) self.assertTrue(weights[3] < 0) weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetMorganFingerprint( m, i, fpType='bv', useFeatures=True)) self.assertTrue(weights[3] < 0) # hashed AP BV refWeights = [0.09523, 0.17366, 0.17366, -0.23809, 0.17366, 0.17366] weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetAPFingerprint(m, i, fpType='bv', nBits=1024)) for w, r in zip(weights, refWeights): self.assertAlmostEqual(w, r, 4) weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetAPFingerprint(m, i, fpType='normal')) self.assertTrue(weights[3] < 0) weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetAPFingerprint(m, i, fpType='hashed')) self.assertTrue(weights[3] < 0) # hashed TT BV refWeights = [0.5, 0.5, -0.16666, -0.5, -0.16666, 0.5] weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetTTFingerprint( m, i, fpType='bv', nBits=1024, nBitsPerEntry=1)) for w, r in zip(weights, refWeights): self.assertAlmostEqual(w, r, 4) weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetTTFingerprint(m, i, fpType='normal')) self.assertTrue(weights[3] < 0) weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetTTFingerprint(m, i, fpType='hashed')) self.assertTrue(weights[3] < 0) # RDK fingerprint BV refWeights = [0.42105, 0.42105, 0.42105, -0.32895, 0.42105, 0.42105] weights = sm.GetAtomicWeightsForFingerprint( self.mol1, self.mol2, lambda m, i: sm.GetRDKFingerprint( m, i, nBits=1024, nBitsPerHash=1)) for w, r in zip(weights, refWeights): self.assertAlmostEqual(w, r, 4)