Example #1
0
def graph_from_smiles(smiles):
    graph = MolGraph()
    mol = MolFromSmiles(smiles)
    Chem.DetectBondStereochemistry(mol, -1)
    Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True)
    Chem.AssignAtomChiralTagsFromStructure(mol, -1)

    if not mol:
        raise ValueError("Could not parse SMILES string:", smiles)
    atoms_by_rd_idx = {}
    for atom in mol.GetAtoms():
        new_atom_node = graph.new_node('atom',
                                       features=atom_features(atom),
                                       rdkit_ix=atom.GetIdx())
        atoms_by_rd_idx[atom.GetIdx()] = new_atom_node

    for bond in mol.GetBonds():
        atom1_node = atoms_by_rd_idx[bond.GetBeginAtom().GetIdx()]
        atom2_node = atoms_by_rd_idx[bond.GetEndAtom().GetIdx()]
        new_bond_node = graph.new_node('bond', features=bond_features(bond))
        new_bond_node.add_neighbors((atom1_node, atom2_node))
        atom1_node.add_neighbors((atom2_node, ))

    mol_node = graph.new_node('molecule')
    mol_node.add_neighbors(graph.nodes['atom'])
    return graph
Example #2
0
def load_mols(target, file_path):
    """Function to load in the 3D molecules
    Takes a Target object and a file path
    Returns None"""
    mols = Chem.SDMolSupplier(file_path)
    if not mols:
        return
    tot = len(mols)
    if tot == 0:
        print "No molecules given"
        return
    old = -1
    print "Adding molecules..."
    for i, m in enumerate(mols):
        # Catch none molecules
        if m is None:
            print "None molecule", sys.exit()
        # Print the progress
        if i * 100 / tot != old:
            old = i * 100 / tot
            sys.stdout.write("\r%d%% complete..." % old)
            sys.stdout.flush()
        Chem.AssignAtomChiralTagsFromStructure(m)
        add_new_mol(m, target)
    old = 100
    sys.stdout.write("\r%d%%" % old)
    sys.stdout.flush()
    print "\nAdding molecules complete"
def chiral_tags(mol):
    """
    Tag methylene and methyl groups with a chiral tag priority defined
    from the atom index of the hydrogens
    """
    li_list = []
    smarts_ch2 = '[!#1][*]([#1])([#1])([!#1])'
    atom_sets = mol.GetSubstructMatches(Chem.MolFromSmarts(smarts_ch2))
    for atoms in atom_sets:
        atoms = sorted(atoms[2:4])
        prioritized_H = atoms[-1]
        li_list.append(prioritized_H)
        mol.GetAtoms()[prioritized_H].SetAtomicNum(9)
    smarts_ch3 = '[!#1][*]([#1])([#1])([#1])'
    atom_sets = mol.GetSubstructMatches(Chem.MolFromSmarts(smarts_ch3))
    for atoms in atom_sets:
        atoms = sorted(atoms[2:])
        H1 = atoms[-1]
        H2 = atoms[-2]
        li_list.append(H1)
        li_list.append(H2)
        mol.GetAtoms()[H1].SetAtomicNum(9)
        mol.GetAtoms()[H2].SetAtomicNum(9)

    Chem.AssignAtomChiralTagsFromStructure(mol, -1)
    rdmolops.AssignStereochemistry(mol)
    for atom_idx in li_list:
        mol.GetAtoms()[atom_idx].SetAtomicNum(1)

    return mol
