diff ob_filter.py @ 10:0c95b0f51114 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 6c84abdd07f292048bf2194073e2e938e94158c4"
author bgruening
date Wed, 25 Mar 2020 16:46:28 -0400
parents ab4318b816bd
children e38d865baa37
line wrap: on
line diff
--- a/ob_filter.py	Thu Aug 22 10:27:02 2019 -0400
+++ b/ob_filter.py	Wed Mar 25 16:46:28 2020 -0400
@@ -29,6 +29,10 @@
         help="Specify the filters to apply",
         required=True,
         )
+    parser.add_argument('--list_of_names', 
+        help="A file with list of molecule names to extract. Every name is in one line.",
+        required=False,
+        )
     return parser.parse_args()
 
 def filter_precalculated_compounds(args, filters):
@@ -83,12 +87,24 @@
         sys.stdout.write(stdout.decode('utf-8'))
         sys.stdout.write(stderr.decode('utf-8'))
 
+def filter_by_name(args):
+    outfile = pybel.Outputfile(args.oformat, args.output, overwrite=True)
+    for mol in pybel.readfile('sdf', args.input):
+        for name in open(args.list_of_names):
+            if mol.title.strip() == name.strip():
+                outfile.write(mol)
+    outfile.close()
 
 def __main__():
     """
         Select compounds with certain properties from a small library
     """
     args = parse_command_line()
+    
+    if args.filters == '__filter_by_name__':
+        filter_by_name(args)
+        return
+    
     # Its a small trick to get the parameters in an easy way from the xml file.
     # To keep it readable in the xml file, many white-spaces are included in that string it needs to be removed.
     # Also the last loop creates a ',{' that is not an valid jason expression.