Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add pytest parse ped #46

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions .github/workflows/utils_lint.yml
ellendejong marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Utils Lint
on:
pull_request:
paths: Utils/**

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
59 changes: 59 additions & 0 deletions .github/workflows/utils_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Source: https://github.com/marketplace/actions/install-poetry-action
name: Utils Test
on:
pull_request:
paths: Utils/**
jobs:
pytest:
runs-on: ubuntu-latest
defaults:
run:
working-directory: Utils/
steps:
#----------------------------------------------
# check-out repo and set-up python
#----------------------------------------------
- name: Check out repository
uses: actions/checkout@v4
- name: Set up python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.11.5'
#----------------------------------------------
# install & configure poetry
#----------------------------------------------
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv_utils-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
#----------------------------------------------
# install dependencies if cache does not exist
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
#----------------------------------------------
# install root project
#----------------------------------------------
- name: Install project
run: poetry install --no-interaction
#----------------------------------------------
# run pytest
#----------------------------------------------
- name: Run tests
run: |
source .venv/bin/activate
pytest .
6 changes: 6 additions & 0 deletions Utils/data/multi_family.ped
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
U000001 2024D00001 0 0 1 1
U000001 2024D00002 0 0 2 1
U000001 2024D00003 2024D00001 2024D00002 2 2
U000002 2024D00004 0 0 1 1
U000002 2024D00005 0 0 2 1
U000002 2024D00006 2024D00004 2024D00005 2 2
4 changes: 4 additions & 0 deletions Utils/data/multi_siblings.ped
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
U000001 2024D00001 0 0 1 1
U000001 2024D00002 0 0 2 1
U000001 2024D00003 2024D00001 2024D00002 2 2
U000001 2024D00004 2024D00001 2024D00002 2 2
2 changes: 2 additions & 0 deletions Utils/data/multi_unrelated_samples.ped
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
U000001 2024D00001 0 0 1 1
U000002 2024D00002 0 0 2 1
3 changes: 3 additions & 0 deletions Utils/data/single_family.ped
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
U000001 2024D00001 0 0 1 1
U000001 2024D00002 0 0 2 1
U000001 2024D00003 2024D00001 2024D00002 2 2
1 change: 0 additions & 1 deletion Utils/parse_child_from_fulltrio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

def parse_ped(ped_file):
samples = {} # 'sample_id': {'family': 'fam_id', 'parents': ['sample_id', 'sample_id']}

for line in ped_file:
ped_data = line.strip().split()
family, sample, father, mother, sex, phenotype = ped_data
Expand Down
102 changes: 102 additions & 0 deletions Utils/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Utils/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tool.poetry]
name = "utils"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
license = "MIT"
package-mode = false

[tool.poetry.dependencies]
python = "^3.11"
pytest-unordered = "0.5.2"
pytest-datadir = "^1.5.0"
ellendejong marked this conversation as resolved.
Show resolved Hide resolved

[tool.ruff]
line-length = 127
indent-width = 4

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
49 changes: 49 additions & 0 deletions Utils/test_parse_child_from_fulltrio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
# Import statements, alphabetic order of main package.

# Third party libraries alphabetic order of main package.
import pytest
from pytest_unordered import unordered

# Custom libraries alphabetic order of main package.
from parse_child_from_fulltrio import parse_ped
@pytest.mark.parametrize("input_file,exp_dict_samples", [
("multi_family.ped",
{
"2024D00001": {'family': 'U000001', 'parents': [], 'children': ["2024D00003"]},
"2024D00002": {'family': 'U000001', 'parents': [], 'children': ["2024D00003"]},
"2024D00003": {'family': 'U000001', 'parents': ["2024D00001","2024D00002"], 'children': []},
"2024D00004": {'family': 'U000002', 'parents': [], 'children': ["2024D00006"]},
"2024D00005": {'family': 'U000002', 'parents': [], 'children': ["2024D00006"]},
"2024D00006": {'family': 'U000002', 'parents': ["2024D00004","2024D00005"], 'children': []},
},
),
("multi_siblings.ped",
{
"2024D00001": {'family': 'U000001', 'parents': [], 'children': ["2024D00003", "2024D00004"]},
"2024D00002": {'family': 'U000001', 'parents': [], 'children': ["2024D00003", "2024D00004"]},
"2024D00003": {'family': 'U000001', 'parents': ["2024D00001","2024D00002"], 'children': []},
"2024D00004": {'family': 'U000001', 'parents': ["2024D00001","2024D00002"], 'children': []},
},
),
("multi_unrelated_samples.ped",
{
"2024D00001": {'family': 'U000001', 'parents': [], 'children': []},
"2024D00002": {'family': 'U000002', 'parents': [], 'children': []},
},
),
("single_family.ped",
{
"2024D00001": {'family': 'U000001', 'parents': [], 'children': ["2024D00003"]},
"2024D00002": {'family': 'U000001', 'parents': [], 'children': ["2024D00003"]},
"2024D00003": {'family': 'U000001', 'parents': ["2024D00001","2024D00002"], 'children': []},
},
),
])
def test_parse_ped(input_file, exp_dict_samples, shared_datadir):
dict_samples = parse_ped(open(f"{shared_datadir}/{input_file}", "r"))
assert dict_samples.keys() == unordered(exp_dict_samples.keys())
for sample, meta in dict_samples.items():
assert meta.get("family") == exp_dict_samples.get(sample).get("family")
assert meta.get("parents") == unordered(exp_dict_samples.get(sample).get("parents"))
assert meta.get("children") == unordered(exp_dict_samples.get(sample).get("children"))