annotate data_manager/data_manager_fetch_motifs.py @ 0:cab6e1cb4c2a draft default tip

Uploaded
author jeremyjliu
date Sat, 16 May 2015 17:23:06 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
1 #!/usr/bin/env python
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
2 # Jeremy Liu
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
3 # February 2015
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
4 # Adapted from Dan Blackenburg's sample data manager
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
5
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
6 import sys
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
7 import os
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
8 import tempfile
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
9 import shutil
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
10 import optparse
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
11 import urllib2
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
12 #import uuid
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
13 from ftplib import FTP
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
14 import tarfile
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
15 import zipfile
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
16 import gzip
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
17 import bz2
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
18
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
19 from galaxy.util.json import from_json_string, to_json_string
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
20
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
21 CHUNK_SIZE = 2**20 #1mb
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
22
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
23 def download_motif_databases( data_manager_dict, params, target_directory, motif_db ):
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
24
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
25 # Select download URL, file name, data table name, and path using motif_db selector variable
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
26 if motif_db == "encode":
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
27 BGZ = ['http://compbio.med.harvard.edu/motif-enrichment/pouya_motifs.bed.bgz',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
28 "pouya_motifs.bed.bgz", "encode_bgz", "Encode Motifs (hg19) BGZ"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
29 TBI = ['http://compbio.med.harvard.edu/motif-enrichment/pouya_motifs.bed.bgz.tbi',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
30 "pouya_motifs.bed.bgz.tbi", "encode_tbi", "Encode Motifs (hg19) TBI"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
31 PWM = ['http://compbio.med.harvard.edu/motif-enrichment/pwms/pouya.pwms.from.seq.meme.txt',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
32 "pouya.pwms.from.seq.meme.txt", "encode_pwm", "Encode Motifs (hg19) PWM MEME"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
33 elif motif_db == "jaspar":
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
34 BGZ = ['http://compbio.med.harvard.edu/motif-enrichment/jaspar_jolma_motifs.bed.bgz',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
35 "jaspar_jolma_motifs.bed.bgz", "jaspar_bgz", "Jaspar and Jolma Motifs (hg19) BGZ"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
36 TBI = ['http://compbio.med.harvard.edu/motif-enrichment/jaspar_jolma_motifs.bed.bgz.tbi',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
37 "jaspar_jolma_motifs.bed.bgz.tbi", "jaspar_tbi", "Jaspar and Jolma Motifs (hg19) TBI"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
38 PWM = ['http://compbio.med.harvard.edu/motif-enrichment/pwms/jaspar.jolma.pwms.from.seq.meme.txt',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
39 "jaspar.jolma.pwms.from.seq.meme.txt", "jaspar_pwm", "Jaspar and Jolma Motifs (hg19) PWM MEME"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
40 elif motif_db == "mouse":
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
41 BGZ = ['http://compbio.med.harvard.edu/motif-enrichment/mm9_motifs_split.bed.bgz',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
42 "mm9_motifs_split.bed.bgz", "mouse_bgz", "Mouse Motifs (mm9) BGZ"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
43 TBI = ['http://compbio.med.harvard.edu/motif-enrichment/mm9_motifs_split.bed.bgz.tbi',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
44 "mm9_motifs_split.bed.bgz.tbi", "mouse_tbi", "Mouse Motifs (mm9) TBI"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
45 PWM = ['http://compbio.med.harvard.edu/motif-enrichment/pwms/mm9.pwms.from.seq.meme.txt',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
46 "mm9.pwms.from.seq.meme.txt", "mouse_pwm", "Mouse Motifs (mm9) PWM MEME"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
47 else:
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
48 BGZ = ['http://compbio.med.harvard.edu/motif-enrichment/pouya_test_motifs.bed.bgz',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
49 "pouya_test_motifs.bed.bgz", "test_bgz", "Test Encode Motifs (hg19) BGZ"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
50 TBI = ['http://compbio.med.harvard.edu/motif-enrichment/pouya_test_motifs.bed.bgz.tbi',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
51 "pouya_test_motifs.bed.bgz.tbi", "test_tbi", "Test Encode Motifs (hg19) TBI"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
52 PWM = ['http://compbio.med.harvard.edu/motif-enrichment/pwms/pouya.pwms.from.seq.meme.txt',
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
53 "pouya.pwms.from.seq.meme.txt", "test_pwm", "Test Encode Motifs (hg19) PWM MEME"]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
54
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
55
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
56 # Save and add motif bgz file to motif_databases data table
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
57 bgz_reader = urllib2.urlopen( BGZ[0] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
58 bgz_data_table_entry = _stream_fasta_to_file( bgz_reader, target_directory, params,
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
59 BGZ[1], BGZ[2], BGZ[3] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
60 _add_data_table_entry( data_manager_dict, 'motif_databases', bgz_data_table_entry )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
61
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
62 # Save and add motif tbi file to motif_databases data table
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
63 tbi_reader = urllib2.urlopen( TBI[0] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
64 tbi_data_table_entry = _stream_fasta_to_file( tbi_reader, target_directory, params,
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
65 TBI[1], TBI[2], TBI[3] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
66 _add_data_table_entry( data_manager_dict, 'motif_databases', tbi_data_table_entry )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
67
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
68 # Save and add motif pwm file to motif_databases data table
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
69 tbi_reader = urllib2.urlopen( PWM[0] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
70 tbi_data_table_entry = _stream_fasta_to_file( tbi_reader, target_directory, params,
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
71 PWM[1], PWM[2], PWM[3] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
72 _add_data_table_entry( data_manager_dict, 'motif_databases', tbi_data_table_entry )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
73
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
74 def _add_data_table_entry( data_manager_dict, data_table, data_table_entry ):
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
75 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
76 data_manager_dict['data_tables'][data_table] = data_manager_dict['data_tables'].get( data_table, [] )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
77 data_manager_dict['data_tables'][data_table].append( data_table_entry )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
78 return data_manager_dict
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
79
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
80 def _stream_fasta_to_file( fasta_stream, target_directory, params,
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
81 fasta_base_filename, value, name, close_stream=True ):
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
82 fasta_filename = os.path.join( target_directory, fasta_base_filename )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
83 fasta_writer = open( fasta_filename, 'wb+' )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
84
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
85 while True:
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
86 buffer = fasta_stream.read(CHUNK_SIZE)
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
87 if not buffer:
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
88 break
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
89
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
90 fasta_writer.write(buffer)
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
91
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
92 fasta_stream.close()
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
93 fasta_writer.close()
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
94
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
95 return dict( value=value, name=name, path=fasta_base_filename )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
96
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
97 def main():
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
98 #Parse Command Line
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
99 parser = optparse.OptionParser()
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
100 parser.add_option( '-m', '--motif_db', dest='motif_db', action='store', type="string", default=None, help='motif_db' )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
101 (options, args) = parser.parse_args()
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
102
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
103 filename = args[0]
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
104
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
105 params = from_json_string( open( filename ).read() )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
106 target_directory = params[ 'output_data' ][0]['extra_files_path']
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
107 os.mkdir( target_directory )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
108 data_manager_dict = {}
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
109
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
110 #Fetch the Motif Database
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
111 download_motif_databases( data_manager_dict, params, target_directory, options.motif_db )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
112
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
113 #save info to json file
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
114 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) )
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
115
cab6e1cb4c2a Uploaded
jeremyjliu
parents:
diff changeset
116 if __name__ == "__main__": main()