This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_nx.py
143 lines (115 loc) · 4.33 KB
/
analysis_nx.py
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
import numpy as np
import pandas
import analysis as lan
from collections import namedtuple
PathwayConfig = namedtuple("PathwayConfig", ["measure", "hierarchy"])
def retrieve_mutations(pid, seq_data):
patient_data = seq_data[
(seq_data["PatientFirstName"] == pid)
& (seq_data["Technology"] == "NGS Q3")
# We only care about variants and pathogenic mutations
& (seq_data["TestResult"].isin(["variantdetected", "Mutated, Pathogenic"]))
]
patient_data = patient_data[["Biomarker", "NGS_PercentMutated"]]
return patient_data
def util_unweight(g):
nodes = g.nodes()
return {node: 1 for node in nodes}
def calculate_patient_mutations_with_f(pid, seq_data, pathways, f, factor_famcom=False):
patient_data = seq_data[
(seq_data["PatientFirstName"] == pid)
& (seq_data["Technology"] == "NGS Q3")
# We only care about variants and pathogenic mutations
& (seq_data["TestResult"].isin(["variantdetected", "Mutated, Pathogenic"]))
]
patient_data = patient_data[["Biomarker", "NGS_PercentMutated"]]
# Realistically, should never happen
if patient_data.empty:
return {}
results = {}
for pw in pathways:
pathway_mutations = patient_data[patient_data["Biomarker"].isin(pw.get_genes())]
if pathway_mutations.empty:
results[pw.name] = np.float64(0.0)
continue
weights = pw.calculate_measure(f, factor_famcom)
patient_mutations = pathway_mutations.groupby("Biomarker").max()[
"NGS_PercentMutated"
]
total_weights = weights.sum()
if total_weights != 0:
perc_mutation = (
weights.mul(patient_mutations, fill_value=np.float64(0.0)).sum()
/ total_weights
)
else:
perc_mutation = 0
results[pw.name] = perc_mutation
return results
def calculate_patient_mutations_with_config(
pid, seq_data, pathways, legacy_pathways, config
):
patient_data = seq_data[
(seq_data["PatientFirstName"] == pid)
& (seq_data["Technology"] == "NGS Q3")
# We only care about variants and pathogenic mutations
& (seq_data["TestResult"].isin(["variantdetected", "Mutated, Pathogenic"]))
]
patient_data = patient_data[["Biomarker", "NGS_PercentMutated"]]
# Realistically, should never happen
if patient_data.empty:
return {}
results = {}
legacy_to_compute = []
for pw in pathways:
pathway_mutations = patient_data[patient_data["Biomarker"].isin(pw.get_genes())]
if pathway_mutations.empty:
results[pw.name] = np.float64(0.0)
continue
if config[pw.name].measure == "baseline":
legacy_to_compute.append(pw.name)
continue
weights = pw.calculate_measure(
config[pw.name].measure, config[pw.name].hierarchy
)
patient_mutations = pathway_mutations.groupby("Biomarker").max()[
"NGS_PercentMutated"
]
total_weights = weights.sum()
if total_weights != 0:
perc_mutation = (
weights.mul(patient_mutations, fill_value=np.float64(0.0)).sum()
/ total_weights
)
else:
perc_mutation = 0
results[pw.name] = perc_mutation
legacy_results = lan.calculate_patient_mutations(
pid, seq_data, [pw for pw in legacy_pathways if pw[0] in legacy_to_compute]
)
results = {**results, **legacy_results}
return results
def process_patients_with_f(patients, f, pathways, mutations_data, complexes=False):
results = {}
for patient in patients:
results[patient] = calculate_patient_mutations_with_f(
patient, mutations_data, pathways, f, complexes
)
return (
pandas.DataFrame.from_dict(results, orient="index")
.rename_axis("PatientFirstName")
.reset_index()
)
def process_patients_with_config(
patients, pathways, legacy_pathways, mutations_data, config
):
results = {}
for patient in patients:
results[patient] = calculate_patient_mutations_with_config(
patient, mutations_data, pathways, legacy_pathways, config
)
return (
pandas.DataFrame.from_dict(results, orient="index")
.rename_axis("PatientFirstName")
.reset_index()
)