def _get_smiles(inchi_term): '''Get smiles.''' try: mol = inchi.MolFromInchi(inchi_term, treatWarningAsError=True) return rdmolfiles.MolToSmiles(mol) except Exception: return None
def parse_f(f): names = [''] cid = -1 CAS = f.split('/')[1] if '/' in f else f CAS = CAS.split('.')[0] if CAS in ignored_CASs: return None failed_mol = False try: if CAS in syn_data: d = syn_data[CAS] if 'pubchem' in d: raise Exception( 'Pubchem specified, not trying to use the mol file') elif 'formula' in d: raise Exception( 'Formula specified, not trying to use the mol file') try: mol = Chem.MolFromMolFile(f) assert mol is not None except: print('Cannot read %s' % f) 1 / 0 try: inchi_val = inchi.MolToInchi(mol) except: print('BAILING ON %s' % f) 1 / 0 mol = inchi.MolFromInchi(inchi_val) # Works better for ions if mol is None: print('BAILING ON reconversion to mol %s' % f) 1 / 0 except: failed_mol = True if CAS in syn_data: d = syn_data[CAS] if 'pubchem' in d: if str(d['pubchem']) in mycache: cid, iupac_name, names, mw, smi, inchi_val, inchikey, formula = mycache[ str(d['pubchem'])] else: pc = Compound.from_cid(d['pubchem']) cid = pc.cid iupac_name = pc.iupac_name names = pc.synonyms mw = pc.molecular_weight smi = pc.canonical_smiles inchi_val = pc.inchi inchikey = pc.inchikey formula = pc.molecular_formula mycache[str(d['pubchem'])] = (cid, iupac_name, names, mw, smi, inchi_val, inchikey, formula) else: cid = -1 names = d['synonyms'] if 'synonyms' in d else [''] mw = float(d['MW']) smi = d['smiles'] if 'smiles' in d else '' formula = d['formula'] if 'formula' in d else '' inchi_val = d['inchi'] if 'inchi' in d else '' inchikey = d['inchikey'] if 'inchikey' in d else '' iupac_name = '' else: print('FAILED on %s and no custom data was available either' % CAS) return None if not failed_mol: smi = Chem.MolToSmiles(mol, True) inchi_val = inchi.MolToInchi(mol) inchikey = inchi.InchiToInchiKey(inchi_val) mw = Descriptors.MolWt(mol) # for i in mol.GetAtoms(): # if i.GetIsotope(): # mw = Descriptors.ExactMolWt(mol) # break formula = CalcMolFormula(mol, True, True) iupac_name = '' try: if not failed_mol: if str(inchikey) in mycache: cid, iupac_name, names = mycache[str(inchikey)] else: try: pc = get_compounds(inchikey, 'inchikey')[0] cid = pc.cid iupac_name = pc.iupac_name names = pc.synonyms mycache[str(inchikey)] = (cid, iupac_name, names) except: mycache[str(inchikey)] = (-1, '', ['']) except: cid = -1 iupac_name = '' names = [''] other_CAS = [] if CAS in pdf_data: d = pdf_data[CAS] name = d['Name'] if 'Other Names' in d: syns = d['Other Names'] else: syns = [] if not iupac_name: iupac_name = name else: syns.insert(0, name) if 'Deleted CAS' in d: other_CAS.extend(d['Deleted CAS']) if 'Alternate CAS' in d: other_CAS.extend(d['Alternate CAS']) syns = [i for i in syns if i not in dup_names] names = syns + [i for i in names if i not in all_names] + other_CAS actual_names = [] for name in names: if name in all_user_names: # If the name is in the user db, only add it if it corresponds to this CAS number if CAS in syn_data and 'synonyms' in syn_data[ CAS] and name in syn_data[CAS]['synonyms']: actual_names.append(name) else: # Discard it otherwise pass else: # If the name is not in the user db we're all good actual_names.append(name) if CAS in syn_data and 'synonyms' in syn_data[CAS]: # If the user has any syns for this cas number, add those names if the name hasn't already been aded for n in syn_data[CAS]['synonyms']: if n not in actual_names: actual_names.append(n) actual_names = [i for i in actual_names if i] if inchi_val is not None: inchi_val = inchi_val.replace('InChI=1S/', '') formula = serialize_formula(formula) s = '%d\t%s\t%s\t%g\t%s\t%s\t%s\t%s\t' % (cid, CAS, formula, mw, smi, inchi_val, inchikey, iupac_name) s += '\t'.join(actual_names) print(s) return None
for lig in chembl_help: if chembl[i][0] == lig[0] or chembl[i][0] == None or str( chembl[i][0]) == '': delete = True if delete == True: i = i + 1 continue chembl_help.append(list(chembl[i])) i = i + 1 #pprint (chembl_help) #Chembl standardize for lig in range(0, len(chembl_help)): #print ('Now I do this from Chembl: ' + chembl_help[lig][0]) mol = inchi.MolFromInchi(chembl_help[lig][0], sanitize=False) try: rdmolops.RemoveStereochemistry(mol) except Exception: print("Not able to remove stereochemistry. Chembl.") try: mol = standardise.run(mol) except standardise.StandardiseException as e: logging.warn(e.message) try: mol = s.standardize(mol) except Exception: print("Not able to standardize. Chembl.") try: mol = s.tautomer_parent(mol, skip_standardize=True) except Exception:
curs2.execute("select receptor_id, uniprot_name from receptor") receptor_dict = dict(curs2.fetchall()) curs3 = conn.cursor() curs3.execute("select source_id, article_doi from source") source_dict = dict(curs3.fetchall()) global similar_dict similar_dict = {} #drawing and saving picture of ligand, if not exists for i in ligand_d: exists = os.path.isfile( '/home/adam/django_database/web/static/web/inchi/ligand_' + str(i[0]) + '.png') if not exists: m = inchi.MolFromInchi(i[1], sanitize=False) if m != None and m != '': Draw.MolToFile( m, '/home/adam/django_database/web/static/web/inchi/ligand_' + str(i[0]) + '.png') class ActivityList(ListView): model = Activity def get_context_data(self, **kwargs): context = super(ActivityList, self).get_context_data(**kwargs) return context class ReceptorList(ListView):