Beispiel #1
0
def mol_supplier(lines, no_halt, assign_descriptors):
    """Yields molecules generated from CTAB text

    Args:
        lines (iterable): CTAB text lines
        no_halt (boolean):
            True: shows warning messages for invalid format and go on.
            False: throws an exception for it and stop parsing.
        assign_descriptors (boolean):
            if True, default descriptors are automatically assigned.
    """
    def sdf_block(lns):
        mol = []
        opt = []
        is_mol = True
        for line in lns:
            if line.startswith("$$$$"):
                yield mol[:], opt[:]
                is_mol = True
                mol.clear()
                opt.clear()
            elif line.startswith("M  END"):
                is_mol = False
            elif is_mol:
                mol.append(line.rstrip())
            else:
                opt.append(line.rstrip())
        if mol:
            yield mol, opt

    for i, (mol, opt) in enumerate(sdf_block(lines)):
        try:
            c = molecule(mol)
            if assign_descriptors:
                molutil.assign_descriptors(c)
        except ValueError as err:
            if no_halt:
                print("Unsupported symbol: {} (#{} in v2000reader)".format(
                    err, i + 1))
                c = molutil.null_molecule(assign_descriptors)
            else:
                raise ValueError("Unsupported symbol: {}".format(err))
        except:
            if no_halt:
                print("Unexpected error (#{} in v2000reader)".format(i + 1))
                c = molutil.null_molecule(assign_descriptors)
                c.data = optional_data(opt)
                yield c
                continue
            else:
                print(traceback.format_exc())
                raise Exception("Unsupported Error")
        c.data = optional_data(opt)
        yield c
Beispiel #2
0
def from_indigo(idgmol, assign_descriptor=True):
    """Convert RDMol to molecule"""
    mol = Compound()
    for atom in idgmol.iterateAtoms():
        key = atom.index()
        a = Atom(atom.symbol())
        a.coords = list(atom.xyz())
        mol.add_atom(key, a)
    for bond in idgmol.iterateBonds():
        u = bond.source()
        v = bond.destination()
        b = Bond()
        b.order = int(bond.bondOrder())
        mol.add_bond(u.index(), v.index(), b)
    if assign_descriptor:
        molutil.assign_descriptors(mol)
    return mol
Beispiel #3
0
def smiles_to_compound(smiles, assign_descriptors=True):
    """Convert SMILES text to compound object

    Raises:
        ValueError: SMILES with unsupported format
    """
    it = iter(smiles)
    mol = molecule()
    try:
        for token in it:
            mol(token)
        result, _ = mol(None)
    except KeyError as err:
        raise ValueError("Unsupported Symbol: {}".format(err))
    result.graph.remove_node(0)
    logger.debug(result)
    if assign_descriptors:
        molutil.assign_descriptors(result)
    return result
Beispiel #4
0
def from_rdmol(rdmol, assign_descriptor=True):
    """Convert RDMol to molecule"""
    mol = Compound()
    conf = rdmol.GetConformer()
    Chem.Kekulize(rdmol)
    for atom in rdmol.GetAtoms():
        key = atom.GetIdx()
        a = Atom(atom.GetSymbol())
        a.coords = conf.GetAtomPosition(key)
        mol.add_atom(key, a)
    for bond in rdmol.GetBonds():
        u = bond.GetBeginAtomIdx()
        v = bond.GetEndAtomIdx()
        b = Bond()
        b.order = int(bond.GetBondTypeAsDouble())
        mol.add_bond(u, v, b)
    if assign_descriptor:
        molutil.assign_descriptors(mol)
    return mol