Iterating over atoms + atom deletion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Iterating over atoms + atom deletion

Pascal Muller-3
Hi all,

I would like to generate the Murcko framework for a library.
CDK should be able to do this, but the code should be updated and the
function is currently not fully usable - if I understood well.
If anybody is aware of an other solution...?

So, I'm beginning to do a script with pybel. First, I just want to
remove atoms which have only one neighbour.

If there less than 2 neighbours, I do this:

for atom in ob.OBMolAtomIter(mol.OBMol):
        (...)
        if (neighborsCount <= 1):
            mol.OBMol.DeleteAtom(atom)

But it scans only 13 atoms, with 2 atom deleted, instead of 14 in the
test molecule (and theorically 3 deletion).
If I don't delete atoms, all is doing well (i.e., it scans all 14 atoms).
Any advice for doing it in the right manner?

Full code below, Open Babel 2.2.3.
Smiles used:
c1(n(c2c(cccc2)o1)C[C@@H](N)C)=O        ART12966845

Thanks,
Regards,
Pascal



#!/usr/bin/env python
# encoding: utf-8

import sys
import os, pybel
import openbabel as ob

def main():
        if len(sys.argv) < 2:
                print "No input file provided: Murcko.py filetosprocess.ext"
                sys.exit(1)
        for mol in pybel.readfile(sys.argv[1].split('.')[1], sys.argv[1]):
                mol.OBMol.DeleteHydrogens()
                atomCount = 0
                for atom in ob.OBMolAtomIter(mol.OBMol):
                        atomCount = atomCount + 1
                print "Atom count:", atomCount
                atomCount = 0
                for atom in ob.OBMolAtomIter(mol.OBMol):
                        atomCount = atomCount + 1
                        if not atom.IsInRing():
                                neighborsCount = 0
                                for neighbor in ob.OBAtomAtomIter(atom):
                                        neighborsCount = neighborsCount + 1
                                if (neighborsCount <= 1):
                                        print "Idx:", atom.GetIdx(), "Type:", atom.GetType(),"Deleted"
                                        mol.OBMol.DeleteAtom(atom)
                print "Atom count:", atomCount
                smilesMurcko = mol.write("smi")
                print "Current smiles:",  smilesMurcko

if __name__ == '__main__':
        main()

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
OpenBabel-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Iterating over atoms + atom deletion

Noel O'Boyle
Administrator
Hi Pascal,

You cannot iterate over something that you are changing. (This is a
general rule in C++, and maybe in life)

Instead you need too loops: in the first you create a list of OBAtoms
you want to delete; in the second, you iterate over this list and
delete them.

BTW, try googling "pybel murcko" - Florian Nigsch has written an implementation.

- Noel

On 1 October 2010 14:43, Pascal Muller <[hidden email]> wrote:

> Hi all,
>
> I would like to generate the Murcko framework for a library.
> CDK should be able to do this, but the code should be updated and the
> function is currently not fully usable - if I understood well.
> If anybody is aware of an other solution...?
>
> So, I'm beginning to do a script with pybel. First, I just want to
> remove atoms which have only one neighbour.
>
> If there less than 2 neighbours, I do this:
>
> for atom in ob.OBMolAtomIter(mol.OBMol):
>        (...)
>        if (neighborsCount <= 1):
>            mol.OBMol.DeleteAtom(atom)
>
> But it scans only 13 atoms, with 2 atom deleted, instead of 14 in the
> test molecule (and theorically 3 deletion).
> If I don't delete atoms, all is doing well (i.e., it scans all 14 atoms).
> Any advice for doing it in the right manner?
>
> Full code below, Open Babel 2.2.3.
> Smiles used:
> c1(n(c2c(cccc2)o1)C[C@@H](N)C)=O        ART12966845
>
> Thanks,
> Regards,
> Pascal
>
>
>
> #!/usr/bin/env python
> # encoding: utf-8
>
> import sys
> import os, pybel
> import openbabel as ob
>
> def main():
>        if len(sys.argv) < 2:
>                print "No input file provided: Murcko.py filetosprocess.ext"
>                sys.exit(1)
>        for mol in pybel.readfile(sys.argv[1].split('.')[1], sys.argv[1]):
>                mol.OBMol.DeleteHydrogens()
>                atomCount = 0
>                for atom in ob.OBMolAtomIter(mol.OBMol):
>                        atomCount = atomCount + 1
>                print "Atom count:", atomCount
>                atomCount = 0
>                for atom in ob.OBMolAtomIter(mol.OBMol):
>                        atomCount = atomCount + 1
>                        if not atom.IsInRing():
>                                neighborsCount = 0
>                                for neighbor in ob.OBAtomAtomIter(atom):
>                                        neighborsCount = neighborsCount + 1
>                                if (neighborsCount <= 1):
>                                        print "Idx:", atom.GetIdx(), "Type:", atom.GetType(),"Deleted"
>                                        mol.OBMol.DeleteAtom(atom)
>                print "Atom count:", atomCount
>                smilesMurcko = mol.write("smi")
>                print "Current smiles:",  smilesMurcko
>
> if __name__ == '__main__':
>        main()
>
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________
> OpenBabel-discuss mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
>

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
OpenBabel-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Iterating over atoms + atom deletion

Pascal Muller-3
Hi,


> You cannot iterate over something that you are changing. (This is a
> general rule in C++, and maybe in life)

Ok, right.


> Instead you need too loops: in the first you create a list of OBAtoms
> you want to delete;

Create a list? :) I'll need to google and find an exemple to copy
paste... hope to find some code easy to understand.

> in the second, you iterate over this list and delete them.

Ok.
Currently, I bypassed the problem by checking if the molecule has an
atom with only one neighbor. As long the answer is yes, I delete some
atom with the snippet of code I just sended, with:

isAnAtomWithOneNeighbor = checkIfIsOnlyOneNeighbor(mol)
        while (isAnAtomWithOneNeighbor):
                mol = delete(mol)
                isAnAtomWithOneNeighbor = checkIfIsOnlyOneNeighbor(mol)


I'll try do a list of atom(s) to delete.


> BTW, try googling "pybel murcko" - Florian Nigsch has written an implementation.

Sure, I already did it, but Florian Nigsch generate all cyclic
fragments, and not the Murcko framework.
With CDK, there is also fragmenter2.java from Rajarshi Guha, but it
generates all possible fragments - cyclic or not.

Thanks for your answer!
Pascal

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
OpenBabel-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Iterating over atoms + atom deletion

Geoffrey Hutchison
> Create a list? :) I'll need to google and find an exemple to copy
> paste... hope to find some code easy to understand.

There's plenty of code in src/mol.cpp, for example for deleting hydrogens:

    OBMol *mol;
    OBAtom *atom;
    vector<OBAtom*>::iterator i;
    vector<OBAtom*> delatoms;

    for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
      if (atom->IsHydrogen())
        delatoms.push_back(atom);

    for (i = delatoms.begin();i != delatoms.end();++i)
      DeleteAtom((OBAtom *)*i);

Hope that helps,
-Geoff


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
OpenBabel-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss