From 874b707f4b7e2f68a42a5656d7c4bf17a4d51640 Mon Sep 17 00:00:00 2001 From: Yapeng Lang Date: Sun, 15 Sep 2024 13:34:35 +1000 Subject: [PATCH 1/4] TST: added tests for invoking metacoag --- tests/test_cli_metacoag.py | 92 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/tests/test_cli_metacoag.py b/tests/test_cli_metacoag.py index 5cbab5a..6a68b01 100644 --- a/tests/test_cli_metacoag.py +++ b/tests/test_cli_metacoag.py @@ -1,18 +1,19 @@ +# import os import pathlib -import pytest +import pytest from click.testing import CliRunner from gbintk.cli import metacoag - __author__ = "Vijini Mallawaarachchi" __credits__ = ["Vijini Mallawaarachchi"] DATADIR = pathlib.Path(__file__).parent / "data" -@pytest.fixture(scope="session") + +@pytest.fixture(scope="function") def tmp_dir(tmpdir_factory): return tmpdir_factory.mktemp("tmp") @@ -29,7 +30,24 @@ def runner(): return CliRunner() -def test_metacoag_run(runner, tmp_dir): +# def get_files_and_seq_counts(output_path): +# output_files = os.listdir(output_path) +# seq_counts = [] +# for file in output_files: +# seq_count = 0 +# with open(f"{output_path}/{file}", "r") as myfile: +# for line in myfile: +# if line.strip().startswith(">"): +# seq_count += 1 +# seq_counts.append(seq_count) + +# seq_counts.sort() + +# return len(output_files), seq_counts + + +# @pytest.fixture(scope="function") +def test_metacoag_spades_run(runner, tmp_dir): outpath = tmp_dir graph = DATADIR / "5G_metaSPAdes" / "assembly_graph_with_scaffolds.gfa" contigs = DATADIR / "5G_metaSPAdes" / "contigs.fasta" @@ -37,4 +55,70 @@ def test_metacoag_run(runner, tmp_dir): abundance = DATADIR / "5G_metaSPAdes" / "coverm_mean_coverage.tsv" args = f"--assembler spades --graph {graph} --contigs {contigs} --paths {paths} --abundance {abundance} --output {outpath}".split() r = runner.invoke(metacoag, args, catch_exceptions=False) + + assert r.exit_code == 0, r.output + + # n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + # return n_bins, seq_counts + + +# @pytest.fixture(scope="function") +def test_metacoag_megahit_run(runner, tmp_dir): + outpath = tmp_dir + graph = DATADIR / "5G_MEGAHIT" / "final.gfa" + contigs = DATADIR / "5G_MEGAHIT" / "final.contigs.fa" + abundance = DATADIR / "5G_MEGAHIT" / "abundance.tsv" + args = f"--assembler megahit --graph {graph} --contigs {contigs} --abundance {abundance} --output {outpath}".split() + r = runner.invoke(metacoag, args, catch_exceptions=False) + + assert r.exit_code == 0, r.output # Check if the command ran successfully + + # n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + # return n_bins, seq_counts + + +@pytest.fixture(scope="function") +def test_metacoag_flye_run(tmp_dir, runner): + outpath = tmp_dir + graph = DATADIR / "1Y3B_Flye" / "assembly_graph.gfa" + contigs = DATADIR / "1Y3B_Flye" / "assembly.fasta" + paths = DATADIR / "1Y3B_Flye" / "assembly_info.txt" + abundance = DATADIR / "1Y3B_Flye" / "abundance.tsv" + args = f"--assembler flye --graph {graph} --contigs {contigs} --paths {paths} --abundance {abundance} --output {outpath}".split() + r = runner.invoke(metacoag, args, catch_exceptions=False) + assert r.exit_code == 0, r.output + + n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + return n_bins, seq_counts + + +def test_n_bins_metacoag_spades(test_metacoag_spades_run): + n_bins, seq_counts = test_metacoag_spades_run + + # Assert number of bins + assert n_bins == 5 + + # Assert bin sizes + assert seq_counts == [10, 23, 48, 69, 78] + + +def test_n_bins_metacoag_megahit(test_metacoag_megahit_run): + n_bins, seq_counts = test_metacoag_megahit_run + + # Assert number of bins + assert n_bins == 5 + + # Assert bin sizes + assert seq_counts == [36, 40, 46, 84, 127] + + +def test_n_bins_metacoag_flye(test_metacoag_flye_run): + outpath = test_metacoag_flye_run + n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + + # Assert number of bins + assert n_bins == 3 + + # Assert bin sizes + assert seq_counts == [1, 1, 1] From 5b68881e8bf02cb1425a3d48412aa1a27ebc218c Mon Sep 17 00:00:00 2001 From: Yapeng Lang Date: Sun, 15 Sep 2024 13:49:48 +1000 Subject: [PATCH 2/4] TST: ignored the runtime warnings for unreached vertices --- tests/test_cli_metacoag.py | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/tests/test_cli_metacoag.py b/tests/test_cli_metacoag.py index 6a68b01..3ca1705 100644 --- a/tests/test_cli_metacoag.py +++ b/tests/test_cli_metacoag.py @@ -1,4 +1,4 @@ -# import os +import os import pathlib import pytest @@ -30,23 +30,23 @@ def runner(): return CliRunner() -# def get_files_and_seq_counts(output_path): -# output_files = os.listdir(output_path) -# seq_counts = [] -# for file in output_files: -# seq_count = 0 -# with open(f"{output_path}/{file}", "r") as myfile: -# for line in myfile: -# if line.strip().startswith(">"): -# seq_count += 1 -# seq_counts.append(seq_count) +def get_files_and_seq_counts(output_path): + output_files = os.listdir(output_path) + seq_counts = [] + for file in output_files: + seq_count = 0 + with open(f"{output_path}/{file}", "r") as myfile: + for line in myfile: + if line.strip().startswith(">"): + seq_count += 1 + seq_counts.append(seq_count) -# seq_counts.sort() + seq_counts.sort() -# return len(output_files), seq_counts + return len(output_files), seq_counts -# @pytest.fixture(scope="function") +@pytest.fixture(scope="function") def test_metacoag_spades_run(runner, tmp_dir): outpath = tmp_dir graph = DATADIR / "5G_metaSPAdes" / "assembly_graph_with_scaffolds.gfa" @@ -56,13 +56,13 @@ def test_metacoag_spades_run(runner, tmp_dir): args = f"--assembler spades --graph {graph} --contigs {contigs} --paths {paths} --abundance {abundance} --output {outpath}".split() r = runner.invoke(metacoag, args, catch_exceptions=False) - assert r.exit_code == 0, r.output + assert r.exit_code == 0, r.output # Check if the command ran successfully - # n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") - # return n_bins, seq_counts + n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + return n_bins, seq_counts -# @pytest.fixture(scope="function") +@pytest.fixture(scope="function") def test_metacoag_megahit_run(runner, tmp_dir): outpath = tmp_dir graph = DATADIR / "5G_MEGAHIT" / "final.gfa" @@ -73,8 +73,8 @@ def test_metacoag_megahit_run(runner, tmp_dir): assert r.exit_code == 0, r.output # Check if the command ran successfully - # n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") - # return n_bins, seq_counts + n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + return n_bins, seq_counts @pytest.fixture(scope="function") @@ -103,6 +103,7 @@ def test_n_bins_metacoag_spades(test_metacoag_spades_run): assert seq_counts == [10, 23, 48, 69, 78] +@pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_n_bins_metacoag_megahit(test_metacoag_megahit_run): n_bins, seq_counts = test_metacoag_megahit_run @@ -113,9 +114,9 @@ def test_n_bins_metacoag_megahit(test_metacoag_megahit_run): assert seq_counts == [36, 40, 46, 84, 127] +@pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_n_bins_metacoag_flye(test_metacoag_flye_run): - outpath = test_metacoag_flye_run - n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") + n_bins, seq_counts = test_metacoag_flye_run # Assert number of bins assert n_bins == 3 From 22d5aae17d3fe69dc4dc71a17f2f21484c6d316f Mon Sep 17 00:00:00 2001 From: Yapeng Lang Date: Sun, 15 Sep 2024 14:32:27 +1000 Subject: [PATCH 3/4] TST: added tests for where wrong inputs are given --- tests/test_cli_metacoag.py | 65 +++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/tests/test_cli_metacoag.py b/tests/test_cli_metacoag.py index 3ca1705..a1058c5 100644 --- a/tests/test_cli_metacoag.py +++ b/tests/test_cli_metacoag.py @@ -71,7 +71,7 @@ def test_metacoag_megahit_run(runner, tmp_dir): args = f"--assembler megahit --graph {graph} --contigs {contigs} --abundance {abundance} --output {outpath}".split() r = runner.invoke(metacoag, args, catch_exceptions=False) - assert r.exit_code == 0, r.output # Check if the command ran successfully + assert r.exit_code == 0, r.output n_bins, seq_counts = get_files_and_seq_counts(outpath / "bins") return n_bins, seq_counts @@ -123,3 +123,66 @@ def test_n_bins_metacoag_flye(test_metacoag_flye_run): # Assert bin sizes assert seq_counts == [1, 1, 1] + + +@pytest.mark.parametrize( + "graph, contigs, paths, abundance", + [ + ( + DATADIR / "invlid_path" / "assembly_graph_with_scaffolds.gfa", + DATADIR / "5G_metaSPAdes" / "contigs.fasta", + DATADIR / "5G_metaSPAdes" / "contigs.paths", + DATADIR / "5G_metaSPAdes" / "coverm_mean_coverage.tsv", + ), + ], +) +def test_metacoag_spades_invalid_run(runner, tmp_dir, graph, contigs, paths, abundance): + outpath = tmp_dir + args = f"--assembler spades --graph {graph} --contigs {contigs} --paths {paths} --abundance {abundance} --output {outpath}".split() + r = runner.invoke(metacoag, args, catch_exceptions=False) + assert r.exit_code != 0 + assert "Error" in r.output and "Path" in r.output # Check for error messages + + +@pytest.mark.parametrize( + "graph, contigs, abundance", + [ + ( + DATADIR / "invalid_path" / "final.gfa", + DATADIR / "5G_MEGAHIT" / "final.contigs.fa", + DATADIR / "5G_MEGAHIT" / "abundance.tsv", + ), + ], +) +def test_metacoag_megahit_invalid_run( + runner, tmp_path_factory, graph, contigs, abundance +): + outpath = tmp_path_factory.mktemp("tmp") + args = f"--assembler megahit --graph {graph} --contigs {contigs} --abundance {abundance} --output {outpath}".split() + r = runner.invoke(metacoag, args, catch_exceptions=True) + + assert r.exit_code != 0 + assert "Error" in r.output and "Path" in r.output + + +@pytest.mark.parametrize( + "graph, contigs, paths, abundance", + [ + ( + DATADIR / "invalid_path" / "assembly_graph.gfa", + DATADIR / "1Y3B_Flye" / "assembly.fasta", + DATADIR / "1Y3B_Flye" / "assembly_info.txt", + DATADIR / "1Y3B_Flye" / "abundance.tsv", + ), + ], +) +def test_metacoag_flye_invalid_run( + runner, tmp_path_factory, graph, contigs, paths, abundance +): + outpath = tmp_path_factory.mktemp("tmp") + args = f"--assembler flye --graph {graph} --contigs {contigs} --paths {paths} --abundance {abundance} --output {outpath}".split() + r = runner.invoke(metacoag, args, catch_exceptions=True) + + assert r.exit_code != 0 + assert "Error" in r.output and "Path" in r.output + print(r.output) From 4a93697246725e3f352e95a902ea39b157047dd1 Mon Sep 17 00:00:00 2001 From: Yapeng Lang Date: Sun, 15 Sep 2024 14:46:58 +1000 Subject: [PATCH 4/4] MAINT: cleaned test function --- tests/test_cli_metacoag.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_cli_metacoag.py b/tests/test_cli_metacoag.py index a1058c5..ffe74dd 100644 --- a/tests/test_cli_metacoag.py +++ b/tests/test_cli_metacoag.py @@ -185,4 +185,3 @@ def test_metacoag_flye_invalid_run( assert r.exit_code != 0 assert "Error" in r.output and "Path" in r.output - print(r.output)