Skip to content

Commit

Permalink
added validation positive for emty files
Browse files Browse the repository at this point in the history
  • Loading branch information
VinzentRisch committed Jul 10, 2024
1 parent bbaca6e commit b97152f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
25 changes: 14 additions & 11 deletions q2_amr/amrfinderplus/types/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def amr_dna_tab_path_maker(self):


class ARMFinderPlusAnnotationFormat(model.TextFileFormat):
def _validate(self, n_records=None):
def _validate(self):
header_coordinates = [
"Protein identifier",
"Contig id",
Expand All @@ -98,16 +98,19 @@ def _validate(self, n_records=None):
"Hierarchy node",
]
header = header_coordinates[:1] + header_coordinates[5:]
header_obs = pd.read_csv(str(self), sep="\t", nrows=0).columns.tolist()
if header != header_obs and header_coordinates != header_obs:
raise ValidationError(
"Header line does not match ARMFinderPlusAnnotation format. Must "
"consist of the following values: "
+ ", ".join(header_coordinates)
+ ".\nWhile Contig id, Start, Stop and Strand are optional."
+ ".\n\nFound instead: "
+ ", ".join(header_obs)
)
try:
header_obs = pd.read_csv(str(self), sep="\t", nrows=0).columns.tolist()
if header != header_obs and header_coordinates != header_obs:
raise ValidationError(
"Header line does not match ARMFinderPlusAnnotation format. Must "
"consist of the following values: "
+ ", ".join(header_coordinates)
+ ".\nWhile Contig id, Start, Stop and Strand are optional."
+ ".\n\nFound instead: "
+ ", ".join(header_obs)
)
except pd.errors.EmptyDataError:
pass

def _validate_(self, level):
self._validate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import os
import tempfile

from qiime2.core.exceptions import ValidationError
from qiime2.plugin.testing import TestPluginBase

Expand Down Expand Up @@ -40,6 +43,14 @@ def test_amrfinderplus_annotation_format_validate_positive_coordinates(self):
format = ARMFinderPlusAnnotationFormat(filepath, mode="r")
format.validate()

def test_amrfinderplus_annotation_format_validate_positive_empty(self):
with tempfile.TemporaryDirectory() as temp_dir:
temp_file_path = os.path.join(temp_dir, "amr_annotations.tsv")
with open(temp_file_path, "w"):
pass
format = ARMFinderPlusAnnotationFormat(temp_file_path, mode="r")
format.validate()

def test_amrfinderplus_annotation_format_validation_error(self):
with self.assertRaises(ValidationError) as context:
path = self.get_data_path("annotation_wrong/amr_annotation.tsv")
Expand Down

0 comments on commit b97152f

Please sign in to comment.