Example #4
0
    def _prepare_input_file(self, logger=None):
        from rdkit import Chem
        import rdkit.Chem.rdmolops as rd

        self.parameters.input = "input.conf"

        params = self.parameters

        # Check input file
        limit_atoms_ligand = 100
        ch.check_limit_number_atoms(params.ligands, limit_atoms_ligand)

        # Get core of the ligand
        mol = Chem.MolFromPDBFile(params.core)
        try:
            ligand_core = rd.SplitMolByPDBResidues(mol)[params.residue]
        except KeyError:
            raise ce.MissResidueFlag("Missing residue flag to specify " +
                                     "the ligand core residue name. " +
                                     "i.e resname: 'LIG'")

        # Get sdf full grown ligands
        ligands_grown = Chem.SDMolSupplier(params.ligands, removeHs=False)
        fragment_files = []

        # For each full grown ligand create neutral fragment
        with open(params.input, "w") as fout:
            pass
        for ligand in ligands_grown:
            Chem.AssignAtomChiralTagsFromStructure(ligand)
            try:
                line, fragment = \
                    self._create_fragment_from_ligand(ligand,
                                                      ligand_core)
            except Exception as e:
                try:
                    line, fragment = \
                        self._create_fragment_from_ligand(ligand,
                                                          ligand_core,
                                                          substructure=False)
                except Exception as e:
                    try:
                        # Try to fix symmetry
                        line, fragment = \
                            self._create_fragment_from_ligand(ligand,
                                                              ligand_core,
                                                              symmetry=True)
                    except Exception as e:
                        # Try with second substructure search
                        line, fragment = \
                            self._create_fragment_from_ligand(
                                ligand, ligand_core,
                                result=1, substructure=False)

            logger.info(f"Ligand {fragment.file} preprocessed")
            fragment_files.append(fragment.file)
            with open(params.input, "a") as fout:
                fout.write(line + "\n")

        return fragment_files
Example #5
0
def chiral_stereo_check(mol):
    Chem.SanitizeMol(mol)
    Chem.DetectBondStereochemistry(mol, -1)
    Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True)
    Chem.AssignAtomChiralTagsFromStructure(mol, -1)

    return mol
def choose_resonance_structure(mol):
    """
    This function creates all resonance structures of the mol object, counts
    the number of rotatable bonds for each structure and chooses the one with
    fewest rotatable bonds (most 'locked' structure)
    """
    resonance_mols = rdchem.ResonanceMolSupplier(mol,
                                                 rdchem.ResonanceFlags.ALLOW_CHARGE_SEPARATION)
    res_status = True
    new_mol = None
    if not resonance_mols:
        print("using input mol")
        new_mol = mol
        res_status = False
    for res_mol in resonance_mols:
        Chem.SanitizeMol(res_mol)
        n_rot_bonds = Chem.rdMolDescriptors.CalcNumRotatableBonds(res_mol)
        if new_mol is None:
            smallest_rot_bonds = n_rot_bonds
            new_mol = res_mol
        if n_rot_bonds < smallest_rot_bonds:
            smallest_rot_bonds = n_rot_bonds
            new_mol = res_mol

    Chem.DetectBondStereochemistry(new_mol, -1)
    rdmolops.AssignStereochemistry(new_mol, flagPossibleStereoCenters=True,
                                   force=True)
    Chem.AssignAtomChiralTagsFromStructure(new_mol, -1)
    return new_mol, res_status
    def init_search(self, smiles, algorithmname, algorithm_tag):

        print('Conformational search with a ' + algorithmname + ' method')
        print('Energy method: ' + self.method)
        print('Smiles: ' + smiles)
        print('Job name: ' + self.jobname)

        #init molecule and optimise
        mol = Chem.MolFromSmiles(self.smiles)
        mol = Chem.AddHs(mol, explicitOnly=False)
        AllChem.EmbedMolecule(mol,
                              randomSeed=int(time.time()),
                              useRandomCoords=True)

        #write pre optimised molecule
        self.write_pdb(mol, self.jobname + algorithm_tag + 'preoptimised.pdb')

        #optimise start molecule
        AllChem.MMFFOptimizeMolecule(mol)
        mp = AllChem.MMFFGetMoleculeProperties(mol)
        ffm = AllChem.MMFFGetMoleculeForceField(mol, mp)

        #write optimised molecule
        self.write_pdb(mol, self.jobname + algorithm_tag + 'optimised.pdb')

        Chem.SanitizeMol(mol)
        Chem.DetectBondStereochemistry(mol, -1)
        Chem.AssignStereochemistry(mol,
                                   flagPossibleStereoCenters=True,
                                   force=True)
        Chem.AssignAtomChiralTagsFromStructure(mol, -1)

        self.ring_atoms_nr = self.count_ringatoms(mol)
        print('Number of non aromatic ring atoms:', str(self.ring_atoms_nr))

        #create start conformer
        start_conformer = Conformer(mol)
        start_conformer.update_molecule('MMFF94')

        self.min_conformer = start_conformer
        self.min_energy = start_conformer.energy
        print('Start energy:', self.min_energy)

        self.original_conformer = self.min_conformer
        self.original_smiles = self.get_smiles(
            self.original_conformer.molecule)

        start_angles = start_conformer.get_dihedrals()

        print('Initial angles')
        start_angles_only = []
        for angle in start_angles:
            print(angle)
            self.dihedral_atom_id.append(
                (angle[0], angle[1], angle[2], angle[3]))
            start_angles_only.append(angle)
        print('Number of dihedrals:', len(start_angles))

        return start_conformer
