...and just for your information, I'd avoid repeated accesses to mol.atoms
as it creates a new list of the molecules atoms every time you call it
(which can be a speed hit). A good idea is to stick it in a variable on
first access and then refer to that variable.

On 9 August 2017 at 10:20, Noel O'Boyle <baoille...@gmail.com> wrote:

> Close, but not quite. You need to delete the C++ atom in the C++ molecule.
>
> Something like...
> for atom in mol:
>     remove.append(atom.OBAtom)
> for obatom in remove:
>     mol.OBMol.DeleteAtom(obatom)
>
> - Noel
>
> On 9 August 2017 at 09:46, David van der Spoel <sp...@xray.bmc.uu.se>
> wrote:
>
>> Hi,
>>
>> I am trying to remove ions from sdf compounds that I downloaded from
>> pubchem using a python script. The script correctly locates the atoms to
>> remove but the del command does not seem to have any effect. Am I making a
>> silly python error?
>>
>> An example input is here https://pubchem.ncbi.nlm.nih.gov/compound/20022
>>
>> Cheers,
>> --
>> David van der Spoel, Ph.D., Professor of Biology
>> Head of Department, Cell & Molecular Biology, Uppsala University.
>> Box 596, SE-75124 Uppsala, Sweden. Phone: +46184714205.
>> http://www.icm.uu.se
>>
>>
>> #!/usr/bin/env python3
>>
>> from pybel import *
>> import os, argparse
>>
>> def parseArguments():
>>     parser = argparse.ArgumentParser()
>>     parser.add_argument("-i", "--inputfile", help="Input file for
>> reading",   type=str,
>>         default=None)
>>     parser.add_argument("-o", "--outputfile", help="Output file for
>> writing",   type=str,
>>         default="out.sdf")
>>     args = parser.parse_args()
>>     return args
>>
>> if __name__ == '__main__':
>>     args  = parseArguments()
>>
>>     if (args.inputfile):
>>         print("Inputfile: %s Outputfile %s" % ( args.inputfile,
>> args.outputfile ) )
>>         if (os.path.isfile(args.outputfile)):
>>             os.remove(args.outputfile)
>>         outfile = Outputfile("sdf", args.outputfile)
>>         for mol in readfile("sdf", args.inputfile):
>>             print("Mol Weight %f" % mol.molwt)
>>             print("# atoms %d" % len(mol.atoms) )
>>             remove = []
>>             for aa in range(len(mol.atoms)):
>>                 atom = mol.atoms[aa]
>>                 # print("Type %s Valence %s" % ( atom.type, atom.valence
>> ) )
>>                 if ((atom.type == "Cl" or atom.type == "Br") and
>>                     atom.valence == 0):
>>                     # We found an ion
>>                     remove.append(aa)
>>             print("There are %d atoms to remove" % len(remove))
>>             for r in remove:
>>                 del mol.atoms[r]
>>             outfile.write(mol)
>>             # Only write one molecule, break out of the loop
>>             break
>>         outfile.close()
>>
>> ------------------------------------------------------------
>> ------------------
>> Check out the vibrant tech community on one of the world's most
>> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>> _______________________________________________
>> OpenBabel-discuss mailing list
>> OpenBabel-discuss@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
>>
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to