Skip to content

Commit

Permalink
fix(study_locus_valiation): ensure the the transQtlColumn is missing
Browse files Browse the repository at this point in the history
* build(deps): update scipy requirementV

---------

Co-authored-by: project-defiant <[email protected]>
  • Loading branch information
dependabot[bot] and SzymonSzyszkowski committed Feb 7, 2025
1 parent 8d018de commit b690df9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/gentropy/dataset/study_locus.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ def _qc_subsignificant_associations(
def qc_abnormal_pips(
self: StudyLocus,
sum_pips_lower_threshold: float = 0.99,
sum_pips_upper_threshold: float = 1.0001, # Set slightly above 1 to account for floating point errors
# Set slightly above 1 to account for floating point errors
sum_pips_upper_threshold: float = 1.0001,
) -> StudyLocus:
"""Filter study-locus by sum of posterior inclusion probabilities to ensure that the sum of PIPs is within a given range.
Expand Down Expand Up @@ -691,6 +692,7 @@ def flag_trans_qtls(
"""Flagging transQTL credible sets based on genomic location of the measured gene.
Process:
0. Make sure that the `isTransQtl` column does not exist (remove if exists)
1. Enrich study-locus dataset with geneId based on study metadata. (only QTL studies are considered)
2. Enrich with transcription start site and chromosome of the studied gegne.
3. Flagging any tagging variant of QTL credible sets, if chromosome is different from the gene or distance is above the threshold.
Expand All @@ -709,6 +711,12 @@ def flag_trans_qtls(
if "geneId" not in study_index.df.columns:
return self

# We have to remove the column `isTransQtl` to ensure the column is not duplicated
# The duplication can happen when one reads the StudyLocus from parquet with
# predefined schema that already contains the `isTransQtl` column.
if "isTransQtl" in self.df.columns:
self.df = self.df.drop("isTransQtl")

# Process study index:
processed_studies = (
study_index.df
Expand Down
36 changes: 31 additions & 5 deletions tests/gentropy/dataset/test_study_locus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from pathlib import Path
from typing import Any

import pyspark.sql.functions as f
Expand All @@ -18,6 +19,8 @@
StructType,
)

from gentropy.common.schemas import SchemaValidationError
from gentropy.common.session import Session
from gentropy.dataset.colocalisation import Colocalisation
from gentropy.dataset.l2g_feature_matrix import L2GFeatureMatrix
from gentropy.dataset.ld_index import LDIndex
Expand Down Expand Up @@ -1209,7 +1212,6 @@ class TestTransQtlFlagging:
]

STUDY_LOCUS_COLUMNS = ["studyLocusId", "variantId", "studyId"]

STUDY_DATA = [
("s1", "p1", "qtl", "g1"),
("s2", "p2", "gwas", None),
Expand All @@ -1221,21 +1223,21 @@ class TestTransQtlFlagging:
GENE_COLUMNS = ["id", "strand", "start", "end", "chromosome", "tss"]

@pytest.fixture(autouse=True)
def _setup(self: TestTransQtlFlagging, spark: SparkSession) -> None:
def _setup(self: TestTransQtlFlagging, session: Session) -> None:
"""Setup study locus for testing."""
self.study_locus = StudyLocus(
_df=(
spark.createDataFrame(
session.spark.createDataFrame(
self.STUDY_LOCUS_DATA, self.STUDY_LOCUS_COLUMNS
).withColumn("locus", f.array(f.struct("variantId")))
)
)
self.study_index = StudyIndex(
_df=spark.createDataFrame(self.STUDY_DATA, self.STUDY_COLUMNS)
_df=session.spark.createDataFrame(self.STUDY_DATA, self.STUDY_COLUMNS)
)
self.target_index = TargetIndex(
_df=(
spark.createDataFrame(self.GENE_DATA, self.GENE_COLUMNS).select(
session.spark.createDataFrame(self.GENE_DATA, self.GENE_COLUMNS).select(
f.struct(
f.col("strand").cast(IntegerType()).alias("strand"),
"start",
Expand Down Expand Up @@ -1283,3 +1285,27 @@ def test_correctness_found_trans(self: TestTransQtlFlagging) -> None:
assert (
self.qtl_flagged.df.filter(f.col("isTransQtl")).count() == 2
), "Expected number of rows differ from observed."

def test_add_flag_if_column_is_present(
self: TestTransQtlFlagging, tmp_path: Path, session: Session
) -> None:
"""Test adding flag if the `isTransQtl` column is already present.
When reading the dataset, the reader will add the `isTransQtl` column to
the schema, which can cause column duplication captured only by Dataset schema validation.
This test ensures that the column is dropped before the `flag_trans_qtls` is run.
"""
dataset_path = str(tmp_path / "study_locus")
self.study_locus.df.write.parquet(dataset_path)
schema_validated_study_locus = StudyLocus.from_parquet(session, dataset_path)
assert (
"isTransQtl" in schema_validated_study_locus.df.columns
), "`isTransQtl` column is missing after reading the dataset."
# Rerun the flag addition and check if any error is raised by the schema validation
try:
schema_validated_study_locus.flag_trans_qtls(
self.study_index, self.target_index, self.THRESHOLD
)
except SchemaValidationError:
pytest.fail("Failed to validate the schema when adding isTransQtl flag")

0 comments on commit b690df9

Please sign in to comment.