annotate data_manager/data_manager_fetch_tx2gene.py @ 2:7d3ffe28ff3f draft

Uploaded
author ieguinoa
date Wed, 10 Oct 2018 11:44:17 -0400
parents a4d4da89aae1
children d71f65b854de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
1 #!/usr/bin/env python
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
2 #Dan Blankenberg
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
3
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
4 import sys
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
5 import os
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
6 import tempfile
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
7 import shutil
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
8 import optparse
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
9 from ftplib import FTP
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
10 import tarfile
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
11 import zipfile
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
12 import gzip
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
13 import bz2
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
14 try:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
15 # For Python 3.0 and later
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
16 from urllib.request import urlopen
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
17 from io import BytesIO as StringIO
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
18 from io import UnsupportedOperation
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
19 except ImportError:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
20 # Fall back to Python 2's urllib2
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
21 from urllib2 import urlopen
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
22 from StringIO import StringIO
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
23 UnsupportedOperation = AttributeError
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
24 from json import loads, dumps
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
25
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
26
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
27 CHUNK_SIZE = 2**20 # 1mb
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
28
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
29 DATA_TABLE_NAME = 'tx2gene'
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
30
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
31 def cleanup_before_exit( tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
32 if tmp_dir and os.path.exists( tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
33 shutil.rmtree( tmp_dir )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
34
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
35
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
36 def stop_err(msg):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
37 sys.stderr.write(msg)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
38 sys.exit(1)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
39
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
40
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
41 def get_dbkey_dbname_id_name( params, dbkey_description=None ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
42 # dbkey = params['param_dict']['dbkey_source']['dbkey']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
43 #TODO: ensure sequence_id is unique and does not already appear in location file
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
44 sequence_id = params['param_dict']['sequence_id']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
45 if not sequence_id:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
46 sequence_id = dbkey #uuid.uuid4() generate and use an uuid instead?
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
47
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
48 # if params['param_dict']['dbkey_source']['dbkey_source_selector'] == 'new':
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
49 # dbkey_name = params['param_dict']['dbkey_source']['dbkey_name']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
50 # if not dbkey_name:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
51 # dbkey_name = dbkey
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
52 # else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
53 # dbkey_name = None
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
54 dbkey = params['param_dict']['dbkey']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
55 dbkey_name = dbkey_description
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
56 sequence_name = params['param_dict']['sequence_name']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
57 if not sequence_name:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
58 sequence_name = dbkey_description
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
59 if not sequence_name:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
60 sequence_name = dbkey
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
61 return dbkey, dbkey_name, sequence_id, sequence_name
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
62
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
63
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
64 def _get_files_in_ftp_path( ftp, path ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
65 path_contents = []
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
66 ftp.retrlines( 'MLSD %s' % ( path ), path_contents.append )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
67 return [ line.split( ';' )[ -1 ].lstrip() for line in path_contents ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
68
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
69
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
70 def _get_stream_readers_for_tar( fh, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
71 fasta_tar = tarfile.open( fileobj=fh, mode='r:*' )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
72 return [x for x in [fasta_tar.extractfile(member) for member in fasta_tar.getmembers()] if x]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
73
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
74
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
75 def _get_stream_readers_for_zip( fh, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
76 """
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
77 Unpacks all archived files in a zip file.
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
78 Individual files will be concatenated (in _stream_fasta_to_file)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
79 """
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
80 fasta_zip = zipfile.ZipFile( fh, 'r' )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
81 rval = []
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
82 for member in fasta_zip.namelist():
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
83 fasta_zip.extract( member, tmp_dir )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
84 rval.append( open( os.path.join( tmp_dir, member ), 'rb' ) )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
85 return rval
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
86
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
87
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
88 def _get_stream_readers_for_gzip( fh, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
89 return [ gzip.GzipFile( fileobj=fh, mode='rb') ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
90
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
91
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
92 def _get_stream_readers_for_bz2( fh, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
93 return [ bz2.BZ2File( fh.name, 'rb') ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
94
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
95
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
96 def convert_tx2gene( fasta_filename, file_type, params ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
97 if file_type is 'tx2gene':
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
98 return #no need to extract tx2gene table
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
99 #If the file is actually a GFF/GTF file then extract the tx2gene
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
100 gff_temp_filename = tempfile.NamedTemporaryFile().name
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
101 shutil.move(fasta_filename, gff_temp_filename)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
102 args= ['Rscript']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
103 args.append(RSCRIPT_GFF_TO_TX2GENE)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
104 args.append(gff_temp_filename)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
105 args.append(fasta_filename)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
106
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
107 #assert sort_method in SORTING_METHODS, ValueError( "%s is not a valid sorting option." % sort_method )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
108 #return SORTING_METHODS[ sort_method ]( fasta_filename, params )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
109
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
110 def _download_file(start, fh):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
111 tmp = tempfile.NamedTemporaryFile()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
112 tmp.write(start)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
113 tmp.write(fh.read())
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
114 tmp.flush()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
115 tmp.seek(0)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
116 return tmp
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
117
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
118
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
119 def get_stream_reader(fh, tmp_dir):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
120 """
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
121 Check if file is compressed and return correct stream reader.
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
122 If file has to be downloaded, do it now.
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
123 """
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
124 magic_dict = {
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
125 b"\x1f\x8b\x08": _get_stream_readers_for_gzip,
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
126 b"\x42\x5a\x68": _get_stream_readers_for_bz2,
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
127 b"\x50\x4b\x03\x04": _get_stream_readers_for_zip,
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
128 }
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
129 start_of_file = fh.read(CHUNK_SIZE)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
130 try:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
131 fh.seek(0)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
132 except UnsupportedOperation: # This is if fh has been created by urlopen
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
133 fh = _download_file(start_of_file, fh)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
134 for k,v in magic_dict.items():
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
135 if start_of_file.startswith(k):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
136 return v(fh, tmp_dir)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
137 try: # Check if file is tar file
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
138 if tarfile.open(fileobj=StringIO(start_of_file)):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
139 return _get_stream_readers_for_tar(fh, tmp_dir)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
140 except tarfile.ReadError:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
141 pass
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
142 return fh
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
143
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
144
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
145
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
146 def add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
147 for data_table_name, data_table_entry in _stream_fasta_to_file( fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
148 if data_table_entry:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
149 _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
150
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
151
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
152 def download_from_url( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
153 urls = filter( bool, map( lambda x: x.strip(), params['param_dict']['reference_source']['user_url'].split( '\n' ) ) )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
154 fasta_readers = [ get_stream_reader(urlopen( url ), tmp_dir) for url in urls ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
155 add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id,sequence_name, params)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
156
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
157
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
158 def download_from_history( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
159 #TODO: allow multiple FASTA input files
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
160 input_filename = params['param_dict']['reference_source']['input_fasta']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
161 if isinstance( input_filename, list ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
162 fasta_readers = [ get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
163 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
164 fasta_readers = get_stream_reader(open(input_filename), tmp_dir)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
165 add_fasta_to_table(data_manager_dict, fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
166
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
167
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
168 def copy_from_directory( data_manager_dict, params, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, tmp_dir ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
169 input_filename = params['param_dict']['reference_source']['fasta_filename']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
170 create_symlink = params['param_dict']['reference_source']['create_symlink'] == 'create_symlink'
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
171 if create_symlink:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
172 data_table_entries = _create_symlink( input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
173 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
174 if isinstance( input_filename, list ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
175 fasta_readers = [ get_stream_reader(open(filename, 'rb'), tmp_dir) for filename in input_filename ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
176 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
177 fasta_readers = get_stream_reader(open(input_filename), tmp_dir)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
178 data_table_entries = _stream_fasta_to_file( fasta_readers, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
179 for data_table_name, data_table_entry in data_table_entries:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
180 if data_table_entry:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
181 _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
182
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
183
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
184 def _add_data_table_entry( data_manager_dict, data_table_entry, data_table_name ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
185 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
186 data_manager_dict['data_tables'][data_table_name] = data_manager_dict['data_tables'].get( DATA_TABLE_NAME, [] )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
187 data_manager_dict['data_tables'][data_table_name].append( data_table_entry )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
188 return data_manager_dict
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
189
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
190
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
191 def _stream_fasta_to_file( fasta_stream, target_directory, dbkey, dbkey_name, sequence_id, sequence_name, params, close_stream=True ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
192 fasta_base_filename = "%s_tx2gene.tab" % sequence_id
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
193 fasta_filename = os.path.join( target_directory, fasta_base_filename )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
194 with open( fasta_filename, 'wb+' ) as fasta_writer:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
195
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
196 if isinstance( fasta_stream, list ) and len( fasta_stream ) == 1:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
197 fasta_stream = fasta_stream[0]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
198
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
199 if isinstance( fasta_stream, list ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
200 last_char = None
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
201 for fh in fasta_stream:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
202 if last_char not in [ None, '\n', '\r', b'\n', b'\r' ]:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
203 fasta_writer.write( b'\n' )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
204 while True:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
205 data = fh.read( CHUNK_SIZE )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
206 if data:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
207 fasta_writer.write( data )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
208 last_char = data[-1]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
209 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
210 break
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
211 if close_stream:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
212 fh.close()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
213 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
214 while True:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
215 data = fasta_stream.read( CHUNK_SIZE )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
216 if data:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
217 fasta_writer.write( data )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
218 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
219 break
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
220 if close_stream:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
221 fasta_stream.close()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
222
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
223 convert_to_tx2gene( fasta_filename, params['param_dict']['file_type'], params )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
224 return [ ( DATA_TABLE_NAME, dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename ) ) ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
225
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
226
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
227 def compute_fasta_length( fasta_file, out_file, keep_first_word=False ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
228
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
229 infile = fasta_file
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
230 out = open( out_file, 'w')
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
231
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
232 fasta_title = ''
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
233 seq_len = 0
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
234
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
235 first_entry = True
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
236
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
237 for line in open( infile ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
238 line = line.strip()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
239 if not line or line.startswith( '#' ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
240 continue
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
241 if line[0] == '>':
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
242 if first_entry == False:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
243 if keep_first_word:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
244 fasta_title = fasta_title.split()[0]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
245 out.write( "%s\t%d\n" % ( fasta_title[ 1: ], seq_len ) )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
246 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
247 first_entry = False
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
248 fasta_title = line
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
249 seq_len = 0
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
250 else:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
251 seq_len += len(line)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
252
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
253 # last fasta-entry
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
254 if keep_first_word:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
255 fasta_title = fasta_title.split()[0]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
256 out.write( "%s\t%d\n" % ( fasta_title[ 1: ], seq_len ) )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
257 out.close()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
258
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
259
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
260 def _create_symlink( input_filename, target_directory, dbkey, dbkey_name, sequence_id, sequence_name ):
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
261 fasta_base_filename = "%s.fa" % sequence_id
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
262 fasta_filename = os.path.join( target_directory, fasta_base_filename )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
263 os.symlink( input_filename, fasta_filename )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
264 return [ ( DATA_TABLE_NAME, dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_filename ) ) ]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
265
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
266
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
267 REFERENCE_SOURCE_TO_DOWNLOAD = dict( url=download_from_url, history=download_from_history, directory=copy_from_directory )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
268
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
269
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
270 def main():
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
271 #Parse Command Line
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
272 parser = optparse.OptionParser()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
273 parser.add_option( '-d', '--dbkey_description', dest='dbkey_description', action='store', type="string", default=None, help='dbkey_description' )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
274 parser.add_option( '-t', '--type', dest='file_type', action='store', type='string', default=None, help='file_type')
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
275 (options, args) = parser.parse_args()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
276
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
277 filename = args[0]
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
278 #global DATA_TABLE_NAME
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
279 global RSCRIPT_GFF_TO_TX2GENE= os.path.join( options.base_dir, 'tximport.r')
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
280
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
281
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
282 if options.file_type == 'gff_gtf':
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
283 #DATA_TABLE_NAME= 'representative_gff'
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
284 else: #file_type='tx2gene'
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
285
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
286 params = loads( open( filename ).read() )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
287 target_directory = params[ 'output_data' ][0]['extra_files_path']
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
288 os.mkdir( target_directory )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
289 data_manager_dict = {}
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
290
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
291 dbkey, dbkey_name, sequence_id, sequence_name = get_dbkey_dbname_id_name( params, dbkey_description=options.dbkey_description )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
292
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
293 if dbkey in [ None, '', '?' ]:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
294 raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( dbkey ) )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
295
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
296 # Create a tmp_dir, in case a zip file needs to be uncompressed
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
297 tmp_dir = tempfile.mkdtemp()
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
298 #Fetch the input file
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
299 try:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
300 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)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
301 finally:
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
302 cleanup_before_exit(tmp_dir)
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
303 #save info to json file
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
304 open( filename, 'wb' ).write( dumps( data_manager_dict ).encode() )
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
305
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
306 if __name__ == "__main__":
a4d4da89aae1 Uploaded
ieguinoa
parents:
diff changeset
307 main()