Repository: AroneyS/binchicken (language: python)

Issue:
Deduplicate contig names in incoming genomes for unmapping



Discussion / hints:

Fix patch:
diff --git a/ibis/workflow/coassemble.smk b/ibis/workflow/coassemble.smk
index 5f99a432..ebe93695 100644
--- a/ibis/workflow/coassemble.smk
+++ b/ibis/workflow/coassemble.smk
@@ -402,10 +402,10 @@ rule sra_qc:
     output:
         out1 = output_dir + "/sra_qc/{sra}_1.fastq.gz",
         out2 = output_dir + "/sra_qc/{sra}_2.fastq.gz",
+        stats = output_dir + "/sra_qc/{sra}_qc.stats",
     params:
         in1 = output_dir + "/sra/{sra}_1.fastq.gz",
         in2 = output_dir + "/sra/{sra}_2.fastq.gz",
-        stats = output_dir + "/sra_qc/{sra}_qc.stats",
     threads: 16
     resources:
         mem_mb=125*1000,
@@ -422,7 +422,7 @@ rule sra_qc:
         "ktrim=r k=23 mink=11 hdist=1 "
         "qtrim=r trimq=10 "
         "minlen=30 "
-        "stats={params.stats} statscolumns=3 "
+        "stats={output.stats} statscolumns=3 "
         "t={threads} "
         "&> {log} "
 
diff --git a/ibis/workflow/scripts/collect_reference_bins.py b/ibis/workflow/scripts/collect_reference_bins.py
index 3088a8bd..d5ef55dc 100755
--- a/ibis/workflow/scripts/collect_reference_bins.py
+++ b/ibis/workflow/scripts/collect_reference_bins.py
@@ -98,6 +98,11 @@ def pipeline(appraise_binned, appraise_unbinned, sample, MIN_APPRAISED=0.1, TRIM
         cmd = f"touch {output_path}"
         extern.run(cmd)
     else:
-        for bin in reference_bins:
-            cmd = f"cat {genomes[bin]} >> {output_path}"
-            extern.run(cmd)
\ No newline at end of file
+        with open(str(output_path), "w") as outfile:
+            for bin in reference_bins:
+                bin_name = os.path.splitext(os.path.basename(genomes[bin]))[0]
+                with open(genomes[bin], "r") as infile:
+                    for line in infile:
+                        if line.startswith(">"):
+                            line = f">{bin_name}~{line[1:]}"
+                        outfile.write(line)


Test patch:
diff --git a/test/data/GB_GCA_013286235.3.fna b/test/data/GB_GCA_013286235.3.fna
new file mode 100644
index 00000000..27f2c6ca
--- /dev/null
+++ b/test/data/GB_GCA_013286235.3.fna
@@ -0,0 +1,4 @@
+>JABDGE010000038.1_13
+ATGAGAAAGTCGAAACCAAAAAAGAGAATTATCCTTCCTGATCCAAAGTTCAACGACACTTTGGTAACAA
+>JABDGE010000038.1_14
+ATGCCTACTATTCAGCAATTAGTTAGAAAAGGTAGAGTAGCTCTGGTTGACAAGAGTAAGTCGCCAGCGT
diff --git a/test/data/mock_coassemble/appraise/binned_three.otu_table.tsv b/test/data/mock_coassemble/appraise/binned_three.otu_table.tsv
new file mode 100644
index 00000000..14e235b4
--- /dev/null
+++ b/test/data/mock_coassemble/appraise/binned_three.otu_table.tsv
@@ -0,0 +1,7 @@
+gene	sample	sequence	num_hits	coverage	taxonomy	found_in
+S3.7.ribosomal_protein_S7	sample_1.1	TTCCAGGTGCCTACTGAAGTTCGTCCCGAGCGTAAAATTGCATTGGGTATGAAATGGCTG	2	3.28	Root; d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Sphingobacteriales; f__Sphingobacteriaceae; g__Mucilaginibacter; s__Mucilaginibacter sp013286235	GB_GCA_013286235.1_protein
+S3.7.ribosomal_protein_S7	sample_2.1	TTCCAGGTGCCTACTGAAGTTCGTCCCGAGCGTAAAATTGCATTGGGTATGAAATGGCTG	1	1.64	Root; d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Sphingobacteriales; f__Sphingobacteriaceae; g__Mucilaginibacter; s__Mucilaginibacter sp013286235	GB_GCA_013286235.1_protein
+S3.7.ribosomal_protein_S7	sample_3.1	TTCCAGGTGCCTACTGAAGTTCGTCCCGAGCGTAAAATTGCATTGGGTATGAAATGGCTG	1	1.64	Root; d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Sphingobacteriales; f__Sphingobacteriaceae; g__Mucilaginibacter; s__Mucilaginibacter sp013286235	GB_GCA_013286235.1_protein
+S3.7.ribosomal_protein_S7	sample_1.1	TTCCAGGTGCCTACTGAAGTTCGTCCCGAGCGTAAAATTGCATTGGGTATGAAATGGCTG	2	3.28	Root; d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Sphingobacteriales; f__Sphingobacteriaceae; g__Mucilaginibacter; s__Mucilaginibacter sp013286235	GB_GCA_013286235.3_protein
+S3.7.ribosomal_protein_S7	sample_2.1	TTCCAGGTGCCTACTGAAGTTCGTCCCGAGCGTAAAATTGCATTGGGTATGAAATGGCTG	1	1.64	Root; d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Sphingobacteriales; f__Sphingobacteriaceae; g__Mucilaginibacter; s__Mucilaginibacter sp013286235	GB_GCA_013286235.3_protein
+S3.7.ribosomal_protein_S7	sample_3.1	TTCCAGGTGCCTACTGAAGTTCGTCCCGAGCGTAAAATTGCATTGGGTATGAAATGGCTG	1	1.64	Root; d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Sphingobacteriales; f__Sphingobacteriaceae; g__Mucilaginibacter; s__Mucilaginibacter sp013286235	GB_GCA_013286235.3_protein
diff --git a/test/test_update.py b/test/test_update.py
index 9c791040..c8307444 100644
--- a/test/test_update.py
+++ b/test/test_update.py
@@ -25,10 +25,16 @@
     os.path.join(path_to_data, "GB_GCA_013286235.1.fna"),
     os.path.join(path_to_data, "GB_GCA_013286235.2.fna"),
     ])
