Skip to content

Other features

List of features

gentropy.dataset.l2g_features.other.GeneCountFeature dataclass

Bases: L2GFeature

Counts the number of genes within a specified window size from the study locus.

Source code in src/gentropy/dataset/l2g_features/other.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class GeneCountFeature(L2GFeature):
    """Counts the number of genes within a specified window size from the study locus."""

    feature_dependency_type = TargetIndex
    feature_name = "geneCount500kb"

    @classmethod
    def compute(
        cls: type[GeneCountFeature],
        study_loci_to_annotate: StudyLocus | L2GGoldStandard,
        feature_dependency: dict[str, Any],
    ) -> GeneCountFeature:
        """Computes the gene count feature.

        Args:
            study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
            feature_dependency (dict[str, Any]): Dictionary containing dependencies, with target index and window size

        Returns:
            GeneCountFeature: Feature dataset
        """
        genomic_window = 500000
        gene_count_df = common_genecount_feature_logic(
            study_loci_to_annotate=study_loci_to_annotate,
            feature_name=cls.feature_name,
            genomic_window=genomic_window,
            **feature_dependency,
        )

        return cls(
            _df=convert_from_wide_to_long(
                gene_count_df,
                id_vars=("studyLocusId", "geneId"),
                var_name="featureName",
                value_name="featureValue",
            ),
            _schema=cls.get_schema(),
        )

compute(study_loci_to_annotate: StudyLocus | L2GGoldStandard, feature_dependency: dict[str, Any]) -> GeneCountFeature classmethod

Computes the gene count feature.

Parameters:

Name Type Description Default
study_loci_to_annotate StudyLocus | L2GGoldStandard

The dataset containing study loci that will be used for annotation

required
feature_dependency dict[str, Any]

Dictionary containing dependencies, with target index and window size

required

Returns:

Name Type Description
GeneCountFeature GeneCountFeature

Feature dataset

Source code in src/gentropy/dataset/l2g_features/other.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
@classmethod
def compute(
    cls: type[GeneCountFeature],
    study_loci_to_annotate: StudyLocus | L2GGoldStandard,
    feature_dependency: dict[str, Any],
) -> GeneCountFeature:
    """Computes the gene count feature.

    Args:
        study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
        feature_dependency (dict[str, Any]): Dictionary containing dependencies, with target index and window size

    Returns:
        GeneCountFeature: Feature dataset
    """
    genomic_window = 500000
    gene_count_df = common_genecount_feature_logic(
        study_loci_to_annotate=study_loci_to_annotate,
        feature_name=cls.feature_name,
        genomic_window=genomic_window,
        **feature_dependency,
    )

    return cls(
        _df=convert_from_wide_to_long(
            gene_count_df,
            id_vars=("studyLocusId", "geneId"),
            var_name="featureName",
            value_name="featureValue",
        ),
        _schema=cls.get_schema(),
    )

gentropy.dataset.l2g_features.other.ProteinGeneCountFeature dataclass

Bases: L2GFeature

Counts the number of protein coding genes within a specified window size from the study locus.

Source code in src/gentropy/dataset/l2g_features/other.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class ProteinGeneCountFeature(L2GFeature):
    """Counts the number of protein coding genes within a specified window size from the study locus."""

    feature_dependency_type = TargetIndex
    feature_name = "proteinGeneCount500kb"

    @classmethod
    def compute(
        cls: type[ProteinGeneCountFeature],
        study_loci_to_annotate: StudyLocus | L2GGoldStandard,
        feature_dependency: dict[str, Any],
    ) -> ProteinGeneCountFeature:
        """Computes the gene count feature.

        Args:
            study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
            feature_dependency (dict[str, Any]): Dictionary containing dependencies, with target index and window size

        Returns:
            ProteinGeneCountFeature: Feature dataset
        """
        genomic_window = 500000
        gene_count_df = common_genecount_feature_logic(
            study_loci_to_annotate=study_loci_to_annotate,
            feature_name=cls.feature_name,
            genomic_window=genomic_window,
            protein_coding_only=True,
            **feature_dependency,
        )

        return cls(
            _df=convert_from_wide_to_long(
                gene_count_df,
                id_vars=("studyLocusId", "geneId"),
                var_name="featureName",
                value_name="featureValue",
            ),
            _schema=cls.get_schema(),
        )

compute(study_loci_to_annotate: StudyLocus | L2GGoldStandard, feature_dependency: dict[str, Any]) -> ProteinGeneCountFeature classmethod

Computes the gene count feature.

Parameters:

Name Type Description Default
study_loci_to_annotate StudyLocus | L2GGoldStandard

The dataset containing study loci that will be used for annotation

required
feature_dependency dict[str, Any]

Dictionary containing dependencies, with target index and window size

required

Returns:

Name Type Description
ProteinGeneCountFeature ProteinGeneCountFeature

Feature dataset

Source code in src/gentropy/dataset/l2g_features/other.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
@classmethod
def compute(
    cls: type[ProteinGeneCountFeature],
    study_loci_to_annotate: StudyLocus | L2GGoldStandard,
    feature_dependency: dict[str, Any],
) -> ProteinGeneCountFeature:
    """Computes the gene count feature.

    Args:
        study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
        feature_dependency (dict[str, Any]): Dictionary containing dependencies, with target index and window size

    Returns:
        ProteinGeneCountFeature: Feature dataset
    """
    genomic_window = 500000
    gene_count_df = common_genecount_feature_logic(
        study_loci_to_annotate=study_loci_to_annotate,
        feature_name=cls.feature_name,
        genomic_window=genomic_window,
        protein_coding_only=True,
        **feature_dependency,
    )

    return cls(
        _df=convert_from_wide_to_long(
            gene_count_df,
            id_vars=("studyLocusId", "geneId"),
            var_name="featureName",
            value_name="featureValue",
        ),
        _schema=cls.get_schema(),
    )

gentropy.dataset.l2g_features.other.ProteinCodingFeature dataclass

Bases: L2GFeature

Indicates whether a gene is protein-coding within a specified window size from the study locus.

Source code in src/gentropy/dataset/l2g_features/other.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
class ProteinCodingFeature(L2GFeature):
    """Indicates whether a gene is protein-coding within a specified window size from the study locus."""

    feature_dependency_type = VariantIndex
    feature_name = "isProteinCoding"

    @classmethod
    def compute(
        cls: type[ProteinCodingFeature],
        study_loci_to_annotate: StudyLocus | L2GGoldStandard,
        feature_dependency: dict[str, Any],
    ) -> ProteinCodingFeature:
        """Computes the protein coding feature.

        Args:
            study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
            feature_dependency (dict[str, Any]): Dictionary containing dependencies, including variant index

        Returns:
            ProteinCodingFeature: Feature dataset with 1 if the gene is protein-coding, 0 otherwise
        """
        genomic_window = 500_000
        protein_coding_df = is_protein_coding_feature_logic(
            study_loci_to_annotate=study_loci_to_annotate,
            feature_name=cls.feature_name,
            genomic_window=genomic_window,
            **feature_dependency,
        )

        return cls(
            _df=convert_from_wide_to_long(
                protein_coding_df,
                id_vars=("studyLocusId", "geneId"),
                var_name="featureName",
                value_name="featureValue",
            ),
            _schema=cls.get_schema(),
        )

compute(study_loci_to_annotate: StudyLocus | L2GGoldStandard, feature_dependency: dict[str, Any]) -> ProteinCodingFeature classmethod

Computes the protein coding feature.

Parameters:

Name Type Description Default
study_loci_to_annotate StudyLocus | L2GGoldStandard

The dataset containing study loci that will be used for annotation

required
feature_dependency dict[str, Any]

Dictionary containing dependencies, including variant index

required

Returns:

Name Type Description
ProteinCodingFeature ProteinCodingFeature

Feature dataset with 1 if the gene is protein-coding, 0 otherwise

Source code in src/gentropy/dataset/l2g_features/other.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
@classmethod
def compute(
    cls: type[ProteinCodingFeature],
    study_loci_to_annotate: StudyLocus | L2GGoldStandard,
    feature_dependency: dict[str, Any],
) -> ProteinCodingFeature:
    """Computes the protein coding feature.

    Args:
        study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
        feature_dependency (dict[str, Any]): Dictionary containing dependencies, including variant index

    Returns:
        ProteinCodingFeature: Feature dataset with 1 if the gene is protein-coding, 0 otherwise
    """
    genomic_window = 500_000
    protein_coding_df = is_protein_coding_feature_logic(
        study_loci_to_annotate=study_loci_to_annotate,
        feature_name=cls.feature_name,
        genomic_window=genomic_window,
        **feature_dependency,
    )

    return cls(
        _df=convert_from_wide_to_long(
            protein_coding_df,
            id_vars=("studyLocusId", "geneId"),
            var_name="featureName",
            value_name="featureValue",
        ),
        _schema=cls.get_schema(),
    )

