def main(argv=[__name__]):

    itf = oechem.OEInterface(InterfaceData)
    oechem.OEConfigureSplitMolComplexOptions(itf)

    if not oechem.OEParseCommandLine(itf, argv):
        oechem.OEThrow.Fatal("Unable to interpret command line!")

    if itf.GetBool("-verbose"):
        oechem.OEThrow.SetLevel(oechem.OEErrorLevel_Verbose)

    iname = itf.GetString("-in")
    oname = itf.GetString("-out")

    ims = oechem.oemolistream()
    if not itf.GetUnsignedInt("-modelnum") == 1:
        ims.SetFlavor(
            oechem.OEFormat_PDB,
            oechem.OEGetDefaultIFlavor(oechem.OEFormat_PDB)
            & ~oechem.OEIFlavor_PDB_ENDM)
    if not ims.open(iname):
        oechem.OEThrow.Fatal("Cannot open input file!")

    oms = oechem.oemolostream()
    if not oms.open(oname):
        oechem.OEThrow.Fatal("Cannot open output file!")

    mol = oechem.OEGraphMol()
    if not oechem.OEReadMolecule(ims, mol):
        oechem.OEThrow.Fatal("Cannot read input file!")

    if itf.GetBool("-verbose"):
        oechem.OEThrow.SetLevel(oechem.OEErrorLevel_Verbose)

    opt = oechem.OESplitMolComplexOptions()
    oechem.OESetupSplitMolComplexOptions(opt, itf)

    # @ <SNIPPET-SPLIT-MOL-COMPLEX-LOWLEVEL-FRAGMENT>
    # for input molecule mol and options opt ...
    frags = oechem.OEAtomBondSetVector()

    if oechem.OEGetMolComplexFragments(frags, mol, opt):
        # ...
        # @ </SNIPPET-SPLIT-MOL-COMPLEX-LOWLEVEL-FRAGMENT>
        oechem.OEThrow.Verbose("Able to fragment mol complex from %s" % iname)
    else:
        oechem.OEThrow.Fatal("Unable to fragment mol complex from %s" % iname)

    # @ <SNIPPET-SPLIT-MOL-COMPLEX-LOWLEVEL-COMBINE>
    # for options opt and OEAtomBondSetVector frags produced earlier ...
    numSites = oechem.OECountMolComplexSites(frags)
    oechem.OEThrow.Verbose("sites %d" % numSites)

    lig = oechem.OEGraphMol()
    ligfilter = opt.GetLigandFilter()
    if not oechem.OECombineMolComplexFragments(lig, frags, opt, ligfilter):
        oechem.OEThrow.Warning("Unable to combine ligand frags from %s" %
                               mol.GetTitle())

    protComplex = oechem.OEGraphMol()
    p = opt.GetProteinFilter()
    w = opt.GetWaterFilter()
    if not oechem.OECombineMolComplexFragments(protComplex, frags, opt,
                                               oechem.OEOrRoleSet(p, w)):
        oechem.OEThrow.Warning("Unable to combine complex frags from %s" %
                               mol.GetTitle())
    # @ </SNIPPET-SPLIT-MOL-COMPLEX-LOWLEVEL-COMBINE>

    if not lig.NumAtoms() == 0:
        oechem.OEThrow.Verbose("  lig %s" % lig.GetTitle())
        oechem.OEWriteMolecule(oms, lig)

    if not protComplex.NumAtoms() == 0:
        oechem.OEThrow.Verbose(" prot %s" % protComplex.GetTitle())
        oechem.OEWriteMolecule(oms, protComplex)

    oms.close()
