1919# import functions
2020from src .plassembler .utils .assembly import run_flye , run_raven
2121from src .plassembler .utils .bam import bam_to_fastq_short , sam_to_bam , split_bams
22- from src .plassembler .utils .cleanup import remove_directory , remove_file
22+ from src .plassembler .utils .cleanup import remove_file
2323from src .plassembler .utils .external_tools import ExternalTool
2424from src .plassembler .utils .mapping import minimap_long_reads , minimap_short_reads
2525from src .plassembler .utils .qc import chopper , fastp
@@ -179,28 +179,24 @@ class test_qc_gzip(unittest.TestCase):
179179 def test_chopper_gzip (self ):
180180 expected_return = True
181181 input_long_reads = os .path .join (test_data , "test_long.fastq.gz" )
182- chopper (
183- input_long_reads , fake_out_dir , "500" , "9" , True , "1" , logdir
184- ) # True for gunzip
185- remove_file (os .path .join (fake_out_dir , "chopper_long_reads.fastq.gz" ))
182+ with tempfile .TemporaryDirectory () as tmp :
183+ chopper (input_long_reads , tmp , "500" , "9" , True , "1" , logdir ) # gunzip
186184 self .assertEqual (expected_return , True )
187185
188186 def test_chopper_not_gzip (self ):
189187 expected_return = True
190188 input_long_reads = os .path .join (test_data , "test_long.fastq" )
191- chopper (
192- input_long_reads , fake_out_dir , "500" , "9" , False , "1" , logdir
193- ) # fasle for gunzip
194- remove_file (os .path .join (fake_out_dir , "chopper_long_reads.fastq.gz" ))
189+ with tempfile .TemporaryDirectory () as tmp :
190+ chopper (input_long_reads , tmp , "500" , "9" , False , "1" , logdir ) # no gunzip
195191 self .assertEqual (expected_return , True )
196192
197193 def test_fastp_gzip (self ):
198194 expected_return = True
199195 short_one = Path (f"{ test_data } /C11_subsetsim_R1.fastq.gz" )
200196 short_two = Path (f"{ test_data } /C11_subsetsim_R2.fastq.gz" )
201- fastp ( short_one , short_two , fake_out_dir , logdir )
202- remove_file ( os . path . join ( fake_out_dir , "trimmed_R1.fastq" ) )
203- remove_file ( os . path . join ( fake_out_dir , "trimmed_R2.fastq" ))
197+ with tempfile . TemporaryDirectory () as tmp :
198+ fastp ( short_one , short_two , tmp , logdir )
199+ # fastp writes its report to the CWD regardless of output dir
204200 remove_file ("fastp.html" )
205201 remove_file ("fastp.json" )
206202 self .assertEqual (expected_return , True )
@@ -209,9 +205,8 @@ def test_fastp_nozip(self):
209205 expected_return = True
210206 short_one = Path (f"{ test_data } /C11_subsetsim_R1.fastq" )
211207 short_two = Path (f"{ test_data } /C11_subsetsim_R2.fastq" )
212- fastp (short_one , short_two , fake_out_dir , logdir )
213- remove_file (os .path .join (fake_out_dir , "trimmed_R1.fastq" ))
214- remove_file (os .path .join (fake_out_dir , "trimmed_R2.fastq" ))
208+ with tempfile .TemporaryDirectory () as tmp :
209+ fastp (short_one , short_two , tmp , logdir )
215210 remove_file ("fastp.html" )
216211 remove_file ("fastp.json" )
217212 self .assertEqual (expected_return , True )
@@ -223,28 +218,24 @@ class test_assemblers(unittest.TestCase):
223218
224219 def test_flye (self ):
225220 expected_return = True
226- # C11 sim reads
227- run_flye (test_data , 8 , raw_flag = False , pacbio_model = "nothing" , logdir = logdir )
228- shutil .rmtree (os .path .join (test_data , "00-assembly" ))
229- shutil .rmtree (os .path .join (test_data , "10-consensus" ))
230- shutil .rmtree (os .path .join (test_data , "20-repeat" ))
231- shutil .rmtree (os .path .join (test_data , "30-contigger" ))
232- shutil .rmtree (os .path .join (test_data , "40-polishing" ))
233- remove_file (os .path .join (test_data , "assembly.fasta" ))
234- remove_file (os .path .join (test_data , "assembly_info.txt" ))
235- remove_file (os .path .join (test_data , "assembly_graph.gfa" ))
236- remove_file (os .path .join (test_data , "assembly_graph.gv" ))
237- remove_file (os .path .join (test_data , "flye.log" ))
221+ # run_flye reads chopper_long_reads.fastq.gz from its outdir
222+ with tempfile .TemporaryDirectory () as tmp :
223+ shutil .copy (
224+ os .path .join (test_data , "chopper_long_reads.fastq.gz" ),
225+ os .path .join (tmp , "chopper_long_reads.fastq.gz" ),
226+ )
227+ run_flye (tmp , 8 , raw_flag = False , pacbio_model = "nothing" , logdir = logdir )
238228 self .assertEqual (expected_return , True )
239229
240230 def test_raven (self ):
241231 expected_return = True
242- # C11 sim reads
243- run_raven (test_data , 1 , logdir = logdir )
244- remove_file (os .path .join (test_data , "assembly.fasta" ))
245- remove_file (os .path .join (test_data , "assembly_graph.gfa" ))
246- remove_file (os .path .join (test_data , "params.json" ))
247- remove_file ("raven.cereal" )
232+ with tempfile .TemporaryDirectory () as tmp :
233+ shutil .copy (
234+ os .path .join (test_data , "chopper_long_reads.fastq.gz" ),
235+ os .path .join (tmp , "chopper_long_reads.fastq.gz" ),
236+ )
237+ run_raven (tmp , 1 , logdir = logdir )
238+ remove_file ("raven.cereal" ) # raven writes this to the CWD
248239 self .assertEqual (expected_return , True )
249240
250241 def test_unicycler_good (self ):
@@ -253,19 +244,19 @@ def test_unicycler_good(self):
253244 short_one = Path (f"{ test_data } /short_read_concat_good_R1.fastq" )
254245 short_two = Path (f"{ test_data } /short_read_concat_good_R2.fastq" )
255246 longreads = Path (f"{ test_data } /plasmid_long_good.fastq" )
256- unicycler_output_dir = Path (f"{ test_data } /unicycler_output" )
257247 threads = 1
258- run_unicycler (
259- threads ,
260- logdir ,
261- short_one ,
262- short_two ,
263- longreads ,
264- unicycler_output_dir ,
265- unicycler_options = None ,
266- spades_options = None ,
267- )
268- remove_directory (unicycler_output_dir )
248+ with tempfile .TemporaryDirectory () as tmp :
249+ unicycler_output_dir = Path (tmp ) / "unicycler_output"
250+ run_unicycler (
251+ threads ,
252+ logdir ,
253+ short_one ,
254+ short_two ,
255+ longreads ,
256+ unicycler_output_dir ,
257+ unicycler_options = None ,
258+ spades_options = None ,
259+ )
269260 self .assertEqual (expected_return , True )
270261
271262 def test_unicycler_bad (self ):
@@ -274,19 +265,19 @@ def test_unicycler_bad(self):
274265 short_one = Path (f"{ test_data } /C11_subsetsim_R1.fastq" )
275266 short_two = Path (f"{ test_data } /C11_subsetsim_R2.fastq" )
276267 longreads = Path (f"{ test_data } /plasmid_long_good.fastq" )
277- unicycler_output_dir = Path (f"{ test_data } /unicycler_output_bad" )
278268 threads = 1
279- run_unicycler (
280- threads ,
281- logdir ,
282- short_one ,
283- short_two ,
284- longreads ,
285- unicycler_output_dir ,
286- unicycler_options = None ,
287- spades_options = None ,
288- )
289- remove_directory (unicycler_output_dir )
269+ with tempfile .TemporaryDirectory () as tmp :
270+ unicycler_output_dir = Path (tmp ) / "unicycler_output_bad"
271+ run_unicycler (
272+ threads ,
273+ logdir ,
274+ short_one ,
275+ short_two ,
276+ longreads ,
277+ unicycler_output_dir ,
278+ unicycler_options = None ,
279+ spades_options = None ,
280+ )
290281 self .assertEqual (expected_return , True )
291282
292283
0 commit comments