gentropy.dataset.l2g_features.other.CredibleSetConfidenceFeature dataclass

Bases: L2GFeature

Distance of the sentinel variant to gene TSS. This is not weighted by the causal probability.

Source code in src/gentropy/dataset/l2g_features/other.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
class CredibleSetConfidenceFeature(L2GFeature):
    """Distance of the sentinel variant to gene TSS. This is not weighted by the causal probability."""

    feature_dependency_type = [StudyLocus, VariantIndex]
    feature_name = "credibleSetConfidence"

    @classmethod
    def compute(
        cls: type[CredibleSetConfidenceFeature],
        study_loci_to_annotate: StudyLocus | L2GGoldStandard,
        feature_dependency: dict[str, Any],
    ) -> CredibleSetConfidenceFeature:
        """Computes the feature.

        Args:
            study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
            feature_dependency (dict[str, Any]): Dataset that contains the distance information

        Returns:
            CredibleSetConfidenceFeature: Feature dataset
        """
        full_credible_set = feature_dependency["study_locus"].df.select(
            "studyLocusId",
            "studyId",
            f.explode("locus.variantId").alias("variantId"),
            cls.score_credible_set_confidence(f.col("confidence")).alias(
                cls.feature_name
            ),
        )

        return cls(
            _df=convert_from_wide_to_long(
                (
                    study_loci_to_annotate.df.drop("studyLocusId")
                    # Annotate genes
                    .join(
                        feature_dependency["variant_index"].df.select(
                            "variantId",
                            f.explode("transcriptConsequences.targetId").alias(
                                "geneId"
                            ),
                        ),
                        on="variantId",
                        how="inner",
                    )
                    # Annotate credible set confidence
                    .join(full_credible_set, ["variantId", "studyId"])
                    .select("studyLocusId", "geneId", cls.feature_name)
                ),
                id_vars=("studyLocusId", "geneId"),
                var_name="featureName",
                value_name="featureValue",
            ),
            _schema=cls.get_schema(),
        )

    @classmethod
    def score_credible_set_confidence(
        cls: type[CredibleSetConfidenceFeature],
        confidence_column: Column,
    ) -> Column:
        """Expression that assigns a score to the credible set confidence.

        Args:
            confidence_column (Column): Confidence column in the StudyLocus object

        Returns:
            Column: A confidence score between 0 and 1
        """
        return (
            f.when(
                f.col("confidence")
                == CredibleSetConfidenceClasses.FINEMAPPED_IN_SAMPLE_LD.value,
                f.lit(1.0),
            )
            .when(
                f.col("confidence")
                == CredibleSetConfidenceClasses.FINEMAPPED_OUT_OF_SAMPLE_LD.value,
                f.lit(0.75),
            )
            .when(
                f.col("confidence")
                == CredibleSetConfidenceClasses.PICSED_SUMMARY_STATS.value,
                f.lit(0.5),
            )
            .when(
                f.col("confidence")
                == CredibleSetConfidenceClasses.PICSED_TOP_HIT.value,
                f.lit(0.25),
            )
            .when(
                f.col("confidence") == CredibleSetConfidenceClasses.UNKNOWN.value,
                f.lit(0.0),
            )
        )

compute(study_loci_to_annotate: StudyLocus | L2GGoldStandard, feature_dependency: dict[str, Any]) -> CredibleSetConfidenceFeature classmethod

Computes the feature.

Parameters:

Name Type Description Default
study_loci_to_annotate StudyLocus | L2GGoldStandard

The dataset containing study loci that will be used for annotation

required
feature_dependency dict[str, Any]

Dataset that contains the distance information

required

Returns:

Name Type Description
CredibleSetConfidenceFeature CredibleSetConfidenceFeature

Feature dataset