Example #8
0
def toRDKITmol(mol, protidx, sanitize=True, removeHs=False):
    # Taken from rdkit/Code/GraphMol/FileParsers/PDBParser.cpp
    conformer = Chem.Conformer(len(protidx))
    conformer.Set3D(True)
    conformer.SetId(0)
    rdmol = Chem.RWMol()
    atomlist = []
    for ii, i in enumerate(protidx):
        a = Chem.Atom(mol.element[i])
        a.SetFormalCharge(int(mol.charge[i]))
        info = Chem.AtomPDBResidueInfo(atomName=mol.name[i],
                                       serialNumber=int(mol.serial[i]),
                                       altLoc=mol.altloc[i],
                                       residueName=mol.resname[i],
                                       residueNumber=int(mol.resid[i]),
                                       chainId=mol.chain[i],
                                       insertionCode=mol.insertion[i],
                                       occupancy=float(mol.occupancy[i]),
                                       tempFactor=float(mol.beta[i]),
                                       isHeteroAtom=mol.record[i] == 'HETATM')
        a.SetMonomerInfo(info)

        rdmol.AddAtom(a)
        atomlist.append(a)
        coor = [float(c) for c in mol.coords[i, :, mol.frame]]
        conformer.SetAtomPosition(ii, Point3D(coor[0], coor[1],
                                              coor[2]))  # Correct the atom idx
    rdmol.AddConformer(conformer)

    # Here I diverge from the C++ parser because you cannot instantiate Chem.Bond objects in python
    # I also don't take into account double/triple bonds etc since I don't think we actually store them in Molecule
    for b in mol._getBonds():
        if b[0] in protidx and b[1] in protidx:
            bond = rdmol.GetBondBetweenAtoms(int(b[0]), int(b[1]))
            if bond is None:
                rdmol.AddBond(int(np.where(protidx == b[0])[0]),
                              int(np.where(protidx == b[1])[0]),
                              Chem.BondType.SINGLE)

    # Proximitybonds I already did by using _getBonds which calls _guessBonds
    # TODO: Set PDB double bonds

    # Calculate explicit valence of atoms
    for a in atomlist:
        pass

    if sanitize:
        if removeHs:
            Chem.RemoveHs(rdmol)
        else:
            Chem.SanitizeMol(rdmol)
    else:
        rdmol.UpdatePropertyCache()

    # Set tetrahedral chirality from 3D co-ordinates
    Chem.AssignAtomChiralTagsFromStructure(rdmol)
    StandardPDBResidueChirality(rdmol)

    return rdmol
Example #9
0
def chiral_stereo_check(mol):
    # avoid sanitization error e.g., dsgdb9nsd_037900.xyz
    Chem.SanitizeMol(mol, SanitizeFlags.SANITIZE_ALL - SanitizeFlags.SANITIZE_PROPERTIES)
    Chem.DetectBondStereochemistry(mol,-1)
    # ignore stereochemistry for now
    Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True)
    Chem.AssignAtomChiralTagsFromStructure(mol,-1)
    return mol
 def updateStereoChemAssignments(self):
     if not self.__rdMol:
         return False
     try:
         Chem.AssignAtomChiralTagsFromStructure(self.__rdMol)
         Chem.AssignStereochemistry(self.__rdMol, True, True, True)
         # self.__rdMol.Debug()
     except Exception as e:
         logger.exception("Failing with %s", str(e))
     return False
