From a8dc90054098d8763f628af29a4e852c4f320ac7 Mon Sep 17 00:00:00 2001 From: Khoroshevskyi Date: Mon, 11 Dec 2023 14:28:56 -0500 Subject: [PATCH] fixed docs and added from_pep_config method --- docs/initialize.md | 27 +++++++++++++++++++++------ peppy/project.py | 31 +++++++++++++++++++++++++++++++ peppy/utils.py | 1 - tests/test_Project.py | 4 ++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/docs/initialize.md b/docs/initialize.md index 1ae33c8a..a762045e 100644 --- a/docs/initialize.md +++ b/docs/initialize.md @@ -7,13 +7,13 @@ Additionally, peppy can be initiated using Python objects such as a pandas dataf ## 1. Using a configuration file ```python import peppy -project = peppy.Project("path/to/project/config.yaml") +project = peppy.Project.from_pep_config("path/to/project/config.yaml") ``` ## 2. Using csv file (sample sheet) ```python import peppy -project = peppy.Project("path/to/project/sample_sheet.csv") +project = peppy.Project.from_pep_config("path/to/project/sample_sheet.csv") ``` ## 3. Using yaml sample sheet @@ -34,13 +34,28 @@ project = peppy.Project.from_pandas(df) ## 5. Using a peppy generated dict ```python import peppy -project = peppy.Project.from_dict({`_config`: str, - `_samples`: list | dict, - `_subsamples`: list[list | dict]}) +project = peppy.Project.from_dict( + {'_config': {'description': None, + 'name': 'example_basic', + 'pep_version': '2.0.0', + 'sample_table': 'sample_table.csv',}, + '_sample_dict': [{'organism': 'pig', 'sample_name': 'pig_0h', 'time': '0'}, + {'organism': 'pig', 'sample_name': 'pig_1h', 'time': '1'}, + {'organism': 'frog', 'sample_name': 'frog_0h', 'time': '0'}, + {'organism': 'frog', 'sample_name': 'frog_1h', 'time': '1'}], + '_subsample_list': [[{'read1': 'frog1a_data.txt', + 'read2': 'frog1a_data2.txt', + 'sample_name': 'frog_0h'}, + {'read1': 'frog1b_data.txt', + 'read2': 'frog1b_data2.txt', + 'sample_name': 'pig_0h'}, + {'read1': 'frog1c_data.txt', + 'read2': 'frog1b_data2.txt', + 'sample_name': 'pig_0h'}]]}) ``` ## 6. Using a csv file from a url ```python import peppy -project = peppy.Project("example_url.csv") +project = peppy.Project("https://raw.githubusercontent.com/pepkit/example_peps/master/example_basic/sample_table.csv") ``` diff --git a/peppy/project.py b/peppy/project.py index 742565a7..5fe05822 100644 --- a/peppy/project.py +++ b/peppy/project.py @@ -235,6 +235,37 @@ def from_dict(cls, pep_dictionary: dict): return tmp_obj + @classmethod + def from_pep_config( + cls, + cfg: str = None, + amendments: Union[str, Iterable[str]] = None, + sample_table_index: Union[str, Iterable[str]] = None, + subsample_table_index: Union[str, Iterable[str]] = None, + defer_samples_creation: bool = False, + ): + """ + Init a peppy project instance from a yaml file + + :param str cfg: Project config file (YAML) or sample table (CSV/TSV) + with one row per sample to constitute project + :param str | Iterable[str] sample_table_index: name of the columns to set + the sample_table index to + :param str | Iterable[str] subsample_table_index: name of the columns to set + the subsample_table index to + :param str | Iterable[str] amendments: names of the amendments to activate + :param Iterable[str] amendments: amendments to use within configuration file + :param bool defer_samples_creation: whether the sample creation should be skipped + """ + # TODO: this is just a copy of the __init__ method. It should be refactored + return cls( + cfg=cfg, + amendments=amendments, + sample_table_index=sample_table_index, + subsample_table_index=subsample_table_index, + defer_samples_creation=defer_samples_creation, + ) + @classmethod def from_yaml(cls, yaml_file: str): """ diff --git a/peppy/utils.py b/peppy/utils.py index e4fed739..35e6017a 100644 --- a/peppy/utils.py +++ b/peppy/utils.py @@ -5,7 +5,6 @@ from typing import Dict, Mapping from urllib.request import urlopen -import yacman import yaml from ubiquerg import expandpath, is_url diff --git a/tests/test_Project.py b/tests/test_Project.py index 685d7259..4aa14b27 100644 --- a/tests/test_Project.py +++ b/tests/test_Project.py @@ -350,6 +350,10 @@ def test_from_dict_instatiation(self, example_pep_cfg_path): representation. """ p1 = Project(cfg=example_pep_cfg_path) + ff = p1.to_dict(extended=True) + import pprint + + pprint.pprint(ff) p2 = Project.from_dict(p1.to_dict(extended=True)) assert p1 == p2