annotate data_manager/data_manager_fetch_tx2gene.py @ 5:c380b7da5b65 draft default tip

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