Skip to content

Commit

Permalink
fixed docs and added from_pep_config method
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Dec 11, 2023
1 parent a59208c commit a8dc900
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
27 changes: 21 additions & 6 deletions docs/initialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
```
31 changes: 31 additions & 0 deletions peppy/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
1 change: 0 additions & 1 deletion peppy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Dict, Mapping
from urllib.request import urlopen

import yacman
import yaml
from ubiquerg import expandpath, is_url

Expand Down
4 changes: 4 additions & 0 deletions tests/test_Project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a8dc900

Please sign in to comment.