+THREE_GENOMES = " ".join([
+    os.path.join(path_to_data, "GB_GCA_013286235.1.fna"),
+    os.path.join(path_to_data, "GB_GCA_013286235.2.fna"),
+    os.path.join(path_to_data, "GB_GCA_013286235.3.fna"),
+    ])
 
 MOCK_COASSEMBLE = os.path.join(path_to_data, "mock_coassemble")
 MOCK_UNBINNED = os.path.join(MOCK_COASSEMBLE, "appraise", "unbinned.otu_table.tsv")
 MOCK_BINNED = os.path.join(MOCK_COASSEMBLE, "appraise", "binned.otu_table.tsv")
+MOCK_BINNED_THREE = os.path.join(MOCK_COASSEMBLE, "appraise", "binned_three.otu_table.tsv")
 MOCK_TARGETS = os.path.join(MOCK_COASSEMBLE, "target", "targets.tsv")
 MOCK_ELUSIVE_EDGES = os.path.join(MOCK_COASSEMBLE, "target", "elusive_edges.tsv")
 MOCK_ELUSIVE_CLUSTERS = os.path.join(MOCK_COASSEMBLE, "target", "elusive_clusters.tsv")
@@ -490,6 +496,41 @@ def test_update_specific_coassembly(self):
             self.assertFalse(os.path.exists(os.path.join("test", "coassemble", "mapping", "sample_4_unmapped.1.fq.gz")))
             self.assertFalse(os.path.exists(os.path.join("test", "coassemble", "mapping", "sample_4_unmapped.2.fq.gz")))
 
+    def test_update_duplicate_contig_names(self):
+        with in_tempdir():
+            cmd = (
+                f"ibis update "
+                f"--assemble-unmapped "
+                f"--forward {SAMPLE_READS_FORWARD} "
+                f"--reverse {SAMPLE_READS_REVERSE} "
+                f"--genomes {THREE_GENOMES} "
+                f"--coassemble-unbinned {MOCK_UNBINNED} "
+                f"--coassemble-binned {MOCK_BINNED_THREE} "
+                f"--coassemble-targets {MOCK_TARGETS} "
+                f"--coassemble-elusive-edges {MOCK_ELUSIVE_EDGES} "
+                f"--coassemble-elusive-clusters {MOCK_ELUSIVE_CLUSTERS} "
+                f"--coassemble-summary {MOCK_SUMMARY} "
+                f"--output test "
+                f"--conda-prefix {path_to_conda} "
+                f"--snakemake-args \" --notemp\" "
+            )
+            extern.run(cmd)
+            # try:
+            #     import subprocess
+            #     subprocess.run(cmd, shell=True, check=True)
+            # except:
+            #     pass
+            # import pdb; pdb.set_trace()
+
+            bins_reference_path = os.path.join("test", "coassemble", "mapping", "sample_1_reference.fna")
+            self.assertTrue(os.path.exists(bins_reference_path))
+            with open(bins_reference_path) as f:
+                file = f.read()
+                self.assertTrue(">GB_GCA_013286235.1~JABDGE010000038.1_13" in file)
+                self.assertTrue(">GB_GCA_013286235.1~JABDGE010000038.1_14" in file)
+                self.assertTrue(">GB_GCA_013286235.3~JABDGE010000038.1_13" in file)
+                self.assertTrue(">GB_GCA_013286235.3~JABDGE010000038.1_14" in file)
+
 
 if __name__ == '__main__':
     unittest.main()