Example #11
0
def get_sdf_chiral_center(file_name):
    mols_suppl = Chem.SDMolSupplier(file_name)

    for mol in mols_suppl:
        # mol3的类型=<class 'rdkit.Chem.rdchem.Mol'>
        #print('类型=',type(mol))

        Chem.AssignAtomChiralTagsFromStructure(mol)
        # 找到分子的手性中心
        chiral_center = Chem.FindMolChiralCenters(mol)
        return chiral_center
Example #12
0
def chiral_stereo_check(mol):
    """
    Find and embed chiral information into the model based on the coordinates
    args:
        mol - rdkit molecule, with embeded conformer
    """
    Chem.SanitizeMol(mol)
    Chem.DetectBondStereochemistry(mol, -1)
    Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True)
    Chem.AssignAtomChiralTagsFromStructure(mol, -1)

    return
Example #13
0
def genConf(m, nc, rms, efilter, rmspost):
    nr = int(AllChem.CalcNumRotatableBonds(m))
    #m = Chem.AddHs(m)
    Chem.AssignAtomChiralTagsFromStructure(m, replaceExistingTags=True)
    if not nc: nc = 3**nr

    print(dashedline + "\n   |    " + ("FULL_MONTE search").ljust(leftcol) +
          ("|").rjust(rightcol))
    #log.Write("   | o  "+("COMP: "+str(Params.COMP)+" degrees").ljust(leftcol)+("|").rjust(rightcol))
    #log.Write("   | o  "+("LEVL: "+str(Params.LEVL)+" force field").ljust(leftcol)+("|").rjust(rightcol))
    #log.Write("   | o  "+("DEMX: "+str(Params.DEMX)+" kcal/mol").ljust(leftcol)+("|").rjust(rightcol))
    print("   | o  " + ("EWIN: " + str(efilter) + " kcal/mol").ljust(leftcol) +
          ("|").rjust(rightcol))
    print("   | o  " +
          ("MCNV: " + str(nr) + " ROTATABLE BONDS").ljust(leftcol) +
          ("|").rjust(rightcol))
    #log.Write("   |    "+torstring.ljust(leftcol)+("|").rjust(rightcol))
    print("   | o  " + ("STEP: " + str(nc) + " (ESTIMATED CONFORMER SPACE: " +
                        str(nr**3) + ")").ljust(leftcol) +
          ("|").rjust(rightcol))
    print(dashedline + "\n")

    if not rms: rms = -1
    ids = AllChem.EmbedMultipleConfs(m, numConfs=nc)

    if len(ids) == 0:
        ids = m.AddConformer(m.GetConformer, assignID=True)

    diz = []
    diz2 = []
    diz3 = []
    for id in ids:
        prop = AllChem.MMFFGetMoleculeProperties(m, mmffVariant="MMFF94s")
        ff = AllChem.MMFFGetMoleculeForceField(m, prop, confId=id)
        ff.Minimize()
        en = float(ff.CalcEnergy())
        econf = (en, id)
        diz.append(econf)

    if efilter != "Y":
        n, diz2 = energy_filter(m, diz, efilter)
    else:
        n = m
        diz2 = diz

    if rmspost != None and n.GetNumConformers() > 1:
        o, diz3 = postrmsd(n, diz2, rmspost)
    else:
        o = n
        diz3 = diz2

    return o, diz3, nr
Example #14
0
def _removeHs(data):
    mols = _parseMolData(data, loadMol=False, useRDKitChemistry=False)
    ms = []
    for molblock in mols:
        mol = parse_molblock(molblock, useRDKitChemistry=False)
        props = molblock.split("M  END")[1].strip()
        props = props if len(props) > 1 else None
        Chem.FastFindRings(mol)
        mol.UpdatePropertyCache(strict=False)
        Chem.AssignAtomChiralTagsFromStructure(mol)
        Chem.AssignStereochemistry(mol, cleanIt=True, force=True)
        ms.append((remove_hs_from_mol(mol), props))
    return _getSDFString(ms)
