From 88264c31f401a28c99a17b0c9d513c690866e146 Mon Sep 17 00:00:00 2001 From: Khoroshevskyi Date: Fri, 12 Jul 2024 11:52:46 -0400 Subject: [PATCH] Fixed #393 --- peppy/project.py | 5 ++++- peppy/utils.py | 6 ++++-- .../example_subsamples_none/project_config.yaml | 3 +++ .../example_subsamples_none/sample_table.csv | 4 ++++ tests/test_Project.py | 8 ++++++++ 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 tests/data/example_peps-master/example_subsamples_none/project_config.yaml create mode 100644 tests/data/example_peps-master/example_subsamples_none/sample_table.csv diff --git a/peppy/project.py b/peppy/project.py index 8b562603..48dc9e9f 100644 --- a/peppy/project.py +++ b/peppy/project.py @@ -1316,7 +1316,10 @@ def _read_sample_data(self): if self[SUBSAMPLE_TABLES_FILE_KEY] is not None: sst = self[SUBSAMPLE_TABLES_FILE_KEY] else: - if CONFIG_KEY in self and CFG_SUBSAMPLE_TABLE_KEY in self[CONFIG_KEY]: + if ( + CONFIG_KEY in self + and self[CONFIG_KEY].get(CFG_SUBSAMPLE_TABLE_KEY) is not None + ): sst = make_list(self[CONFIG_KEY][CFG_SUBSAMPLE_TABLE_KEY], str) else: sst = None diff --git a/peppy/utils.py b/peppy/utils.py index 35e6017a..d7598e7d 100644 --- a/peppy/utils.py +++ b/peppy/utils.py @@ -2,7 +2,7 @@ import logging import os -from typing import Dict, Mapping +from typing import Dict, Mapping, Type, Union from urllib.request import urlopen import yaml @@ -76,14 +76,16 @@ def grab_project_data(prj): raise KeyError("Project lacks section '{}'".format(CONFIG_KEY)) -def make_list(arg, obj_class): +def make_list(arg: Union[list, str], obj_class: Type) -> list: """ Convert an object of predefined class to a list of objects of that class or ensure a list is a list of objects of that class :param list[obj] | obj arg: string or a list of strings to listify :param str obj_class: name of the class of intrest + :return list: list of objects of the predefined class + :raise TypeError: if a faulty argument was provided """ diff --git a/tests/data/example_peps-master/example_subsamples_none/project_config.yaml b/tests/data/example_peps-master/example_subsamples_none/project_config.yaml new file mode 100644 index 00000000..d90f1261 --- /dev/null +++ b/tests/data/example_peps-master/example_subsamples_none/project_config.yaml @@ -0,0 +1,3 @@ +pep_version: "2.0.0" +sample_table: sample_table.csv +subsample_table: null diff --git a/tests/data/example_peps-master/example_subsamples_none/sample_table.csv b/tests/data/example_peps-master/example_subsamples_none/sample_table.csv new file mode 100644 index 00000000..a800e04d --- /dev/null +++ b/tests/data/example_peps-master/example_subsamples_none/sample_table.csv @@ -0,0 +1,4 @@ +sample_name,protocol,file +frog_1,anySampleType,multi +frog_2,anySampleType,multi +frog_3,anySampleType,multi diff --git a/tests/test_Project.py b/tests/test_Project.py index 18005993..71d94280 100644 --- a/tests/test_Project.py +++ b/tests/test_Project.py @@ -715,3 +715,11 @@ def test_sample_len(self, example_pep_cfg_path): """ p = Project(cfg=example_pep_cfg_path) assert len(p.samples[0]) == 4 + + @pytest.mark.parametrize("example_pep_cfg_path", ["subsamples_none"], indirect=True) + def test_config_with_subsample_null(self, example_pep_cfg_path): + """ + Tests if config can have value with subsample=null + """ + p = Project(cfg=example_pep_cfg_path) + assert p.subsample_table is None