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

fix: srp index error #44

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/build_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
workflow_call:
inputs:
PUSH:
required: true
type: boolean
default: false

jobs:
build_push:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- if: "${{ inputs.PUSH == false }}"
name: Build only
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
push: ${{ inputs.PUSH }}
tags: ghcr.io/${{ github.repository }}:latest
- if: "${{ inputs.PUSH == true }}"
name: Build and push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ inputs.PUSH }}
tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }},ghcr.io/${{ github.repository }}:latest
11 changes: 11 additions & 0 deletions .github/workflows/on_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: CI
on: pull_request
jobs:
ci:
permissions:
packages: write
contents: read
id-token: write
uses: ./.github/workflows/build_push.yml
with:
PUSH: false
14 changes: 14 additions & 0 deletions .github/workflows/on_push_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: CD
on:
push:
tags:
- '*'
jobs:
cd:
permissions:
packages: write
contents: read
id-token: write
uses: ./.github/workflows/build_push.yml
with:
PUSH: true
13 changes: 11 additions & 2 deletions modules/sparse_ref_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
To select a part of the chromosome by position, use range:
sparse_matrix = ref_panel.range(start_pos, end_pos)
"""

import os
import subprocess
from io import BytesIO
Expand Down Expand Up @@ -88,6 +89,10 @@ def __getitem__(self, key: Tuple[Union[int, list, slice]]) -> sparse.csc_matrix:
raise TypeError("Both variant and haplotype slices must be provided")
# handle single row
if isinstance(key[0], int):
if key[0] > self.n_variants - 1:
raise IndexError(
f"Index {key[0]} out of range for {self.n_variants} variants"
)
return self._load_haplotypes(key[0] // self.chunk_size)[
key[0] % self.chunk_size, key[1]
]
Expand All @@ -102,7 +107,9 @@ def __getitem__(self, key: Tuple[Union[int, list, slice]]) -> sparse.csc_matrix:
]
)
row_stop = (
min([key[0].stop, self.n_variants]) if key[0].stop is not None else self.n_variants
min([key[0].stop, self.n_variants - 1])
if key[0].stop is not None
else self.n_variants - 1
)
chunks = list(
range(
Expand Down Expand Up @@ -144,7 +151,9 @@ def __getitem__(self, key: Tuple[Union[int, list, slice]]) -> sparse.csc_matrix:

chunks, splits = np.unique(rows // self.chunk_size, return_index=True)
if any(chunk not in self.chunks[:, 0] for chunk in chunks):
raise IndexError("Variants index out of range")
raise IndexError(
f"Index {key[0]} out of range for {self.n_variants} variants"
)

chunk_idx = np.split(rows % self.chunk_size, splits[1:])

Expand Down
9 changes: 9 additions & 0 deletions selphi.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ def selphi(
ref_panel = SparseReferencePanel(
str(ref_base_path.with_suffix(".srp")), cache_size=2
)
# Confirm that reference panel files match
n_pbwt_samples = ref_base_path.with_suffix(".samples").read_text().count("\n")
n_pbwt_vars = ref_base_path.with_suffix(".sites").read_text().count("\n")
if n_pbwt_samples != ref_panel.n_samples or n_pbwt_vars != ref_panel.n_variants:
raise ValueError(
f"pbwt reference panel has {n_pbwt_samples} samples and {n_pbwt_vars} "
f"variants while sparse reference panel has {ref_panel.n_samples} samples "
f"and {ref_panel.n_variants} variants"
)

# Load target samples and variants
vcf_obj = cyvcf2.VCF(targets_path)
Expand Down