Example #15
0
def molToRDKit(filepath: str):
    try:
        m = Chem.MolFromMolFile(filepath, removeHs=False)
        m = Chem.AddHs(m)
        Chem.AssignAtomChiralTagsFromStructure(m)

        # TODO: skipping for now, error with some molecules
        #AllChem.EmbedMolecule(m,useExpTorsionAnglePrefs=True,useBasicKnowledge=True)
        #AllChem.MMFFOptimizeMolecule(m)

        return m
    except:
        print(filepath + " is not a valid .mol file")
        return False
Example #16
0
    def get_smiles(self, mol):
        """
        Map a molecule name to its corresponding SMILES string.

        Parameters
        ----------
        mol : RDKit Mol
            Molecule.
        """
        if self.assign_stereo_from_3d:  # do this before removing hydrogens
            Chem.AssignAtomChiralTagsFromStructure(mol)
        if self.remove_hydrogens:
            mol = Chem.RemoveHs(mol)  # creates a copy
        return Chem.MolToSmiles(mol, isomericSmiles=True, canonical=True)
Example #17
0
def set_hydrogen_coor_from_pdbmol(refpdbmol, mol, refconfId=-1, confId=-1):
    newmol = Chem.Mol(mol)
    hidxs = get_hydrogen_idxs(mol)
    pdb_2_nhpdb = get_pdb_2_nhpdb(refpdbmol)
    refconf = refpdbmol.GetConformer(-1)
    molconf = newmol.GetConformer(-1)
    molconfid = molconf.GetId()
    hcount = 0
    for atom in refpdbmol.GetAtoms():
        if atom.GetSymbol() == 'H':
            idx = atom.GetIdx()
            pos = refconf.GetAtomPosition(idx)
            molconf.SetAtomPosition(hidxs[hcount], pos)
            hcount += 1
    newmol.AddConformer(molconf, assignId=molconfid)
    Chem.AssignAtomChiralTagsFromStructure(newmol, replaceExistingTags=True)
    return newmol
    def get_smiles(self, molecule):
        """
        Get smiles fromt mol
        """

        molecule_copy = copy.deepcopy(molecule)
        test_molecule = Chem.Mol(molecule_copy)
        test_molecule = Chem.RemoveHs(test_molecule)
        Chem.SanitizeMol(test_molecule)
        Chem.DetectBondStereochemistry(test_molecule, -1)
        Chem.AssignStereochemistry(test_molecule,
                                   flagPossibleStereoCenters=True,
                                   force=True)
        Chem.AssignAtomChiralTagsFromStructure(test_molecule, -1)

        smiles = Chem.MolToSmiles(test_molecule, isomericSmiles=True)

        return smiles
Example #19
0
def FindMatchCID(fn, CidSmilesDf):
    if os.path.exists("%s/%s/%s.list" %
                      (args.ligandfolder, fn.split('/')[-1].split(".")[0],
                       fn.split('/')[-1].split(".")[0])):
        return print("Already Computed")
    if not os.path.isdir("%s/%s" %
                         (args.ligandfolder, fn.split('/')[-1].split(".")[0])):
        os.makedirs("%s/%s" %
                    (args.ligandfolder, fn.split('/')[-1].split(".")[0]))
    print(fn)
    start = time.time()
    # PdbLig is the pdb ligand cast as mol object
    PdbLig = Chem.MolFromMolFile(fn)
    Chem.AssignAtomChiralTagsFromStructure(PdbLig)
    Chem.AssignStereochemistry(PdbLig,
                               cleanIt=False,
                               force=False,
                               flagPossibleStereoCenters=True)

    # Create Fingerprint For ligand
    fp1 = FingerprintingFx(Chem.RemoveHs(PdbLig))

    match_cid = []
    for index, row in CidSmilesDf.iterrows():  # Takes 20 seconds
        mc = Chem.RemoveHs(row['Mol'])
        fp2 = FingerprintingFx(mc)
        # preliminary check
        if (fp1 & fp2) == fp2:
            if PdbLig.HasSubstructMatch(row['Mol']):
                match_cid.append(index)

    pickle.dump(
        match_cid,
        open(
            "%s/%s/%s.list" %
            (args.ligandfolder, fn.split('/')[-1].split(".")[0],
             fn.split('/')[-1].split(".")[0]), "wb"))

    print("Finished analysing %s in %s s" % (fn, time.time() - start))

    return fn, match_cid
