Python rdkit.Chem.SanitizeMol() Examples

The following are 30 code examples of rdkit.Chem.SanitizeMol(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rdkit.Chem , or try the search function .
Example #1
Source File: molViewWidget.py    From rdeditor with GNU Lesser General Public License v3.0 7 votes vote down vote up
def sanitizeMol(self, kekulize=False, drawkekulize=False):
        self.computeNewCoords()
        self._drawmol = Chem.Mol(self._mol.ToBinary()) #Is this necessary?
        try:
            Chem.SanitizeMol(self._drawmol)
            self.sanitizeSignal.emit("Sanitizable")
        except:
            self.sanitizeSignal.emit("UNSANITIZABLE")
            self.logger.warning("Unsanitizable")
            try:
                self._drawmol.UpdatePropertyCache(strict=False)
            except:
                self.sanitizeSignal.emit("UpdatePropertyCache FAIL")
                self.logger.error("Update Property Cache failed")
        #Kekulize
        if kekulize:
            try:
                Chem.Kekulize(self._drawmol)
            except:
                self.logger.warning("Unkekulizable")
        try:
            self._drawmol = rdMolDraw2D.PrepareMolForDrawing(self._drawmol, kekulize=drawkekulize)
        except ValueError:  # <- can happen on a kekulization failure
            self._drawmol = rdMolDraw2D.PrepareMolForDrawing(self._drawmol, kekulize=False) 
Example #2
Source File: fixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def MolToTemplates(mol):
    """Prepare set of templates for a given PDB residue."""

    if mol.HasProp('_Name') and mol.GetProp('_Name') in ['DA', 'DG', 'DT', 'DC',
                                                         'A', 'G', 'T', 'C', 'U']:
        backbone = 'OP(=O)(O)OC'
    else:
        backbone = 'OC(=O)CN'

    match = mol.GetSubstructMatch(Chem.MolFromSmiles(backbone))
    mol2 = Chem.RWMol(mol)
    if match:
        mol2.RemoveAtom(match[0])

    Chem.SanitizeMol(mol2)
    mol2 = mol2.GetMol()
    return (mol, mol2) 
Example #3
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_interresidue_bonding():
    """Test if fixer removes wrong connections between residues"""

    molfile = os.path.join(test_dir, '4e6d_residues.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)

    mol = PreparePDBMol(mol)

    # check if O from PRO
    atom1 = mol.GetAtomWithIdx(11)
    assert atom1.GetAtomicNum() == 8
    assert atom1.GetPDBResidueInfo().GetResidueName() == 'PRO'
    # ...and N from GLN
    atom2 = mol.GetAtomWithIdx(22)
    assert atom2.GetAtomicNum() == 7
    assert atom2.GetPDBResidueInfo().GetResidueName() == 'GLN'
    # ...are not connected
    assert mol.GetBondBetweenAtoms(11, 22) is None

    # mol can be sanitized
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #4
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_intraresidue_bonding():
    """Test if fixer removes wrong connections within single residue"""

    molfile = os.path.join(test_dir, '1idg_connectivity.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol)

    # check if N and C from GLU20 are not connected
    atom1 = mol.GetAtomWithIdx(11)
    assert atom1.GetAtomicNum() == 7
    assert atom1.GetPDBResidueInfo().GetResidueName() == 'GLU'
    assert atom1.GetPDBResidueInfo().GetResidueNumber() == 20
    atom2 = mol.GetAtomWithIdx(13)
    assert atom2.GetAtomicNum() == 6
    assert atom2.GetPDBResidueInfo().GetResidueName() == 'GLU'
    assert atom2.GetPDBResidueInfo().GetResidueNumber() == 20

    assert mol.GetBondBetweenAtoms(11, 13) is None

    # mol can be sanitized
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #5
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_ring():
    """Test if fixer adds missing bond in ring"""

    molfile = os.path.join(test_dir, '4yzm_ring.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol)

    # check if there is double bond between N and C from MSE
    atom1 = mol.GetAtomWithIdx(12)
    assert atom1.GetAtomicNum() == 6
    assert atom1.GetPDBResidueInfo().GetResidueName() == 'PHE'
    atom2 = mol.GetAtomWithIdx(13)
    assert atom2.GetAtomicNum() == 6
    assert atom2.GetPDBResidueInfo().GetResidueName() == 'PHE'

    # there is a bond and it is aromatic
    bond = mol.GetBondBetweenAtoms(12, 13)
    assert bond is not None
    assert_almost_equal(bond.GetBondTypeAsDouble(), 1.5)

    # mol can be sanitized
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #6
Source File: PyPretreatMolutil.py    From PyBioMed with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def normalize(self, mol):
        """Apply a series of Normalization transforms to correct functional groups and recombine charges.

        A series of transforms are applied to the molecule. For each Normalization, the transform is applied repeatedly
        until no further changes occur. If any changes occurred, we go back and start from the first Normalization
        again, in case the changes mean an earlier transform is now applicable. The molecule is returned once the entire
        series of Normalizations cause no further changes or if max_restarts (default 200) is reached.

        :param mol: The molecule to normalize.
        :type mol: :rdkit:`Mol <Chem.rdchem.Mol-class.html>`
        :return: The normalized fragment.
        :rtype: :rdkit:`Mol <Chem.rdchem.Mol-class.html>`
        """
        log.debug("Running Normalizer")
        # Normalize each fragment separately to get around quirky RunReactants behaviour
        fragments = []
        for fragment in Chem.GetMolFrags(mol, asMols=True):
            fragments.append(self._normalize_fragment(fragment))
        # Join normalized fragments into a single molecule again
        outmol = fragments.pop()
        for fragment in fragments:
            outmol = Chem.CombineMols(outmol, fragment)
        Chem.SanitizeMol(outmol)
        return outmol 
Example #7
Source File: sparse_molecular_dataset.py    From MolGAN with MIT License 6 votes vote down vote up
def matrices2mol(self, node_labels, edge_labels, strict=False):
        mol = Chem.RWMol()

        for node_label in node_labels:
            mol.AddAtom(Chem.Atom(self.atom_decoder_m[node_label]))

        for start, end in zip(*np.nonzero(edge_labels)):
            if start > end:
                mol.AddBond(int(start), int(end), self.bond_decoder_m[edge_labels[start, end]])

        if strict:
            try:
                Chem.SanitizeMol(mol)
            except:
                mol = None

        return mol 
Example #8
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_many_missing():
    """Test parsing residues with **many** missing atoms and bonds"""

    molfile = os.path.join(test_dir, '2wb5_GLN.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol)

    assert mol.GetNumAtoms() == 5
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE

    assert mol.GetAtomWithIdx(4).GetDegree() == 0

    # test if removal works
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol, remove_incomplete=True)

    assert mol.GetNumAtoms() == 0
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #9
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_remove_incomplete():
    """Test removing residues with missing atoms"""

    molfile = os.path.join(test_dir, '3cx9_TYR.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)

    # keep all residues
    new_mol = PreparePDBMol(mol, remove_incomplete=False)
    assert new_mol.GetNumAtoms() == 23
    residues = set()
    for atom in new_mol.GetAtoms():
        residues.add(atom.GetPDBResidueInfo().GetResidueNumber())
    assert residues, {137, 138 == 139}
    assert Chem.SanitizeMol(new_mol) == Chem.SanitizeFlags.SANITIZE_NONE

    # remove residue with missing sidechain
    new_mol = PreparePDBMol(mol, remove_incomplete=True)
    assert new_mol.GetNumAtoms() == 17
    residues = set()
    for atom in new_mol.GetAtoms():
        residues.add(atom.GetPDBResidueInfo().GetResidueNumber())
    assert residues, {137 == 139}
    assert Chem.SanitizeMol(new_mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #10
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_metal_bonding():
    """Test if fixer disconnects metals"""

    molfile = os.path.join(test_dir, '1ps3_zn.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)

    mol = PreparePDBMol(mol)

    atom = mol.GetAtomWithIdx(36)
    assert atom.GetAtomicNum() == 30  # is it Zn
    assert atom.GetDegree() == 0  # Zn should have no bonds
    assert atom.GetFormalCharge() == 2
    assert atom.GetNumExplicitHs() == 0

    # mol can be sanitized
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #11
Source File: normalize.py    From MolVS with MIT License 6 votes vote down vote up
def _apply_transform(self, mol, rule):
        """Repeatedly apply normalization transform to molecule until no changes occur.

        It is possible for multiple products to be produced when a rule is applied. The rule is applied repeatedly to
        each of the products, until no further changes occur or after 20 attempts. If there are multiple unique products
        after the final application, the first product (sorted alphabetically by SMILES) is chosen.
        """
        mols = [mol]
        for n in six.moves.range(20):
            products = {}
            for mol in mols:
                for product in [x[0] for x in rule.RunReactants((mol,))]:
                    if Chem.SanitizeMol(product, catchErrors=True) == 0:
                        products[Chem.MolToSmiles(product, isomericSmiles=True)] = product
            if products:
                mols = [products[s] for s in sorted(products)]
            else:
                # If n == 0, the rule was not applicable and we return None
                return mols[0] if n > 0 else None 
Example #12
Source File: normalize.py    From MolVS with MIT License 6 votes vote down vote up
def normalize(self, mol):
        """Apply a series of Normalization transforms to correct functional groups and recombine charges.

        A series of transforms are applied to the molecule. For each Normalization, the transform is applied repeatedly
        until no further changes occur. If any changes occurred, we go back and start from the first Normalization
        again, in case the changes mean an earlier transform is now applicable. The molecule is returned once the entire
        series of Normalizations cause no further changes or if max_restarts (default 200) is reached.

        :param mol: The molecule to normalize.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: The normalized fragment.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        log.debug('Running Normalizer')
        # Normalize each fragment separately to get around quirky RunReactants behaviour
        fragments = []
        for fragment in Chem.GetMolFrags(mol, asMols=True):
            fragments.append(self._normalize_fragment(fragment))
        # Join normalized fragments into a single molecule again
        outmol = fragments.pop()
        for fragment in fragments:
            outmol = Chem.CombineMols(outmol, fragment)
        Chem.SanitizeMol(outmol)
        return outmol 
Example #13
Source File: resonance.py    From MolVS with MIT License 6 votes vote down vote up
def enumerate(self, mol):
        """Enumerate all possible resonance forms and return them as a list.

        :param mol: The input molecule.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: A list of all possible resonance forms of the molecule.
        :rtype: list of rdkit.Chem.rdchem.Mol
        """
        flags = 0
        if self.kekule_all:
            flags = flags | Chem.KEKULE_ALL
        if self.allow_incomplete_octets:
            flags = flags | Chem.ALLOW_INCOMPLETE_OCTETS
        if self.allow_charge_separation:
            flags = flags | Chem.ALLOW_CHARGE_SEPARATION
        if self.unconstrained_anions:
            flags = flags | Chem.UNCONSTRAINED_ANIONS
        if self.unconstrained_cations:
            flags = flags | Chem.UNCONSTRAINED_CATIONS
        results = []
        for result in Chem.ResonanceMolSupplier(mol, flags=flags, maxStructs=self.max_structures):
            # This seems necessary? ResonanceMolSupplier only does a partial sanitization
            Chem.SanitizeMol(result)
            results.append(result)
        return results

        # Potentially interesting: getNumConjGrps(), getBondConjGrpIdx() and getAtomConjGrpIdx() 
Example #14
Source File: chemistry.py    From guacamol with MIT License 6 votes vote down vote up
def smiles_to_rdkit_mol(smiles: str) -> Optional[Chem.Mol]:
    """
    Converts a SMILES string to a RDKit molecule.

    Args:
        smiles: SMILES string of the molecule

    Returns:
        RDKit Mol, None if the SMILES string is invalid
    """
    mol = Chem.MolFromSmiles(smiles)

    #  Sanitization check (detects invalid valence)
    if mol is not None:
        try:
            Chem.SanitizeMol(mol)
        except ValueError:
            return None

    return mol 
Example #15
Source File: sanifix4.py    From auto_martini with GNU General Public License v2.0 6 votes vote down vote up
def _recursivelyModifyNs(mol,matches,indices=None):
    if indices is None:
        indices=[]
    res=None
    while len(matches) and res is None:
        tIndices=indices[:]
        nextIdx = matches.pop(0)
        tIndices.append(nextIdx)
        nm = Chem.Mol(mol.ToBinary())
        nm.GetAtomWithIdx(nextIdx).SetNoImplicit(True)
        nm.GetAtomWithIdx(nextIdx).SetNumExplicitHs(1)
        cp = Chem.Mol(nm.ToBinary())
        try:
            Chem.SanitizeMol(cp)
        except ValueError:
            res,indices = _recursivelyModifyNs(nm,matches,indices=tIndices)
        else:
            indices=tIndices
            res=cp
    return res,indices 
Example #16
Source File: chem.py    From spektral with MIT License 6 votes vote down vote up
def validate_rdkit_mol(mol):
    """
    Sanitizes an RDKit molecules and returns True if the molecule is chemically
    valid.
    :param mol: an RDKit molecule 
    :return: True if the molecule is chemically valid, False otherwise
    """
    if rdc is None:
        raise ImportError('`validate_rdkit_mol` requires RDkit.')
    if len(rdc.GetMolFrags(mol)) > 1:
        return False
    try:
        rdc.SanitizeMol(mol)
        return True
    except ValueError:
        return False 
Example #17
Source File: PyPretreatMolutil.py    From PyBioMed with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def standardize(self, mol):
        """Return a standardized version the given molecule.

        The standardization process consists of the following stages: RDKit
        :rdkit:`RemoveHs <Chem.rdmolops-module.html#RemoveHs>`, RDKit
        :rdkit:`SanitizeMol <Chem.rdmolops-module.html#SanitizeMol>`, :class:`~molvs.metal.MetalDisconnector`,
        :class:`~molvs.normalize.Normalizer`, :class:`~molvs.charge.Reionizer`, RDKit
        :rdkit:`AssignStereochemistry <Chem.rdmolops-module.html#AssignStereochemistry>`.

        :param mol: The molecule to standardize.
        :type mol: :rdkit:`Mol <Chem.rdchem.Mol-class.html>`
        :returns: The standardized molecule.
        :rtype: :rdkit:`Mol <Chem.rdchem.Mol-class.html>`
        """
        mol = copy.deepcopy(mol)
        Chem.RemoveHs(mol)
        Chem.SanitizeMol(mol)
        mol = self.disconnect_metals(mol)
        mol = self.normalize(mol)
        mol = self.reionize(mol)
        Chem.AssignStereochemistry(mol, force=True, cleanIt=True)
        # TODO: Check this removes symmetric stereocenters
        return mol 
Example #18
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_multivalent_Hs():
    """Test if fixer deals with multivalent Hs"""

    # TODO: require mol without Hs in the future (rdkit v. 2018)
    molfile = os.path.join(test_dir, '2c92_hypervalentH.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol, residue_whitelist=[], removeHs=False)

    atom = mol.GetAtomWithIdx(84)
    assert atom.GetAtomicNum() == 1  # is it H
    assert atom.GetDegree() == 1  # H should have 1 bond

    for n in atom.GetNeighbors():  # Check if neighbor is from the same residue
        assert atom.GetPDBResidueInfo().GetResidueName() == n.GetPDBResidueInfo().GetResidueName()

    # mol can be sanitized
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #19
Source File: utils.py    From moses with MIT License 6 votes vote down vote up
def get_mol(smiles_or_mol):
    '''
    Loads SMILES/molecule into RDKit's object
    '''
    if isinstance(smiles_or_mol, str):
        if len(smiles_or_mol) == 0:
            return None
        mol = Chem.MolFromSmiles(smiles_or_mol)
        if mol is None:
            return None
        try:
            Chem.SanitizeMol(mol)
        except ValueError:
            return None
        return mol
    return smiles_or_mol 
Example #20
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_custom_templates():
    """Test using custom templates"""

    molfile = os.path.join(test_dir, '3cx9_TYR.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)

    templates = {
        'TYR': 'CCC(N)C=O',
        'LYS': 'NC(C(O)=O)CCCCN',
        'LEU': 'CC(C)CC(N)C(=O)O',
    }

    mol_templates = {resname: Chem.MolFromSmiles(smi)
                     for resname, smi in templates.items()}

    for kwargs in ({'custom_templates': {'TYR': 'CCC(N)C=O'}},
                   {'custom_templates': {'TYR': Chem.MolFromSmiles('CCC(N)C=O')}},
                   {'custom_templates': templates, 'replace_default_templates': True},
                   {'custom_templates': mol_templates, 'replace_default_templates': True}):

        # use TYR without sidechain - all matches should be complete
        new_mol = PreparePDBMol(mol, remove_incomplete=True, **kwargs)
        assert new_mol.GetNumAtoms() == 23
        residues = set()
        for atom in new_mol.GetAtoms():
            residues.add(atom.GetPDBResidueInfo().GetResidueNumber())
        assert residues, {137, 138 == 139}
        assert Chem.SanitizeMol(new_mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #21
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_HOH_bonding():
    """Test if fixer unbinds HOH"""

    molfile = os.path.join(test_dir, '2vnf_bindedHOH.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    # don't use templates and don't remove waters
    mol = PreparePDBMol(mol, removeHOHs=False)

    atom = mol.GetAtomWithIdx(5)
    assert atom.GetPDBResidueInfo().GetResidueName() == 'HOH'
    assert atom.GetDegree() == 0  # HOH should have no bonds

    # mol can be sanitized
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #22
Source File: test_rdkitfixer.py    From oddt with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_aromatic_ring():
    """Test aromaticity for partial matches"""

    # ring is complete and should be aromatic
    molfile = os.path.join(test_dir, '5ar7_HIS.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol)

    atom = mol.GetAtomWithIdx(6)
    assert atom.GetAtomicNum() == 7
    info = atom.GetPDBResidueInfo()
    assert info.GetResidueName() == 'HIS'
    assert info.GetResidueNumber() == 246
    assert info.GetName().strip() == 'ND1'
    assert atom.GetIsAromatic()

    atom = mol.GetAtomWithIdx(9)
    assert atom.GetAtomicNum() == 7
    info = atom.GetPDBResidueInfo()
    assert info.GetResidueName() == 'HIS'
    assert info.GetResidueNumber() == 246
    assert info.GetName().strip() == 'NE2'
    assert atom.GetIsAromatic()

    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE

    # there is only one atom from the ring and it shouldn't be aromatic
    molfile = os.path.join(test_dir, '3cx9_TYR.pdb')
    mol = Chem.MolFromPDBFile(molfile, sanitize=False, removeHs=False)
    mol = PreparePDBMol(mol)

    atom = mol.GetAtomWithIdx(14)
    assert atom.GetAtomicNum() == 6
    info = atom.GetPDBResidueInfo()
    assert info.GetResidueName() == 'TYR'
    assert info.GetResidueNumber() == 138
    assert info.GetName().strip() == 'CG'
    assert not atom.GetIsAromatic()
    assert Chem.SanitizeMol(mol) == Chem.SanitizeFlags.SANITIZE_NONE 
Example #23
Source File: PyPretreatMolutil.py    From PyBioMed with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _apply_transform(self, mol, rule):
        """Repeatedly apply normalization transform to molecule until no changes occur.

        It is possible for multiple products to be produced when a rule is applied. The rule is applied repeatedly to
        each of the products, until no further changes occur or after 20 attempts. If there are multiple unique products
        after the final application, the first product (sorted alphabetically by SMILES) is chosen.
        """
        mols = [mol]
        for n in xrange(20):
            products = {}
            for mol in mols:
                for product in [x[0] for x in rule.RunReactants((mol,))]:
                    if Chem.SanitizeMol(product, catchErrors=True) == 0:
                        products[
                            Chem.MolToSmiles(product, isomericSmiles=True)
                        ] = product
            if products:
                mols = [products[s] for s in sorted(products)]
            else:
                # If n == 0, the rule was not applicable and we return None
                return mols[0] if n > 0 else None


# ==============================================================================
# from .tautomer import TAUTOMER_TRANSFORMS, TAUTOMER_SCORES, MAX_TAUTOMERS, TautomerCanonicalizer, TautomerEnumerator
# ============================================================================== 
Example #24
Source File: chemistry.py    From guacamol with MIT License 5 votes vote down vote up
def neutralise_charges(mol, reactions=None):
    replaced = False

    for i, (reactant, product) in enumerate(reactions):
        while mol.HasSubstructMatch(reactant):
            replaced = True
            rms = AllChem.ReplaceSubstructs(mol, reactant, product)
            mol = rms[0]
    if replaced:
        Chem.SanitizeMol(mol)
        return mol, True
    else:
        return mol, False 
Example #25
Source File: molecule_utils.py    From graph-tutorial.pytorch with MIT License 5 votes vote down vote up
def num_bond_features():
    simple_mol = Chem.MolFromSmiles('CC')
    Chem.SanitizeMol(simple_mol)
    return len(bond_features(simple_mol.GetBonds()[0]))

# Create (feature, adj) from a mol 
Example #26
Source File: xyz2mol.py    From BCAI_kaggle_CHAMPS with MIT License 5 votes vote down vote up
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 
Example #27
Source File: mol_graph.py    From KerasNeuralFingerprint with MIT License 5 votes vote down vote up
def num_bond_features():
    # Return length of feature vector using a very simple molecule.
    simple_mol = Chem.MolFromSmiles('CC')
    Chem.SanitizeMol(simple_mol)
    return len(bond_features(simple_mol.GetBonds()[0])) 
Example #28
Source File: canonicalization.py    From ASKCOS with Mozilla Public License 2.0 5 votes vote down vote up
def fix_smiles(self, old_smiles, removeMap = True):
		'''
		For a given SMILES string, this function "fixes" common mistakes
		found in the Lowe parsed database:
		- N=c[nH] structures are turned into the normal [NH]-c[n] forms
		- iminols are turned into amides/carbamates

		It applies the reactions in self.rxns until the SMILES string doesn't change
		'''
		mol = Chem.MolFromSmiles(old_smiles)
		if removeMap: [x.ClearProp('molAtomMapNumber') for x in mol.GetAtoms()]
		if not mol: 
			return old_smiles 

		new_smiles = Chem.MolToSmiles(mol, isomericSmiles = True)
		old_smiles = ''
		while new_smiles != old_smiles:
			old_smiles = new_smiles
			for rxn in self.rxns:
				outcomes = rxn.RunReactants((mol,))
				if not outcomes: 
					continue
				else:
					mol = outcomes[0][0]
					Chem.SanitizeMol(mol)
					new_smiles = Chem.MolToSmiles(mol, isomericSmiles = True)

		return new_smiles 
Example #29
Source File: canonicalization.py    From ochem_predict_nn with MIT License 5 votes vote down vote up
def fix_smiles(self, old_smiles, removeMap = True):
		'''
		For a given SMILES string, this function "fixes" common mistakes
		found in the Lowe parsed database:
		- N=c[nH] structures are turned into the normal [NH]-c[n] forms
		- iminols are turned into amides/carbamates

		It applies the reactions in self.rxns until the SMILES string doesn't change
		'''
		mol = Chem.MolFromSmiles(old_smiles)
		if removeMap: [x.ClearProp('molAtomMapNumber') for x in mol.GetAtoms()]
		if not mol: 
			return old_smiles 

		new_smiles = Chem.MolToSmiles(mol, isomericSmiles = USE_STEREOCHEMISTRY)
		old_smiles = ''
		while new_smiles != old_smiles:
			old_smiles = new_smiles
			for rxn in self.rxns:
				outcomes = rxn.RunReactants((mol,))
				if not outcomes: 
					continue
				else:
					mol = outcomes[0][0]
					Chem.SanitizeMol(mol)
					new_smiles = Chem.MolToSmiles(mol, isomericSmiles = USE_STEREOCHEMISTRY)

		return new_smiles 
Example #30
Source File: dataset_utils.py    From rl_graph_generation with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def nx_to_mol(G):
    mol = Chem.RWMol()
    atomic_nums = nx.get_node_attributes(G, 'atomic_num')
    chiral_tags = nx.get_node_attributes(G, 'chiral_tag')
    formal_charges = nx.get_node_attributes(G, 'formal_charge')
    node_is_aromatics = nx.get_node_attributes(G, 'is_aromatic')
    node_hybridizations = nx.get_node_attributes(G, 'hybridization')
    num_explicit_hss = nx.get_node_attributes(G, 'num_explicit_hs')
    node_to_idx = {}
    for node in G.nodes():
        a=Chem.Atom(atomic_nums[node])
        a.SetChiralTag(chiral_tags[node])
        a.SetFormalCharge(formal_charges[node])
        a.SetIsAromatic(node_is_aromatics[node])
        a.SetHybridization(node_hybridizations[node])
        a.SetNumExplicitHs(num_explicit_hss[node])
        idx = mol.AddAtom(a)
        node_to_idx[node] = idx

    bond_types = nx.get_edge_attributes(G, 'bond_type')
    for edge in G.edges():
        first, second = edge
        ifirst = node_to_idx[first]
        isecond = node_to_idx[second]
        bond_type = bond_types[first, second]
        mol.AddBond(ifirst, isecond, bond_type)

    Chem.SanitizeMol(mol)
    return mol