diff --git a/q2_amr/amrfinderplus/types/_format.py b/q2_amr/amrfinderplus/types/_format.py index 9c2e251..c74f5d3 100644 --- a/q2_amr/amrfinderplus/types/_format.py +++ b/q2_amr/amrfinderplus/types/_format.py @@ -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", @@ -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() diff --git a/q2_amr/amrfinderplus/types/tests/test_types_formats_transformers.py b/q2_amr/amrfinderplus/types/tests/test_types_formats_transformers.py index f0672d5..3164780 100644 --- a/q2_amr/amrfinderplus/types/tests/test_types_formats_transformers.py +++ b/q2_amr/amrfinderplus/types/tests/test_types_formats_transformers.py @@ -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 @@ -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")