Exemplo n.º 2
0
    def split(system, ligand_res_name='LIG'):
        """
        This function splits the passed molecule in components and tracks the
        mapping between the original molecule and the split components. The
        mapping is created as separated atom component index sets.

        Parameters:
        -----------
        system: OEMol
            The system to split in components. The components are:
                the protein atoms,
                the protein carbon alpha atoms
                the water atoms,
                the ion atoms,
                the cofactor atoms
        Returns:
        --------
        dic_set: python dictionary
            The sysetm is splitted in a dictionary with token words as keys
            and for value the related atom set. The token keywords are:
                protein,
                ca_protein,
                ligand,
                water,
                ions,
                cofactors,
                system
        """

        # Define Empty sets
        lig_set = set()
        prot_set = set()
        ca_prot_set = set()
        wat_set = set()
        excp_set = set()
        ion_set = set()
        # cofactor_set = set()
        # system_set = set()

        # Atom Bond Set vector used to contains the whole system
        frags = oechem.OEAtomBondSetVector()

        # Define Options for the Filter
        opt = oechem.OESplitMolComplexOptions()

        # The protein filter is set to avoid that multiple
        # chains are separated during the splitting and peptide
        # molecules are recognized as ligands
        pf = oechem.OEMolComplexFilterFactory(
            oechem.OEMolComplexFilterCategory_Protein)
        peptide = oechem.OEMolComplexFilterFactory(
            oechem.OEMolComplexFilterCategory_Peptide)
        protein_filter = oechem.OEOrRoleSet(pf, peptide)
        opt.SetProteinFilter(protein_filter)

        # The ligand filter is set to recognize just the ligand
        lf = oechem.OEMolComplexFilterFactory(
            oechem.OEMolComplexFilterCategory_Ligand)
        not_protein_filter = oechem.OENotRoleSet(protein_filter)
        ligand_filter = oechem.OEAndRoleSet(lf, not_protein_filter)
        opt.SetLigandFilter(ligand_filter)

        # The water filter is set to recognize just water molecules
        wf = oechem.OEMolComplexFilterFactory(
            oechem.OEMolComplexFilterCategory_Water)
        opt.SetWaterFilter(wf)

        # Set Category
        cat = oechem.OEMolComplexCategorizer()
        cat.AddLigandName(ligand_res_name)
        opt.SetCategorizer(cat)

        # Define the system fragments
        if not oechem.OEGetMolComplexFragments(frags, system, opt):
            raise ValueError('Unable to generate the system fragments')

        # Set empty OEMol containers
        prot = oechem.OEMol()
        lig = oechem.OEMol()
        wat = oechem.OEMol()
        excp = oechem.OEMol()

        # Split the protein from the system
        atommap = oechem.OEAtomArray(system.GetMaxAtomIdx())
        if not oechem.OECombineMolComplexFragments(
                prot, frags, opt, opt.GetProteinFilter(), atommap):
            raise ValueError('Unable to split the Protein')
        # Populate the protein set and the protein carbon alpha set
        pred = oechem.OEIsAlphaCarbon()
        for sys_at in system.GetAtoms():
            sys_idx = sys_at.GetIdx()
            at_idx = atommap[sys_idx]
            if at_idx:
                prot_set.add(sys_idx)
                at = system.GetAtom(oechem.OEHasAtomIdx(sys_idx))
                if pred(at):
                    ca_prot_set.add(sys_idx)
                # print(sys_idx, '->', at_idx)

        # Split the ligand from the system
        atommap = oechem.OEAtomArray(system.GetMaxAtomIdx())
        if not oechem.OECombineMolComplexFragments(
                lig, frags, opt, opt.GetLigandFilter(), atommap):
            raise ValueError('Unable to split the Ligand')
        # Populate the ligand set
        for sys_at in system.GetAtoms():
            sys_idx = sys_at.GetIdx()
            at_idx = atommap[sys_idx]
            if at_idx:
                lig_set.add(sys_idx)
                # print(sys_idx, '->', at_idx)

        # Split the water from the system
        atommap = oechem.OEAtomArray(system.GetMaxAtomIdx())
        if not oechem.OECombineMolComplexFragments(
                wat, frags, opt, opt.GetWaterFilter(), atommap):
            raise ValueError('Unable to split the Water')
        # Populate the water set
        for sys_at in system.GetAtoms():
            sys_idx = sys_at.GetIdx()
            at_idx = atommap[sys_idx]
            if at_idx:
                wat_set.add(sys_idx)
                # print(sys_idx, '->', at_idx)

        # Split the excipients from the system
        atommap = oechem.OEAtomArray(system.GetMaxAtomIdx())
        if not oechem.OECombineMolComplexFragments(
                excp, frags, opt, opt.GetOtherFilter(), atommap):
            raise ValueError('Unable to split the Excipients')
        # Populate the excipient set
        for sys_at in system.GetAtoms():
            sys_idx = sys_at.GetIdx()
            at_idx = atommap[sys_idx]
            if at_idx:
                excp_set.add(sys_idx)
                # print(sys_idx, '->', at_idx)

        # Create the ions set
        for exc_idx in excp_set:
            atom = system.GetAtom(oechem.OEHasAtomIdx(exc_idx))
            if atom.GetDegree() == 0:
                ion_set.add(exc_idx)

        # Create the cofactor set
        cofactor_set = excp_set - ion_set

        # Create the system set
        system_set = prot_set | lig_set | excp_set | wat_set

        if len(system_set) != system.NumAtoms():
            raise ValueError("The total system atom number {} is different "
                             "from its set representation {}".format(
                                 system.NumAtoms(), system_set))

        # The dictionary is used to link the token keywords to the created molecule sets
        dic_set = {
            'ligand': lig_set,
            'protein': prot_set,
            'ca_protein': ca_prot_set,
            'water': wat_set,
            'ions': ion_set,
            'cofactors': cofactor_set,
            'system': system_set
        }

        return dic_set
