comparison esearch.py @ 3:254f40d3ae2b 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:50:11 +0000
parents c8d4ea6376a7
children
comparison
equal deleted inserted replaced
2:cb5a0fe9e036 3:254f40d3ae2b
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 from __future__ import print_function
3 2
4 import argparse 3 import argparse
5 import json 4 import json
5 import logging
6
6 7
7 import eutils 8 import eutils
9
10
11 logging.basicConfig(level=logging.INFO)
8 12
9 13
10 if __name__ == '__main__': 14 if __name__ == '__main__':
11 parser = argparse.ArgumentParser(description='ESearch', epilog='') 15 parser = argparse.ArgumentParser(description='ESearch', epilog='')
12 parser.add_argument('db', help='Database to use') 16 parser.add_argument('db', help='Database to use')
15 parser.add_argument('--datetype', help='Date type') 19 parser.add_argument('--datetype', help='Date type')
16 parser.add_argument('--reldate', help='In past N days') 20 parser.add_argument('--reldate', help='In past N days')
17 parser.add_argument('--mindate', help='Minimum date') 21 parser.add_argument('--mindate', help='Minimum date')
18 parser.add_argument('--maxdate', help='maximum date') 22 parser.add_argument('--maxdate', help='maximum date')
19 # History 23 # History
20 parser.add_argument('--history_out', type=argparse.FileType('w'), 24 parser.add_argument('--history_out', action="store_true", help='Output history file')
21 help='Output history file')
22 parser.add_argument('--user_email', help="User email") 25 parser.add_argument('--user_email', help="User email")
23 parser.add_argument('--admin_email', help="Admin email") 26 parser.add_argument('--admin_email', help="Admin email")
27
28 parser.add_argument('--version', action='version', version=eutils.Client.getVersion(), help='Version (reports Biopython version)')
29
30 # Output
31 parser.add_argument('--retmode', help='Retmode')
32 parser.add_argument('--rettype', help='Rettype')
33 parser.add_argument('--retstart', type=int, default=0, help='Retstart - Starting rec number (0)')
34 parser.add_argument('--retmax', type=int, default=20, help='Retmax - max number of recs returned (20, max 100000)')
35
24 args = parser.parse_args() 36 args = parser.parse_args()
25 37
26 c = eutils.Client(history_file=args.history_file, user_email=args.user_email, admin_email=args.admin_email) 38 c = eutils.Client(history_file=args.history_file, user_email=args.user_email, admin_email=args.admin_email)
27 39
40 max_retmax = 100000
41 min_retmax = 1
42 max = max(min(args.retmax, max_retmax), min_retmax)
43
28 payload = { 44 payload = {
29 'db': args.db, 45 'db': args.db,
30 'term': args.term, 46 'term': args.term,
31 'retstart': 0,
32 'retmax': 20,
33 # hmmm @ retmax
34 } 47 }
35 if args.history_file is not None: 48 if args.history_file is not None:
36 payload.update(c.get_history()) 49 payload.update(c.get_history())
37 if args.history_out is not None: 50
51 # if args.history_out is not None:
52 if args.history_out:
38 payload['usehistory'] = 'y' 53 payload['usehistory'] = 'y'
39 54
40 for attr in ('datetype', 'reldate', 'mindate', 'maxdate'): 55 payload['retmode'] = args.retmode
56
57 for attr in ('datetype', 'reldate', 'mindate', 'maxdate', 'rettype', 'retmax', 'retstart'):
41 if getattr(args, attr, None) is not None: 58 if getattr(args, attr, None) is not None:
42 payload[attr] = getattr(args, attr) 59 payload[attr] = getattr(args, attr)
43 60
61 logging.info("Payload used for query:" + json.dumps(payload, indent=4))
62
44 results = c.search(**payload) 63 results = c.search(**payload)
45 64
46 if args.history_out is not None: 65 # We're going to infer that rettype being uilist means convert to text format (which esearch does not do)
47 history = c.extract_history(results) 66 if args.retmode == 'text':
48 args.history_out.write(json.dumps(history, indent=4)) 67 ids = c.xmlstring2UIlist(results)
49 68 for id in ids:
50 print(results) 69 print(id)
70 elif args.retmode == 'json':
71 json_data = c.jsonstring2jsondata(results)
72 print(json.dumps(json_data, indent=4))
73 else:
74 print(results)