Skip to content

Commit

Permalink
added test for pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
VinzentRisch committed Mar 26, 2024
1 parent df5530e commit f24d553
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 6 deletions.
16 changes: 11 additions & 5 deletions q2_amr/card/reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,41 @@ def annotate_reads_card(
include_other_models=False,
num_partitions=None,
):
# Define all actions used by the pipeline
if reads.type <= SampleData[SequencesWithQuality]:
partition_method = ctx.get_action("demux", "partition_samples_single")
elif reads.type <= SampleData[PairedEndSequencesWithQuality]:
partition_method = ctx.get_action("demux", "partition_samples_paired")

annotate = ctx.get_action("amr", "_annotate_reads_card")

collate_allele_annotations = ctx.get_action(
"amr", "collate_reads_allele_annotations"
)
collate_gene_annotations = ctx.get_action("amr", "collate_reads_gene_annotations")
merge_tables = ctx.get_action("feature-table", "merge")

if reads.type <= SampleData[SequencesWithQuality]:
partition_method = ctx.get_action("demux", "partition_samples_single")
elif reads.type <= SampleData[PairedEndSequencesWithQuality]:
partition_method = ctx.get_action("demux", "partition_samples_paired")

# Partition the reads
(partitioned_seqs,) = partition_method(reads, num_partitions)

allele_annotations = []
gene_annotations = []
allele_tables = []
gene_tables = []

# Run _annotate_reads_card for every partition
for read in partitioned_seqs.values():
(allele_annotation, gene_annotation, allele_table, gene_table) = annotate(
read, card_db, aligner, threads, include_wildcard, include_other_models
)

# Append output artifacts to lists
allele_annotations.append(allele_annotation)
gene_annotations.append(gene_annotation)
allele_tables.append(allele_table)
gene_tables.append(gene_table)

# Collate annotation and feature table artifacts
(collated_allele_annotations,) = collate_allele_annotations(allele_annotations)
(collated_gene_annotations,) = collate_gene_annotations(gene_annotations)
(collated_allele_tables,) = merge_tables(allele_tables)
Expand Down
2 changes: 2 additions & 0 deletions q2_amr/card/tests/data/reads_paired/MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sample-id,filename,direction
sample1,sample1_00_L001_R1_001.fastq.gz,forward
1 change: 1 addition & 0 deletions q2_amr/card/tests/data/reads_paired/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
phred-offset: 33
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions q2_amr/card/tests/data/reads_single/MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sample-id,filename,direction
sample1,sample1_00_L001_R1_001.fastq.gz,forward
1 change: 1 addition & 0 deletions q2_amr/card/tests/data/reads_single/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
phred-offset: 33
Binary file not shown.
50 changes: 49 additions & 1 deletion q2_amr/card/tests/test_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
SingleLanePerSamplePairedEndFastqDirFmt,
SingleLanePerSampleSingleEndFastqDirFmt,
)
from qiime2 import Artifact
from qiime2.plugin.testing import TestPluginBase

from q2_amr.card.reads import _annotate_reads_card, run_rgi_bwt
from q2_amr.card.reads import _annotate_reads_card, annotate_reads_card, run_rgi_bwt
from q2_amr.types import (
CARDAlleleAnnotationDirectoryFormat,
CARDDatabaseDirectoryFormat,
Expand Down Expand Up @@ -199,3 +200,50 @@ def test_exception_raised(self):
mock_run_command.side_effect = subprocess.CalledProcessError(1, "cmd")
run_rgi_bwt()
self.assertEqual(str(cm.exception), expected_message)

def test_kmer_query_reads_card_paired(self):
reads_path = self.get_data_path("reads_paired")
reads = SingleLanePerSamplePairedEndFastqDirFmt(reads_path, "r")
reads_artifact = Artifact.import_data(
"SampleData[PairedEndSequencesWithQuality]", reads
)
self._test_kmer_query_reads_card(reads_artifact)

def test_kmer_query_reads_card_single(self):
reads_path = self.get_data_path("reads_single")
reads = SingleLanePerSampleSingleEndFastqDirFmt(reads_path, "r")
reads_artifact = Artifact.import_data("SampleData[SequencesWithQuality]", reads)
self._test_kmer_query_reads_card(reads_artifact)

def _test_kmer_query_reads_card(self, reads):
# Mock the get_action method to return MagicMock objects
mock_ctx = MagicMock()
mock_ctx.get_action.side_effect = [
MagicMock(
return_value=({"1": "artifact_reads_1", "2": "artifact_reads_2"},)
),
MagicMock(
return_value=(
"artifact_annotation_allele",
"artifact_annotation_gene",
"artifact_feature_table_allele",
"artifact_feature_table_gene",
)
),
MagicMock(return_value=("artifact_allele_annotation_collated",)),
MagicMock(return_value=("artifact_gene_annotation_collated",)),
MagicMock(return_value=("artifact_feature_table_merged",)),
]

# Call function with mocked ctx
result = annotate_reads_card(ctx=mock_ctx, reads=reads, card_db=None)

self.assertEqual(
result,
(
"artifact_allele_annotation_collated",
"artifact_gene_annotation_collated",
"artifact_feature_table_merged",
"artifact_feature_table_merged",
),
)

0 comments on commit f24d553

Please sign in to comment.