def main(argv=[__name__]):

    itf = oechem.OEInterface(InterfaceData)
    oechem.OEConfigureSplitMolComplexOptions(itf)

    if not oechem.OEParseCommandLine(itf, argv):
        oechem.OEThrow.Fatal("Unable to interpret command line!")

    if itf.GetBool("-verbose"):
        oechem.OEThrow.SetLevel(oechem.OEErrorLevel_Verbose)

    iname = itf.GetString("-in")
    oname = itf.GetString("-out")

    ims = oechem.oemolistream()
    if not itf.GetUnsignedInt("-modelnum") == 1:
        ims.SetFlavor(
            oechem.OEFormat_PDB,
            oechem.OEGetDefaultIFlavor(oechem.OEFormat_PDB)
            & ~oechem.OEIFlavor_PDB_ENDM)
    if not ims.open(iname):
        oechem.OEThrow.Fatal("Cannot open input file!")

    oms = oechem.oemolostream()
    if not oms.open(oname):
        oechem.OEThrow.Fatal("Cannot open output file!")

    inmol = oechem.OEGraphMol()
    if not oechem.OEReadMolecule(ims, inmol):
        oechem.OEThrow.Fatal("Cannot read input file!")

    opts = oechem.OESplitMolComplexOptions()
    oechem.OESetupSplitMolComplexOptions(opts, itf)

    frags = oechem.OEAtomBondSetVector()
    if not oechem.OEGetMolComplexFragments(frags, inmol, opts):
        oechem.OEThrow.Fatal("Unable to split mol complex from %s" % iname)

    numSites = oechem.OECountMolComplexSites(frags)

    if itf.GetBool("-verbose"):
        oechem.OEThrow.SetLevel(oechem.OEErrorLevel_Verbose)
        oechem.OEThrow.Verbose("sites %d" % numSites)

    lig = oechem.OEGraphMol()
    prot = oechem.OEGraphMol()
    wat = oechem.OEGraphMol()
    other = oechem.OEGraphMol()

    if not oechem.OECombineMolComplexFragments(lig, frags, opts,
                                               opts.GetLigandFilter()):
        oechem.OEThrow.Fatal("Unable to split ligand from %s" % iname)

    if not oechem.OECombineMolComplexFragments(prot, frags, opts,
                                               opts.GetProteinFilter()):
        oechem.OEThrow.Fatal("Unable to split protein complex from %s" % iname)

    if not oechem.OECombineMolComplexFragments(wat, frags, opts,
                                               opts.GetWaterFilter()):
        oechem.OEThrow.Fatal("Unable to split waters from %s" % iname)

    if not oechem.OECombineMolComplexFragments(other, frags, opts,
                                               opts.GetOtherFilter()):
        oechem.OEThrow.Fatal("Unable to split other mols from %s" % iname)

    if not lig.NumAtoms() == 0:
        oechem.OEThrow.Verbose("  lig %s" % lig.GetTitle())
        oechem.OEWriteMolecule(oms, lig)

    if not prot.NumAtoms() == 0:
        oechem.OEThrow.Verbose(" prot %s" % prot.GetTitle())
        oechem.OEWriteMolecule(oms, prot)

    if not wat.NumAtoms() == 0:
        oechem.OEThrow.Verbose("  wat %s" % wat.GetTitle())
        oechem.OEWriteMolecule(oms, wat)

    if not other.NumAtoms() == 0:
        oechem.OEThrow.Verbose("other %s" % other.GetTitle())
        oechem.OEWriteMolecule(oms, other)

    oms.close()