Ejemplo n.º 1
0
def smiles_to_inchi_inchikey(smiles,verbose=0):
          
    error_counter=0
    warning_counter=0
    inchis=[]
    inchis2=[]
    inchiKeys=[]

    for elem in smiles:
        mol=Chem.MolFromSmiles(elem) 
        if(mol!=None):
            inchi, retcode, message, logs, aux =rdinchi.MolToInchi(mol)
            inchiKey=rdinchi.InchiToInchiKey(inchi)   
            inchis.append(inchi)  
            inchiKeys.append(inchiKey)   
            try:
                mol, retcode, message, logs=rdinchi.InchiToMol(inchi)
                if(mol!=None):                
                    inchi2, retcode, message, logs, aux =rdinchi.MolToInchi(mol)
                    inchis2.append(inchi2)  
                else: 
                    inchis2.append("XXX") 
                    if(verbose!=0):
                        print(elem+ ": InChI cannot converted to mol object, added XXX instead!")
            except:
                if(verbose!=0):
                    print(retcode)
                    print(message)
                    print("Smiles:"+elem)
                inchis2.append("XXX")  
            
        else:  
            inchis.append("XXX")
            inchiKeys.append("XXX")
            inchis2.append("XXX")            
            if(verbose!=0):
                print(elem+ ": can not converted added XXX instead! ")            
            error_counter=error_counter+1

    for i1,i2 in zip(inchis,inchis2):
        if(i1!=i2):
            if(verbose!=0):
                print("Warning:"+i1+" - "+i2)
            warning_counter=warning_counter+1

    print("\nGeneration of InChI and InChIKey from SMILES is completed.")    
    print("Total errors:"+str(error_counter))
    print("Total warnings:"+str(warning_counter)+"\n")    
                                          
    return inchis,inchiKeys
Ejemplo n.º 2
0
def MolFromInchi(inchi,
                 sanitize=True,
                 removeHs=True,
                 logLevel=None,
                 treatWarningAsError=False):
    """Construct a molecule from a InChI string

    Keyword arguments:
    sanitize -- set to True to enable sanitization of the molecule. Default is
    True
    removeHs -- set to True to remove Hydrogens from a molecule. This only
    makes sense when sanitization is enabled
    logLevel -- the log level used for logging logs and messages from InChI
    API. set to None to diable the logging completely
    treatWarningAsError -- set to True to raise an exception in case of a
    molecule that generates warning in calling InChI API. The resultant
    molecule  and error message are part of the excpetion

    Returns:
    a rdkit.Chem.rdchem.Mol instance
    """
    try:
        mol, retcode, message, log = rdinchi.InchiToMol(
            inchi, sanitize, removeHs)
    except ValueError as e:
        logger.error(str(e))
        return None

    if logLevel is not None:
        if logLevel not in logLevelToLogFunctionLookup:
            raise ValueError("Unsupported log level: %d" % logLevel)
        log = logLevelToLogFunctionLookup[logLevel]
        if retcode == 0:
            log(message)

    if retcode != 0:
        if retcode == 1:
            logger.warning(message)
        else:
            logger.error(message)
    if treatWarningAsError and retcode != 0:
        raise InchiReadWriteError(mol, message)
    return mol