Skip to content

Commit

Permalink
feat(airflow): include COLOC as a node in the DAG (#530)
Browse files Browse the repository at this point in the history
* feat: split colocalisation step into two nodes: coloc and ecaviar

* feat: filter credible sets to be colocalised based on method

* fix(dag): update coloc dependencies

* revert(config): revert colocalisation output location

* feat(coloc): _get_colocalisation_class to import methods dynamically

* refactor(step): remove method enum

* refactor(step): do not hardcode method value

* fix(config): typo in static_assets

* refactor(dag): uncomment steps
  • Loading branch information
ireneisdoomed authored May 15, 2024
1 parent d89ec86 commit bc1a112
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ defaults:
credible_set_path: ${datasets.credible_set}
study_index_path: ${datasets.study_index}
coloc_path: ${datasets.colocalisation}
colocalisation_method: Coloc
7 changes: 7 additions & 0 deletions config/step/ot_colocalisation_ecaviar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defaults:
- colocalisation

credible_set_path: ${datasets.credible_set}
study_index_path: ${datasets.study_index}
coloc_path: ${datasets.colocalisation}
colocalisation_method: ECaviar
9 changes: 6 additions & 3 deletions src/airflow/dags/configs/dag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
prerequisites:
- "ot_variant_index"
- "ot_gene_index"
- id: "ot_colocalisation"
- id: "ot_colocalisation_ecaviar"
- id: "ot_colocalisation_coloc"
- id: "ot_locus_to_gene_train"
prerequisites:
- "ot_variant_index"
- "ot_variant_to_gene"
- "ot_colocalisation"
- "ot_colocalisation_ecaviar"
- "ot_colocalisation_coloc"
- id: "ot_locus_to_gene_predict"
prerequisites:
- "ot_locus_to_gene_train"
- "ot_variant_index"
- "ot_variant_to_gene"
- "ot_colocalisation"
- "ot_colocalisation_ecaviar"
- "ot_colocalisation_coloc"
56 changes: 51 additions & 5 deletions src/gentropy/colocalisation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""Step to generate colocalisation results."""
from __future__ import annotations

import inspect
from importlib import import_module

from pyspark.sql.functions import col

from gentropy.common.session import Session
from gentropy.dataset.study_index import StudyIndex
from gentropy.dataset.study_locus import CredibleInterval, StudyLocus
from gentropy.method.colocalisation import ECaviar
from gentropy.method.colocalisation import Coloc


class ColocalisationStep:
Expand All @@ -19,6 +24,7 @@ def __init__(
credible_set_path: str,
study_index_path: str,
coloc_path: str,
colocalisation_method: str,
) -> None:
"""Run Colocalisation step.
Expand All @@ -27,10 +33,18 @@ def __init__(
credible_set_path (str): Input credible sets path.
study_index_path (str): Input study index path.
coloc_path (str): Output Colocalisation path.
colocalisation_method (str): Colocalisation method.
"""
colocalisation_class = self._get_colocalisation_class(colocalisation_method)
# Extract
credible_set = StudyLocus.from_parquet(
session, credible_set_path, recursiveFileLookup=True
credible_set = (
StudyLocus.from_parquet(
session, credible_set_path, recursiveFileLookup=True
).filter(col("finemappingMethod") == "SuSie")
if colocalisation_class is Coloc
else StudyLocus.from_parquet(
session, credible_set_path, recursiveFileLookup=True
)
)
si = StudyIndex.from_parquet(
session, study_index_path, recursiveFileLookup=True
Expand All @@ -40,7 +54,39 @@ def __init__(
overlaps = credible_set.filter_credible_set(
CredibleInterval.IS95
).find_overlaps(si)
ecaviar_results = ECaviar.colocalise(overlaps)
colocalisation_results = colocalisation_class.colocalise(overlaps) # type: ignore

# Load
ecaviar_results.df.write.mode(session.write_mode).parquet(coloc_path)
colocalisation_results.df.write.mode(session.write_mode).parquet(
f"{coloc_path}/{colocalisation_method.lower()}"
)

@classmethod
def _get_colocalisation_class(cls: type[ColocalisationStep], method: str) -> type:
"""Get colocalisation class.
Args:
method (str): Colocalisation method.
Returns:
type: Colocalisation class.
Raises:
ValueError: if method not available.
Examples:
>>> ColocalisationStep._get_colocalisation_class("ECaviar")
<class 'gentropy.method.colocalisation.ECaviar'>
"""
module_name = "gentropy.method.colocalisation"
module = import_module(module_name)

available_methods = []
for class_name, class_obj in inspect.getmembers(module, inspect.isclass):
if class_obj.__module__ == module_name:
available_methods.append(class_name)
if class_name == method:
return class_obj
raise ValueError(
f"Method {method} is not supported. Available: {(', ').join(available_methods)}"
)
1 change: 1 addition & 0 deletions src/gentropy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ColocalisationConfig(StepConfig):
credible_set_path: str = MISSING
study_index_path: str = MISSING
coloc_path: str = MISSING
colocalisation_method: str = MISSING
_target_: str = "gentropy.colocalisation.ColocalisationStep"


Expand Down

0 comments on commit bc1a112

Please sign in to comment.