@@ -27,6 +27,25 @@ def test_motif_discovery():
2727 # Check that the CLI tool executed successfully
2828 assert result .returncode == 0 , "CLI tool did not exit successfully"
2929
30+ def test_motif_discovery_gzip ():
31+ """
32+ """
33+ outdir = "tests/cli_test_motif_discovery"
34+
35+ cmd = [
36+ "nanomotif" , "motif_discovery" ,
37+ "-t" , "1" ,
38+ "nanomotif/datasets/geobacillus-plasmids.assembly.fasta" ,
39+ "nanomotif/datasets/geobacillus-plasmids.pileup.bed.gz" ,
40+ "-c" , "nanomotif/datasets/geobacillus-contig-bin.tsv" ,
41+ "--out" , outdir
42+ ]
43+
44+ result = subprocess .run (cmd )
45+ shutil .rmtree (outdir )
46+
47+ # Check that the CLI tool executed successfully
48+ assert result .returncode == 0 , "CLI tool did not exit successfully"
3049
3150def test_check_installation ():
3251 """
@@ -152,6 +171,104 @@ def test_detect_contamination():
152171 else :
153172 print (f"File not found: { outfile } " )
154173
174+
175+
176+ def test_detect_contamination_bgzip ():
177+ """
178+ """
179+ import polars as pl
180+ from Bio import SeqIO
181+ from Bio .Seq import Seq
182+ from Bio .SeqRecord import SeqRecord
183+ from epymetheus .epymetheus import bgzf_pileup
184+
185+ infile = "nanomotif/datasets/geobacillus-plasmids.assembly.fasta"
186+ outfile_a = "nanomotif/datasets/geobacillus-plasmids.assembly.duplicated.fasta"
187+
188+ # Read all records from the original file
189+ records = list (SeqIO .parse (infile , "fasta" ))
190+
191+ # We'll store our new records here
192+ new_records = []
193+
194+ # For each record, if it's contig_2 or contig_3, create 5 duplicates
195+ # with IDs appended by _1.._5. Otherwise, keep it as is.
196+ for record in records :
197+ if record .id in ["contig_2" , "contig_3" ]:
198+ for i in range (1 , 6 ):
199+ new_id = f"{ record .id } _{ i } "
200+ # Create a new SeqRecord with the same sequence
201+ new_record = SeqRecord (
202+ record .seq ,
203+ id = new_id ,
204+ description = ""
205+ )
206+ new_records .append (new_record )
207+ else :
208+ # For non-contig_2/3, simply keep the original record
209+ new_records .append (record )
210+
211+ # Write out the new FASTA file
212+ SeqIO .write (new_records , outfile_a , "fasta" )
213+
214+ print (f"Duplicated contigs written to: { outfile_a } " )
215+
216+
217+ infile = "nanomotif/datasets/geobacillus-plasmids.pileup.bed"
218+ outfile_p = "nanomotif/datasets/geobacillus-plasmids.pileup.duplicated.bed"
219+
220+ p = pl .read_csv (infile , has_header = False , separator = "\t " )
221+
222+ p_dup = pl .DataFrame ()
223+ for contig in ["contig_3" , "contig_2" ]:
224+ p_tmp = p .filter (pl .col ("column_1" ) == contig )
225+
226+ for i in range (1 , 6 ):
227+ p_i = p_tmp .with_columns (
228+ (pl .col ("column_1" ) + f"_{ i } " ).alias ("column_1" )
229+ )
230+
231+ p_dup = pl .concat ([p_dup , p_i ])
232+
233+ p_dup .write_csv (outfile_p , separator = "\t " , include_header = False )
234+ bgzf_pileup (outfile_p )
235+
236+
237+
238+ outfile_b = "nanomotif/datasets/geobacillus-plasmids.contig_bin.tmp.tsv"
239+ contig_bin = pl .DataFrame ({
240+ "contig" : [f"contig_{ i } _{ j } " for i in [2 , 3 ] for j in range (1 , 6 )],
241+ "bin" : ["bin_1" ] * 10 ,
242+ })
243+
244+ contig_bin .write_csv (outfile_b , separator = "\t " , include_header = False )
245+
246+ outdir = "tests/cli_test_detect_contamination"
247+
248+ cmd = [
249+ "nanomotif" , "detect_contamination" ,
250+ "-t" , "1" ,
251+ "--force" ,
252+ "--assembly" , outfile_a ,
253+ "--pileup" , outfile_p + ".gz" ,
254+ "--contig_bins" , outfile_b ,
255+ "--bin_motifs" , "nanomotif/datasets/geobacillus-plasmids.bin-motifs.tsv" ,
256+ "--out" , outdir
257+ ]
258+ result = subprocess .run (cmd )
259+
260+ # Check that the CLI tool executed successfully
261+ shutil .rmtree (outdir )
262+ assert result .returncode == 0 , "CLI tool did not exit successfully"
263+ for outfile in [outfile_a , outfile_p , outfile_b , outfile_p + ".gz" , outfile_p + "..gz.tbi" ]:
264+ if os .path .exists (outfile ):
265+ os .remove (outfile )
266+ print (f"Deleted: { outfile } " )
267+ else :
268+ print (f"File not found: { outfile } " )
269+
270+
271+
155272def test_detect_contamination_weighted_mean ():
156273 """
157274 """
0 commit comments