7
7
__all__ = ["FastaDumpApp" , "FastqDumpApp" ]
8
8
9
9
import abc
10
+ from os .path import join
10
11
from subprocess import Popen , SubprocessError , PIPE , TimeoutExpired
11
12
import glob
12
- from tempfile import gettempprefix , NamedTemporaryFile
13
- from ..localapp import cleanup_tempfile
13
+ from tempfile import TemporaryDirectory
14
14
from ..application import Application , AppState , AppStateError , \
15
15
requires_state
16
16
from ...sequence .seqtypes import NucleotideSequence
@@ -54,13 +54,11 @@ def __init__(self, uid, output_path_prefix=None,
54
54
self ._prefetch_path = prefetch_path
55
55
self ._fasterq_dump_path = fasterq_dump_path
56
56
self ._uid = uid
57
+ self ._sra_dir = TemporaryDirectory (suffix = "_sra" )
57
58
if output_path_prefix is None :
58
- self ._prefix = gettempprefix ( )
59
+ self ._prefix = join ( self . _sra_dir . name , self . _uid )
59
60
else :
60
61
self ._prefix = output_path_prefix
61
- self ._sra_file = NamedTemporaryFile (
62
- "w" , suffix = ".sra" , delete = False
63
- )
64
62
self ._prefetch_process = None
65
63
self ._fasterq_dump_process = None
66
64
@@ -94,10 +92,14 @@ def join(self, timeout=None):
94
92
95
93
96
94
def run (self ):
95
+ # Prefetch into a temp directory with file name equaling UID
96
+ # This ensures that the ID in the header is not the temp prefix
97
+ sra_file_name = join (self ._sra_dir .name , self ._uid )
97
98
command = (
98
- f"{ self ._prefetch_path } -q -o { self ._sra_file .name } { self ._uid } ; "
99
+ f"{ self ._prefetch_path } -q -O { self ._sra_dir .name } "
100
+ f"{ self .get_prefetch_options ()} { self ._uid } ; "
99
101
f"{ self ._fasterq_dump_path } -q -o { self ._prefix } .fastq "
100
- f"{ self .get_fastq_dump_options ()} { self . _sra_file . name } "
102
+ f"{ self .get_fastq_dump_options ()} { sra_file_name } "
101
103
)
102
104
self ._process = Popen (
103
105
command , stdout = PIPE , stderr = PIPE , shell = True , encoding = "UTF-8"
@@ -120,8 +122,8 @@ def evaluate(self):
120
122
if exit_code != 0 :
121
123
err_msg = self ._stderr .replace ("\n " , " " )
122
124
raise SubprocessError (
123
- f"'{ self . _bin_path } ' returned with exit code { exit_code } : "
124
- f"{ err_msg } "
125
+ f"'prefetch' or 'fasterq-dump' returned with exit code "
126
+ f"{ exit_code } : { err_msg } "
125
127
)
126
128
127
129
self ._file_names = (
@@ -130,6 +132,8 @@ def evaluate(self):
130
132
# For entries with multiple reads per spot
131
133
glob .glob (self ._prefix + "_*.fastq" )
132
134
)
135
+ print (self ._prefix )
136
+ print (self ._file_names )
133
137
# Only load FASTQ files into memory when needed
134
138
self ._fastq_files = None
135
139
@@ -142,9 +146,24 @@ def wait_interval(self):
142
146
def clean_up (self ):
143
147
if self .get_app_state () == AppState .CANCELLED :
144
148
self ._process .kill ()
145
- cleanup_tempfile (self ._sra_file )
149
+ # Directory with temp files does not need to be deleted,
150
+ # as temp dir is automatically deleted upon object destruction
146
151
147
152
153
+ @requires_state (AppState .CREATED )
154
+ def get_prefetch_options (self ):
155
+ """
156
+ Get additional options for the `prefetch` call.
157
+
158
+ PROTECTED: Override when inheriting.
159
+
160
+ Returns
161
+ -------
162
+ options: str
163
+ The additional options.
164
+ """
165
+ return ""
166
+
148
167
@requires_state (AppState .CREATED )
149
168
def get_fastq_dump_options (self ):
150
169
"""
@@ -155,7 +174,7 @@ def get_fastq_dump_options(self):
155
174
Returns
156
175
-------
157
176
options: str
158
- The additional options
177
+ The additional options.
159
178
"""
160
179
return ""
161
180
@@ -359,6 +378,14 @@ def __init__(self, uid, output_path_prefix=None, prefetch_path="prefetch",
359
378
self ._fasta_files = None
360
379
361
380
381
+ @requires_state (AppState .CREATED )
382
+ def get_prefetch_options (self ):
383
+ return
384
+ # TODO: Use '--eliminate-quals'
385
+ # when https://github.com/ncbi/sra-tools/issues/883 is resolved
386
+ # return "--eliminate-quals"
387
+
388
+
362
389
@requires_state (AppState .CREATED )
363
390
def get_fastq_dump_options (self ):
364
391
return "--fasta"
0 commit comments