(smiles,
                    canonize=True,
                    min_atoms=-1,
                    max_atoms=-1,
                    return_num_atoms=False,
                    allowed_tokens=None,
                    allow_charges=False,
                    return_max_len=False,
                    logging="warn")
Takes list of SMILES strings and returns list of their sanitized versions.
    For definition of sanitized SMILES check
    http://www.rdkit.org/docs/api/rdkit.Chem.rdmolops-module.html#SanitizeMol
        Args:
            smiles (list): list of SMILES strings
            canonize (bool): parameter specifying whether to return
            canonical SMILES or not.
            min_atoms (int): minimum allowed number of atoms
            max_atoms (int): maxumum allowed number of atoms
            return_num_atoms (bool): return additional array of atom numbers
            allowed_tokens (iterable, optional): allowed tokens set
            allow_charges (bool): allow nonzero charges of atoms
            logging ("warn", "info", "none"): logging level
        Output:
            new_smiles (list): list of SMILES and NaNs if SMILES string is
            invalid or unsanitized.
            If 'canonize = True', return list of canonical SMILES.
        When 'canonize = True' the function is analogous to:
        canonize_smiles(smiles, sanitize=True).
Takes list of SMILES strings and returns list of their sanitized versions.
    For definition of sanitized SMILES check
    http://www.rdkit.org/docs/api/rdkit.Chem.rdmolops-module.html#SanitizeMol
        Args:
            smiles (list): list of SMILES strings
            canonize (bool): parameter specifying whether to return
            canonical SMILES or not.
            min_atoms (int): minimum allowed number of atoms
            max_atoms (int): maxumum allowed number of atoms
            return_num_atoms (bool): return additional array of atom numbers
            allowed_tokens (iterable, optional): allowed tokens set
            allow_charges (bool): allow nonzero charges of atoms
            logging ("warn", "info", "none"): logging level
        Output:
            new_smiles (list): list of SMILES and NaNs if SMILES string is
            invalid or unsanitized.
            If 'canonize = True', return list of canonical SMILES.
        When 'canonize = True' the function is analogous to:
        canonize_smiles(smiles, sanitize=True).
Takes list of SMILES strings and returns list of their sanitized versions . For definition of sanitized SMILES check http : // www . rdkit . org / docs / api / rdkit . Chem . rdmolops - module . html#SanitizeMol Args : smiles ( list ) : list of SMILES strings canonize ( bool ) : parameter specifying whether to return canonical SMILES or not . min_atoms ( int ) : minimum allowed number of atoms max_atoms ( int ) : maxumum allowed number of atoms return_num_atoms ( bool ) : return additional array of atom numbers allowed_tokens ( iterable optional ) : allowed tokens set allow_charges ( bool ) : allow nonzero charges of atoms logging ( warn info none ) : logging level Output : new_smiles ( list ) : list of SMILES and NaNs if SMILES string is invalid or unsanitized . If canonize = True return list of canonical SMILES . When canonize = True the function is analogous to : canonize_smiles ( smiles sanitize = True ) .
def sanitize_smiles(smiles,
                    canonize=True,
                    min_atoms=-1,
                    max_atoms=-1,
                    return_num_atoms=False,
                    allowed_tokens=None,
                    allow_charges=False,
                    return_max_len=False,
                    logging="warn"):
    """
    Takes list of SMILES strings and returns list of their sanitized versions.
    For definition of sanitized SMILES check
    http://www.rdkit.org/docs/api/rdkit.Chem.rdmolops-module.html#SanitizeMol
        Args:
            smiles (list): list of SMILES strings
            canonize (bool): parameter specifying whether to return
            canonical SMILES or not.
            min_atoms (int): minimum allowed number of atoms
            max_atoms (int): maxumum allowed number of atoms
            return_num_atoms (bool): return additional array of atom numbers
            allowed_tokens (iterable, optional): allowed tokens set
            allow_charges (bool): allow nonzero charges of atoms
            logging ("warn", "info", "none"): logging level
        Output:
            new_smiles (list): list of SMILES and NaNs if SMILES string is
            invalid or unsanitized.
            If 'canonize = True', return list of canonical SMILES.
        When 'canonize = True' the function is analogous to:
        canonize_smiles(smiles, sanitize=True).
    """
    assert logging in ["warn", "info", "none"]

    new_smiles = []
    idx = []
    num_atoms = []
    smiles_lens = []
    for i in range(len(smiles)):
        sm = smiles[i]
        mol = Chem.MolFromSmiles(sm, sanitize=False)
        sm_new = Chem.MolToSmiles(mol) if canonize and mol is not None else sm
        good = mol is not None
        if good and allowed_tokens is not None:
            good &= all([t in allowed_tokens for t in sm_new])

        if good and not allow_charges:
            good &= all([a.GetFormalCharge() == 0 for a in mol.GetAtoms()])

        if good:
            n = mol.GetNumAtoms()
            if (n < min_atoms and min_atoms > -1) or (n > max_atoms > -1):
                good = False
        else:
            n = 0

        if good:
            new_smiles.append(sm_new)
            idx.append(i)
            num_atoms.append(n)
            smiles_lens.append(len(sm_new))
        else:
            new_smiles.append(' ')
            num_atoms.append(0)

    smiles_set = set(new_smiles)
    num_unique = len(smiles_set) - ('' in smiles_set)
    if len(idx) > 0:
        valid_unique_rate = float(num_unique) / len(idx)
        invalid_rate = 1.0 - float(len(idx)) / len(smiles)
    else:
        valid_unique_rate = 0.0
        invalid_rate = 1.0
    num_bad = len(smiles) - len(idx)

    if len(idx) != len(smiles) and logging == "warn":
        warnings.warn('{:d}/{:d} unsanitized smiles ({:.1f}%)'.format(num_bad, len(smiles), 100 * invalid_rate))
    elif logging == "info":
        print("Valid: {}/{} ({:.2f}%)".format(len(idx), len(smiles), 100 * (1 - invalid_rate)))
        print("Unique valid: {:.2f}%".format(100 * valid_unique_rate))

    if return_num_atoms and return_max_len:
        return new_smiles, idx, num_atoms, max(smiles_lens)
    elif return_num_atoms and not return_max_len:
        return new_smiles, idx, num_atoms
    elif not return_num_atoms and return_max_len:
        return new_smiles, idx, max(smiles_lens)
    else:
        return new_smiles, idx
