Example #1
0
 def fix(self) -> Rectifier:
     self.fix_rings()  # from _RectifierRingMixin
     self.prevent_oddities()  # from _RectifierOddMixin
     self.ununspecified_bonds()  # from _RectifierValenceMixin
     self.triage_rings()  # from _RectifierValenceMixin
     Chem.Cleanup(self.rwmol)
     self.fix_issues()  # from _RectifierValenceMixin
     Chem.SanitizeMol(self.rwmol, sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL)
     return self
Example #2
0
 def __init__(self,
              mol: Chem.Mol,
              valence_correction: str = 'element',
              debug: bool = False):
     self.valence_correction = valence_correction
     self.mol = mol
     self._valence_mode = 'max'
     self._iterations_done = 0
     self._subiterations_done = 0
     self.ununspecified_bonds()
     self.triage_rings()
     Chem.Cleanup(self.mol)
     self.fix_issues()
     Chem.SanitizeMol(self.mol, sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL)
Example #3
0
def ketcher_clean():

    mol = Chem.MolFromMolBlock(request.json['struct'])

    if mol is not None:

        output_format = 'chemical/x-mdl-molfile'
        Chem.Cleanup(mol)
        response = {'format': output_format, 'struct': Chem.MolToMolBlock(mol)}

        return response
    else:
        response = {'error': 'Please provide valid structures'}
        return response, 400
Example #4
0
def generate_library(parent_smiles):
    parent_mol = Chem.MolFromSmiles(parent_smiles, sanitize=False)

    # append linkers to parent molecule to generate unsubstituted cores
    unsubstituted_cores = []
    place_holder_count = len(
        parent_mol.GetSubstructMatches(linker_placeholder_mol))
    for linker in linker_rxns:
        rxn = AllChem.ReactionFromSmarts(linker_rxns[linker])
        core = parent_mol
        for i in range(place_holder_count):
            new_mols = list(chain.from_iterable(rxn.RunReactants((core, ))))
            core = new_mols[0]
            Chem.SanitizeMol(core)
        unsubstituted_cores.append(core)

    # append terminal groups
    all_mols = []
    for core in unsubstituted_cores:
        place_holder_count = len(
            core.GetSubstructMatches(terminal_placeholder_mol))
        if place_holder_count == 0:
            all_mols.append(core)
            continue
        for terminal in terminal_rxns:
            new_mol = core
            rxn = AllChem.ReactionFromSmarts(terminal_rxns[terminal])
            for i in range(place_holder_count):
                new_mols = list(
                    chain.from_iterable(rxn.RunReactants((new_mol, ))))
                new_mol = new_mols[0]
                Chem.Cleanup(new_mol)
            all_mols.append(Chem.MolFromSmiles(Chem.MolToSmiles(new_mol)))

    # canonicalize smiles to remove duplicates
    all_mols = [
        Chem.MolFromSmiles(smiles)
        for smiles in [Chem.MolToSmiles(mol) for mol in all_mols]
    ]
    all_smiles = list(set([Chem.MolToSmiles(mol) for mol in all_mols]))

    return all_smiles
def create_rdkit_2d_mol_from_ccdc(mol):
    "create an rdkit molecule atom-by-atom. does not handle chirality"
    ed_mol = Chem.rdchem.EditableMol(Chem.rdchem.Mol())
    at_idx = {}
    # add atoms
    for a in mol.atoms:
        rd_atom = Chem.rdchem.Atom(a.atomic_number)
        rd_atom.SetFormalCharge(a.formal_charge)
        at_idx[a] = ed_mol.AddAtom(rd_atom)
    # add bonds
    for b in mol.bonds:
        ed_mol.AddBond(at_idx[b.atoms[0]], at_idx[b.atoms[1]],
                       rdkit_bond_type(b))
    # tidy up
    rd_mol = ed_mol.GetMol()
    rd_mol.ClearComputedProps()
    Chem.GetSymmSSSR(rd_mol)
    rd_mol.UpdatePropertyCache(False)
    Chem.Cleanup(rd_mol)
    Chem.SanitizeMol(rd_mol)
    return rd_mol
