diff multi_obgrep.py @ 13:b578d0c101cd draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
author bgruening
date Mon, 19 Oct 2020 14:43:40 +0000
parents bf4e668b6690
children 22a866858b81
line wrap: on
line diff
--- a/multi_obgrep.py	Tue Jul 28 08:41:46 2020 -0400
+++ b/multi_obgrep.py	Mon Oct 19 14:43:40 2020 +0000
@@ -4,40 +4,43 @@
     Output: Molecule file filtered with obgrep.
     Copyright 2013, Bjoern Gruening and Xavier Lucas
 """
-import sys, os
 import argparse
 import multiprocessing
-import tempfile
-import subprocess
-import shutil
+import os
 import shlex
+import shutil
+import subprocess
+import tempfile
 
-from openbabel import openbabel, pybel
-openbabel.obErrorLog.StopLogging()
+
 def parse_command_line():
     parser = argparse.ArgumentParser()
     parser.add_argument('-i', '--infile', required=True, help='Molecule file.')
-    parser.add_argument('-q', '--query',  required=True, help='Query file, containing different SMARTS in each line.')
+    parser.add_argument('-q', '--query', required=True, help='Query file, containing different SMARTS in each line.')
     parser.add_argument('-o', '--outfile', required=True, help='Path to the output file.')
     parser.add_argument("--iformat", help="Input format, like smi, sdf, inchi")
     parser.add_argument("--n-times", dest="n_times", type=int,
-                    default=0, help="Print a molecule only if the pattern occurs # times inside the molecule.")
+                        default=0, help="Print a molecule only if the pattern occurs # times inside the molecule.")
     parser.add_argument('-p', '--processors', type=int, default=multiprocessing.cpu_count())
     parser.add_argument("--invert-matches", dest="invert_matches", action="store_true",
-                    default=False, help="Invert the matching, print non-matching molecules.")
+                        default=False, help="Invert the matching, print non-matching molecules.")
     parser.add_argument("--only-name", dest="only_name", action="store_true",
-                    default=False, help="Only print the name of the molecules.")
+                        default=False, help="Only print the name of the molecules.")
     parser.add_argument("--full-match", dest="full_match", action="store_true",
-                    default=False, help="Full match, print matching-molecules only when the number of heavy atoms is also equal to the number of atoms in the SMARTS pattern.")
+                        default=False, help="Full match, print matching-molecules only when the number of heavy atoms is also equal to the number of atoms in the SMARTS pattern.")
     parser.add_argument("--number-of-matches", dest="number_of_matches", action="store_true",
-                    default=False, help="Print the number of matches.")
+                        default=False, help="Print the number of matches.")
     return parser.parse_args()
 
+
 results = list()
+
+
 def mp_callback(res):
     results.append(res)
 
-def mp_helper( query, args ):
+
+def mp_helper(query, args):
     """
         Helper function for multiprocessing.
         That function is a wrapper around obgrep.
@@ -57,44 +60,44 @@
 
     tmp = tempfile.NamedTemporaryFile(delete=False)
     cmd = 'obgrep %s "%s" %s' % (' '.join(cmd_list), query, args.infile)
-    child = subprocess.Popen(shlex.split(cmd),
-        stdout=open(tmp.name, 'w+'), stderr=subprocess.PIPE)
+    child = subprocess.Popen(shlex.split(cmd), stdout=open(tmp.name, 'w+'), stderr=subprocess.PIPE)
 
     stdout, stderr = child.communicate()
     return (tmp.name, query)
 
 
-def obgrep( args ):
-
+def obgrep(args):
     temp_file = tempfile.NamedTemporaryFile()
     temp_link = "%s.%s" % (temp_file.name, args.iformat)
     temp_file.close()
     os.symlink(args.infile, temp_link)
     args.infile = temp_link
 
-    pool = multiprocessing.Pool( args.processors )
-    for query in open( args.query ):
+    pool = multiprocessing.Pool(args.processors)
+    for query in open(args.query):
         pool.apply_async(mp_helper, args=(query.strip(), args), callback=mp_callback)
-        #mp_callback( mp_helper(query.strip(), args) )
+        # mp_callback(mp_helper(query.strip(), args))
     pool.close()
     pool.join()
 
-    out_handle = open( args.outfile, 'wb' )
+    out_handle = open(args.outfile, 'wb')
     for result_file, query in results:
-        res_handle = open(result_file,'rb')
-        shutil.copyfileobj( res_handle, out_handle )
+        res_handle = open(result_file, 'rb')
+        shutil.copyfileobj(res_handle, out_handle)
         res_handle.close()
-        os.remove( result_file )
+        os.remove(result_file)
     out_handle.close()
 
-    os.remove( temp_link )
+    os.remove(temp_link)
+
 
 def __main__():
     """
         Multiprocessing obgrep search.
     """
     args = parse_command_line()
-    obgrep( args )
+    obgrep(args)
+
 
-if __name__ == "__main__" :
+if __name__ == "__main__":
     __main__()