annotate data_manager/fetch_reference_data.py @ 2:c4737db26737 draft

planemo upload
author yating-l
date Wed, 03 May 2017 16:32:05 -0400
parents 5349705442d0
children 2804b5f00dc7
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 CHUNK_SIZE = 2**20 #1mb
5349705442d0 planemo upload
yating-l
parents:
diff changeset
15
5349705442d0 planemo upload
yating-l
parents:
diff changeset
16 def cleanup_before_exit(tmp_dir):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
17 if tmp_dir and os.path.exists(tmp_dir):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
18 shutil.rmtree(tmp_dir)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
19
5349705442d0 planemo upload
yating-l
parents:
diff changeset
20 def stop_err(msg):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
21 sys.stderr.write(msg)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
22 sys.exit(1)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
23
5349705442d0 planemo upload
yating-l
parents:
diff changeset
24 def get_reference_id_name(params):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
25 genome_id = params['param_dict']['genome_id']
5349705442d0 planemo upload
yating-l
parents:
diff changeset
26 genome_name = params['param_dict']['genome_name']
5349705442d0 planemo upload
yating-l
parents:
diff changeset
27 return genome_id, genome_name
5349705442d0 planemo upload
yating-l
parents:
diff changeset
28
5349705442d0 planemo upload
yating-l
parents:
diff changeset
29 def download_from_GlimmerHMM(data_manager_dict, params, target_directory, sequence_id, sequence_name ):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
30 GlimmerHMM_DOWNLOAD_URL = 'ftp://ccb.jhu.edu/pub/software/glimmerhmm/GlimmerHMM-3.0.4.tar.gz'
5349705442d0 planemo upload
yating-l
parents:
diff changeset
31 GlimmerHMM_TRAINED_DIR = os.path.join('GlimmerHMM', 'trained_dir', sequence_id)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
32 with tarfile.open('GlimmerHMM-3.0.4.tar', mode='r:*') as tar:
5349705442d0 planemo upload
yating-l
parents:
diff changeset
33 subdir = [
5349705442d0 planemo upload
yating-l
parents:
diff changeset
34 tarinfo for tarinfo in tar.getmembers()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
35 if sequence_id in tarinfo.name
5349705442d0 planemo upload
yating-l
parents:
diff changeset
36 ]
5349705442d0 planemo upload
yating-l
parents:
diff changeset
37 tar.extractall(members=subdir)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
38 glimmerhmm_trained_target_dir = os.path.join(target_directory, sequence_id)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
39 shutil.copytree(GlimmerHMM_TRAINED_DIR, glimmerhmm_trained_target_dir)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
40 data_table_entry = dict(value=sequence_id, name=sequence_name, path=glimmerhmm_trained_target_dir)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
41 _add_data_table_entry(data_manager_dict, data_table_entry)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
42
5349705442d0 planemo upload
yating-l
parents:
diff changeset
43 cleanup_before_exit(GlimmerHMM_TRAINED_DIR)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
44
5349705442d0 planemo upload
yating-l
parents:
diff changeset
45 def _add_data_table_entry( data_manager_dict, data_table_entry ):
5349705442d0 planemo upload
yating-l
parents:
diff changeset
46 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
5349705442d0 planemo upload
yating-l
parents:
diff changeset
47 data_manager_dict['data_tables']['reference_data'] = data_manager_dict['data_tables'].get('reference_data', [])
5349705442d0 planemo upload
yating-l
parents:
diff changeset
48 data_manager_dict['data_tables']['reference_data'].append( data_table_entry )
5349705442d0 planemo upload
yating-l
parents:
diff changeset
49 return data_manager_dict
5349705442d0 planemo upload
yating-l
parents:
diff changeset
50
5349705442d0 planemo upload
yating-l
parents:
diff changeset
51 REFERENCE_SOURCE_TO_DOWNLOAD = dict(glimmerhmm=download_from_GlimmerHMM)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
52
5349705442d0 planemo upload
yating-l
parents:
diff changeset
53 def main():
5349705442d0 planemo upload
yating-l
parents:
diff changeset
54 #Parse Command Line
5349705442d0 planemo upload
yating-l
parents:
diff changeset
55 parser = argparse.ArgumentParser()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
56 args = parser.parse_args()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
57
5349705442d0 planemo upload
yating-l
parents:
diff changeset
58 filename = args[0]
5349705442d0 planemo upload
yating-l
parents:
diff changeset
59
5349705442d0 planemo upload
yating-l
parents:
diff changeset
60 params = from_json_string(open(filename).read())
5349705442d0 planemo upload
yating-l
parents:
diff changeset
61 target_directory = params['output_data'][0]['extra_files_path']
5349705442d0 planemo upload
yating-l
parents:
diff changeset
62 os.mkdir(target_directory)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
63 data_manager_dict = {}
5349705442d0 planemo upload
yating-l
parents:
diff changeset
64
5349705442d0 planemo upload
yating-l
parents:
diff changeset
65 sequence_id, sequence_name = get_reference_id_name(params)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
66
5349705442d0 planemo upload
yating-l
parents:
diff changeset
67 #Fetch the FASTA
5349705442d0 planemo upload
yating-l
parents:
diff changeset
68 REFERENCE_SOURCE_TO_DOWNLOAD[params['param_dict']['file_path']](data_manager_dict, params, target_directory, sequence_id, sequence_name)
5349705442d0 planemo upload
yating-l
parents:
diff changeset
69
5349705442d0 planemo upload
yating-l
parents:
diff changeset
70 #save info to json file
5349705442d0 planemo upload
yating-l
parents:
diff changeset
71 open(filename, 'wb').write(to_json_string(data_manager_dict))
5349705442d0 planemo upload
yating-l
parents:
diff changeset
72
5349705442d0 planemo upload
yating-l
parents:
diff changeset
73 if __name__ == "__main__":
5349705442d0 planemo upload
yating-l
parents:
diff changeset
74 main()
5349705442d0 planemo upload
yating-l
parents:
diff changeset
75