-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecompute-results.py
More file actions
177 lines (143 loc) · 6.37 KB
/
precompute-results.py
File metadata and controls
177 lines (143 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
83
84
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
142
143
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
import os
import polars as pl
import functools
import json
DATA_PATH = os.environ.get("BATCH_QUERY_DATA_PATH", "./batch_query_service_data_parquet")
def get_value_from_df(df, dfKey, unwrap_value=True):
val = df[dfKey].unique().to_list()
if len(val) == 1 and unwrap_value:
val = val[0]
return val
def get_filtered_list(df, alleleSymbol, dfColumn, significant_data=True):
filteredDf = df.filter((pl.col("alleleSymbol") == alleleSymbol) & (
pl.col("significant") == significant_data))
list_with_data = filteredDf[dfColumn].unique().to_list()
return list(filter(None, list_with_data))
def get_filtered_significant_systems(df, alleleSymbol, significant_data=True):
filteredDf = df.filter((pl.col("alleleSymbol") ==
alleleSymbol) & (pl.col("significant") == significant_data))
filteredDf = filteredDf.with_columns(
systemNames=pl.concat_list("topLevelPhenotypeNames")
)
return list(dict.fromkeys(sum(filter(None, filteredDf["systemNames"].to_list()), [])))
def get_phenotype_names(phenotype_list):
return phenotype_list.map_elements(lambda p: p["name"], return_dtype=pl.Utf8)
def ensemble_allele_result(allele, df):
significantLifeStages = get_filtered_list(df, allele, "lifeStageName")
significantPhenotypes = get_filtered_list(
df, allele, "significantPhenotypeName")
notSignificantPhenotypes = get_filtered_list(
df, allele, "significantPhenotypeName", False)
significantSystems = get_filtered_significant_systems(df, allele)
notSignificantSystems = get_filtered_significant_systems(df, allele, False)
significantLifeStages.sort()
significantPhenotypes.sort()
significantSystems.sort()
notSignificantPhenotypes.sort()
notSignificantSystems.sort()
alleleData = {
"allele": allele,
"significantLifeStages": significantLifeStages,
"significantPhenotypes": significantPhenotypes,
"notSignificantPhenotypes": notSignificantPhenotypes,
"significantSystems": significantSystems,
"notSignificantSystems": notSignificantSystems
}
return alleleData
def get_all_significant_phenotypes(df):
filteredDf = df.filter(pl.col("significant") == True)
list_with_data = filteredDf["significantPhenotypeName"].unique().to_list()
return list(filter(None, list_with_data))
def get_all_significant_systems(df):
filteredDf = df.filter(pl.col("significant") == True)
filteredDf = filteredDf.with_columns(
systemNames=pl.concat_list("topLevelPhenotypeNames")
)
return list(dict.fromkeys(sum(filter(None, filteredDf["systemNames"].to_list()), [])))
def ensemble_gene_result(df):
human_gene_symbols = get_value_from_df(df, "humanGeneSymbol")
human_gene_ids = get_value_from_df(df, "hgncGeneAccessionId")
gene_id = get_value_from_df(df, "id")
alleles = get_value_from_df(df, "alleleSymbol", unwrap_value=False)
mouse_gene_symbol = df["alleleSymbol"].to_list()[0].split("<")[0]
all_significant_systems = get_all_significant_systems(df)
all_significant_phenotypes = get_all_significant_phenotypes(df)
alelles_data = list(map(functools.partial(
ensemble_allele_result, df=df), alleles))
geneData = {
"mouseGeneSymbol": mouse_gene_symbol,
"allSignificantSystems": all_significant_systems,
"allSignificantPhenotypes": all_significant_phenotypes,
"humanGeneSymbols": human_gene_symbols,
"humanGeneIds": human_gene_ids,
"geneId": gene_id,
"alleles": alelles_data,
}
return geneData
def process_gene(full_dataset, mgi_id):
gene_data = full_dataset.filter(pl.col("mgiGeneAccessionId").eq(mgi_id))
df = gene_data.drop(["statisticalResultId", "statisticalResultId", "alleleName",
"dataType", "effectSize", "femaleMutantCount", "maleMutantCount", "metadataGroup", "pValue", "parameterName",
"parameterStableId", "phenotypeSexes", "phenotypingCentre", "pipelineStableId", "procedureMinAnimals",
"procedureMinFemales", "procedureMinMales", "procedureName", "procedureStableId", "projectName",
"statisticalMethod", "status", "zygosity", "intermediatePhenotypes", "potentialPhenotypes", "humanPhenotypes",
"alleleAccessionId"
])
df = df.with_columns(
displayPhenotypeName=pl.when(
pl.col("displayPhenotype").is_not_null()
)
.then(
pl.col("displayPhenotype").map_elements(
lambda phenotype: phenotype["name"], return_dtype=pl.Utf8)
),
displayPhenotypeId=pl.when(
pl.col("displayPhenotype").is_not_null()
)
.then(
pl.col("displayPhenotype").map_elements(
lambda phenotype: phenotype["id"], return_dtype=pl.Utf8)
),
significantPhenotypeName=pl.when(
pl.col("significantPhenotype").is_not_null()
)
.then(
pl.col("significantPhenotype").map_elements(
lambda phenotype: phenotype["name"], return_dtype=pl.Utf8)
),
significantPhenotypeId=pl.when(
pl.col("significantPhenotype").is_not_null()
)
.then(
pl.col("significantPhenotype").map_elements(
lambda phenotype: phenotype["id"], return_dtype=pl.Utf8)
),
topLevelPhenotypeNames=pl.when(
pl.col("topLevelPhenotypes").is_not_null()
)
.then(
pl.col("topLevelPhenotypes")
.map_elements(get_phenotype_names, return_dtype=pl.List(pl.Utf8))
)
)
df = df.drop(
["displayPhenotype", "topLevelPhenotypes", "significantPhenotype"])
df = df.unique()
df = df.with_row_index()
return ensemble_gene_result(df)
def create_dataset():
return pl.read_parquet(f"{DATA_PATH}/*.parquet")
def main():
full_dataset = create_dataset()
mgi_ids = full_dataset["mgiGeneAccessionId"].unique().to_list()
mgi_ids = list(filter(None, mgi_ids))
ids_count = len(mgi_ids)
print(f"total of {ids_count} genes")
full_results = {}
for index, mgi_id in enumerate(mgi_ids):
full_results[mgi_id] = process_gene(full_dataset, mgi_id)
with open("pre-computed-results.json", "w") as outfile:
print("\nDONE")
json.dump(full_results, outfile)
if __name__ == "__main__":
main()