annotate data_manager/data_manager_fetch_gff.py @ 0:ac4fde07eaed draft

Uploaded
author ieguinoa
date Mon, 09 Jul 2018 11:58:50 -0400
parents
children c57bd7f3fb46
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
1 #!/usr/bin/env python
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
2 #Dan Blankenberg
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
3
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
4 import sys
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
5 import os
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
6 import tempfile
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
7 import shutil
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
8 import optparse
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
9 from ftplib import FTP
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
10 import tarfile
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
11 import zipfile
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
12 import gzip
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
13 import bz2
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
14 try:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
15 # For Python 3.0 and later
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
16 from urllib.request import urlopen
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
17 from io import BytesIO as StringIO
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
18 from io import UnsupportedOperation
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
19 except ImportError:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
20 # Fall back to Python 2's urllib2
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
21 from urllib2 import urlopen
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
22 from StringIO import StringIO
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
23 UnsupportedOperation = AttributeError
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
24 from json import loads, dumps
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
25
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
26
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
27 CHUNK_SIZE = 2**20 # 1mb
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
28
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
29 DATA_TABLE_NAME = 'all_gff'
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
30
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
31 def cleanup_before_exit( tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
32 if tmp_dir and os.path.exists( tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
33 shutil.rmtree( tmp_dir )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
34
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
35
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
36 def stop_err(msg):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
37 sys.stderr.write(msg)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
38 sys.exit(1)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
39
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
40
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
41 def get_dbkey_dbname_id_name( params, dbkey_description=None ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
42 dbkey = params['param_dict']['dbkey_source']['dbkey']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
43 #TODO: ensure sequence_id is unique and does not already appear in location file
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
44 sequence_id = params['param_dict']['sequence_id']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
45 if not sequence_id:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
46 sequence_id = dbkey #uuid.uuid4() generate and use an uuid instead?
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
47
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
48 if params['param_dict']['dbkey_source']['dbkey_source_selector'] == 'new':
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
49 dbkey_name = params['param_dict']['dbkey_source']['dbkey_name']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
50 if not dbkey_name:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
51 dbkey_name = dbkey
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
52 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
53 dbkey_name = None
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
54
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
55 sequence_name = params['param_dict']['sequence_name']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
56 if not sequence_name:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
57 sequence_name = dbkey_description
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
58 if not sequence_name:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
59 sequence_name = dbkey
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
60 return dbkey, dbkey_name, sequence_id, sequence_name
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
61
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
62
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
63 def _get_files_in_ftp_path( ftp, path ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
64 path_contents = []
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
65 ftp.retrlines( 'MLSD %s' % ( path ), path_contents.append )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
66 return [ line.split( ';' )[ -1 ].lstrip() for line in path_contents ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
67
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
68
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
69 def _get_stream_readers_for_tar( fh, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
70 fasta_tar = tarfile.open( fileobj=fh, mode='r:*' )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
71 return [x for x in [fasta_tar.extractfile(member) for member in fasta_tar.getmembers()] if x]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
72
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
73
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
74 def _get_stream_readers_for_zip( fh, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
75 """
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
76 Unpacks all archived files in a zip file.
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
77 Individual files will be concatenated (in _stream_fasta_to_file)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
78 """
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
79 fasta_zip = zipfile.ZipFile( fh, 'r' )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
80 rval = []
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
81 for member in fasta_zip.namelist():
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
82 fasta_zip.extract( member, tmp_dir )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
83 rval.append( open( os.path.join( tmp_dir, member ), 'rb' ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
84 return rval
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
85
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
86
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
87 def _get_stream_readers_for_gzip( fh, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
88 return [ gzip.GzipFile( fileobj=fh, mode='rb') ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
89
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
90
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
91 def _get_stream_readers_for_bz2( fh, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
92 return [ bz2.BZ2File( fh.name, 'rb') ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
93
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
94
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
95 def sort_fasta( fasta_filename, sort_method, params ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
96 if sort_method is None:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
97 return
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
98 assert sort_method in SORTING_METHODS, ValueError( "%s is not a valid sorting option." % sort_method )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
99 return SORTING_METHODS[ sort_method ]( fasta_filename, params )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
100
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
101
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
102 def _move_and_index_fasta_for_sorting( fasta_filename ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
103 unsorted_filename = tempfile.NamedTemporaryFile().name
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
104 shutil.move( fasta_filename, unsorted_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
105 fasta_offsets = {}
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
106 unsorted_fh = open( unsorted_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
107 while True:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
108 offset = unsorted_fh.tell()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
109 line = unsorted_fh.readline()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
110 if not line:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
111 break
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
112 if line.startswith( ">" ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
113 line = line.split( None, 1 )[0][1:]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
114 fasta_offsets[ line ] = offset
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
115 unsorted_fh.close()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
116 current_order = map( lambda x: x[1], sorted( map( lambda x: ( x[1], x[0] ), fasta_offsets.items() ) ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
117 return ( unsorted_filename, fasta_offsets, current_order )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
118
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
119
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
120 def _write_sorted_fasta( sorted_names, fasta_offsets, sorted_fasta_filename, unsorted_fasta_filename ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
121 unsorted_fh = open( unsorted_fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
122 sorted_fh = open( sorted_fasta_filename, 'wb+' )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
123
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
124 for name in sorted_names:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
125 offset = fasta_offsets[ name ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
126 unsorted_fh.seek( offset )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
127 sorted_fh.write( unsorted_fh.readline() )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
128 while True:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
129 line = unsorted_fh.readline()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
130 if not line or line.startswith( ">" ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
131 break
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
132 sorted_fh.write( line )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
133 unsorted_fh.close()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
134 sorted_fh.close()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
135
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
136
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
137 def _sort_fasta_as_is( fasta_filename, params ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
138 return
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
139
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
140 def _sort_fasta_lexicographical( fasta_filename, params ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
141 ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
142 sorted_names = sorted( fasta_offsets.keys() )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
143 if sorted_names == current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
144 shutil.move( unsorted_filename, fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
145 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
146 _write_sorted_fasta( sorted_names, fasta_offsets, fasta_filename, unsorted_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
147
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
148
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
149 def _sort_fasta_gatk( fasta_filename, params ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
150 #This method was added by reviewer request.
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
151 ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
152 sorted_names = map( str, range( 1, 23 ) ) + [ 'X', 'Y' ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
153 #detect if we have chrN, or just N
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
154 has_chr = False
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
155 for chrom in sorted_names:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
156 if "chr%s" % chrom in current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
157 has_chr = True
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
158 break
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
159
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
160 if has_chr:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
161 sorted_names = map( lambda x: "chr%s" % x, sorted_names)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
162 sorted_names.insert( 0, "chrM" )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
163 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
164 sorted_names.insert( 0, "MT" )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
165 sorted_names.extend( map( lambda x: "%s_random" % x, sorted_names ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
166
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
167 existing_sorted_names = []
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
168 for name in sorted_names:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
169 if name in current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
170 existing_sorted_names.append( name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
171 for name in current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
172 #TODO: confirm that non-canonical names do not need to be sorted specially
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
173 if name not in existing_sorted_names:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
174 existing_sorted_names.append( name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
175
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
176 if existing_sorted_names == current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
177 shutil.move( unsorted_filename, fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
178 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
179 _write_sorted_fasta( existing_sorted_names, fasta_offsets, fasta_filename, unsorted_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
180
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
181
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
182 def _sort_fasta_custom( fasta_filename, params ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
183 ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
184 sorted_names = []
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
185 for id_repeat in params['param_dict']['sorting']['sequence_identifiers']:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
186 sorted_names.append( id_repeat[ 'identifier' ] )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
187 handle_not_listed = params['param_dict']['sorting']['handle_not_listed_selector']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
188 if handle_not_listed.startswith( 'keep' ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
189 add_list = []
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
190 for name in current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
191 if name not in sorted_names:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
192 add_list.append( name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
193 if add_list:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
194 if handle_not_listed == 'keep_append':
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
195 sorted_names.extend( add_list )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
196 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
197 add_list.extend( sorted_names )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
198 sorted_names = add_list
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
199 if sorted_names == current_order:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
200 shutil.move( unsorted_filename, fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
201 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
202 _write_sorted_fasta( sorted_names, fasta_offsets, fasta_filename, unsorted_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
203
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
204
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
205 def _download_file(start, fh):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
206 tmp = tempfile.NamedTemporaryFile()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
207 tmp.write(start)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
208 tmp.write(fh.read())
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
209 tmp.flush()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
210 tmp.seek(0)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
211 return tmp
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
212
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
213
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
214 def get_stream_reader(fh, tmp_dir):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
215 """
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
216 Check if file is compressed and return correct stream reader.
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
217 If file has to be downloaded, do it now.
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
218 """
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
219 magic_dict = {
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
220 b"\x1f\x8b\x08": _get_stream_readers_for_gzip,
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
221 b"\x42\x5a\x68": _get_stream_readers_for_bz2,
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
222 b"\x50\x4b\x03\x04": _get_stream_readers_for_zip,
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
223 }
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
224 start_of_file = fh.read(CHUNK_SIZE)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
225 try:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
226 fh.seek(0)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
227 except UnsupportedOperation: # This is if fh has been created by urlopen
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
228 fh = _download_file(start_of_file, fh)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
229 for k,v in magic_dict.items():
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
230 if start_of_file.startswith(k):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
231 return v(fh, tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
232 try: # Check if file is tar file
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
233 if tarfile.open(fileobj=StringIO(start_of_file)):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
234 return _get_stream_readers_for_tar(fh, tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
235 except tarfile.ReadError:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
236 pass
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
237 return fh
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
238
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
239
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
240 def _get_ucsc_download_address(params, dbkey):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
241 """
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
242 Check if we can find the correct file for the supplied dbkey on UCSC's FTP server
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
243 """
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
244 UCSC_FTP_SERVER = 'hgdownload.cse.ucsc.edu'
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
245 UCSC_DOWNLOAD_PATH = '/goldenPath/%s/bigZips/'
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
246 COMPRESSED_EXTENSIONS = ['.tar.gz', '.tgz', '.tar.bz2', '.zip', '.fa.gz', '.fa.bz2']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
247
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
248 email = params['param_dict']['__user_email__']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
249 if not email:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
250 email = 'anonymous@example.com'
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
251
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
252 ucsc_dbkey = params['param_dict']['reference_source']['requested_dbkey'] or dbkey
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
253 UCSC_CHROM_FA_FILENAMES = ['%s.chromFa' % ucsc_dbkey, 'chromFa', ucsc_dbkey]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
254
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
255 ftp = FTP(UCSC_FTP_SERVER)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
256 ftp.login('anonymous', email)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
257
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
258 ucsc_path = UCSC_DOWNLOAD_PATH % ucsc_dbkey
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
259 path_contents = _get_files_in_ftp_path(ftp, ucsc_path)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
260 ftp.quit()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
261
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
262 for ucsc_chrom_fa_filename in UCSC_CHROM_FA_FILENAMES:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
263 for ext in COMPRESSED_EXTENSIONS:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
264 if "%s%s" % (ucsc_chrom_fa_filename, ext) in path_contents:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
265 ucsc_file_name = "%s%s%s" % (ucsc_path, ucsc_chrom_fa_filename, ext)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
266 return "ftp://%s%s" % (UCSC_FTP_SERVER, ucsc_file_name)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
267
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
268 raise Exception('Unable to determine filename for UCSC Genome for %s: %s' % (ucsc_dbkey, path_contents))
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
269
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
270 def add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
271 for data_table_name, data_table_entry in _stream_fasta_to_file( fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
272 if data_table_entry:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
273 _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
274
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
275
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
276 def download_from_ucsc( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
277 url = _get_ucsc_download_address(params, dbkey)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
278 fasta_readers = get_stream_reader(urlopen(url), tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
279 add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
280
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
281
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
282 def download_from_ncbi( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
283 NCBI_DOWNLOAD_URL = 'http://togows.dbcls.jp/entry/ncbi-nucleotide/%s.fasta' #FIXME: taken from dave's genome manager...why some japan site?
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
284 requested_identifier = params['param_dict']['reference_source']['requested_identifier']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
285 url = NCBI_DOWNLOAD_URL % requested_identifier
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
286 fasta_readers = get_stream_reader(urlopen(url), tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
287 add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
288
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
289
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
290 def download_from_url( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
291 urls = filter( bool, map( lambda x: x.strip(), params['param_dict']['reference_source']['user_url'].split( '\n' ) ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
292 fasta_readers = [ get_stream_reader(urlopen( url ), tmp_dir) for url in urls ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
293 add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id,sequence_name, params)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
294
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
295
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
296 def download_from_history( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
297 #TODO: allow multiple FASTA input files
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
298 input_filename = params['param_dict']['reference_source']['input_fasta']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
299 if isinstance( input_filename, list ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
300 fasta_readers = [ get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
301 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
302 fasta_readers = get_stream_reader(open(input_filename), tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
303 add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
304
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
305
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
306 def copy_from_directory( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
307 input_filename = params['param_dict']['reference_source']['fasta_filename']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
308 create_symlink = params['param_dict']['reference_source']['create_symlink'] == 'create_symlink'
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
309 if create_symlink:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
310 data_table_entries = _create_symlink( input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
311 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
312 if isinstance( input_filename, list ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
313 fasta_readers = [ get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
314 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
315 fasta_readers = get_stream_reader(open(input_filename), tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
316 data_table_entries = _stream_fasta_to_file( fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
317 for data_table_name, data_table_entry in data_table_entries:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
318 if data_table_entry:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
319 _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
320
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
321
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
322 def _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
323 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
324 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get( DATA_TABLE_NAME, [] )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
325 data_manager_dict['data_tables'][data_table_name].append( data_table_entry )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
326 return data_manager_dict
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
327
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
328
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
329 def _stream_fasta_to_file( fasta_stream, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params, close_stream=True ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
330 fasta_base_filename = "%s.gff" % sequence_id
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
331 fasta_filename = os.path.join( target_directory, fasta_base_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
332 with open( fasta_filename, 'wb+' ) as fasta_writer:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
333
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
334 if isinstance( fasta_stream, list ) and len( fasta_stream ) == 1:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
335 fasta_stream = fasta_stream[0]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
336
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
337 if isinstance( fasta_stream, list ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
338 last_char = None
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
339 for fh in fasta_stream:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
340 if last_char not in [ None, '\n', '\r', b'\n', b'\r' ]:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
341 fasta_writer.write( b'\n' )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
342 while True:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
343 data = fh.read( CHUNK_SIZE )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
344 if data:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
345 fasta_writer.write( data )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
346 last_char = data[-1]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
347 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
348 break
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
349 if close_stream:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
350 fh.close()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
351 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
352 while True:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
353 data = fasta_stream.read( CHUNK_SIZE )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
354 if data:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
355 fasta_writer.write( data )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
356 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
357 break
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
358 if close_stream:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
359 fasta_stream.close()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
360
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
361 #sort_fasta( fasta_filename, params['param_dict']['sorting']['sort_selector'], params )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
362
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
363 dbkey_dict = None
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
364 if dbkey_name:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
365 #do len calc here
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
366 #len_base_name = "%s.len" % ( dbkey )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
367 #compute_fasta_length( fasta_filename, os.path.join( target_directory, len_base_name ), keep_first_word=True )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
368 dbkey_dict = dict( value=dbkey, name=dbkey_name, len_path='' )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
369
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
370 return [ ( '__dbkeys__', dbkey_dict ), ( DATA_TABLE_NAME, dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename ) ) ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
371
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
372
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
373 def compute_fasta_length( fasta_file, out_file, keep_first_word=False ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
374
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
375 infile = fasta_file
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
376 out = open( out_file, 'w')
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
377
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
378 fasta_title = ''
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
379 seq_len = 0
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
380
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
381 first_entry = True
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
382
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
383 for line in open( infile ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
384 line = line.strip()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
385 if not line or line.startswith( '#' ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
386 continue
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
387 if line[0] == '>':
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
388 if first_entry == False:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
389 if keep_first_word:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
390 fasta_title = fasta_title.split()[0]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
391 out.write( "%s\t%d\n" % ( fasta_title[ 1: ], seq_len ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
392 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
393 first_entry = False
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
394 fasta_title = line
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
395 seq_len = 0
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
396 else:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
397 seq_len += len(line)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
398
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
399 # last fasta-entry
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
400 if keep_first_word:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
401 fasta_title = fasta_title.split()[0]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
402 out.write( "%s\t%d\n" % ( fasta_title[ 1: ], seq_len ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
403 out.close()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
404
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
405
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
406 def _create_symlink( input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name ):
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
407 fasta_base_filename = "%s.fa" % sequence_id
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
408 fasta_filename = os.path.join( target_directory, fasta_base_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
409 os.symlink( input_filename, fasta_filename )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
410
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
411 dbkey_dict = None
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
412 if dbkey_name:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
413 #do len calc here
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
414 len_base_name = "%s.len" % ( dbkey )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
415 compute_fasta_length( fasta_filename, os.path.join( target_directory, len_base_name ), keep_first_word=True )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
416 dbkey_dict = dict( value=dbkey, name=dbkey_name, len_path=len_base_name )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
417
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
418 return [ ( '__dbkeys__', dbkey_dict ), ( DATA_TABLE_NAME, dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename ) ) ]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
419
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
420
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
421 REFERENCE_SOURCE_TO_DOWNLOAD = dict( ucsc=download_from_ucsc, ncbi=download_from_ncbi, url=download_from_url, history=download_from_history, directory=copy_from_directory )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
422
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
423 SORTING_METHODS = dict( as_is=_sort_fasta_as_is, lexicographical=_sort_fasta_lexicographical, gatk=_sort_fasta_gatk, custom=_sort_fasta_custom )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
424
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
425
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
426 def main():
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
427 #Parse Command Line
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
428 parser = optparse.OptionParser()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
429 parser.add_option( '-d', '--dbkey_description', dest='dbkey_description', action='store', type="string", default=None, help='dbkey_description' )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
430 parser.add_option( '-t', '--type', dest='file_type', action='store', type='string', default=None, help='file_type')
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
431 (options, args) = parser.parse_args()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
432
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
433 filename = args[0]
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
434 global DATA_TABLE_NAME
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
435 if options.file_type == 'representative':
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
436 DATA_TABLE_NAME= 'representative_gff'
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
437 params = loads( open( filename ).read() )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
438 target_directory = params[ 'output_data' ][0]['extra_files_path']
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
439 os.mkdir( target_directory )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
440 data_manager_dict = {}
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
441
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
442 dbkey, dbkey_name, sequence_id, sequence_name = get_dbkey_dbname_id_name( params, dbkey_description=options.dbkey_description )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
443
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
444 if dbkey in [ None, '', '?' ]:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
445 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
446
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
447 # Create a tmp_dir, in case a zip file needs to be uncompressed
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
448 tmp_dir = tempfile.mkdtemp()
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
449 #Fetch the FASTA
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
450 try:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
451 REFERENCE_SOURCE_TO_DOWNLOAD[ params['param_dict']['reference_source']['reference_source_selector'] ]( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
452 finally:
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
453 cleanup_before_exit(tmp_dir)
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
454 #save info to json file
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
455 open( filename, 'wb' ).write( dumps( data_manager_dict ).encode() )
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
456
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
457 if __name__ == "__main__":
ac4fde07eaed Uploaded
ieguinoa
parents:
diff changeset
458 main()