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