66"""
77
88# import
9+ import shutil
10+ import tempfile
911import unittest
1012from pathlib import Path
1113
1214import pytest
1315
14- from src .plassembler .utils .cleanup import remove_file
1516from src .plassembler .utils .plass_class import Assembly , Plass
1617
1718# import functions
@@ -65,11 +66,12 @@ def test_check_get_depth(self):
6566 plass = Plass ()
6667 pacbio_model = "nothing"
6768 threads = 1
68- # set to the depth dir for intermediate files
69- plass .outdir = plass_class_depth_dir
70- plass .get_depth (logdir , pacbio_model , threads )
71- remove_file (Path (f"{ plass_class_depth_dir } /combined_short.sam" ))
72- remove_file (Path (f"{ plass_class_depth_dir } /combined_sorted_short.bam" ))
69+ with tempfile .TemporaryDirectory () as tmp :
70+ # work in a copy so the committed fixtures are not mutated
71+ workdir = Path (tmp ) / "depth"
72+ shutil .copytree (plass_class_depth_dir , workdir )
73+ plass .outdir = workdir
74+ plass .get_depth (logdir , pacbio_model , threads )
7375 self .assertEqual (expected , True )
7476
7577 @pytest .mark .slow
@@ -78,14 +80,12 @@ def test_check_get_depth_long(self):
7880 plass = Plass ()
7981 pacbio_model = "nothing"
8082 threads = 1
81- plasmids_for_sketching = Path (
82- f"{ plass_class_depth_dir } /plasmids_for_sketching.fasta"
83- )
84- # set to the depth dir for intermediate files
85- plass .outdir = plass_class_depth_dir
86- plass .get_depth_long (logdir , pacbio_model , threads , plasmids_for_sketching )
87- remove_file (Path (f"{ plass_class_depth_dir } /combined_long.sam" ))
88- remove_file (Path (f"{ plass_class_depth_dir } /combined_sorted_long.bam" ))
83+ with tempfile .TemporaryDirectory () as tmp :
84+ workdir = Path (tmp ) / "depth"
85+ shutil .copytree (plass_class_depth_dir , workdir )
86+ plasmids_for_sketching = workdir / "plasmids_for_sketching.fasta"
87+ plass .outdir = workdir
88+ plass .get_depth_long (logdir , pacbio_model , threads , plasmids_for_sketching )
8989 self .assertEqual (expected , True )
9090
9191 def test_process_mash_tsv (self ):
@@ -100,18 +100,16 @@ def test_process_mash_tsv(self):
100100 def test_combine_tsv (self ):
101101 expected = True
102102 plass = Plass ()
103- plass .outdir = plass_class_depth_dir
104103 prefix = "plassembler"
105104 pacbio_model = "nothing"
106105 threads = 1
107- plass .get_depth (logdir , pacbio_model , threads )
108- plass .process_mash_tsv (plassembler_db_dir )
109- plass .combine_depth_mash_tsvs (prefix , depth_filter = 0.1 , skip_mash = False )
110- remove_file (Path (f"{ plass_class_depth_dir } /combined_long.sam" ))
111- remove_file (Path (f"{ plass_class_depth_dir } /combined_short.sam" ))
112- remove_file (Path (f"{ plass_class_depth_dir } /combined_sorted_long.bam" ))
113- remove_file (Path (f"{ plass_class_depth_dir } /combined_sorted_short.bam" ))
114- remove_file (Path (f"{ plass_class_depth_dir } /{ prefix } _summary.tsv" ))
106+ with tempfile .TemporaryDirectory () as tmp :
107+ workdir = Path (tmp ) / "depth"
108+ shutil .copytree (plass_class_depth_dir , workdir )
109+ plass .outdir = workdir
110+ plass .get_depth (logdir , pacbio_model , threads )
111+ plass .process_mash_tsv (plassembler_db_dir )
112+ plass .combine_depth_mash_tsvs (prefix , depth_filter = 0.1 , skip_mash = False )
115113 self .assertEqual (expected , True )
116114
117115
@@ -133,13 +131,11 @@ def test_check_get_depth(self):
133131 assembly = Assembly ()
134132 pacbio_model = "nothing"
135133 threads = 1
136- # set to the depth dir for intermediate files
137- assembly .outdir = assembly_depth_dir
138- assembly .get_depth (logdir , pacbio_model , threads )
139- remove_file (Path (f"{ assembly_depth_dir } /combined_long.sam" ))
140- remove_file (Path (f"{ assembly_depth_dir } /combined_short.sam" ))
141- remove_file (Path (f"{ assembly_depth_dir } /combined_sorted_long.bam" ))
142- remove_file (Path (f"{ assembly_depth_dir } /combined_sorted_short.bam" ))
134+ with tempfile .TemporaryDirectory () as tmp :
135+ workdir = Path (tmp ) / "depth"
136+ shutil .copytree (assembly_depth_dir , workdir )
137+ assembly .outdir = workdir
138+ assembly .get_depth (logdir , pacbio_model , threads )
143139 self .assertEqual (expected , True )
144140
145141 def test_process_mash_tsv (self ):
@@ -155,17 +151,15 @@ def test_process_mash_tsv(self):
155151 def test_combine_tsv (self ):
156152 expected = True
157153 assembly = Assembly ()
158- assembly .outdir = assembly_depth_dir
159154 prefix = "plassembler"
160155 pacbio_model = "nothing"
161156 threads = 1
162- plasmid_fasta = Path (f"{ assembly_depth_dir } /plasmids.fasta" )
163- assembly .get_depth (logdir , pacbio_model , threads )
164- assembly .process_mash_tsv (plassembler_db_dir , plasmid_fasta )
165- assembly .combine_depth_mash_tsvs (prefix , False )
166- remove_file (Path (f"{ assembly_depth_dir } /combined_long.sam" ))
167- remove_file (Path (f"{ assembly_depth_dir } /combined_short.bam" ))
168- remove_file (Path (f"{ assembly_depth_dir } /combined_sorted_long.bam" ))
169- remove_file (Path (f"{ assembly_depth_dir } /combined_sorted_short.bam" ))
170- remove_file (Path (f"{ assembly_depth_dir } /{ prefix } _summary.tsv" ))
157+ with tempfile .TemporaryDirectory () as tmp :
158+ workdir = Path (tmp ) / "depth"
159+ shutil .copytree (assembly_depth_dir , workdir )
160+ assembly .outdir = workdir
161+ plasmid_fasta = workdir / "plasmids.fasta"
162+ assembly .get_depth (logdir , pacbio_model , threads )
163+ assembly .process_mash_tsv (plassembler_db_dir , plasmid_fasta )
164+ assembly .combine_depth_mash_tsvs (prefix , False )
171165 self .assertEqual (expected , True )
0 commit comments