Source code in src/gentropy/dataset/l2g_features/other.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
@classmethod
def compute(
    cls: type[CredibleSetConfidenceFeature],
    study_loci_to_annotate: StudyLocus | L2GGoldStandard,
    feature_dependency: dict[str, Any],
) -> CredibleSetConfidenceFeature:
    """Computes the feature.

    Args:
        study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci that will be used for annotation
        feature_dependency (dict[str, Any]): Dataset that contains the distance information

    Returns:
        CredibleSetConfidenceFeature: Feature dataset
    """
    full_credible_set = feature_dependency["study_locus"].df.select(
        "studyLocusId",
        "studyId",
        f.explode("locus.variantId").alias("variantId"),
        cls.score_credible_set_confidence(f.col("confidence")).alias(
            cls.feature_name
        ),
    )

    return cls(
        _df=convert_from_wide_to_long(
            (
                study_loci_to_annotate.df.drop("studyLocusId")
                # Annotate genes
                .join(
                    feature_dependency["variant_index"].df.select(
                        "variantId",
                        f.explode("transcriptConsequences.targetId").alias(
                            "geneId"
                        ),
                    ),
                    on="variantId",
                    how="inner",
                )
                # Annotate credible set confidence
                .join(full_credible_set, ["variantId", "studyId"])
                .select("studyLocusId", "geneId", cls.feature_name)
            ),
            id_vars=("studyLocusId", "geneId"),
            var_name="featureName",
            value_name="featureValue",
        ),
        _schema=cls.get_schema(),
    )

score_credible_set_confidence(confidence_column: Column) -> Column classmethod

Expression that assigns a score to the credible set confidence.

Parameters:

Name Type Description Default
confidence_column Column

Confidence column in the StudyLocus object

required

Returns:

Name Type Description
Column Column

A confidence score between 0 and 1

Source code in src/gentropy/dataset/l2g_features/other.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
@classmethod
def score_credible_set_confidence(
    cls: type[CredibleSetConfidenceFeature],
    confidence_column: Column,
) -> Column:
    """Expression that assigns a score to the credible set confidence.

    Args:
        confidence_column (Column): Confidence column in the StudyLocus object

    Returns:
        Column: A confidence score between 0 and 1
    """
    return (
        f.when(
            f.col("confidence")
            == CredibleSetConfidenceClasses.FINEMAPPED_IN_SAMPLE_LD.value,
            f.lit(1.0),
        )
        .when(
            f.col("confidence")
            == CredibleSetConfidenceClasses.FINEMAPPED_OUT_OF_SAMPLE_LD.value,
            f.lit(0.75),
        )
        .when(
            f.col("confidence")
            == CredibleSetConfidenceClasses.PICSED_SUMMARY_STATS.value,
            f.lit(0.5),
        )
        .when(
            f.col("confidence")
            == CredibleSetConfidenceClasses.PICSED_TOP_HIT.value,
            f.lit(0.25),
        )
        .when(
            f.col("confidence") == CredibleSetConfidenceClasses.UNKNOWN.value,
            f.lit(0.0),
        )
    )

Common logic

gentropy.dataset.l2g_features.other.common_genecount_feature_logic(study_loci_to_annotate: StudyLocus | L2GGoldStandard, *, target_index: TargetIndex, feature_name: str, genomic_window: int, protein_coding_only: bool = False) -> DataFrame

Computes the feature.

Parameters:

Name Type Description Default
study_loci_to_annotate StudyLocus | L2GGoldStandard

The dataset containing study loci that will be used for annotation

required
target_index TargetIndex

Dataset containing information related to all genes in release.

required
feature_name str

The name of the feature

required
genomic_window int

The maximum window size to consider

required
protein_coding_only bool

Whether to only consider protein coding genes in calculation.

False

Returns:

Name Type Description
DataFrame DataFrame

Feature dataset

