annotate fetch_reference_data.py @ 7:0f39f40ef695 draft

planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
author yating-l
date Wed, 03 May 2017 15:15:36 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
1 #!/usr/bin/env python
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
2 # ref: https://galaxyproject.org/admin/tools/data-managers/how-to/define/
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
3
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
4 import sys
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
5 import os
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
6 import tempfile
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
7 import shutil
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
8 import argparse
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
9 import urllib2
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
10 import tarfile
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
11
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
12 from galaxy.util.json import from_json_string, to_json_string
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
13
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
14 CHUNK_SIZE = 2**20 #1mb
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
15
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
16 def cleanup_before_exit(tmp_dir):
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
17 if tmp_dir and os.path.exists(tmp_dir):
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
18 shutil.rmtree(tmp_dir)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
19
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
20 def stop_err(msg):
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
21 sys.stderr.write(msg)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
22 sys.exit(1)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
23
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
24 def get_reference_id_name(params):
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
25 genome_id = params['param_dict']['genome_id']
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
26 genome_name = params['param_dict']['genome_name']
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
27 return genome_id, genome_name
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
28
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
29 def download_from_GlimmerHMM(data_manager_dict, params, target_directory, sequence_id, sequence_name ):
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
30 GlimmerHMM_DOWNLOAD_URL = 'ftp://ccb.jhu.edu/pub/software/glimmerhmm/GlimmerHMM-3.0.4.tar.gz'
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
31 GlimmerHMM_TRAINED_DIR = os.path.join('GlimmerHMM', 'trained_dir', sequence_id)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
32 with tarfile.open('GlimmerHMM-3.0.4.tar', mode='r:*') as tar:
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
33 subdir = [
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
34 tarinfo for tarinfo in tar.getmembers()
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
35 if sequence_id in tarinfo.name
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
36 ]
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
37 tar.extractall(members=subdir)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
38 glimmerhmm_trained_target_dir = os.path.join(target_directory, sequence_id)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
39 shutil.copytree(GlimmerHMM_TRAINED_DIR, glimmerhmm_trained_target_dir)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
40 data_table_entry = dict(value=sequence_id, name=sequence_name, path=glimmerhmm_trained_target_dir)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
41 _add_data_table_entry(data_manager_dict, data_table_entry)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
42
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
43 cleanup_before_exit(GlimmerHMM_TRAINED_DIR)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
44
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
45 def _add_data_table_entry( data_manager_dict, data_table_entry ):
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
46 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
47 data_manager_dict['data_tables']['reference_data'] = data_manager_dict['data_tables'].get('reference_data', [])
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
48 data_manager_dict['data_tables']['reference_data'].append( data_table_entry )
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
49 return data_manager_dict
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
50
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
51 REFERENCE_SOURCE_TO_DOWNLOAD = dict(glimmerhmm=download_from_GlimmerHMM)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
52
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
53 def main():
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
54 #Parse Command Line
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
55 parser = argparse.ArgumentParser()
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
56 args = parser.parse_args()
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
57
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
58 filename = args[0]
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
59
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
60 params = from_json_string(open(filename).read())
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
61 target_directory = params['output_data'][0]['extra_files_path']
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
62 os.mkdir(target_directory)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
63 data_manager_dict = {}
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
64
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
65 sequence_id, sequence_name = get_reference_id_name(params)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
66
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
67 #Fetch the FASTA
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
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)
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
69
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
70 #save info to json file
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
71 open(filename, 'wb').write(to_json_string(data_manager_dict))
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
72
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
73 if __name__ == "__main__":
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
74 main()
0f39f40ef695 planemo upload for repository https://github.com/remimarenco/multi_fasta_glimmerhmm.git commit 59eb9b27c7382ae4736eb7425615e4b575757378
yating-l
parents:
diff changeset
75