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 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 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 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 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 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 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 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 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 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)
#name: Gasteiger Partial Charges #description: The Gasteiger partial charges visualization, RDKit based #help-url: /help/domains/chem/functions/gasteiger-charges.md #language: python #tags: demo, chem, rdkit, panel #condition: true #input: string mol = COc1cccc2cc(C(=O)NCCCCN3CCN(c4cccc5nccnc54)CC3)oc21 {semType: Molecule} [Molecule, in SMILES format] #input: int contours = 10 #output: graphics charges [The Gasteiger partial charges] from rdkit import Chem from rdkit.Chem import AllChem from rdkit.Chem.Draw import SimilarityMaps mol = Chem.MolFromSmiles(mol) if mol is not None: AllChem.ComputeGasteigerCharges(mol) contribs = [ float(mol.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) for i in range(mol.GetNumAtoms()) ] charges = SimilarityMaps.GetSimilarityMapFromWeights(mol, contribs, contourLines=contours)
targetmol = Chem.MolFromSmiles( 'COc1cccc2cc(C(=O)NCCCCN3CCN(c4cccc5nccnc54)CC3)oc21') # 参考分子 refmol = Chem.MolFromSmiles('CCCN(CCCCN1CCN(c2ccccc2OC)CC1)Cc1ccc2ccccc2c1') d = Draw.MolDraw2DSVG(400, 400) d.ClearDrawing() target_mol_simi_fig, maxweight = SimilarityMaps.GetSimilarityMapForFingerprint( refmol, targetmol, lambda m, i: SimilarityMaps.GetMorganFingerprint( m, i, radius=2, fpType='bv'), draw2d=d ) print(target_mol_simi_fig) # Figure(250x250) print(maxweight) # 0.12255947497949138 d.FinishDrawing() with open('/drug_development/studyRdkit/st_rdcit/img/mol28.svg', 'w+') as outf: outf.write(d.GetDrawingText()) # 原子颜色越绿,对相似性的贡献越大。 # 或者可以用以下方法 weights = SimilarityMaps.GetAtomicWeightsForFingerprint( refmol, mol, SimilarityMaps.GetMorganFingerprint) print(['%.2f' % w for w in weights]) # ['0.11', '0.11', '0.08', '0.07', '-0.03', '0.07', '0.02'] target_mol_simi_fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, weights)
import matplotlib.pyplot as plt %matplotlib inline from rdkit.Chem import rdMolDescriptors from rdkit import Chem from rdkit.Chem import Draw from rdkit.Chem.Draw import SimilarityMaps from rdkit.Chem import Descriptors from rdkit.Chem import AllChem from rdkit.Chem.Draw import IPythonConsole IPythonConsole.ipython_useSVG = True mol = Chem.MolFromSmiles("BrC1=CC2=C(OCO2)C=C1") smi = Chem.MolToSmiles(mol) smi #weight = [0, 0.0025, 0.01, 0.038, 0.01, 0,0,0,0,0.002,0,0,0,0,0,0,0.002,0,0] #weight = [0,0,0,0,0.005,0.12,0.005,0,0,0,0,0] #weight = [0,0.002,0,0,0.05,0.09,0.002,0] weight = [0,0.005,0.005,0.005,0.005,0.005,0.005,0.03,0.01,0.03] mol = Chem.MolFromSmiles("Brc1ccc2c(c1)OCO2") #colors = ['PiYG', 'PiYG_r', 'PuOr', 'PuOr_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'coolwarm','coolwarm_r','seismic','seismic_r'] fig = SimilarityMaps.GetSimilarityMapFromWeights(mol, weight, size=(800,800), colorMap="seismic", contourLines=None, alpha=None, step=0.01, coordScale=1) # coordScale=1.2 #plt.show() plt.savefig('attention.tif', bbox_inches = 'tight')# dpi = 1200
plt.clf() bond = mol.GetBondWithIdx(0) idx1 = bond.GetBeginAtomIdx() idx2 = bond.GetEndAtomIdx() sigma = 0.5 * math.sqrt( sum([(mol._atomPs[idx1][i] - mol._atomPs[idx2][i])**2 for i in range(2)])) scale = bivariate_normal(0, 0, sigma, sigma, 0, 0) for (mol, w, normw) in zip(mols, weights, normweights): fig = SimilarityMaps.GetSimilarityMapFromWeights( mol, normw, size=size, scale=scale, sigma=sigma, colorMap=colors, contourLines=1, alpha=0, coordscale=1) # alpha hides contour lines if not args.labels: # I don't like white boxes on labels for elem in fig.axes[0].get_children(): if isinstance(elem, matplotlib.text.Text): elem.set_visible(False) plt.axis("off") # this is the code that plots the weights to the compounds. if args.weights: