annotate data_manager_gene_annotation/data_manager/data_manager.py @ 43:e42284a13168 draft

planemo upload
author scottx611x
date Fri, 08 Jul 2016 14:45:18 -0400
parents bf05cda04e1f
children f8cfca91827a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
1 import os
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
2 import sys
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
3 import json
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
4 import argparse
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
5 import requests
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
6 from requests.exceptions import ContentDecodingError
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
7
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
8
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
9 def url_download(url):
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
10 """Attempt to download gene annotation file from a given url
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
11 :param url: full url to gene annotation file
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
12 :type url: str.
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
13 :returns: name of downloaded gene annotation file
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
14 :raises: ContentDecodingError, IOError
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
15 """
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
16 response = requests.get(url=url, stream=True)
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
17
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
18 # Generate file_name
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
19 file_name = response.url.split("/")[-1]
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
20
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
21 block_size = 10 * 1024 * 1024 # 10MB chunked download
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
22 with open(file_name, 'w+') as f:
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
23 try:
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
24 # Good to note here that requests' iter_content() will
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
25 # automatically handle decoding "gzip" and "deflate" encoding
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
26 # formats
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
27 for buf in response.iter_content(block_size):
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
28 f.write(buf)
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
29 except (ContentDecodingError, IOError) as e:
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
30 sys.stderr.write("Error occured downloading reference file: %s"
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
31 % e)
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
32 os.remove(file_name)
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
33
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
34 return file_name
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
35
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
36
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
37 def main():
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
38
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
39 # Generate and parse command line args
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
40 parser = argparse.ArgumentParser(description='Create data manager JSON.')
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
41 parser.add_argument('--out', dest='output', action='store',
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
42 help='JSON filename')
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
43 parser.add_argument('--name', dest='name', action='store',
42
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
44 default=None, help='Data table entry unique ID'
40
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
45 )
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
46 parser.add_argument('--url', dest='url', action='store',
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
47 help='Url to download gtf file from')
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
48
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
49 args = parser.parse_args()
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
50
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
51 work_dir = os.getcwd()
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
52
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
53 # Attempt to download gene annotation file from given url
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
54 gene_annotation_file_name = url_download(args.url)
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
55
42
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
56 name = str(args.name)
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
57
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
58 if not name:
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
59 name = gene_annotation_file_name.split(".")[0]
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
60
40
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
61 # Update Data Manager JSON and write to file
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
62 data_manager_entry = {
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
63 'data_tables': {
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
64 'gene_annotation': {
42
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
65 'value': gene_annotation_file_name.split(".")[0],
41
2dbaaa6e8344 planemo upload
scottx611x
parents: 40
diff changeset
66 'dbkey': gene_annotation_file_name.split(".")[0],
42
bf05cda04e1f planemo upload
scottx611x
parents: 41
diff changeset
67 'name': name,
43
e42284a13168 planemo upload
scottx611x
parents: 42
diff changeset
68 'path': gene_annotation_file_name
40
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
69 }
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
70 }
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
71 }
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
72
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
73 with open(os.path.join(args.output), "w+") as f:
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
74 f.write(json.dumps(data_manager_entry))
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
75
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
76 if __name__ == '__main__':
87a921902f5e planemo upload
scottx611x
parents:
diff changeset
77 main()