Example #20
0
def _get_stereo_labels_rdkit(molstr, propgetter):
    from rdkit import Chem

    mol = Chem.MolFromMolBlock(molstr, False, False)

    # if kekulizing fails, then e.g. [R1]P(=O)([O-])[R2] will be chiral.
    flags_warn = (Chem.SanitizeFlags.SANITIZE_KEKULIZE
                  | Chem.SanitizeFlags.SANITIZE_PROPERTIES)

    # raises an exception on invalid input
    Chem.SanitizeMol(mol, Chem.SanitizeFlags.SANITIZE_ALL & ~flags_warn)

    # warns about invalid input, but keeps going
    try:
        Chem.SanitizeMol(mol, flags_warn)
    except ValueError as e:
        print(' RDKit-Warning: ' + str(e).strip())

    Chem.AssignAtomChiralTagsFromStructure(mol)
    Chem.AssignStereochemistry(mol, True, True)

    return [propgetter(a) for a in mol.GetAtoms()]
Example #21
0
noconff = open('chembl_20_chiral.noconfs.smi', 'w+')
for i, line in enumerate(gzip.open('../Data/chembl_20_chiral.smi.gz')):
    line = line.strip().decode().split(' ')
    mol = Chem.MolFromSmiles(line[0])
    if not mol:
        continue
    cents = Chem.FindMolChiralCenters(mol, includeUnassigned=True)
    if len([y for x, y in cents if y == '?']):
        continue
    nm = line[1]
    csmi = Chem.MolToSmiles(mol, True)
    for j in range(100):
        mh = Chem.AddHs(mol)
        ok = AllChem.EmbedMolecule(mh, randomSeed=j + 1)
        if ok >= 0:
            Chem.AssignAtomChiralTagsFromStructure(mh)
            newm = Chem.RemoveHs(mh)
            smi = Chem.MolToSmiles(newm, True)
            if smi != csmi:
                print('%d %d %s:\n%s\n%s' % (i, j, nm, csmi, smi))
                print('%s %s %d' % (line[0], line[1], j + 1), file=outf)

                if v is not None:
                    v.ShowMol(mh, name='%s-%d' % (nm, j), showOnly=False)
                    break  # move immediately onto the next molecule
        else:
            print('noconf %d %d %s: %s' % (i, j, nm, line[0]))
            print('%s %s %d' % (line[0], line[1], j + 1), file=noconff)

    print('Done with mol %d' % i)