Source code in src/gentropy/dataset/l2g_features/other.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def common_genecount_feature_logic(
    study_loci_to_annotate: StudyLocus | L2GGoldStandard,
    *,
    target_index: TargetIndex,
    feature_name: str,
    genomic_window: int,
    protein_coding_only: bool = False,
) -> DataFrame:
    """Computes the feature.

    Args:
        study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci
            that will be used for annotation
        target_index (TargetIndex): Dataset containing information related to all genes in release.
        feature_name (str): The name of the feature
        genomic_window (int): The maximum window size to consider
        protein_coding_only (bool): Whether to only consider protein coding genes in calculation.

    Returns:
            DataFrame: Feature dataset
    """
    study_loci_window = (
        study_loci_to_annotate.df.withColumn(
            "window_start", f.col("position") - (genomic_window / 2)
        )
        .withColumn("window_end", f.col("position") + (genomic_window / 2))
        .withColumnRenamed("chromosome", "SL_chromosome")
    )
    target_index_filter = target_index.df

    if protein_coding_only:
        target_index_filter = target_index_filter.filter(
            f.col("biotype") == "protein_coding"
        )

    distinct_gene_counts = (
        study_loci_window.join(
            target_index_filter.alias("genes"),
            on=(
                (f.col("SL_chromosome") == f.col("genes.genomicLocation.chromosome"))
                & (f.col("genes.tss") >= f.col("window_start"))
                & (f.col("genes.tss") <= f.col("window_end"))
            ),
            how="inner",
        )
        .groupBy("studyLocusId")
        .agg(f.approx_count_distinct(f.col("id").alias("geneId")).alias(feature_name))
    )

    return (
        study_loci_window.join(
            target_index_filter.alias("genes"),
            on=(
                (f.col("SL_chromosome") == f.col("genes.genomicLocation.chromosome"))
                & (f.col("genes.tss") >= f.col("window_start"))
                & (f.col("genes.tss") <= f.col("window_end"))
            ),
            how="inner",
        )
        .join(distinct_gene_counts, on="studyLocusId", how="inner")
        .select("studyLocusId", f.col("id").alias("geneId"), feature_name)
        .distinct()
    )

gentropy.dataset.l2g_features.other.is_protein_coding_feature_logic(study_loci_to_annotate: StudyLocus | L2GGoldStandard, *, variant_index: VariantIndex, feature_name: str, genomic_window: int = 500000) -> DataFrame

Computes the feature to indicate if a gene is protein-coding or not.

Parameters:

Name Type Description Default
study_loci_to_annotate StudyLocus | L2GGoldStandard

The dataset containing study loci that will be used for annotation

required
variant_index VariantIndex

Dataset containing information related to all overlapping genes within a genomic window.

required
feature_name str

The name of the feature

required
genomic_window int

The window size around the locus to consider. Defaults to its maximum value: 500kb up and downstream the locus

500000

Returns:

Name Type Description
DataFrame DataFrame

Feature dataset, with 1 if the gene is protein-coding, 0 if not.

Source code in src/gentropy/dataset/l2g_features/other.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def is_protein_coding_feature_logic(
    study_loci_to_annotate: StudyLocus | L2GGoldStandard,
    *,
    variant_index: VariantIndex,
    feature_name: str,
    genomic_window: int = 500_000,
) -> DataFrame:
    """Computes the feature to indicate if a gene is protein-coding or not.

    Args:
        study_loci_to_annotate (StudyLocus | L2GGoldStandard): The dataset containing study loci
            that will be used for annotation
        variant_index (VariantIndex): Dataset containing information related to all overlapping genes within a genomic window.
        feature_name (str): The name of the feature
        genomic_window (int): The window size around the locus to consider. Defaults to its maximum value: 500kb up and downstream the locus

    Returns:
        DataFrame: Feature dataset, with 1 if the gene is protein-coding, 0 if not.
    """
    assert genomic_window <= 500_000, "Genomic window must be less than 500kb."
    genes_in_window = (
        variant_index.df.withColumn(
            "transcriptConsequence", f.explode("transcriptConsequences")
        )
        .select(
            "variantId",
            f.col("transcriptConsequence.targetId").alias("geneId"),
            f.col("transcriptConsequence.biotype").alias("biotype"),
            f.col("transcriptConsequence.distanceFromFootprint").alias(
                "distanceFromFootprint"
            ),
        )
        .filter(f.col("distanceFromFootprint") <= genomic_window)
    )
    if isinstance(study_loci_to_annotate, StudyLocus):
        variants_df = study_loci_to_annotate.df.select(
            f.explode_outer("locus.variantId").alias("variantId"),
            "studyLocusId",
        ).filter(f.col("variantId").isNotNull())
    elif isinstance(study_loci_to_annotate, L2GGoldStandard):
        variants_df = study_loci_to_annotate.df.select("studyLocusId", "variantId")
    return (
        # Annotate all genes in the window of a locus
        variants_df.join(
            genes_in_window,
            on="variantId",
        )
        # Apply flag across all variants in the locus
        .withColumn(
            feature_name,
            f.when(f.col("biotype") == "protein_coding", f.lit(1.0)).otherwise(
                f.lit(0.0)
            ),
        )
        .select("studyLocusId", "geneId", feature_name)
        .distinct()
    )