コード例 #1
0
    def draw(self):
        """
        This function plots the NetworkX graph by using Matplotlib
        
        """

        logging.info('\nDrawing....')

        if nx.number_of_nodes(self.resultGraph) > self.max_nodes:
            logging.info('The number of generated graph nodes %d exceed the max number of drawable nodes %s' % (
            nx.number_of_nodes(self.resultGraph), self.max_nodes))
            return

        def max_dist_mol(mol):

            max_dist = 0.0
            conf = mol.GetConformer()

            for i in range(0, conf.GetNumAtoms()):

                crdi = np.array([conf.GetAtomPosition(i).x, conf.GetAtomPosition(i).y, conf.GetAtomPosition(i).z])

                for j in range(i + 1, conf.GetNumAtoms()):
                    crdj = np.array([conf.GetAtomPosition(j).x, conf.GetAtomPosition(i).y, conf.GetAtomPosition(j).z])
                    dist = np.linalg.norm(crdi - crdj)

                    if dist > max_dist:
                        max_dist = dist

            return max_dist

        # Determine the screen resolution by using dxpyinfo and removing massive qt dependency
        command = ('xdpyinfo | grep dimensions')
        p = subprocess.run(command, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
        width = int(p.stdout.split()[1].split(b'x')[0])
        height = int(p.stdout.split()[1].split(b'x')[1])

        # Canvas scale factor 
        scale_canvas = 0.75

        # Canvas resolution
        max_canvas_size = (int(width * scale_canvas), int(height * scale_canvas))

        fig = plt.figure(1, facecolor='white')

        fig.set_dpi(100)

        fig.set_size_inches(max_canvas_size[0] / fig.get_dpi(), max_canvas_size[1] / fig.get_dpi(), forward=True)

        ax = plt.subplot(111)
        plt.axis('off')

        pos = nx.nx_agraph.graphviz_layout(self.resultGraph, prog="neato")

        strict_edges = [(u, v) for (u, v, d) in self.resultGraph.edges(data=True) if d['strict_flag'] == True]
        loose_edges = [(u, v) for (u, v, d) in self.resultGraph.edges(data=True) if d['strict_flag'] == False]

        node_labels = dict([(u, d['ID']) for u, d in self.resultGraph.nodes(data=True)])

        # Draw nodes
        nx.draw_networkx_nodes(self.resultGraph, pos, node_size=500, node_color='r')
        # Draw node labels
        nx.draw_networkx_labels(self.resultGraph, pos, labels=node_labels, font_size=10)

        if self.edge_labels:
            edge_weight_strict = dict([((u, v,), d['similarity']) for u, v, d in self.resultGraph.edges(data=True) if
                                       d['strict_flag'] == True])
            edge_weight_loose = dict([((u, v,), d['similarity']) for u, v, d in self.resultGraph.edges(data=True) if
                                      d['strict_flag'] == False])

            for key in edge_weight_strict:
                edge_weight_strict[key] = round(edge_weight_strict[key], 2)

            for key in edge_weight_loose:
                edge_weight_loose[key] = round(edge_weight_loose[key], 2)

            # edge strict
            nx.draw_networkx_edge_labels(self.resultGraph, pos, edge_labels=edge_weight_strict, font_color='g')
            # edge loose
            nx.draw_networkx_edge_labels(self.resultGraph, pos, edge_labels=edge_weight_loose, font_color='r')

        # edges strict
        nx.draw_networkx_edges(self.resultGraph, pos, edgelist=strict_edges, edge_color='g')
        # edges loose
        nx.draw_networkx_edges(self.resultGraph, pos, edgelist=loose_edges, edge_color='r')

        if nx.number_of_nodes(self.resultGraph) <= self.max_images:

            trans = ax.transData.transform
            trans2 = fig.transFigure.inverted().transform

            cut = 1.0

            frame = 10
            xmax = cut * max(xx for xx, yy in pos.values()) + frame
            ymax = cut * max(yy for xx, yy in pos.values()) + frame

            xmin = cut * min(xx for xx, yy in pos.values()) - frame
            ymin = cut * min(yy for xx, yy in pos.values()) - frame

            plt.xlim(xmin, xmax)
            plt.ylim(ymin, ymax)

            h = 20
            w = 20

            mol_size = (200, 200)

            for each_node in self.resultGraph:

                id_mol = self.resultGraph.node[each_node]['ID']
                # skip remove Hs by rdkit if Hs cannot be removed
                try:
                    mol = AllChem.RemoveHs(self.dbase[id_mol].getMolecule())
                except:
                    ###### need to ask RDKit to fix this if possible, see the code
                    # issue tracker for more details######
                    mol = self.dbase[id_mol].getMolecule()
                    logging.info(
                        "Error attempting to remove hydrogens for molecule %s using RDKit. RDKit cannot kekulize the molecule" %
                        self.dbase[id_mol].getName())

                # max_dist = max_dist_mol(mol)
                # if max_dist > 7.0:
                #     continue

                AllChem.Compute2DCoords(mol)
                # add try exception for cases cannot be draw
                try:
                    img_mol = Draw.MolToImage(mol, mol_size, kekulize=False)
                except Exception as ex:
                    img_mol = None
                    logging.exception(
                        "This mol cannot be draw using the RDKit Draw function, need to check for more details...")

                xx, yy = trans(pos[each_node])
                xa, ya = trans2((xx, yy))

                nodesize_1 = (300.0 / (h * 100))
                nodesize_2 = (300.0 / (w * 100))

                p2_2 = nodesize_2 / 2
                p2_1 = nodesize_1 / 2

                a = plt.axes([xa - p2_2, ya - p2_1, nodesize_2, nodesize_1])
                # self.resultGraph.node[id_mol]['image'] = img_mol
                # a.imshow(self.resultGraph.node[each_node]['image'])
                a.imshow(img_mol)
                a.axis('off')

        # plt.savefig('graph.png', facecolor=fig.get_facecolor())
        # print 'Graph .png file has been generated...'

        plt.show()

        return
コード例 #2
0
ファイル: app.py プロジェクト: iwatobipen/chem_streamlit
from rdkit.Chem.Draw import SimilarityMaps
from io import BytesIO
from functools import partial
from PIL import Image
from rdkit.Chem.Draw import rdDepictor
rdDepictor.SetPreferCoordGen(True)
#rfc = pickle.load(open('rf.pkl', 'rb'))
#for streamlit app
rfc = pickle.load(open('/app/chem_streamlit/chemstreamlit/rf.pkl', 'rb'))
fpfunc = partial(SimilarityMaps.GetMorganFingerprint, radius=2)

#mols = [m for m in Chem.SDMolSupplier('./solubility.test.sdf')]
mols = [m for m in Chem.SDMolSupplier('/app/chem_streamlit/chemstreamlit/solubility.test.sdf')]

option = st.selectbox(
    'Please select index of test molecules',
    [i for i in range(len(mols))]
)

st.write('you selected:', option, Chem.MolToSmiles(mols[option]))
fp = [datamaker.mol2fp(mols[option])]
kls = rfc.predict(fp)


img = Draw.MolToImage(mols[option])
bio = BytesIO()
img.save(bio, format='png')
st.image(img)

st.write('predicted class:', datamaker.rclasses[kls[0]])
コード例 #3
0
suppl = Chem.SDMolSupplier(sys.argv[1])  # Path to a input SDF file.
mols = [mol for mol in suppl if mol is not None]
#for mol in mols:
#	AllChem.Compute2DCoords(mol)
#	Draw.MolToFile(mol, mol.GetProp("_Name") + "." + sys.argv[2]) # Supported file formats are pdf, svg, ps, png.
mcs = rdFMCS.FindMCS(mols)
ref = Chem.MolFromSmarts(mcs.smartsString)
AllChem.Compute2DCoords(ref)
refCnf = ref.GetConformer()
molsPerRow = 3
subImgSize = (400, 400)
nRows = 1 + (len(mols) - 1) // molsPerRow
img = Image.new("RGBA", (molsPerRow * subImgSize[0], nRows * subImgSize[1]),
                (255, 255, 255, 0))
for k, mol in enumerate(mols):
    #	AllChem.GenerateDepictionMatching2DStructure(mol, ref)
    coordMap = {}
    for i, idx in enumerate(mol.GetSubstructMatch(ref)):
        pt3 = refCnf.GetAtomPosition(i)
        coordMap[idx] = rdGeometry.Point2D(pt3.x, pt3.y)  # pt3.z == 0.0
    AllChem.Compute2DCoords(mol,
                            clearConfs=True,
                            canonOrient=False,
                            coordMap=coordMap)
    row = k // molsPerRow
    col = k - molsPerRow * row
    img.paste(Draw.MolToImage(mol, subImgSize, legend=mol.GetProp("_Name")),
              (col * subImgSize[0], row * subImgSize[1]))
img.save(sys.argv[2])  # Path to a output PNG or JPG file.
コード例 #4
0
ファイル: Envs.py プロジェクト: shuan4638/ChemSearchDQN
 def show_mol(self):
     mol = Chem.MolFromSmiles(self.smiles)
     return Draw.MolToImage(mol)
コード例 #5
0
def draw_2D(smi_list):
    for smi in smi_list:
        m = Chem.MolFromSmiles(smi)
        img = Draw.MolToImage(m)
        plt.title(smi)
        plt.imshow(img)
コード例 #6
0
ファイル: reaction.py プロジェクト: hyperji/ChemGo
    # product_smiles = Chem.MolToSmiles(Chem.MolFromSmarts(reaction_smiles.split('>>')[-1]))
    reaction_rxn.RemoveMappingNumbersFromReactions()
    products = reaction_rxn.GetProducts()[0]
    product_smiles = Chem.MolToSmiles(products)
    print ("products", product_smiles)


    product_rule_smiles = reaction_rule_smiles.split('>>')[-1]
    reactant_rule_smiles = reaction_rule_smiles.split(">>")[0]
    reactant_list = reaction_rule_smiles.split('.')
    synthesis_reaction_smiles = product_rule_smiles+'>>' + reactant_rule_smiles
    print ("reaction_rule_smiles", synthesis_reaction_smiles)
    print ('product_smiles', product_smiles)

    product_img = Draw.MolToImage(Chem.MolFromSmiles(product_smiles))
    product_img.save('product.png')

    synthesis_rule = Reaction(synthesis_reaction_smiles)
    synthesis_rule.RemoveUnmappedProductTemplates()
    synthesis_rule.RemoveUnmappedReactantTemplates()
    # synthesis_rule_img = Draw.ReactionToImage(synthesis_rule.rxn)
    # synthesis_rule_img.show()
    predict_reactants_mol = synthesis_rule.RunReactants([Chem.MolFromSmiles(product_smiles)])[0]
    print ('result:', predict_reactants_mol)
    predict_reactant_smiles = []
    for ii, mol in enumerate(predict_reactants_mol):

        predict_smiles = Chem.MolToSmiles(mol)
        predict_reactant_smiles.append(predict_smiles)
コード例 #7
0
with open('KNApSAck_mol/C00015228.mol'as fi:
    mol = Chem.MolFromMolBlock(fi.read())
    rdDepictor.Compute2DCoords(mol)
    filename = "Streptomyces/C00015228.png"
    Draw.MolToFile(mol, filename, size=(400, 400), transparent=True)


# In[28]:

from PIL import Image
with open('KNApSAck_mol/C00015228.mol')as fi:
    mol = Chem.MolFromMolBlock(fi.read())
    rdDepictor.Compute2DCoords(mol)
    filename = "Streptomyces/C00015228.png"
    im = Draw.MolToImage(mol, size=(500, 500))
    trans = Image.new('RGBA', im.size, (0, 0, 0, 0))
    width = im.size[0]
    height = im.size[1]
    for x in range(width):
        for y in range(height):
            pixel = im.getpixel( (x, y) )
        
            # 白なら処理しない
            if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
                continue
        
            # 白以外なら、用意した画像にピクセルを書き込み
            trans.putpixel( (x, y), pixel )
    # 透過画像を保存
    trans.save(filename)
コード例 #8
0
 def smiles2img(usr_smiles):
     _mol = Chem.MolFromSmiles(usr_smiles)
     _img = Draw.MolToImage(_mol, size=(900, 900))
     _img.show()
     return _mol
コード例 #9
0
ファイル: utils.py プロジェクト: m2ms/fragalysis-backend
def draw_mol(
    smiles,
    height=34,
    width=150,
    img_type=None,
    highlightAtoms=[],
    atomcolors=[],
    highlightBonds=[],
    bondcolors={},
    mol=None,
):
    """
    Draw a molecule from a smiles
    :param smiles: the SMILES to render
    :param height: the height in px
    :param width: the width in px
    :return: an SVG as a string of the inage
    """
    if mol is None:
        mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return "None Mol"
    # AllChem.Compute2DCoords(mol)
    Chem.Kekulize(mol)
    if not height:
        height = 200
    if not width:
        width = 200
    if img_type == "png":
        img = Draw.MolToImage(
            mol,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )
        img = img.convert("RGBA")
        datas = img.getdata()
        newData = []
        for item in datas:
            if item[0] == 255 and item[1] == 255 and item[2] == 255:
                newData.append((255, 255, 255, 0))
            else:
                newData.append(item)
        img.putdata(newData)
        response = HttpResponse(content_type="image/png")
        img.save(response, "PNG")
        return response
    else:
        rdMolDraw2D.PrepareMolForDrawing(mol, wedgeBonds=False)
        drawer = rdMolDraw2D.MolDraw2DSVG(height, width)

        drawopt = drawer.drawOptions()
        drawopt.clearBackground = False
        drawopt.fixedBondLength = 30
        drawopt.padding = 0.01
        drawopt.bondLineWidth = 1
        drawopt.additionalAtomLabelPadding = 0.05

        drawer.SetFontSize(1)

        drawer.DrawMolecule(
            mol,
            highlightAtoms=highlightAtoms,
            highlightAtomColors=atomcolors,
            highlightBonds=highlightBonds,
            highlightBondColors=bondcolors,
        )

        drawer.FinishDrawing()
        return drawer.GetDrawingText().replace("svg:", "").replace(
            'stroke-width:2px', 'stroke-width:1.5px')
コード例 #10
0
    mol,
    size=(300, 300),
    kekulize=True,
    wedgeBonds=True,
    fitImage=False,
    options=None,
    canvas=None,
    **kwargs
)

# 1.3 分子对象转图片
opts = DrawingOptions()
m = Chem.MolFromSmiles('OC1C2C1CC2')
opts.includeAtomNumbers = True
opts.bondLineWidth = 2.8
draw = Draw.MolToImage(m, options=opts)
draw.save('/drug_development/studyRdkit/st_rdcit/img/mol10.jpg')

# 1.4 多个分子按照grid显示
smis = [
    'COC1=C(C=CC(=C1)NS(=O)(=O)C)C2=CN=CN3C2=CC=C3',
    'C1=CC2=C(C(=C1)C3=CN=CN4C3=CC=C4)ON=C2C5=CC=C(C=C5)F',
    'COC(=O)C1=CC2=CC=CN2C=N1',
    'C1=C2C=C(N=CN2C(=C1)Cl)C(=O)O',
]
mols = []
for smi in smis:
    mol = Chem.MolFromSmiles(smi)
    mols.append(mol)

img = Draw.MolsToGridImage(
コード例 #11
0
def mol_view(smi):
    """Will pop-up image (PNG) of molecule in image viewing software like MS Paint. """
    mol = Chem.MolFromSmiles(smi)
    img = Draw.MolToImage(mol)
    img.show()
コード例 #12
0
 def drawMoleculeQuery(self):
     size = (400, 400)
     # fig = Draw.MolToMPL(self.mol, size=size)
     img = Draw.MolToImage(self.mol, size=size)
     img.save('static/images/queryMolecule/queryMolecule.png')
     return
コード例 #13
0
def visualize(iqspr_results, RDKit_FPs, mdls, data_ss, beta):
    # re-calculate the property values for the proposed molecules
    x_mean, x_std, y_mean, y_std = [], [], [], []
    r_std = []
    FPs_samples = []
    for i, smis in enumerate(iqspr_results["samples"]):
        tmp_fps = RDKit_FPs.transform(smis)
        FPs_samples.append(tmp_fps)

        tmp1, tmp2 = mdls["E"].predict(tmp_fps, return_std=True)
        x_mean.append(tmp1)
        x_std.append(tmp2)

        tmp1, tmp2 = mdls["H**O-LUMO gap"].predict(tmp_fps, return_std=True)
        y_mean.append(tmp1)
        y_std.append(tmp2)

        r_std.append([np.sqrt(x_std[-1][i] ** 2 + y_std[-1][i] ** 2) for i in range(len(x_std[-1]))])

    # flatten the list for max/min calculation
    flat_list = [item for sublist in r_std for item in sublist]
    print('Range of std. dev.: (%.4f,%.4f)' % (min(flat_list), max(flat_list)))

    # prepare a folder to save all the figures
    ini_dir = './iQSPR_tutorial_prd/'
    if not os.path.exists(ini_dir):
        os.makedirs(ini_dir)

    flat_list = np.asarray([item for sublist in r_std for item in sublist])
    s_max, s_min = max(flat_list), min(flat_list)
    flat_list = np.concatenate((data_ss["E"],
                                np.asarray([item for sublist in x_mean for item in sublist])))
    x_max, x_min = max(flat_list), min(flat_list)
    flat_list = np.concatenate((data_ss["H**O-LUMO gap"],
                                np.asarray([item for sublist in y_mean for item in sublist])))
    y_max, y_min = max(flat_list), min(flat_list)
    tmp_beta = np.hstack([0, beta])

    for i in range(len(r_std)):
        dot_size = 45 * ((np.asarray(r_std[i]) - s_min) / (s_max - s_min)) + 5

        plt.figure(figsize=(5, 5))
        rectangle = plt.Rectangle((0, 0), 200, 3, fc='y', alpha=0.1)
        plt.gca().add_patch(rectangle)
        plt.scatter(data_ss["E"], data_ss["H**O-LUMO gap"], s=3, c='b', alpha=0.2)
        plt.scatter(x_mean[i], y_mean[i], s=dot_size, c='r', alpha=0.5)
        plt.title('Step: %i (beta = %.3f)' % (i, tmp_beta[i]))
        plt.xlim(x_min, x_max)
        plt.ylim(y_min, y_max)
        plt.xlabel('Internal energy')
        plt.ylabel('H**O-LUMO gap')
        # plt.show()
        plt.savefig(ini_dir + 'Step_%02i.png' % i, dpi=500)
        plt.close()

    # prepare a folder to save all the figures
    ini_dir = './iQSPR_tutorial_smiles/'
    if not os.path.exists(ini_dir):
        os.makedirs(ini_dir)

    n_S = 25
    for i, smis in enumerate(iqspr_results['samples']):
        tmp_smis = iqspr_results['samples'][i][
            np.argsort(iqspr_results['loglike'][i])[::-1]]
        fig, ax = plt.subplots(5, 5)
        fig.set_size_inches(20, 20)
        fig.set_tight_layout(True)
        for j in range(n_S):
            xaxis = j // 5
            yaxis = j % 5
            try:
                img = Draw.MolToImage(Chem.MolFromSmiles(tmp_smis[j]))
                ax[xaxis, yaxis].clear()
                ax[xaxis, yaxis].set_frame_on(False)
                ax[xaxis, yaxis].imshow(img)
            except:
                pass
            ax[xaxis, yaxis].set_axis_off()
        fig.savefig(ini_dir + 'Step_%02i.png' % i, dpi=500)
        plt.close()

    target_smis = [Chem.MolToSmiles(Chem.MolFromSmiles(smi)) for i, smi in enumerate(data_ss['SMILES'])
                   if ((data_ss['H**O-LUMO gap'].iloc[i] <= 3) and (data_ss['E'].iloc[i] <= 200))]

    # prepare a folder to save all the figures
    ini_dir = './iQSPR_tutorial_target_smiles/'
    if not os.path.exists(ini_dir):
        os.makedirs(ini_dir)

    n_S = 25

    fig, ax = plt.subplots(5, 5)
    fig.set_size_inches(20, 20)
    fig.set_tight_layout(True)
    for j in range(n_S):
        xaxis = j // 5
        yaxis = j % 5
        try:
            img = Draw.MolToImage(Chem.MolFromSmiles(target_smis[j]))
            ax[xaxis, yaxis].clear()
            ax[xaxis, yaxis].set_frame_on(False)
            ax[xaxis, yaxis].imshow(img)
        except:
            pass
        ax[xaxis, yaxis].set_axis_off()
    fig.savefig(ini_dir + 'target_region.png', dpi=500)
    plt.close()
コード例 #14
0
model = model_zoo.chem.GCNClassifier(in_feats=74,
                                     gcn_hidden_feats=[64 for _ in range(2)],
                                     n_tasks=12,
                                     classifier_hidden_feats=64)
model.load_state_dict(
    torch.load("./model/early_stop_2020-06-10_14-46-25.pth")
    ['model_state_dict'])
model.eval()
with torch.no_grad():
    samplename = input("Please input the compound's name\n")
    print(samplename)
    results = pc.get_compounds(samplename, 'name')
    if not results:
        print("No information")
        raise SystemExit
    print(results)
    for compound in results:
        testsample = compound.canonical_smiles
        if (testsample == 0):
            print("No information")
            raise SystemExit
        m = Chem.MolFromSmiles(testsample)
        a = Draw.MolToImage(m, (600, 600))
        a.show()
        sample = smile_to_bigraph(testsample)
        atom_feature = sample.ndata.pop('h')
        predictions = model(sample, atom_feature)
        predictions = torch.nn.Sigmoid()(predictions)
        print(predictions)
コード例 #15
0
    def display_mol(self):
        highlight_lipinski = self.check_lipinski.isChecked()

        if self.curr_sdf_mol_index == 0:
            self.btn_prev.setEnabled(False)
        else:
            self.btn_prev.setEnabled(True)

        if self.curr_sdf_mol_index == self.curr_sdf_num_of_mols - 1:
            self.btn_next.setEnabled(False)
        else:
            self.btn_next.setEnabled(True)

        img_file = IO()  # for structure depiction
        img = sdft.autocrop(
            Draw.MolToImage(self.curr_sdf[self.curr_sdf_mol_index]), "white")
        img.save(img_file, format='PNG')
        # qimg = QtGui.QImage.fromData(img_file.getvalue())
        self.qpixmap = QtGui.QPixmap()
        self.qpixmap.loadFromData(img_file.getvalue(), "PNG")
        self.label_molimage.setPixmap(self.qpixmap)
        self.le_recnumber.setText("{} of {}".format(
            self.curr_sdf_mol_index + 1, self.curr_sdf_num_of_mols))

        if self.SDF_CHANGED:
            # self.SDF_CHANGED = False
            # self.selected_fields = self.get_selected_fields()
            self.table_props.clear()
            self.table_props.setHorizontalHeaderLabels(["prop", "value"])

        for row, prop in enumerate(self.curr_sdf_fields):
            tbl_item = QtGui.QTableWidgetItem(prop[2:])
            # tbl_item.setFlags(QtCore.Qt.ItemIsEnabled)
            self.table_props.setItem(row, 0, tbl_item)

            if self.SDF_CHANGED:
                if self.selected_fields and prop in self.selected_fields:
                    self.table_props.setItemSelected(tbl_item, True)

            if prop in self.curr_sdf[self.curr_sdf_mol_index].GetPropNames():
                value = self.curr_sdf[self.curr_sdf_mol_index].GetProp(prop)
                tbl_item = QtGui.QTableWidgetItem(value)
                # QtCore.Qt.ItemIsEditable is required to edit the cells
                # tbl_item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable)
                tbl_item.setFlags(QtCore.Qt.ItemIsEnabled
                                  | QtCore.Qt.ItemIsEditable)
                if highlight_lipinski and prop in self.highlight_dict:
                    if float(value) > self.highlight_dict[prop]:
                        tbl_item.setBackgroundColor(QtGui.QColor(255, 0, 0))
                    else:
                        tbl_item.setBackgroundColor(QtGui.QColor(0, 255, 0))
                self.table_props.setItem(row, 1, tbl_item)
            else:
                tbl_item = QtGui.QTableWidgetItem("n.d.")
                # see above for flag QtCore.Qt.ItemIsEditable
                tbl_item.setFlags(QtCore.Qt.ItemIsEnabled
                                  | QtCore.Qt.ItemIsEditable)
                self.table_props.setItem(row, 1, tbl_item)

            self.table_props.setRowHeight(row, 18)

        self.SDF_CHANGED = False

        if self.curr_sdf_mol_index in self.selected_recs:
            self.check_rec_selected.setChecked(True)
        else:
            self.check_rec_selected.setChecked(False)
コード例 #16
0
def MolToImage(mol,
               max_size=(1000, 1000),
               kekulize=True,
               options=None,
               canvas=None,
               **kwargs):
    """Wrapper for RDKit's ``MolToImage``.

    Args:
        mol (Chem.Mol or str): Molecule or arrow to draw.
        max_size (2-tuple of (int, int), optional): Maximum image size to
            return. (default: {(1000, 1000)})
        kekulize (bool, optional): Whether to kekulize the molecule.
            (default: {True})
        options (None or ??, optional): RDKit drawing options. If None, will use
            defaults. (default: {None})
        canvas (None or Draw.Canvas, optional): Canvas to draw image onto.
            (default: {None})
        **kwargs: Additional optional arugments passed to RDKit's
            ``MolToImage``.

    Returns:
        PIL.Image: Drawn molecule.
    """

    if not options:
        options = defaultDrawOptions()
    if mol == '->':
        subImgSize = (100, 100)
        img, canvas = Draw._createCanvas(subImgSize)
        p0 = (10, subImgSize[1] // 2)
        p1 = (subImgSize[0] - 10, subImgSize[1] // 2)
        p3 = (subImgSize[0] - 20, subImgSize[1] // 2 - 10)
        p4 = (subImgSize[0] - 20, subImgSize[1] // 2 + 10)
        canvas.addCanvasLine(p0, p1, lineWidth=2, color=(0, 0, 0))
        canvas.addCanvasLine(p3, p1, lineWidth=2, color=(0, 0, 0))
        canvas.addCanvasLine(p4, p1, lineWidth=2, color=(0, 0, 0))
        if hasattr(canvas, 'flush'):
            canvas.flush()
        else:
            canvas.save()
        return img
    elif mol == '<-':  # retro arrow or error
        subImgSize = (100, 100)
        (a, b) = subImgSize
        img, canvas = Draw._createCanvas(subImgSize)
        canvas.addCanvasLine((10, b // 2 - 7), (a - 17, b // 2 - 7),
                             lineWidth=2,
                             color=(0, 0, 0))
        canvas.addCanvasLine((10, b // 2 + 7), (a - 17, b // 2 + 7),
                             lineWidth=2,
                             color=(0, 0, 0))
        canvas.addCanvasLine((a - 24, b // 2 - 14), (a - 10, b // 2),
                             lineWidth=2,
                             color=(0, 0, 0))
        canvas.addCanvasLine((a - 24, b // 2 + 14), (a - 10, b // 2),
                             lineWidth=2,
                             color=(0, 0, 0))
        if hasattr(canvas, 'flush'):
            canvas.flush()
        else:
            canvas.save()
        return img
    elif mol is not None:
        return Draw.MolToImage(mol,
                               size=max_size,
                               kekulize=kekulize,
                               options=options,
                               canvas=canvas,
                               **kwargs)
コード例 #17
0
def smiles2image(smiles):
    mol = AllChem.MolFromSmiles(smiles)
    img = Draw.MolToImage(mol)
    # PIL image, save using img.save(filename)
    return img
コード例 #18
0
def smiles_to_image(smiles):
    a = io.BytesIO()
    m = Chem.MolFromSmiles(smiles)
    z = Draw.MolToImage(m)
    z.save(a, format="PNG")
    return a.getvalue()
コード例 #19
0
ファイル: UnitTestDraw.py プロジェクト: wgchang/rdkit
 def testGithubIssue54(self):
     # Assert that radicals depict with PIL
     os.environ['RDKIT_CANVAS'] = 'sping'
     mol = Chem.MolFromSmiles('c1([O])ccc(O)cc1')
     img = Draw.MolToImage(mol)
     self.assertTrue(img)
コード例 #20
0
ファイル: sar.py プロジェクト: leelasdSI/rdkit_ipynb_tools
def generate_sar_table(db_list,
                       core,
                       id_prop,
                       act_prop,
                       sort_reverse=True,
                       dir_name="html/sar_table",
                       color_prop="logp"):
    """core: smiles string; id_prop, act_prop: string
    colorprop_is_lin: whether or not the property used for coloring is linear (e.g. LogP or PercActivity) or needs to be logarithmitized (e.g. IC50_uM)."""

    tools.create_dir_if_not_exist(dir_name)
    tools.create_dir_if_not_exist(op.join(dir_name, "img"))

    db_list.sort_list(act_prop, reverse=sort_reverse)

    act_xy = np.zeros([55, 55], dtype=np.float)  # coordinates for the activity
    # color_xy = np.zeros([55, 55], dtype=np.float)
    color_xy = np.full([55, 55], np.NaN, dtype=np.float)
    molid_xy = np.zeros([55, 55], dtype=np.int)
    # molid_xy = np.arange(900, dtype=np.int).reshape(30, 30)  # coordinates for the molid
    rx_dict = {}  # axes for the residues
    ry_dict = {}
    max_x = -1  # keep track of the arraysize
    max_y = -1
    res_pos_x = -1
    res_pos_y = -1

    core_mol = Chem.MolFromSmiles(core)
    Draw.MolToFile(core_mol, "%s/img/core.png" % dir_name, [90, 90])

    for idx, mol in enumerate(db_list):
        act = float(mol.GetProp(act_prop))
        color = float(mol.GetProp(color_prop))
        molid = int(mol.GetProp(id_prop))
        tmp = Chem.ReplaceCore(mol, core_mol, labelByIndex=True)
        frag_mols = list(Chem.GetMolFrags(tmp, asMols=True))
        frag_smiles = [Chem.MolToSmiles(m, True) for m in frag_mols]
        if len(frag_mols) == 1:
            # one of the two residues is H:
            pos = get_res_pos(frag_smiles[0])
            if pos == res_pos_x:
                h_smiles = "[%d*]([H])" % res_pos_y
                frag_smiles.append(h_smiles)
                frag_mols.append(Chem.MolFromSmiles(h_smiles))
            else:
                h_smiles = "[%d*]([H])" % res_pos_x
                frag_smiles.insert(0, h_smiles)
                frag_mols.insert(0, Chem.MolFromSmiles(h_smiles))

            print(" adding H residue in pos {} to  mol #{} (molid: {})".format(
                pos, idx, mol.GetProp(id_prop)))

        elif len(frag_mols) > 2:
            print(
                "*  incorrect number of fragments ({}) in mol #{} (molid: {})".
                format(len(frag_mols), idx, mol.GetProp(id_prop)))
            continue

        if res_pos_x == -1:
            # print frag_smiles[0], frag_smiles[1]
            res_pos_x = get_res_pos(frag_smiles[0])
            res_pos_y = get_res_pos(frag_smiles[1])
            # print "res_pos_x: {}     res_pos_y: {}".format(res_pos_x, res_pos_y)
        else:
            test_pos_x = get_res_pos(frag_smiles[0])
            if test_pos_x != res_pos_x:  # switch residues
                frag_smiles = frag_smiles[::-1]
                frag_mols = frag_mols[::-1]
        if frag_smiles[0] in rx_dict:
            curr_x = rx_dict[frag_smiles[0]]
        else:
            max_x += 1
            rx_dict[frag_smiles[0]] = max_x
            curr_x = max_x
            Draw.MolToFile(frag_mols[0],
                           "%s/img/frag_x_%02d.png" % (dir_name, max_x),
                           [100, 100])
        if frag_smiles[1] in ry_dict:
            curr_y = ry_dict[frag_smiles[1]]
        else:
            max_y += 1
            ry_dict[frag_smiles[1]] = max_y
            curr_y = max_y
            Draw.MolToFile(frag_mols[1],
                           "%s/img/frag_y_%02d.png" % (dir_name, max_y),
                           [100, 100])

        # draw thw whole molecule for the tooltip
        img_file = op.join(dir_name, "img/",
                           "cpd_{}_{}.png".format(curr_x, curr_y))
        img = tools.autocrop(Draw.MolToImage(mol), "white")
        img.save(img_file, format='PNG')

        act_xy[curr_x][curr_y] = act
        color_xy[curr_x][curr_y] = color
        molid_xy[curr_x][curr_y] = molid

    return act_xy, molid_xy, color_xy, max_x, max_y
コード例 #21
0
from rdkit import Chem
from rdkit.Chem import Descriptors
from rdkit.Chem import rdMolDescriptors
from rdkit.Chem import Draw

smiles = open('rb/smiles.smi')
line = smiles.readline()

salida = open('rb/descriptors.csv','w')
i = 0
while(line):
	mol = Chem.MolFromSmiles(line)
	img = Draw.MolToImage(mol,useSVG=False)
	img.save('./images/'+str(i)+'.png')
	#img = Draw.MolToImage(mol,useSVG=True)
	#img.save('./images/'+str(i)+'.svg')  
	line = smiles.readline()
	i+=1	
salida.close()

コード例 #22
0
def core_table(mol, props=None, hist=None):
    if props is None:
        props = ["Cluster_No", "Num_Members", "Producers"]

    td_opt = {"align": "center"}
    header_opt = {"bgcolor": "#94CAEF", "align": "center"}
    table_list = []

    cells = html.td(html.b("Molecule"), header_opt)
    for prop in props:
        pl = prop.lower()
        if (pl.endswith("min") or pl.endswith("max") or pl.endswith("mean")
                or pl.endswith("median") or pl.endswith("ic50")
                or pl.endswith("ic50)") or pl.endswith("activity")
                or pl.endswith("acctivity)")):
            pos = prop.rfind("_")
            if pos > 0:
                prop = prop[:pos] + "<br>" + prop[pos + 1:]
        cells.extend(html.td(html.b(prop), header_opt))

    if hist is not None:
        header_opt["class"] = "histogram"
        cells.extend(html.td(html.b("Histogram"), header_opt))

    rows = html.tr(cells)

    cells = []

    if not mol:
        cells.extend(html.td("no structure"))

    else:
        mol_props = mol.GetPropNames()
        cl_no = mol.GetProp("Cluster_No")
        img_file = "img/core_{}.png".format(cl_no)
        img = tools.autocrop(Draw.MolToImage(mol))
        img.save(img_file, format='PNG')
        img_src = img_file

        cell = html.img(img_src)
        cells.extend(html.td(cell, td_opt))

    for prop in props:
        td_opt = {"align": "center"}
        if prop in mol_props:
            prop_val = mol.GetProp(prop)
            cells.extend(html.td(prop_val, td_opt))
        else:
            cells.extend(html.td("", td_opt))

    if hist is not None:
        td_opt["class"] = "histogram"
        if "img/" not in hist:
            hist = "img/" + hist
        img_opt = {"height": "220"}
        cell = html.img(hist, img_opt)
        cells.extend(html.td(cell, td_opt))

    rows.extend(html.tr(cells))

    table_list.extend(html.table(rows))

    # print(table_list)
    return "".join(table_list)
コード例 #23
0
def PrintAsBase64PNGString(x, renderer=None):
    '''returns the molecules as base64 encoded PNG image
  '''
    return '<img src="data:image/png;base64,%s" alt="Mol"/>' % _get_image(
        Draw.MolToImage(x))
コード例 #24
0
ファイル: 1.py プロジェクト: yutake27/newcomer-exercise
from rdkit import rdBase, Chem
from rdkit.Chem import AllChem, Draw
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem import rdDepictor
from IPython.display import SVG

m = Chem.MolFromSmiles("Cc1nc(Nc2ncc(s2)C(=O)Nc3c(C)cccc3Cl)cc(n1)N4CCN(CCO)CC4")
mol = Draw.MolToImage(m)
コード例 #25
0
            f.write('Property:' + args.property_name + '\n')
            f.write('Validity: ' + str(np.mean(valid_ratio)) + '±' +
                    str(np.std(valid_ratio)) + '\n')
            f.write('Average score: ' + str(np.mean(sorted_scores)) + '±' +
                    str(np.std(sorted_scores)) + '\n')
            if args.save_smiles:
                f.write(str(sorted_smiles) + '\n')
                f.write(str(sorted_scores) + '\n')
            f.write('\n')

    if args.save_fig is not None:
        gen_dir = args.save_fig
        os.makedirs(gen_dir, exist_ok=True)
        for i in range(len(gen_mols)):
            filepath = os.path.join(gen_dir,
                                    'generated_mols_{}.png'.format(i + 1))
            img = Draw.MolToImage(gen_mols[i])
            img.save(filepath)

    print('10 highest score:', sorted_scores[0:10])
    print('==========================================')
    print('Average score:', str(np.mean(sorted_scores)), '±',
          str(np.std(sorted_scores)))
    print('==========================================')
    print("Validity: {:.3f}% ± {:.3f}%, vals={}".format(
        np.mean(valid_ratio), np.std(valid_ratio), valid_ratio))
    print('------------------------------------------')
    print("Generation Time: {:.3f} ± {:.3f} seconds, vals={}".format(
        np.mean(gen_time), np.std(gen_time), gen_time))
    print('==========================================')
コード例 #26
0
ファイル: fragenum.py プロジェクト: SolKul/MassMI
def disp100mol(mol):
    im_mol=Draw.MolToImage(mol,size=(200,200))
    display(im_mol.resize((150,150),resample=5))
コード例 #27
0
def gen_plot( QCA, fnm):
    try:
        d = np.loadtxt( fnm, usecols=(5,2,6), skiprows=3)
        if d.size == 0:
            return
    except StopIteration:
        print("No data for", fnm)
        return

    entry   = np.genfromtxt( fnm, usecols=(0,),  skip_header=HEADER_LEN, dtype=np.dtype("str"))[0]
    smiles  = np.genfromtxt( fnm, usecols=(7,),  skip_header=HEADER_LEN, dtype=np.dtype("str"), comments=None)[0]
    scanned = np.genfromtxt( fnm, usecols=( 4,), skip_header=HEADER_LEN, dtype=np.dtype("str"))[0]
    scanned = eval('[' + scanned.replace('-',',') + ']')
    scanned = [x+1 for x in scanned]
    #print(scanned)
    print( fnm, smiles)
    mol_id = np.genfromtxt( fnm, usecols=(1,), skip_header=HEADER_LEN, dtype=np.dtype("str"))

    with open( fnm, 'r') as fd:
        ds_name = fd.readline().strip("\n")
        method  = fd.readline().strip("\n")
        basis   = fd.readline().strip("\n")

    scan = d[:,0]
    qm_abs_min = d[:,1].min()
    qmene = d[:,1] * const.hartree2kcalmol
    qmene -= qmene.min()
    mmene = d[:,2] 
    mmene -= mmene.min()

    mm_min_pt = scan[ np.argsort(mmene)[0]]
    qm_min_pt = scan[ np.argsort(qmene)[0]]

    #AllChem.GenerateDepictionMatching3DStructure()

    qcmol = QCA.db.get( mol_id[0]).get( "data")
    try:
        mol = rdutil.mol.build_from_smiles( smiles)
        molnoindex = rdutil.mol.build_from_smiles( re.sub(":[1-9][0-9]*", "", smiles))
    except Exception as e:
        print(e)
        return
    AllChem.ComputeGasteigerCharges( mol)
    totalcharge=0.0
    for i, atom in enumerate( mol.GetAtoms()):
        totalcharge += float( atom.GetProp('_GasteigerCharge'))
    atom_map = rdutil.mol.atom_map( mol)
    inv = {val:key for key,val in atom_map.items()}
    #print(atom_map)
    scanned = [inv[i] for i in scanned]
    #scanned = [rdutil.mol.atom_map_invert( atom_map)[i] for i in scanned]
    #print(scanned)
    molflat = rdutil.mol.build_from_smiles( smiles)
    try:
        ret = rdutil.mol.embed_qcmol_3d( mol, qcmol)
        #print("EMBED WAS RET:", ret)
        #AllChem.GenerateDepictionMatching3DStructure(molflat, mol)
        AllChem.Compute2DCoords(molnoindex)
    except Exception as e:
        print( "Could not generate conformation:")
        print( e)
        return
    options = DrawingOptions()
    options.atomLabelFontSize = 110
    options.atomLabelFontFace = "sans"
    options.dotsPerAngstrom = 400
    options.bondLineWidth = 8
    options.coordScale = 1
    png = Draw.MolToImage(molnoindex, highlightAtoms=scanned, highlightColor=[0,.8,0], size=(500*12,500*4), fitImage=True, options=options)

    fig = plt.figure(figsize=(8,3.5), dpi=300, constrained_layout=True)
    matplotlib.rc("font", **{"size": 10})
    gs = GridSpec(2, 2, figure=fig)
    ax1 = fig.add_subplot( gs[:2,0])
    ax1.plot( scan, qmene, 'bo-', lw=1, ms=2, label="QM")
    ax1.plot( scan, mmene, 'ro-', lw=1, ms=2, label="MM")
    ax1.set_ylabel("Energy (kcal/mol)")
    ax1.set_xlabel("Scan value")
    ax1.spines['top'].set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax1.legend(loc='best')
    ax2 = fig.add_subplot(gs[0,1])
    ax2.imshow(png)
    ax2.axis( 'off')
    ax3 = fig.add_subplot(gs[1,1])
    #ax3.text(.5,1.1,"{:^60s}".format(re.sub(":[1-9][0-9]*","",smiles)), fontsize=4, fontname="Monospace", ha='center')
    ax3.text(0,.9,"{:<16s}{:>39s}".format("QCA Dataset:", ds_name), fontsize=8, fontname="Monospace")
    ax3.text(0,.8,"{:<16s}{:>39s}".format("QCA Entry:", entry.split("-")[1]), fontsize=8, fontname="Monospace")
    ax3.text(0,.7,"{:<16s}{:>39s}".format("MM spec:", "Parsley unconstrained"), fontsize=8, fontname="Monospace")
    ax3.text(0,.6,"{:<16s}{:>39s}".format("MM charges:", "ANTECHAMBER AM1-BCC"), fontsize=8, fontname="Monospace")
    ax3.text(0,.5,"{:<16s}{:>39s}".format("Total charge", "{:6.2f}".format(totalcharge) ), fontsize=8, fontname="Monospace")
    ax3.text(0,.4,"{:<16s}{:>39s}".format("QM spec:", "{}/{}".format(method, basis)), fontsize=8, fontname="Monospace")
    ax3.text(0,.3,"{:<16s}{:>39s}".format("QM Absolute min:", "{:16.13e} au".format( qm_abs_min)), fontsize=8, fontname="Monospace")
    ax3.text(0,.2,"{:<16s}{:>39s}".format("QM Min point:", "{:8.2f}".format( qm_min_pt)), fontsize=8, fontname="Monospace")
    ax3.text(0,.1,"{:<16s}{:>39s}".format("MM Min point:", "{:8.2f}".format( mm_min_pt)), fontsize=8, fontname="Monospace")
    ax3.text(0,.0,"{:<16s}{:>39s}".format("OFFSB ver", "1.0"), fontsize=8, fontname="Monospace")
    ax3.axis('off')
    fig.savefig( re.sub( "\.*$", ".png", fnm))
    fig = None
    plt.close("all")
    #plt.show()
    #plt.plot(scan, qmene, 'rx-', label="QM")
    #plt.plot(scan, mmene, 'bx-', label="MM")
    return
コード例 #28
0
ファイル: MassPlot.py プロジェクト: shunsunsun/DeepMASS
def visualize_molecule(smiles):
    figs = []
    for smi in smiles:
        figs.append(Draw.MolToImage(MolFromSmiles(smi), size = (160, 160)))
    return figs
コード例 #29
0
corr2 = np.corrcoef(np.log(y_test).reshape(1,-1), y_pred.reshape(1,-1))[0][1]**2
rmse = np.mean((np.log(y_test) - y_pred)**2)**0.5
print("R2 : %0.2F"%corr2)
print("RMSE : %0.2F"%rmse)


# Visualizing the Layers
# It can be interesting to try and understand how the model "sees" the molecules. For this I’ll take an example molecule
# and plot some of the outputs from the different layers. I’ve taken the compound with the lowest IC50, number 143 in the dataset.
molnum = 143
molimage = np.array(list(data["molimage"][molnum:molnum+1]))
mol = data["mol"][molnum]

# The molecule looks like this
from rdkit.Chem import Draw
Draw.MolToImage(mol)

# And has this “chemcepterized” image as shown below
plt.imshow(molimage[0,:,:,:3])

# The first example is the third layer, which is the 1,1 convolution which feeds the 3,3 convolutional layer in tower 2.
layer1_model = Model(inputs=model.input,
                    outputs=model.layers[2].output)
kernels1 = layer1_model.predict(molimage)[0]
def plot_kernels(kernels):
    fig, axes = plt.subplots(2,3, figsize=(12,8))
    for i,ax in enumerate(axes.flatten()):
        ax.matshow(kernels[:,:,i])
        ax.set_title("Kernel %s"%i)
plot_kernels(kernels1)
コード例 #30
0
ファイル: mol_test.py プロジェクト: v-liuwei/molgen
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from Funcs import *
from config import config
dconfig = config()
def mol_with_atom_index( mol ):
    atoms = mol.GetNumAtoms()
    for idx in range( atoms ):
        mol.GetAtomWithIdx( idx ).SetProp( 'molAtomMapNumber', str( mol.GetAtomWithIdx( idx ).GetIdx() ) )
    return mol

mol = Chem.MolFromSmiles('c1c[nH+]c2cc[nH]c2n1')
data = smiles2data('c1c[nH+]c2cc[nH]c2n1',dconfig.max_atom_num,dconfig.possible_atoms,dconfig.possible_bonds,batch=False)
Draw.MolToImage(mol_with_atom_index(mol)).show()
print(data)