Example #22
0
def generate_graph(smiles, label=None):
    mol = MolFromSmiles(smiles)
    Chem.SanitizeMol(mol)
    Chem.DetectBondStereochemistry(mol, -1)
    Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True)
    Chem.AssignAtomChiralTagsFromStructure(mol, -1)

    if not mol:
        raise ValueError("Could not parse SMILES string:", smiles)

    SYMBOL = [
        'B', 'C', 'N', 'O', 'F', 'Si', 'P', 'S', 'Cl', 'As', 'Se', 'Br', 'Te',
        'I', 'At', 'other'
    ]
    HYBRIDIZATION = [
        Chem.rdchem.HybridizationType.SP,
        Chem.rdchem.HybridizationType.SP2,
        Chem.rdchem.HybridizationType.SP3,
        Chem.rdchem.HybridizationType.SP3D,
        Chem.rdchem.HybridizationType.SP3D2,
        'other',
    ]

    num_atom = Chem.RemoveHs(mol).GetNumAtoms()

    symbol = np.zeros((num_atom, 16), np.uint8)
    hybridization = np.zeros((num_atom, 6), np.uint8)
    degree = np.zeros((num_atom, 6), np.uint8)
    num_h = np.zeros((num_atom, 5), np.uint8)
    chirality = np.zeros((num_atom, 3), np.uint8)
    aromatic = np.zeros((num_atom, 1), np.uint8)
    formal_charge = np.zeros((num_atom, 1), np.float32)
    radical_electrons = np.zeros((num_atom, 1), np.float32)

    for i in range(num_atom):
        atom = mol.GetAtomWithIdx(i)
        symbol[i] = one_of_k_encoding_unk(atom.GetSymbol(), SYMBOL)
        hybridization[i] = one_of_k_encoding_unk(atom.GetHybridization(),
                                                 HYBRIDIZATION)
        degree[i] = one_of_k_encoding_unk(atom.GetDegree(), [0, 1, 2, 3, 4, 5])
        num_h[i] = one_of_k_encoding_unk(
            atom.GetTotalNumHs(includeNeighbors=True), [0, 1, 2, 3, 4])
        try:
            chirality[i] = one_of_k_encoding_unk(atom.GetProp('_CIPCode'),
                                                 ['R', 'S', 'unknown'])
        except:
            chirality[i] = one_of_k_encoding_unk(atom.GetChiralTag(), \
                                                 ['CHI_TETRAHEDRAL_CW', 'CHI_TETRAHEDRAL_CCW', 'CHI_UNSPECIFIED'])
        aromatic[i] = atom.GetIsAromatic()
        formal_charge[i] = atom.GetFormalCharge()
        radical_electrons[i] = atom.GetNumRadicalElectrons()

# #     abundant features
# #     won't bring substantial change to predictive performance, sometimes even worse

#     AtomicWeight = np.zeros((num_atom, 1), np.float32)
#     AtomicNumber = np.zeros((num_atom, 1), np.float32)
#     Rvdw = np.zeros((num_atom, 1), np.float32)
#     RCovalent = np.zeros((num_atom, 1), np.float32)
#     DefaultValence = np.zeros((num_atom, 1), np.float32)
#     valence = np.zeros((num_atom, 1), np.float32)
#     NOuterElecs = np.zeros((num_atom, 1), np.float32)
#     ring = np.zeros((num_atom, 7), np.uint8)
#     acceptor = np.zeros((num_atom, 1), np.uint8)
#     donor = np.zeros((num_atom, 1), np.uint8)

#     for i in range(num_atom):
#         atom = mol.GetAtomWithIdx(i)
#         AtomicNum = atom.GetAtomicNum()
#         AtomicNumber[i] = AtomicNum
#         AtomicWeight[i] = Chem.GetPeriodicTable().GetAtomicWeight(AtomicNum)
#         Rvdw[i] = Chem.GetPeriodicTable().GetRvdw(AtomicNum)  # (van der Waals radius)
#         RCovalent[i] = Chem.GetPeriodicTable().GetRcovalent(AtomicNum) #(covalent radius)
#         DefaultValence[i] = Chem.GetPeriodicTable().GetDefaultValence(AtomicNum)
#         valence[i] = atom.GetExplicitValence()
#         NOuterElecs[i] = Chem.GetPeriodicTable().GetNOuterElecs(AtomicNum)
#         ring[i] = [int(atom.IsInRing()), int(atom.IsInRingSize(3)), \
#                    int(atom.IsInRingSize(4)), int(atom.IsInRingSize(5)), \
#                    int(atom.IsInRingSize(6)), int(atom.IsInRingSize(7)), int(atom.IsInRingSize(8))]

