163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
223
224
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
263
264
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 | class StudyLocusFactory(StudyLocus):
"""Feature extraction in study locus."""
@staticmethod
def _get_tss_distance_features(credible_set: StudyLocus, v2g: V2G) -> L2GFeature:
"""Joins StudyLocus with the V2G to extract a score that is based on the distance to a gene TSS of any variant weighted by its posterior probability in a credible set.
Args:
credible_set (StudyLocus): Credible set dataset
v2g (V2G): Dataframe containing the distances of all variants to all genes TSS within a region
Returns:
L2GFeature: Stores the features with the score of weighting the distance to the TSS by the posterior probability of the variant
"""
wide_df = (
credible_set.filter_credible_set(CredibleInterval.IS95)
.df.withColumn("variantInLocus", f.explode_outer("locus"))
.select(
"studyLocusId",
"variantId",
f.col("variantInLocus.variantId").alias("variantInLocusId"),
f.col("variantInLocus.posteriorProbability").alias(
"variantInLocusPosteriorProbability"
),
)
.join(
v2g.df.filter(f.col("datasourceId") == "canonical_tss").selectExpr(
"variantId as variantInLocusId", "geneId", "score"
),
on="variantInLocusId",
how="inner",
)
.withColumn(
"weightedScore",
f.col("score") * f.col("variantInLocusPosteriorProbability"),
)
.groupBy("studyLocusId", "geneId")
.agg(
f.min("weightedScore").alias("distanceTssMinimum"),
f.mean("weightedScore").alias("distanceTssMean"),
)
)
return L2GFeature(
_df=convert_from_wide_to_long(
wide_df,
id_vars=("studyLocusId", "geneId"),
var_name="featureName",
value_name="featureValue",
),
_schema=L2GFeature.get_schema(),
)
@staticmethod
def _get_vep_features(
credible_set: StudyLocus,
v2g: V2G,
) -> L2GFeature:
"""Get the maximum VEP score for all variants in a locus's 95% credible set.
This informs about functional impact of the variants in the locus. For more information on variant consequences, see: https://www.ensembl.org/info/genome/variation/prediction/predicted_data.html
Two metrics: max VEP score per study locus and gene, and max VEP score per study locus.
Args:
credible_set (StudyLocus): Study locus dataset with the associations to be annotated
v2g (V2G): V2G dataset with the variant/gene relationships and their consequences
Returns:
L2GFeature: Stores the features with the max VEP score.
"""
def _aggregate_vep_feature(
df: DataFrame,
aggregation_expr: Column,
aggregation_cols: list[str],
feature_name: str,
) -> DataFrame:
"""Extracts the maximum or average VEP score after grouping by the given columns. Different aggregations return different predictive annotations.
If the group_cols include "geneId", the maximum/mean VEP score per gene is returned.
Otherwise, the maximum/mean VEP score for all genes in the neighborhood of the locus is returned.
Args:
df (DataFrame): DataFrame with the VEP scores for each variant in a studyLocus
aggregation_expr (Column): Aggregation expression to apply
aggregation_cols (list[str]): Columns to group by
feature_name (str): Name of the feature to be returned
Returns:
DataFrame: DataFrame with the maximum VEP score per locus or per locus/gene
"""
if "geneId" in aggregation_cols:
return df.groupBy(aggregation_cols).agg(
aggregation_expr.alias(feature_name)
)
return (
df.groupBy(aggregation_cols)
.agg(
aggregation_expr.alias(feature_name),
f.collect_set("geneId").alias("geneId"),
)
.withColumn("geneId", f.explode("geneId"))
)
credible_set_w_variant_consequences = (
credible_set.filter_credible_set(CredibleInterval.IS95)
.df.withColumn("variantInLocus", f.explode_outer("locus"))
.select(
f.col("studyLocusId"),
f.col("variantId"),
f.col("studyId"),
f.col("variantInLocus.variantId").alias("variantInLocusId"),
f.col("variantInLocus.posteriorProbability").alias(
"variantInLocusPosteriorProbability"
),
)
.join(
# Join with V2G to get variant consequences
v2g.df.filter(f.col("datasourceId") == "variantConsequence").selectExpr(
"variantId as variantInLocusId", "geneId", "score"
),
on="variantInLocusId",
)
.select(
"studyLocusId",
"variantId",
"studyId",
"geneId",
(f.col("score") * f.col("variantInLocusPosteriorProbability")).alias(
"weightedScore"
),
)
.distinct()
)
return L2GFeature(
_df=convert_from_wide_to_long(
reduce(
lambda x, y: x.unionByName(y, allowMissingColumns=True),
[
# Calculate overall max VEP score for all genes in the vicinity
credible_set_w_variant_consequences.transform(
_aggregate_vep_feature,
f.max("weightedScore"),
["studyLocusId"],
"vepMaximumNeighborhood",
),
# Calculate overall max VEP score per gene
credible_set_w_variant_consequences.transform(
_aggregate_vep_feature,
f.max("weightedScore"),
["studyLocusId", "geneId"],
"vepMaximum",
),
# Calculate mean VEP score for all genes in the vicinity
credible_set_w_variant_consequences.transform(
_aggregate_vep_feature,
f.mean("weightedScore"),
["studyLocusId"],
"vepMeanNeighborhood",
),
# Calculate mean VEP score per gene
credible_set_w_variant_consequences.transform(
_aggregate_vep_feature,
f.mean("weightedScore"),
["studyLocusId", "geneId"],
"vepMean",
),
],
),
id_vars=("studyLocusId", "geneId"),
var_name="featureName",
value_name="featureValue",
).filter(f.col("featureValue").isNotNull()),
_schema=L2GFeature.get_schema(),
)
|