diff ob_remIons.py @ 0:b0311f002a5f draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 8350bb3a632722c33bc91ec5238d47a536903729
author bgruening
date Sat, 20 May 2017 08:59:45 -0400
parents
children 78640d0127ce
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ob_remIons.py	Sat May 20 08:59:45 2017 -0400
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+"""
+    Input: molecular input file.
+    Output: Molecule file with removed ions and fragments.
+    Copyright 2012, Bjoern Gruening and Xavier Lucas
+"""
+import sys, os
+import argparse
+import openbabel
+openbabel.obErrorLog.StopLogging()
+import pybel
+
+def parse_command_line():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-iformat', default='sdf' , help='input file format')
+    parser.add_argument('-i', '--input', required=True, help='input file name')
+    parser.add_argument('-o', '--output', required=True, help='output file name')
+    return parser.parse_args()
+
+def remove_ions(args):
+    outfile = pybel.Outputfile(args.iformat, args.output, overwrite=True)
+    for mol in pybel.readfile(args.iformat, args.input):
+        if mol.OBMol.NumHvyAtoms() > 5:
+            mol.OBMol.StripSalts(0)
+            # Check if new small fragments have been created and remove them
+            if mol.OBMol.NumHvyAtoms() > 5:
+                outfile.write(mol)
+    outfile.close()
+
+def __main__():
+    """
+        Remove any counterion and delete any fragment but the largest one for each molecule.
+    """
+    args = parse_command_line()
+    remove_ions(args)
+
+if __name__ == "__main__" :
+    __main__()