Example #6
0
def wash(mol, remove_stereo=False):
    """
    Perform a series of modifications to standardize a molecule.
    :param mol: an RDKit molecule
    :param remove_stereo: if True stereochemical information is removed
    :return: Smiles of a washed molecule object
    """
    mol = Chem.RemoveHs(mol)
    mol = saltDisconection(mol)
    remover = SaltRemover.SaltRemover()
    mol = remover.StripMol(mol)
    Chem.Cleanup(mol)
    mol, _ = NeutraliseCharges(mol)
    Chem.SanitizeMol(mol)
    if not remove_stereo:
        Chem.AssignStereochemistry(mol)
    Chem.SetAromaticity(mol)
    Chem.SetHybridization(mol)
    if remove_stereo:
        Chem.RemoveStereochemistry(mol)
    smi = Chem.MolToSmiles(mol)
    return smi
Example #7
0
def standardize(compound: AllChem.Mol,
                add_hs=True,
                remove_stereo=True,
                thorough=False) -> AllChem.Mol:
    """
    Standardizes an RDKit molecule by running various cleanup and sanitization operations.

    Parameters
    ----------
    compound : rdkit.Chem.rdchem.Mol
        A chemical compound.
    add_hs : bool
        If True, adds hydrogens to the compound.
    remove_stereo : bool
        If True, removes stereochemistry info from the compound.
    thorough : bool
        If True, removes charge, isotopes, and small fragments from the compound.

    Returns
    -------
    rdkit.Chem.rdchem.Mol
        The standardized compound.
    """
    # basic cleanup
    Chem.Cleanup(compound)
    Chem.SanitizeMol(compound,
                     sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL,
                     catchErrors=False)
    AllChem.AssignStereochemistry(compound,
                                  cleanIt=True,
                                  force=True,
                                  flagPossibleStereoCenters=True)

    # remove isotopes, neutralize charge
    if thorough:
        for atom in compound.GetAtoms():
            atom.SetIsotope(0)
        compound = _neutralize_charge(compound)
        Chem.SanitizeMol(compound,
                         sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL,
                         catchErrors=False)

    # remove stereochemistry
    if remove_stereo:
        Chem.RemoveStereochemistry(compound)

    # commute inchi
    compound = _commute_inchi(compound)

    # keep biggest fragment
    if thorough:
        compound = _strip_small_fragments(compound)
    Chem.SanitizeMol(compound,
                     sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL,
                     catchErrors=False)

    # neutralize charge
    compound = _neutralize_charge(compound)
    Chem.SanitizeMol(compound,
                     sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL,
                     catchErrors=False)

    # add protons
    if add_hs:
        return Chem.AddHs(compound, explicitOnly=False, addCoords=True)
    return compound
Example #8
0
# append terminal groups
all_mols = []
for core in unsubstituted_cores:
    place_holder_count = len(
        core.GetSubstructMatches(substituent_place_holder_mol))
    if place_holder_count == 0:
        all_mols.append(core)
        continue
    for terminal in terminal_rxns:
        new_mol = core
        rxn = AllChem.ReactionFromSmarts(terminal_rxns[terminal])
        for i in range(place_holder_count):
            new_mols = list(chain.from_iterable(rxn.RunReactants((new_mol, ))))
            new_mol = new_mols[0]
            Chem.Cleanup(new_mol)
        all_mols.append(Chem.MolFromSmiles(Chem.MolToSmiles(new_mol)))

# canonicalize smiles to remove duplicates
all_mols = [
    Chem.MolFromSmiles(smiles)
    for smiles in [Chem.MolToSmiles(mol) for mol in all_mols]
]
all_smiles = list(set([Chem.MolToSmiles(mol) for mol in all_mols]))

# create directory to store molecules
if not os.path.exists(molecule_name):
    os.makedirs(molecule_name)
out_folder = os.path.abspath(molecule_name)

# write list of SMILES to text file