comparison ob_remIons.py @ 14:c19608db6b15 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 327c29cc43f56d7067ab9fa51323ea31951db98b"
author bgruening
date Tue, 10 Nov 2020 20:38:46 +0000
parents bd678d7db2ae
children 9adf3fae2771
comparison
equal deleted inserted replaced
13:bd678d7db2ae 14:c19608db6b15
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2
2 """ 3 """
3 Input: molecular input file. 4 Input: molecular input file.
4 Output: Molecule file with removed ions and fragments. 5 Output: Molecule file with removed ions and fragments.
5 Copyright 2012, Bjoern Gruening and Xavier Lucas 6 Copyright 2012, Bjoern Gruening and Xavier Lucas
6 """ 7 """
13 def parse_command_line(): 14 def parse_command_line():
14 parser = argparse.ArgumentParser() 15 parser = argparse.ArgumentParser()
15 parser.add_argument('-iformat', default='sdf', help='input file format') 16 parser.add_argument('-iformat', default='sdf', help='input file format')
16 parser.add_argument('-i', '--input', required=True, help='input file name') 17 parser.add_argument('-i', '--input', required=True, help='input file name')
17 parser.add_argument('-o', '--output', required=True, help='output file name') 18 parser.add_argument('-o', '--output', required=True, help='output file name')
19 parser.add_argument('-idx', default=False, action='store_true', help='should output be an indexed text table? works only for inchi/smiles, otherwise is ignored')
18 return parser.parse_args() 20 return parser.parse_args()
19 21
20 22
21 def remove_ions(args): 23 def remove_ions(args):
22 outfile = pybel.Outputfile(args.iformat, args.output, overwrite=True) 24 with open(args.output, 'w') as outfile:
23 for mol in pybel.readfile(args.iformat, args.input): 25 for index, mol in enumerate(pybel.readfile(args.iformat, args.input)):
24 if mol.OBMol.NumHvyAtoms() > 5:
25 mol.OBMol.StripSalts(0)
26 if 'inchi' in mol.data:
27 del mol.data['inchi'] # remove inchi cache so modified mol is saved
28 # Check if new small fragments have been created and remove them
29 if mol.OBMol.NumHvyAtoms() > 5: 26 if mol.OBMol.NumHvyAtoms() > 5:
30 outfile.write(mol) 27 mol.OBMol.StripSalts(0)
31 outfile.close() 28 if 'inchi' in mol.data:
29 del mol.data['inchi'] # remove inchi cache so modified mol is saved
30
31 mol = mol.write(args.iformat) if mol.OBMol.NumHvyAtoms() > 5 else '\n'
32
33 if args.idx and args.iformat in ['inchi', 'smi']:
34 outfile.write(f'{index}\t{mol}')
35 elif mol != '\n':
36 outfile.write(f'{mol}')
32 37
33 38
34 def __main__(): 39 def __main__():
35 """ 40 """
36 Remove any counterion and delete any fragment but the largest one for each molecule. 41 Remove any counterion and delete any fragment but the largest one for each molecule.