Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Added sample_dict to CARDAlleleAnnotationDirectoryFormat , CARDGeneAnnotationDirectoryFormat and CARDAnnotationDirectoryFormat #59

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions q2_amr/types/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import json
import os
import re
from copy import copy

Expand Down Expand Up @@ -277,6 +278,20 @@ def json_path_maker(self, sample_id, bin_id):
def txt_path_maker(self, sample_id, bin_id):
return f"{sample_id}/{bin_id}/amr_annotation.txt"

def sample_dict(self):
sample_dict = {}
for sample in self.path.iterdir():
for mag in sample.iterdir():
files = [
os.path.join(mag, file)
for file in [
"amr_annotation.json",
"amr_annotation.txt",
]
]
sample_dict[sample.name] = {mag.name: files}
VinzentRisch marked this conversation as resolved.
Show resolved Hide resolved
return sample_dict


class CARDAlleleAnnotationFormat(model.TextFileFormat):
def _validate(self, n_records=None):
Expand Down Expand Up @@ -421,6 +436,20 @@ def stats_path_maker(self, sample_id):
def bam_path_maker(self, sample_id):
return "%s/sorted.length_100.bam" % sample_id

def sample_dict(self):
sample_dict = {}
for sample in self.path.iterdir():
files = [
os.path.join(sample, file)
for file in [
"allele_mapping_data.txt",
"overall_mapping_stats.txt",
"sorted.length_100.bam",
]
]
VinzentRisch marked this conversation as resolved.
Show resolved Hide resolved
sample_dict[sample.name] = files
return sample_dict


class CARDGeneAnnotationDirectoryFormat(MultiDirValidationMixin, model.DirectoryFormat):
gene = model.FileCollection(
Expand All @@ -431,6 +460,13 @@ class CARDGeneAnnotationDirectoryFormat(MultiDirValidationMixin, model.Directory
def gene_path_maker(self, sample_id):
return "%s/gene_mapping_data.txt" % sample_id

def sample_dict(self):
sample_dict = {}
for sample in self.path.iterdir():
file = list(os.path.join(sample, "gene_mapping_data.txt"))
VinzentRisch marked this conversation as resolved.
Show resolved Hide resolved
sample_dict[sample.name] = file
return sample_dict


class CARDMAGsKmerAnalysisFormat(model.TextFileFormat):
def _validate(self, n_records=None):
Expand Down
Binary file not shown.
Binary file not shown.
56 changes: 53 additions & 3 deletions q2_amr/types/tests/test_types_formats_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,28 +315,78 @@ def test_CARDAnnotationDirectoryFormat_to_qiime2_Metadata_transformer(self):
metadata_obt = transformer(annotation)
self.assertIsInstance(metadata_obt, qiime2.Metadata)

def test_card_annotation_directory_format_sample_dict(self):
dirpath = self.get_data_path("annotate_mags_output")
annotations = CARDAnnotationDirectoryFormat(dirpath, mode="r")

obs = annotations.sample_dict()
exp = {
"sample1": {
"bin1": [
os.path.join(dirpath, "sample1", "bin1", "amr_annotation.json"),
os.path.join(dirpath, "sample1", "bin1", "amr_annotation.txt"),
]
},
"sample2": {
"bin1": [
os.path.join(dirpath, "sample2", "bin1", "amr_annotation.json"),
os.path.join(dirpath, "sample2", "bin1", "amr_annotation.txt"),
]
},
}
self.assertEqual(obs, exp)


class TestCARDReadsAnnotationTypesAndFormats(AMRTypesTestPluginBase):
def test_CARDGeneAnnotationDirectoryFormat_to_qiime2_Metadata_transformer(self):
transformer = self.get_transformer(
CARDGeneAnnotationDirectoryFormat, qiime2.Metadata
)
annotation = CARDGeneAnnotationDirectoryFormat(
self.get_data_path("annotate_reads_output"), "r"
self.get_data_path("card_gene_annotation"), "r"
)
metadata_obt = transformer(annotation)
self.assertIsInstance(metadata_obt, qiime2.Metadata)

def test_CARDAlleleAnnotationDirectoryFormat_to_qiime2_Metadata_transformer(self):
transformer = self.get_transformer(
CARDGeneAnnotationDirectoryFormat, qiime2.Metadata
CARDAlleleAnnotationDirectoryFormat, qiime2.Metadata
)
annotation = CARDAlleleAnnotationDirectoryFormat(
self.get_data_path("annotate_reads_output"), "r"
self.get_data_path("card_allele_annotation"), "r"
)
metadata_obt = transformer(annotation)
self.assertIsInstance(metadata_obt, qiime2.Metadata)

def test_card_allele_annotation_directory_format_sample_dict(self):
dirpath = self.get_data_path("card_allele_annotation")
annotations = CARDAlleleAnnotationDirectoryFormat(dirpath, mode="r")

obs = annotations.sample_dict()
exp = {
sample: [
os.path.join(dirpath, sample, file)
for file in [
"allele_mapping_data.txt",
"overall_mapping_stats.txt",
"sorted.length_100.bam",
]
VinzentRisch marked this conversation as resolved.
Show resolved Hide resolved
]
for sample in ["sample1", "sample2"]
}
self.assertEqual(obs, exp)

def test_card_gene_annotation_directory_format_sample_dict(self):
dirpath = self.get_data_path("card_gene_annotation")
annotations = CARDGeneAnnotationDirectoryFormat(dirpath, mode="r")

obs = annotations.sample_dict()
exp = {
"sample1": list(os.path.join(dirpath, "sample1", "gene_mapping_data.txt")),
"sample2": list(os.path.join(dirpath, "sample2", "gene_mapping_data.txt")),
VinzentRisch marked this conversation as resolved.
Show resolved Hide resolved
}
self.assertEqual(obs, exp)


class TestKmerTypesAndFormats(AMRTypesTestPluginBase):
def test_card_mags_kmer_analysis_validate_positive(self):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"q2_amr.types.tests": [
"data/*",
"data/annotate_mags_output/*/*/*",
"data/annotate_reads_output/*/*",
"data/card_allele_annotation/*/*",
"data/card_gene_annotation/*/*",
],
"q2_amr.card.tests": ["data/*"],
},
Expand Down
Loading