diff esearch.py @ 3:d1789b4821fc draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ncbi_entrez_eutils commit dae34e5e182b4cceb808d7353080f14aa9a78ca9"
author iuc
date Wed, 23 Sep 2020 09:51:30 +0000
parents 28f69754ddc5
children
line wrap: on
line diff
--- a/esearch.py	Wed Mar 11 04:02:33 2020 -0400
+++ b/esearch.py	Wed Sep 23 09:51:30 2020 +0000
@@ -1,12 +1,16 @@
 #!/usr/bin/env python
-from __future__ import print_function
 
 import argparse
 import json
+import logging
+
 
 import eutils
 
 
+logging.basicConfig(level=logging.INFO)
+
+
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='ESearch', epilog='')
     parser.add_argument('db', help='Database to use')
@@ -17,34 +21,54 @@
     parser.add_argument('--mindate', help='Minimum date')
     parser.add_argument('--maxdate', help='maximum date')
     # History
-    parser.add_argument('--history_out', type=argparse.FileType('w'),
-                        help='Output history file')
+    parser.add_argument('--history_out', action="store_true", help='Output history file')
     parser.add_argument('--user_email', help="User email")
     parser.add_argument('--admin_email', help="Admin email")
+
+    parser.add_argument('--version', action='version', version=eutils.Client.getVersion(), help='Version (reports Biopython version)')
+
+    # Output
+    parser.add_argument('--retmode', help='Retmode')
+    parser.add_argument('--rettype', help='Rettype')
+    parser.add_argument('--retstart', type=int, default=0, help='Retstart - Starting rec number (0)')
+    parser.add_argument('--retmax', type=int, default=20, help='Retmax - max number of recs returned (20, max 100000)')
+
     args = parser.parse_args()
 
     c = eutils.Client(history_file=args.history_file, user_email=args.user_email, admin_email=args.admin_email)
 
+    max_retmax = 100000
+    min_retmax = 1
+    max = max(min(args.retmax, max_retmax), min_retmax)
+
     payload = {
         'db': args.db,
         'term': args.term,
-        'retstart': 0,
-        'retmax': 20,
-        # hmmm @ retmax
     }
     if args.history_file is not None:
         payload.update(c.get_history())
-    if args.history_out is not None:
+
+    # if args.history_out is not None:
+    if args.history_out:
         payload['usehistory'] = 'y'
 
-    for attr in ('datetype', 'reldate', 'mindate', 'maxdate'):
+    payload['retmode'] = args.retmode
+
+    for attr in ('datetype', 'reldate', 'mindate', 'maxdate', 'rettype', 'retmax', 'retstart'):
         if getattr(args, attr, None) is not None:
             payload[attr] = getattr(args, attr)
 
+    logging.info("Payload used for query:" + json.dumps(payload, indent=4))
+
     results = c.search(**payload)
 
-    if args.history_out is not None:
-        history = c.extract_history(results)
-        args.history_out.write(json.dumps(history, indent=4))
-
-    print(results)
+    # We're going to infer that rettype being uilist means convert to text format (which esearch does not do)
+    if args.retmode == 'text':
+        ids = c.xmlstring2UIlist(results)
+        for id in ids:
+            print(id)
+    elif args.retmode == 'json':
+        json_data = c.jsonstring2jsondata(results)
+        print(json.dumps(json_data, indent=4))
+    else:
+        print(results)