-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPubmed.py
executable file
·55 lines (43 loc) · 1.55 KB
/
getPubmed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
#
# grab output from eutils "summary" via a post for pubmed IDs
#
# Output to stdout.
#
import sys
import argparse
import json
import simpleURLLib as surl
import NCBIutilsLib as eulib
#-----------------------------------
def parseCmdLine():
parser = argparse.ArgumentParser( \
description='get summary records from pubmed for the specified IDs.')
parser.add_argument('pmids', nargs=argparse.REMAINDER,
help='pubmed IDs to retrieve')
parser.add_argument('-f', '--format', dest='format', choices=['json','xml'],
default='json', required=False, help="eutils summary output format")
parser.add_argument('-q', '--quiet', dest='verbose', action='store_false',
required=False, help="skip helpful messages to stderr")
args = parser.parse_args()
return args
#----------------------
args = parseCmdLine()
#----------------------
# Main prog
#----------------------
def main():
retmode = args.format
urlReader = surl.ThrottledURLReader( seconds=0.4 ) # don't overwhelm eutils
resultsBytes = eulib.getPostResults('pubmed', args.pmids,
URLReader = urlReader, op='summary',
rettype=None, retmode=retmode,) [0]
if retmode == 'json':
resultsJson = json.loads(resultsBytes)
print(json.dumps(resultsJson,sort_keys=True,indent=4,
separators=(',',': ')) + '\n')
else:
print(resultsBytes.decode())
# ---------------------
if __name__ == "__main__":
main()