Mercurial > repos > scottx611x > data_manager_fetch_gene_annotation
comparison data_manager_gene_annotation/data_manager/data_manager.py @ 37:7af38f95bc8d draft
planemo upload
author | scottx611x |
---|---|
date | Thu, 23 Jun 2016 17:56:47 -0400 |
parents | f38beb0743d5 |
children |
comparison
equal
deleted
inserted
replaced
36:f38beb0743d5 | 37:7af38f95bc8d |
---|---|
5 import argparse | 5 import argparse |
6 import datetime | 6 import datetime |
7 import requests | 7 import requests |
8 from requests.exceptions import ContentDecodingError | 8 from requests.exceptions import ContentDecodingError |
9 | 9 |
10 parser = argparse.ArgumentParser(description='Create data manager json.') | |
11 parser.add_argument('--out', | |
12 dest='output', | |
13 action='store', | |
14 help='JSON filename', | |
15 ) | |
16 parser.add_argument('--name', | |
17 dest='name', | |
18 action='store', | |
19 default=uuid.uuid4(), | |
20 help='Data table entry unique ID' | |
21 ) | |
22 parser.add_argument('--url', | |
23 dest='url', | |
24 action='store', | |
25 help='Download URL' | |
26 ) | |
27 | |
28 args = parser.parse_args() | |
29 | |
30 | 10 |
31 def url_download(url): | 11 def url_download(url): |
12 """Attempt to download gene annotation file from a given url | |
13 :param url: NodeSet UUID. | |
14 :type url: str. | |
15 :returns: name of downloaded gene annotation file | |
16 :raises: ContentDecodingError, IOError | |
17 """ | |
32 response = requests.get(url=url, stream=True) | 18 response = requests.get(url=url, stream=True) |
33 | 19 |
34 # Generate file_name | 20 # Generate file_name |
35 file_name = response.url.split("/")[-1] | 21 file_name = response.url.split("/")[-1] |
36 | 22 |
37 block_size = 10 * 1024 * 1024 # 10MB chunked download | 23 block_size = 10 * 1024 * 1024 # 10MB chunked download |
38 with open(file_name, 'w+') as f: | 24 with open(file_name, 'w+') as f: |
39 try: | 25 try: |
40 # Good to note here that requests' iter_content() will | 26 # Good to note here that requests' iter_content() will |
41 # automatically handle decoding "gzip" and "deflate" formats | 27 # automatically handle decoding "gzip" and "deflate" encoding |
28 # formats | |
42 for buf in response.iter_content(block_size): | 29 for buf in response.iter_content(block_size): |
43 f.write(buf) | 30 f.write(buf) |
44 except (ContentDecodingError, IOError) as e: | 31 except (ContentDecodingError, IOError) as e: |
45 sys.stderr.write("Error occured downloading reference file: %s" | 32 sys.stderr.write("Error occured downloading reference file: %s" |
46 % e) | 33 % e) |
47 os.remove(file_name) | 34 os.remove(file_name) |
48 | 35 |
49 with open("/Users/scott/Desktop/cool.txt", "w+") as f: | |
50 f.write(os.path.join(os.getcwd(), file_name)) | |
51 f.write("\n") | |
52 | |
53 return file_name | 36 return file_name |
54 | 37 |
55 | 38 |
56 def main(args): | 39 def main(): |
40 | |
41 # Generate and parse command line args | |
42 parser = argparse.ArgumentParser(description='Create data manager JSON.') | |
43 parser.add_argument('--out', dest='output', action='store', | |
44 help='JSON filename') | |
45 parser.add_argument('--name', dest='name', action='store', | |
46 default=uuid.uuid4(), help='Data table entry unique ID' | |
47 ) | |
48 parser.add_argument('--url', dest='url', action='store', | |
49 help='Url to download gtf file from') | |
50 | |
51 args = parser.parse_args() | |
57 | 52 |
58 work_dir = os.getcwd() | 53 work_dir = os.getcwd() |
59 | 54 |
60 # Attempt to download gene annotation file from given url | 55 # Attempt to download gene annotation file from given url |
61 gene_annotation_file_name = url_download(args.url) | 56 gene_annotation_file_name = url_download(args.url) |
74 | 69 |
75 with open(os.path.join(args.output), "w+") as f: | 70 with open(os.path.join(args.output), "w+") as f: |
76 f.write(json.dumps(data_manager_entry)) | 71 f.write(json.dumps(data_manager_entry)) |
77 | 72 |
78 if __name__ == '__main__': | 73 if __name__ == '__main__': |
79 main(args) | 74 main() |