annotate data_manager/fetch_reference_data.py @ 18:dda792940610 draft

planemo upload
author yating-l
date Thu, 04 May 2017 12:28:28 -0400
parents 85e817cdf4bd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
1 #!/usr/bin/env python
5349705442d0 planemo upload
yating-l
parents:
diff changeset
2 # ref: https://galaxyproject.org/admin/tools/data-managers/how-to/define/
5349705442d0 planemo upload
yating-l
parents:
diff changeset
3
5349705442d0 planemo upload
yating-l
parents:
diff changeset
4 import sys
5349705442d0 planemo upload
yating-l
parents:
diff changeset
5 import os
5349705442d0 planemo upload
yating-l
parents:
diff changeset
6 import tempfile
5349705442d0 planemo upload
yating-l
parents:
diff changeset
7 import shutil
5349705442d0 planemo upload
yating-l
parents:
diff changeset
8 import argparse
5349705442d0 planemo upload
yating-l
parents:
diff changeset
9 import urllib2
5349705442d0 planemo upload
yating-l
parents:
diff changeset
10 import tarfile
5349705442d0 planemo upload
yating-l
parents:
diff changeset
11
5349705442d0 planemo upload
yating-l
parents:
diff changeset
12 from galaxy.util.json import from_json_string, to_json_string
5349705442d0 planemo upload
yating-l
parents:
diff changeset
13
5349705442d0 planemo upload
yating-l
parents:
diff changeset
14
5349705442d0 planemo upload
yating-l
parents:
diff changeset
15 def cleanup_before_exit(tmp_dir):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
16 if tmp_dir and os.path.exists(tmp_dir):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
17 shutil.rmtree(tmp_dir)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
18
5349705442d0 planemo upload
yating-l
parents:
diff changeset
19 def stop_err(msg):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
20 sys.stderr.write(msg)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
21 sys.exit(1)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
22
5349705442d0 planemo upload
yating-l
parents:
diff changeset
23 def get_reference_id_name(params):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
24 genome_id = params['param_dict']['genome_id']
5349705442d0 planemo upload
yating-l
parents:
diff changeset
25 genome_name = params['param_dict']['genome_name']
5349705442d0 planemo upload
yating-l
parents:
diff changeset
26 return genome_id, genome_name
5349705442d0 planemo upload
yating-l
parents:
diff changeset
27
5
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
28 def get_url(params):
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
29 trained_url = params['param_dict']['trained_url']
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
30 return trained_url
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
31
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
32 def download_from_GlimmerHMM(data_manager_dict, target_directory, sequence_id, sequence_name, trained_dir):
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
33 if not trained_dir:
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
34 trained_dir = 'ftp://ccb.jhu.edu/pub/software/glimmerhmm/GlimmerHMM-3.0.4.tar.gz'
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
35 #Download trained data, ref: https://dzone.com/articles/how-download-file-python
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
36 f = urllib2.urlopen(trained_dir)
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
37 data = f.read()
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
38 downloadpath = 'tmp'
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
39 os.mkdir(downloadpath)
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
40 filepath = os.path.join(downloadpath, 'GlimmerHMM-3.0.4.tar')
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
41 with open(filepath, 'wb') as code:
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
42 code.write(data)
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
43 with tarfile.open(filepath, mode='r:*') as tar:
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
44 subdir = [
5349705442d0 planemo upload
yating-l
parents:
diff changeset
45 tarinfo for tarinfo in tar.getmembers()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
46 if sequence_id in tarinfo.name
5349705442d0 planemo upload
yating-l
parents:
diff changeset
47 ]
6
2eb398f3649c planemo upload
yating-l
parents: 5
diff changeset
48 tar.extractall(path=downloadpath, members=subdir)
2eb398f3649c planemo upload
yating-l
parents: 5
diff changeset
49 glimmerhmm_trained_dir = os.path.join(downloadpath, 'GlimmerHMM', 'trained_dir', sequence_id)
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
50 glimmerhmm_trained_target_dir = os.path.join(target_directory, sequence_id)
6
2eb398f3649c planemo upload
yating-l
parents: 5
diff changeset
51 shutil.copytree(glimmerhmm_trained_dir, glimmerhmm_trained_target_dir)
11
85e817cdf4bd planemo upload
yating-l
parents: 10
diff changeset
52 data_table_entry = dict(value=sequence_id, name=sequence_name, path=glimmerhmm_trained_target_dir)
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
53 _add_data_table_entry(data_manager_dict, data_table_entry)
5
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
54
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
55 cleanup_before_exit('tmp')
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
56
5
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
57 def _add_data_table_entry(data_manager_dict, data_table_entry):
11
85e817cdf4bd planemo upload
yating-l
parents: 10
diff changeset
58 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {})
7
83362eed7868 planemo upload
yating-l
parents: 6
diff changeset
59 data_manager_dict['data_tables']['glimmer_hmm_trained_dir'] = data_manager_dict['data_tables'].get('glimmer_hmm_trained_dir', [])
83362eed7868 planemo upload
yating-l
parents: 6
diff changeset
60 data_manager_dict['data_tables']['glimmer_hmm_trained_dir'].append(data_table_entry)
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
61 return data_manager_dict
5349705442d0 planemo upload
yating-l
parents:
diff changeset
62
5349705442d0 planemo upload
yating-l
parents:
diff changeset
63 def main():
5349705442d0 planemo upload
yating-l
parents:
diff changeset
64 #Parse Command Line
5349705442d0 planemo upload
yating-l
parents:
diff changeset
65 parser = argparse.ArgumentParser()
3
2804b5f00dc7 planemo upload
yating-l
parents: 1
diff changeset
66 parser.add_argument('-o', '--out', help='Output file')
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
67 args = parser.parse_args()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
68
3
2804b5f00dc7 planemo upload
yating-l
parents: 1
diff changeset
69 filename = args.out
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
70
5349705442d0 planemo upload
yating-l
parents:
diff changeset
71 params = from_json_string(open(filename).read())
10
f2e535e1b45c planemo upload
yating-l
parents: 9
diff changeset
72 target_directory = params['output_data'][0]['extra_files_path']
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
73 os.mkdir(target_directory)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
74 data_manager_dict = {}
5349705442d0 planemo upload
yating-l
parents:
diff changeset
75
5349705442d0 planemo upload
yating-l
parents:
diff changeset
76 sequence_id, sequence_name = get_reference_id_name(params)
5
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
77 trained_dir = get_url(params)
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
78 #Fetch the FASTA
5
2f926e7d623d planemo upload
yating-l
parents: 4
diff changeset
79 download_from_GlimmerHMM(data_manager_dict, target_directory, sequence_id, sequence_name, trained_dir)
1
5349705442d0 planemo upload
yating-l
parents:
diff changeset
80 #save info to json file
5349705442d0 planemo upload
yating-l
parents:
diff changeset
81 open(filename, 'wb').write(to_json_string(data_manager_dict))
5349705442d0 planemo upload
yating-l
parents:
diff changeset
82
5349705442d0 planemo upload
yating-l
parents:
diff changeset
83 if __name__ == "__main__":
5349705442d0 planemo upload
yating-l
parents:
diff changeset
84 main()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
85