Also OBMol::CopySubstructure as described at
https://baoilleach.blogspot.com/2018/05/when-all-you-want-is-ring.html and
https://openbabel.github.io/api/3.0/classOpenBabel_1_1OBMol.shtml#a3271860ee44de0d38a27102c79913b1b
. Should be quite a bit faster I would think.

On Fri, 3 Mar 2023, 14:19 Richard Overstreet, <reoverstr...@charter.net>
wrote:

> Hi Tim,
>
> Deleting residues can be tricky. I've found the following to work well.
>
> from openbabel import openbabel
> obconversion = openbabel.OBConversion()
> obconversion.SetInAndOutFormats("pdb", "pdb")
> mol = openbabel.OBMol()
> path = obconversion.ReadFile(mol, 'demo.pdb')
>
> non_lig_atoms=[]
>
> for i in range(mol.NumAtoms()):
>     atom=mol.GetAtom(i+1)
>     resname=atom.GetResidue().GetName()
>     if resname != 'LIG':
>         print(resname)
>         non_lig_atoms.append(atom)
>
> for atom in non_lig_atoms:
>     mol.DeleteAtom(atom)
>
> obconversion.WriteFile(mol,'nolig_demo.pdb')
>
> $ cat demo.pdb
> ATOM      1  N   ALA A   1      -0.525   1.362   0.000  1.00
> 0.00           N
> ATOM      2  CA  ALA A   1       0.000   0.000   0.000  1.00
> 0.00           C
> ATOM      3  C   ALA A   1       1.520   0.000   0.000  1.00
> 0.00           C
> ATOM      4  O   ALA A   1       2.152   0.808  -0.678  1.00
> 0.00           O
> ATOM      5  CB  ALA A   1      -0.507  -0.774  -1.206  1.00
> 0.00           C
> ATOM      6  O   LIG A   2       4.957  -2.553  -0.341  1.00
> 0.00           O
> ATOM      7  N   LIG A   2       2.116  -0.911   0.764  1.00
> 0.00           N
> ATOM      8  CB  LIG A   2       4.075  -0.753   2.287  1.00
> 0.00           C
> ATOM      9  CA  LIG A   2       3.571  -1.003   0.842  1.00
> 0.00           C
> ATOM     10  C6  LIG A   2       5.566  -0.553   2.346  1.00
> 0.00           C
> ATOM     11  C7  LIG A   2       6.420  -1.538   2.843  1.00
> 0.00           C
> ATOM     12  C8  LIG A   2       6.121   0.622   1.833  1.00
> 0.00           C
> ATOM     13  C   LIG A   2       3.965  -2.393   0.368  1.00
> 0.00           C
> ATOM     14  C10 LIG A   2       7.800  -1.358   2.825  1.00
> 0.00           C
> ATOM     15  C11 LIG A   2       7.500   0.804   1.811  1.00
> 0.00           C
> ATOM     16  C12 LIG A   2       8.342  -0.187   2.305  1.00
> 0.00           C
> ATOM     17  N   ALA A   3       3.188  -3.399   0.759  1.00
> 0.00           N
> ATOM     18  CA  ALA A   3       3.470  -4.776   0.365  1.00
> 0.00           C
> ATOM     19  C   ALA A   3       3.451  -4.925  -1.148  1.00
> 0.00           C
> ATOM     20  O   ALA A   3       3.668  -6.018  -1.670  1.00
> 0.00           O
> ATOM     21  CB  ALA A   3       2.461  -5.731   0.981  1.00
> 0.00           C
> ATOM     22  OXT ALA A   3       3.219  -3.956  -1.868  1.00
> 0.00           O
> TER      23      ALA A
> 3
> END
>
> The trick is to store a list of atoms to delete. This way is the more
> stable.
> On 3/3/23 03:34, Tim Dudgeon wrote:
>
> I'm trying to work out how to use openbabel from Python to perform
> operations such as extracting out ligands from macromolecules in PDB or CIF
> format. For instance, if I have a ligand that has the residue name of LIG,
> how do I create a new molecule with just that ligand so that I can write it
> out in, for instance, molfile format.
>
> If found that I can iterate through the residues and filter them with
> something like this:
>
> for obres in openbabel.OBResidueIter(obmol_from_cif_file):
>     if obres.GetName() == 'LIG':
>         print('ligand res')
>     else:
>         print('other res')
>
> But it's not clear to me how to create a new mol or to delete the residues
> I don't want. If I just use obmol_from_cif_file.DeleteResidue(obres) I
> end up getting a Segmentation fault.
>
> Thanks
> Tim
>
>
>
> _______________________________________________
> OpenBabel-discuss mailing 
> listOpenBabel-discuss@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/openbabel-discuss
>
> _______________________________________________
> OpenBabel-discuss mailing list
> OpenBabel-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
>
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to