Python rdkit.Chem.AllChem.MolToSmiles() Examples

The following are 6 code examples of rdkit.Chem.AllChem.MolToSmiles(). 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.AllChem , or try the search function .
Example #1
Source File: outputfingerprints.py    From qsar-tools with Apache License 2.0 6 votes vote down vote up
def calcfingerprint(mol, args):
    '''Return a list of the bits/cnts of the fingerprint of mol.
    Uses fingerprint settings from args which should have been the result
    of a parse of addfpargs'''
    if args.fp == 'rdkit':
        fp = Chem.RDKFingerprint(mol,fpSize=args.fpbits)
        return [int(x) for x in fp.ToBitString()]
    elif args.fp.startswith('ecfp'):
        diameter = int(args.fp.replace('ecfp',''))
        r = diameter/2
        fp = Chem.GetMorganFingerprintAsBitVect(mol,r,nBits=args.fpbits)
        return [int(x) for x in fp.ToBitString()]
    elif args.fp == 'maccs':
        fp = MACCSkeys.GenMACCSKeys(mol)
        return [int(x) for x in fp.ToBitString()]
    elif args.fp == 'smarts':
        if args.smartsfile:
            smarts = args.smartsfile
            ret = [0]*len(smarts)
            for (i,smart) in enumerate(smarts):
                if mol.HasSubstructMatch(smart):
                    ret[i] = 1
            return ret
        else:
            sys.stderr.write("ERROR: Must provide SMARTS file with --smarts\n")
            sys.exit(-1)
    elif args.fp == 'fp2':
        smi = Chem.MolToSmiles(mol) 
        obmol = pybel.readstring('smi',smi)
        fp = obmol.calcfp(fptype='FP2')
        ret = [0]*1021 #FP2 are mod 1021
        for setbit in fp.bits:
            #but pybel makes the bits start at 1 for some reason
            assert(setbit>0)
            ret[setbit-1] = 1
            
        return ret
        
    else:
        return [] 
Example #2
Source File: flipchiral.py    From rdkit-scripts with MIT License 6 votes vote down vote up
def make_iso_smiles(mol):
    '''enumerate chiral centers and return list of smiles'''
    #for some reason rdkit can't detect chiral centers without a structure
    Chem.EmbedMultipleConfs(mol)
    Chem.rdmolops.AssignAtomChiralTagsFromStructure(mol)
    chirals = [] # chiral atoms
    Chem.rdmolops.AssignStereochemistry(mol) #make sure this is done
    for a in mol.GetAtoms():
        if a.GetChiralTag() == Chem.rdchem.CHI_TETRAHEDRAL_CCW or a.GetChiralTag() == Chem.rdchem.CHI_TETRAHEDRAL_CW:
            chirals.append(a)
    cmask = itertools.product(*[[0,1]]*len(chirals))
    if len(chirals) == 0:
        return [Chem.MolToSmiles(mol)]
    else:
        ret = []
        for m in cmask:
            for i in xrange(len(chirals)):
                if m[i]:
                    chirals[i].SetChiralTag(Chem.rdchem.CHI_TETRAHEDRAL_CCW)
                else:
                    chirals[i].SetChiralTag(Chem.rdchem.CHI_TETRAHEDRAL_CW)
            ret.append(Chem.MolToSmiles(mol,True))
        return ret 
Example #3
Source File: mol_utils.py    From chemical_vae with Apache License 2.0 5 votes vote down vote up
def get_molecule_smi(mol_obj):
    return Chem.MolToSmiles(mol_obj) 
Example #4
Source File: mol_utils.py    From chemical_vae with Apache License 2.0 5 votes vote down vote up
def canon_smiles(smi):
    return Chem.MolToSmiles(Chem.MolFromSmiles(smi), isomericSmiles=True, canonical=True) 
Example #5
Source File: load_lowe_examples_into_db_details.py    From ochem_predict_nn with MIT License 4 votes vote down vote up
def mol_to_dic(node, withAmounts = False):
	'''Converts a node containing molecule information into a
	dictionary'''
	dic = {}
	# Get name
	dic['name'] = str(node.getElementsByTagName('name')[0].firstChild.nodeValue)
	# If exact entity match, more data is available
	#print(node.toprettyxml())
	#entityType = node.getElementsByTagName('dl:entityType')[0].firstChild.nodeValue
	#if entityType == 'exact' or entityType == 'definiteReference':
	identifiers = {
		child.attributes.getNamedItem('dictRef').value : \
		child.attributes.getNamedItem('value').value \
		for child in node.getElementsByTagName('identifier')
	}
	if 'cml:inchi' in identifiers.keys():
		mol = AllChem.MolFromInchi(str(identifiers['cml:inchi']))
	elif 'cml:smiles' in identifiers.keys():
		mol = AllChem.MolFromSmiles(str(identifiers['cml:smiles']))
	else:
		print('identifiers: {}'.format(identifiers.keys()))
		raise ValueError('No molecular identifier for {}'.format(dic['name']))
	if not mol: raise ValueError('Couldnt parse molecule: {}'.format(identifiers))

	Chem.SanitizeMol(mol)
	dic['smiles'] = AllChem.MolToSmiles(mol, isomericSmiles=True)
	dic['inchi'] = AllChem.MolToInchi(mol)
	# elif entityType == 'chemicalClass':
	# 	pass # name is all we get
	# else:
	# 	raise ValueError('Unknown entityType for molecule: {}'.format(entityType))
	# Quantity?
	if withAmounts:
		amounts = {
			child.attributes.getNamedItem('units').value : \
			child.firstChild.nodeValue \
			for child in node.getElementsByTagName('amount')
		}
		if 'unit:percentYield' in amounts.keys():
			dic['yield'] = float(amounts['unit:percentYield'])
		if 'unit:g' in amounts.keys():
			dic['amount(g)'] = float(amounts['unit:g'])
	return dic 
Example #6
Source File: encoder.py    From mhfp with MIT License 4 votes vote down vote up
def shingling_from_mol(in_mol, radius=3, rings=True, kekulize=True, min_radius=1):
        """Creates a molecular shingling from a RDKit molecule (rdkit.Chem.rdchem.Mol).
    
    Arguments:
      in_mol {rdkit.Chem.rdchem.Mol} -- A RDKit molecule instance
      radius {int} -- The MHFP radius (a radius of 3 corresponds to MHFP6)  (default: {3})
      rings {boolean} -- Whether or not to include rings in the shingling (default: {True})
      kekulize {boolean} -- Whether or not to kekulize the the extracted SMILES (default: {True})
      min_radius {int} -- The minimum radius that is used to extract n-grams (default: {1})
    
    Returns:
      list -- The molecular shingling.
    """

        shingling = []

        if rings:
            for ring in AllChem.GetSymmSSSR(in_mol):
                bonds = set()
                ring = list(ring)
                for i in ring:
                    for j in ring:
                        if i != j:
                            bond = in_mol.GetBondBetweenAtoms(i, j)
                            if bond != None:
                                bonds.add(bond.GetIdx())
                shingling.append(
                    AllChem.MolToSmiles(
                        AllChem.PathToSubmol(in_mol, list(bonds)), canonical=True
                    ).encode("utf-8")
                )

        if min_radius == 0:
            for i, atom in enumerate(in_mol.GetAtoms()):
                shingling.append(atom.GetSmarts().encode("utf-8"))

        for index, _ in enumerate(in_mol.GetAtoms()):
            for i in range(1, radius + 1):
                p = AllChem.FindAtomEnvironmentOfRadiusN(in_mol, i, index)
                amap = {}
                submol = AllChem.PathToSubmol(in_mol, p, atomMap=amap)

                if index not in amap:
                    continue

                smiles = AllChem.MolToSmiles(
                    submol, rootedAtAtom=amap[index], canonical=True
                )

                if smiles is not "":
                    shingling.append(smiles.encode("utf-8"))

        # Set ensures that the same shingle is not hashed multiple times
        # (which would not change the hash, since there would be no new minima)
        return list(set(shingling))