annotate data_manager_gene_annotation/data_manager/data_manager.py @ 0:0442068f5c91 draft

Uploaded
author scottx611x
date Tue, 26 Apr 2016 13:33:16 -0400
parents
children 810ef478fc98
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
1 import argparse
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
2 import datetime
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
3 import json
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
4 import os
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
5 import shutil
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
6 import sys
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
7 import tarfile
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
8 import urllib2
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
9 import zipfile
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
10
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
11 parser = argparse.ArgumentParser(description='Create data manager json.')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
12 parser.add_argument('--out', dest='output', action='store', help='JSON filename')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
13 parser.add_argument('--name', dest='name', action='store', default=str(datetime.date.today()), help='Data table entry unique ID')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
14 parser.add_argument('--url', dest='url', action='store', help='Download URL', default="http://www.scott-ouellette.com/gene_annotations/chr1-hg19_genes.gtf")
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
15
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
16 args = parser.parse_args()
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
17
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
18 def url_download(url, workdir):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
19 if not os.path.exists(workdir):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
20 os.makedirs(workdir)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
21 file_path = os.path.join(workdir, 'download.dat')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
22 if not os.path.exists(workdir):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
23 os.makedirs(workdir)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
24 src = None
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
25 dst = None
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
26 hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
27 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
28 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
29 'Accept-Encoding': 'none',
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
30 'Accept-Language': 'en-US,en;q=0.8',
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
31 'Connection': 'keep-alive'}
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
32 try:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
33 req = urllib2.Request(url, headers=hdr)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
34 src = urllib2.urlopen(req)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
35 dst = open(file_path, 'wb')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
36 while True:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
37 chunk = src.read(2**10)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
38 if chunk:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
39 dst.write(chunk)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
40 else:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
41 break
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
42 except Exception as e:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
43 print e, "FUCK"
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
44 finally:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
45 if src:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
46 src.close()
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
47 if dst:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
48 dst.close()
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
49 if tarfile.is_tarfile(file_path):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
50 fh = tarfile.open(file_path, 'r:*')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
51 elif zipfile.is_zipfile(file_path):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
52 fh = zipfile.ZipFile(file_path, 'r')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
53 else:
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
54 return
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
55 fh.extractall(workdir)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
56 os.remove(file_path)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
57
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
58
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
59 def main(args):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
60 workdir = os.path.join(os.getcwd(), 'gene_annotation')
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
61 url_download(args.url, workdir)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
62 data_manager_entry = {}
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
63 data_manager_entry['value'] = args.name.lower()
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
64 data_manager_entry['name'] = args.name
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
65 data_manager_entry['path'] = args.output
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
66 data_manager_json = dict(data_tables=dict(gene_annotation=data_manager_entry))
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
67 params = json.loads(open(args.output).read())
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
68 target_directory = params['output_data'][0]['extra_files_path']
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
69 os.mkdir(target_directory)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
70 output_path = os.path.abspath(os.path.join(os.getcwd(), 'gene_annotation'))
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
71 for filename in os.listdir(workdir):
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
72 shutil.move(os.path.join(output_path, filename), target_directory)
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
73 file(args.output, 'w').write(json.dumps(data_manager_json))
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
74
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
75 if __name__ == '__main__':
0442068f5c91 Uploaded
scottx611x
parents:
diff changeset
76 main(args)