#     factory = ChemicalFeatures.BuildFeatureFactory(os.path.join(RDConfig.RDDataDir, 'BaseFeatures.fdef'))
#     feature = factory.GetFeaturesForMol(mol)
#     for t in range(0, len(feature)):
#         if feature[t].GetFamily() == 'Donor':
#             for i in feature[t].GetAtomIds():
#                 donor[i] = 1
#         elif feature[t].GetFamily() == 'Acceptor':
#             for i in feature[t].GetAtomIds():
#                 acceptor[i] = 1

    num_bond = mol.GetNumBonds()
    if num_bond == 0:
        num_bond = 1  # except error caused by CH4, NH3
    bond_feat = np.zeros((num_bond * 2, 10), np.int16)
    bond_index = np.zeros((num_bond * 2, 2), np.int16)

    BOND_TYPE = [
        Chem.rdchem.BondType.SINGLE,
        Chem.rdchem.BondType.DOUBLE,
        Chem.rdchem.BondType.TRIPLE,
        Chem.rdchem.BondType.AROMATIC,
    ]

    BOND_STEREO = ["STEREONONE", "STEREOANY", "STEREOZ", "STEREOE"]
    ij = 0
    for i in range(num_atom):
        for j in range(num_atom):
            if i == j: continue
            bond = mol.GetBondBetweenAtoms(i, j)
            if bond is not None:
                atom1 = mol.GetAtomWithIdx(i)
                atom2 = mol.GetAtomWithIdx(j)
                bond_index[ij] = [i, j]
                bond_type = one_of_k_encoding(bond.GetBondType(), BOND_TYPE)
                bond_ring = [bond.GetIsConjugated(), bond.IsInRing()]
                bond_stereo = one_of_k_encoding(str(bond.GetStereo()),
                                                BOND_STEREO)
                bond_feat[ij] = bond_type + bond_ring + bond_stereo
                ij += 1
    atom_feat = np.concatenate([symbol, hybridization, degree, num_h, chirality, \
                                 aromatic, formal_charge, radical_electrons], -1)
    graph = Graph(
        smiles,
        atom_feat,
        bond_feat,
        bond_index,
        np.array(label).reshape((1, -1)),
    )

    return graph
  2  1  1  6
  2  3  1  0
  2  4  1  0
  4  5  2  0
  4  6  1  0
M  END""")
Draw.MolToImageFile(
    mol,
    '/drug_development/studyRdkit/st_rdcit/img/mol51.jpg',
    legend='L-alanin'
)

# L-丙氨酸

# 根据结构指定原子手性标记
Chem.AssignAtomChiralTagsFromStructure(mol)
# 找到分子的手性中心
chiral_center = Chem.FindMolChiralCenters(mol)
print(chiral_center)  # [(1, 'S')]

# 在smiles中也有该性质
sm = Chem.MolToSmiles(mol)
print(sm)  # C[C@H](N)C(=O)O
m2 = Chem.MolFromSmiles(sm)
Chem.AssignAtomChiralTagsFromStructure(m2)
chiral_center = Chem.FindMolChiralCenters(m2)
print(chiral_center)  # [(1, 'S')]

# 当以非异构体的形式输出读取时,因为分子不在有构象所以手性信息会丢失
m3 = Chem.MolToSmiles(mol, isomericSmiles=False)
print(m3)  # CC(N)C(=O)O
Example #24
0
    # print(p.getName(), p.getSpeed())
    if p.getSpeed() > speed:
        platform = p
        speed = p.getSpeed()

if platform.getName() == 'CUDA' or platform.getName() == 'OpenCL':
    platform.setPropertyDefaultValue('Precision', 'mixed')
    print('Set precision for platform', platform.getName(), 'to mixed')

# Read the molfile into RDKit, add Hs and create an openforcefield Molecule object
print('Reading ligand')
rdkitmol = Chem.MolFromMolFile(mol_in)
print('Adding hydrogens')
rdkitmolh = Chem.AddHs(rdkitmol, addCoords=True)
# ensure the chiral centers are all defined
Chem.AssignAtomChiralTagsFromStructure(rdkitmolh)
ligand_mol = Molecule(rdkitmolh)

print('Preparing system')
# Initialize a SystemGenerator using the GAFF for the ligand and tip3p for the water.
forcefield_kwargs = {
    'constraints': app.HBonds,
    'rigidWater': True,
    'removeCMMotion': False,
    'hydrogenMass': 4 * unit.amu
}
system_generator = SystemGenerator(
    forcefields=['amber/ff14SB.xml', 'amber/tip3p_standard.xml'],
    small_molecule_forcefield='gaff-2.11',
    molecules=[ligand_mol],
    forcefield_kwargs=forcefield_kwargs)