Mercurial > repos > bernhardlutz > rest_tool
comparison rest_tool.py @ 6:9ee84d9fd3a7 draft
Uploaded
| author | bernhardlutz |
|---|---|
| date | Thu, 03 Apr 2014 06:50:57 -0400 |
| parents | |
| children | 3c1e862e8cd6 |
comparison
equal
deleted
inserted
replaced
| 5:1ad356686717 | 6:9ee84d9fd3a7 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import sys, os | |
| 4 import argparse | |
| 5 | |
| 6 import readfile | |
| 7 | |
| 8 txt_output=["cids", "aids", "sids", "synonyms" ] | |
| 9 csv_output=["assaysummary"] | |
| 10 check_for_id_type=["cids", "aids", "sids"] | |
| 11 | |
| 12 def main(args): | |
| 13 url="http://pubchem.ncbi.nlm.nih.gov/rest/pug/"+args.type+"/" | |
| 14 if args.type == "assay": | |
| 15 url+="aid/" | |
| 16 elif args.type == "compound": | |
| 17 url+="cid/" | |
| 18 elif args.type == "substance": | |
| 19 url+="sid/" | |
| 20 if args.id_file is None: | |
| 21 idstring=str(args.id) | |
| 22 else: | |
| 23 idlist=readfile.getListFromFile(args.id_file) | |
| 24 idstring=",".join(idlist) | |
| 25 url+=idstring+"/"+args.operation+"/" | |
| 26 if args.operation in csv_output: | |
| 27 url+="csv" | |
| 28 elif args.operation in txt_output: | |
| 29 url+="txt" | |
| 30 else: | |
| 31 url+="xml" | |
| 32 if args.operation in check_for_id_type and not args.id_type is None: | |
| 33 url+="?"+args.operation+"_type="+args.id_type | |
| 34 print(url) | |
| 35 data=readfile.getresult(url) | |
| 36 outfile=args.outfile | |
| 37 outfile.write(data) | |
| 38 outfile.close() | |
| 39 | |
| 40 if __name__ == "__main__": | |
| 41 parser = argparse.ArgumentParser() | |
| 42 parser.add_argument('--type', type=str, required=True, | |
| 43 help="That you want BioAssay Compund ...") | |
| 44 parser.add_argument('--id', type=str, | |
| 45 help="Specify the ID") | |
| 46 parser.add_argument('--operation', type=str, required=True, | |
| 47 help="Specify the operation") | |
| 48 parser.add_argument('--property-value', dest="property_value", type=str, | |
| 49 help="Specify the property") | |
| 50 parser.add_argument('--id-type', dest="id_type", type=str, | |
| 51 help="Specify the property") | |
| 52 parser.add_argument('--outfile', type=argparse.FileType('w'), required=True, | |
| 53 help="Specify one output file") | |
| 54 parser.add_argument('--id-file', dest="id_file", type=argparse.FileType('r'), | |
| 55 help="Specify a file with a list of ids, one per line") | |
| 56 if len(sys.argv) < 8: | |
| 57 print "Too few arguments..." | |
| 58 parser.print_help() | |
| 59 exit(1) | |
| 60 args = parser.parse_args() | |
| 61 main( args ) |
