annotate data_manager/data_manager.py @ 39:2ab076cf69df draft

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