0
|
1 #!/usr/bin/env python
|
|
2 #Dan Blankenberg
|
|
3
|
|
4 import sys
|
|
5 import os
|
|
6 import tempfile
|
|
7 import shutil
|
|
8 import optparse
|
|
9 import urllib2
|
|
10 #import uuid
|
|
11 from ftplib import FTP
|
|
12 import tarfile
|
|
13 import zipfile
|
|
14 import gzip
|
|
15 import bz2
|
|
16
|
|
17 from galaxy.util.json import from_json_string, to_json_string
|
|
18
|
|
19 CHUNK_SIZE = 2**20 #1mb
|
|
20
|
|
21 def download_motif_databases( data_manager_dict, params, target_directory, motif_db ):
|
|
22
|
6
|
23 if motif_db == "pouya":
|
|
24 BGZ = ['COMPBIO URL HERE',
|
|
25 "pouya_motifs.bed.bgz", "pouya_bgz", "Pouya Encode Motifs (hg19) BGZ"]
|
|
26 TBI = ['COMPBIO URL HERE',
|
|
27 "pouya_motifs.bed.bgz.tbi", "pouya_tbi", "Pouya Encode Motifs (hg19) TBI"]
|
|
28 elif motif_db == "jaspar":
|
|
29 BGZ = ['COMPBIO URL HERE',
|
|
30 "jaspar_jolma_motifs.bed.bgz", "jaspar_bgz", "Jaspar and Jolma Motifs (hg19) BGZ"]
|
|
31 TBI = ['COMPBIO URL HERE',
|
|
32 "jaspar_jolma_motifs.bed.bgz.tbi", "jaspar_tbi", "Jaspar and Jolma Motifs (hg19) TBI"]
|
|
33 elif motif_db == "mouse":
|
|
34 BGZ = ['COMPBIO URL HERE',
|
|
35 "mouse_motifs.bed.bgz", "mouse_bgz", "Mouse Motifs (mm9) BGZ"]
|
|
36 TBI = ['COMPBIO URL HERE',
|
|
37 "mouse_motifs.bed.bgz.tbi", "mouse_tbi", "Mouse Motifs (mm9) TBI"]
|
|
38 else:
|
|
39 BGZ = ['http://gehlenborg.com/wp-content/uploads/motif/pouya_test_motifs.bed.bgz',
|
|
40 "pouya_test_motifs.bed.bgz", "test_bgz", "Test Pouya Subset (hg19) BGZ"]
|
|
41 TBI = ['http://gehlenborg.com/wp-content/uploads/motif/pouya_test_motifs.bed.bgz.tbi',
|
|
42 "pouya_test_motifs.bed.bgz.tbi", "test_tbi", "Test Pouya Subset (hg19) TBI"]
|
|
43
|
|
44 bgz_reader = urllib2.urlopen( BGZ[0] )
|
4
|
45 bgz_data_table_entry = _stream_fasta_to_file( bgz_reader, target_directory, params,
|
6
|
46 BGZ[1], BGZ[2], BGZ[3] )
|
4
|
47 _add_data_table_entry( data_manager_dict, 'motif_databases', bgz_data_table_entry )
|
|
48
|
6
|
49 tbi_reader = urllib2.urlopen( TBI[0] )
|
4
|
50 tbi_data_table_entry = _stream_fasta_to_file( tbi_reader, target_directory, params,
|
6
|
51 TBI[1], TBI[2], TBI[3] )
|
4
|
52 _add_data_table_entry( data_manager_dict, 'motif_databases', tbi_data_table_entry )
|
0
|
53
|
2
|
54 def _add_data_table_entry( data_manager_dict, data_table, data_table_entry ):
|
0
|
55 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
|
2
|
56 data_manager_dict['data_tables'][data_table] = data_manager_dict['data_tables'].get( data_table, [] )
|
|
57 data_manager_dict['data_tables'][data_table].append( data_table_entry )
|
0
|
58 return data_manager_dict
|
|
59
|
5
|
60 def _stream_fasta_to_file( fasta_stream, target_directory, params,
|
|
61 fasta_base_filename, value, name, close_stream=True ):
|
0
|
62 fasta_filename = os.path.join( target_directory, fasta_base_filename )
|
|
63 fasta_writer = open( fasta_filename, 'wb+' )
|
|
64
|
|
65 while True:
|
|
66 buffer = fasta_stream.read(CHUNK_SIZE)
|
|
67 if not buffer:
|
|
68 break
|
|
69
|
|
70 fasta_writer.write(buffer)
|
|
71
|
|
72 fasta_stream.close()
|
|
73 fasta_writer.close()
|
|
74
|
4
|
75 return dict( value=value, name=name, path=fasta_base_filename )
|
0
|
76
|
|
77 def main():
|
|
78 #Parse Command Line
|
|
79 parser = optparse.OptionParser()
|
|
80 parser.add_option( '-m', '--motif_db', dest='motif_db', action='store', type="string", default=None, help='motif_db' )
|
|
81 (options, args) = parser.parse_args()
|
|
82
|
|
83 filename = args[0]
|
|
84
|
|
85 params = from_json_string( open( filename ).read() )
|
|
86 target_directory = params[ 'output_data' ][0]['extra_files_path']
|
|
87 os.mkdir( target_directory )
|
|
88 data_manager_dict = {}
|
|
89
|
|
90 #Fetch the Motif Database
|
1
|
91 download_motif_databases( data_manager_dict, params, target_directory, options.motif_db )
|
0
|
92
|
|
93 #save info to json file
|
|
94 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) )
|
|
95
|
|
96 if __name__ == "__main__": main()
|