Skip to content

Commit

Permalink
TST: Update tests to check bins
Browse files Browse the repository at this point in the history
  • Loading branch information
Vini2 committed May 15, 2023
1 parent d61672a commit fc3ea57
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
Empty file modified tests/data/5G_metaspades/coverm_mean_coverage.tsv
100755 → 100644
Empty file.
74 changes: 59 additions & 15 deletions tests/test_metacoag.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import subprocess
from pathlib import Path

Expand All @@ -9,24 +10,13 @@
__version__ = "1.1.1"
__maintainer__ = "Vijini Mallawaarachchi"
__email__ = "vijini.mallawaarachchi@anu.edu.au"
__status__ = "Development"
__status__ = "Stable Release"


TEST_ROOTDIR = Path(__file__).parent
EXEC_ROOTDIR = Path(__file__).parent.parent


@pytest.fixture(scope="session")
def tmp_dir(tmpdir_factory):
return tmpdir_factory.mktemp("tmp")


@pytest.fixture(autouse=True)
def workingdir(tmp_dir, monkeypatch):
"""set the working directory for all tests"""
monkeypatch.chdir(tmp_dir)


def exec_command(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
"""executes shell command and returns stdout if completes exit code 0
Parameters
Expand All @@ -44,8 +34,28 @@ def exec_command(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
return out.decode("utf8") if out is not None else None


def test_metacoag_spades_command(tmp_dir):
"""test metacoag on spades assembly"""
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="session")
def test_metacoag_spades_command(tmp_path_factory):
"""test metacoag on metaspades assembly"""

tmp_dir = tmp_path_factory.mktemp("tmp")

dir_name = TEST_ROOTDIR / "data" / "5G_metaspades"
graph = dir_name / "assembly_graph_with_scaffolds.gfa"
contigs = dir_name / "contigs.fasta"
Expand All @@ -54,12 +64,46 @@ def test_metacoag_spades_command(tmp_dir):
cmd = f"metacoag --assembler spades --graph {graph} --contigs {contigs} --paths {paths} --abundance {abundance} --output {tmp_dir}"
exec_command(cmd)

n_bins, seq_counts = get_files_and_seq_counts(tmp_dir / "bins")

return n_bins, seq_counts


def test_n_bins_metacoag_spades(test_metacoag_spades_command):
n_bins, seq_counts = test_metacoag_spades_command

def test_metacoag_megahit_command(tmp_dir):
# Assert number of bins
assert n_bins == 5

# Assert bin sizes
assert seq_counts == [10, 23, 48, 69, 78]


@pytest.fixture(scope="session")
def test_metacoag_megahit_command(tmp_path_factory):
"""test metacoag on megahit assembly"""

tmp_dir = tmp_path_factory.mktemp("tmp")

dir_name = TEST_ROOTDIR / "data" / "5G_MEGAHIT"
graph = dir_name / "final.gfa"
contigs = dir_name / "final.contigs.fa"
abundance = dir_name / "abundance.tsv"
cmd = f"metacoag --assembler megahit --graph {graph} --contigs {contigs} --abundance {abundance} --output {tmp_dir}"
exec_command(cmd)

n_bins, seq_counts = get_files_and_seq_counts(tmp_dir / "bins")

return n_bins, seq_counts


def test_n_bins_metacoag_megahit(test_metacoag_megahit_command):
n_bins, seq_counts = test_metacoag_megahit_command

# Assert number of bins
assert n_bins == 5

# Assert bin sizes
assert seq_counts == [36, 40, 46, 84, 127]


0 comments on commit fc3ea57

Please sign in to comment.