def sanitize_smiles ( smiles , canonize = True , min_atoms = - 1 , max_atoms = - 1 , return_num_atoms = False , allowed_tokens = None , allow_charges = False , return_max_len = False , logging = "warn" ) : assert logging in [ "warn" , "info" , "none" ] new_smiles = [ ] idx = [ ] num_atoms = [ ] smiles_lens = [ ] for i in range ( len ( smiles ) ) : sm = smiles [ i ] mol = Chem . MolFromSmiles ( sm , sanitize = False ) sm_new = Chem . MolToSmiles ( mol ) if canonize and mol is not None else sm good = mol is not None if good and allowed_tokens is not None : good &= all ( [ t in allowed_tokens for t in sm_new ] ) if good and not allow_charges : good &= all ( [ a . GetFormalCharge ( ) == 0 for a in mol . GetAtoms ( ) ] ) if good : n = mol . GetNumAtoms ( ) if ( n < min_atoms and min_atoms > - 1 ) or ( n > max_atoms > - 1 ) : good = False else : n = 0 if good : new_smiles . append ( sm_new ) idx . append ( i ) num_atoms . append ( n ) smiles_lens . append ( len ( sm_new ) ) else : new_smiles . append ( ' ' ) num_atoms . append ( 0 ) smiles_set = set ( new_smiles ) num_unique = len ( smiles_set ) - ( '' in smiles_set ) if len ( idx ) > 0 : valid_unique_rate = float ( num_unique ) / len ( idx ) invalid_rate = 1.0 - float ( len ( idx ) ) / len ( smiles ) else : valid_unique_rate = 0.0 invalid_rate = 1.0 num_bad = len ( smiles ) - len ( idx ) if len ( idx ) != len ( smiles ) and logging == "warn" : warnings . warn ( '{:d}/{:d} unsanitized smiles ({:.1f}%)' . format ( num_bad , len ( smiles ) , 100 * invalid_rate ) ) elif logging == "info" : print ( "Valid: {}/{} ({:.2f}%)" . format ( len ( idx ) , len ( smiles ) , 100 * ( 1 - invalid_rate ) ) ) print ( "Unique valid: {:.2f}%" . format ( 100 * valid_unique_rate ) ) if return_num_atoms and return_max_len : return new_smiles , idx , num_atoms , max ( smiles_lens ) elif return_num_atoms and not return_max_len : return new_smiles , idx , num_atoms elif not return_num_atoms and return_max_len : return new_smiles , idx , max ( smiles_lens ) else : return new_smiles , idx