annotate data_manager_gene_annotation/data_manager/data_manager.py @ 41:2dbaaa6e8344 draft

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