-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgenerate_pathogens_list.py
More file actions
348 lines (297 loc) · 11.2 KB
/
Copy pathgenerate_pathogens_list.py
File metadata and controls
348 lines (297 loc) · 11.2 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
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
341
342
343
344
345
346
347
348
from collections import defaultdict
import gspread
import pandas as pd
from gspread_dataframe import get_as_dataframe
from datetime import datetime
import json
import os
import re
# --- Configuration ---
CREDENTIALS_PATH = "credentials_gsheet.json"
SHEET_URL = "https://docs.google.com/spreadsheets/d/1nrG329whDaeVv8BpocWUuSc-lgv-TJVf6RIxW3Bd8jg/edit"
CSV_PATH = "data/complete_priority_pathogens.csv"
JSON_PATH = "data/complete_priority_pathogens.json"
PDN_JSON_PATH = "data/pdn_priority_pathogens.json"
def compute_score(entry):
"""Define a scoring function to compute the score of a pathogen based on its attributes.
The score is computed as follows:
- number_of_appearances_who_niaid_ecdc * 1
- number_of_priority_lists * 2
- WasteWater * 1
- Fungi * 3
- Parasite (Protozoa) * 4
The higher the score, the more priority the pathogen has.
The score is used to rank the pathogens in the final JSON output.
Parameters
----------
entry : dict
A dictionary representing a pathogen entry from the Google Sheet.
The dictionary contains the following keys:
- "number_of_appearances_who_niaid_ecdc"
- "number_of_priority_lists"
- "WasteWater"
- "Pathogen Type"
The values of these keys are used to compute the score.
Returns
-------
int
The score is an integer value that represents the priority of the pathogen.
"""
# Compute the score based on the attributes of the pathogen
appearances1 = entry.get("number_of_appearances_who_niaid_ecdc", 0)
appearances2 = entry.get("number_of_appearances_who_niaid_ecdc_africacdc", 0)
priority_types = entry.get("number_of_priority_lists", 0)
wastewater = 1 if str(entry.get("wastewater", "")).strip().lower() == "yes" else 0
base = appearances1 * 1 + appearances2 * 2 + priority_types * 1 + wastewater * 1
if entry.get("pathogen_type") == "Fungi":
base += 3
elif entry.get("pathogen_type") == "Parasite (Protozoa)":
base += 4
return base
def normalize_key(key):
"""Convert a string to snake_case and remove special characters."""
key = re.sub(r"[^\w\s]", "", key) # Remove punctuation
key = re.sub(r"\s+", "_", key.strip()) # Replace whitespace with underscore
return key.lower()
def load_pathogen_data(spreadsheet):
"""Load the pathogen data from the Google Sheet and return it as a DataFrame.
Parameters
----------
spreadsheet
gspread.Spreadsheet
The gspread.Spreadsheet object representing the Google Sheet.
Returns
-------
pd.DataFrame
A pandas DataFrame containing the pathogen data.
The DataFrame contains the following columns:
- "Pathogen"
- "Pathogen Type"
- "Taxids"
- "Priority type"
- "number_of_priority_lists"
- "number_of_appearances_who_niaid_ecdc"
- "Number of appearances (WHO, NIAID, ECDC, AFRICACDC)"
- etc.
"""
ws = spreadsheet.worksheet("pathogens")
data = ws.get_all_values()
df = pd.DataFrame(data[1:], columns=data[0])
df = df.dropna(how="all")
if "taxids" in df.columns:
df["taxids"] = pd.to_numeric(df["taxids"], errors="coerce").astype("Int64")
return df
def load_organizations(spreadsheet):
"""Load the organizations data from the Google Sheet and return it as a DataFrame.
Parameters
----------
spreadsheet
gspread.Spreadsheet
The gspread.Spreadsheet object representing the Google Sheet.
Returns
-------
pd.DataFrame
A pandas DataFrame containing the organizations data.
The DataFrame contains the following columns:
- "Acronym"
- "url list"
- "Added?"
"""
ws = spreadsheet.worksheet("organizations")
df = get_as_dataframe(ws).dropna(how="all")
return df[df["Added?"] == 1]
def build_source_urls(active_orgs):
"""Build a dictionary of source URLs from the active organizations DataFrame.
Parameters
----------
active_orgs
pd.DataFrame
A pandas DataFrame containing the organizations data.
The DataFrame contains the following columns:
- "Acronym"
- "url list"
- "Added?"
Returns
-------
dict
A dictionary where the keys are the acronyms of the organizations
and the values are lists of URLs associated with each acronym.
The URLs are extracted from the "url list" column of the DataFrame.
Each URL is stripped of leading and trailing whitespace.
"""
urls = defaultdict(list)
for _, row in active_orgs.iterrows():
acronym = row["Acronym"]
url_list = str(row["url list"]).strip()
if pd.notna(acronym) and url_list:
urls[acronym].extend(u.strip() for u in url_list.split("\n") if u.strip())
return dict(urls)
def transform_pathogen_row(row, priority_sources):
"""Transform a row of the pathogen DataFrame into a dictionary entry.
Parameters
----------
row
pd.Series
A pandas Series representing a row of the pathogen DataFrame.
priority_sources
list
A list of acronyms representing the priority sources.
The acronyms are used to identify the organizations that prioritized the pathogen.
Returns
-------
dict
A dictionary representing the pathogen entry.
The dictionary contains the following keys:
- "Pathogen"
- "Pathogen Type"
- "Taxids"
- "Priority type"
- "number_of_priority_lists"
- "number_of_appearances_who_niaid_ecdc"
- "Number of appearances (WHO, NIAID, ECDC, AFRICACDC)"
- etc.
"""
entry = {}
prioritized_by = [
source
for source in priority_sources
if str(row.get(source)).strip().upper() == "X"
]
for col in row.index:
if col in priority_sources:
continue
val = row[col]
if col == "Priority type" and pd.notna(val):
entry[normalize_key(col)] = [x.strip() for x in str(val).split(",")]
elif (
col
not in [
"Numer of priority lists",
"Number of appearances (WHO, NIAD, ECDC)",
"Number of appearances (WHO, NIAD, ECDC, AFRICACDC)",
"Priority order",
"Priority score",
]
and pd.notna(val)
and str(val).strip()
):
entry[normalize_key(col)] = val
entry["prioritized_by"] = prioritized_by
entry["number_of_priority_lists"] = sum(
1
for p in entry.get("priority_type", [])
if p.strip().lower() != "animal disease"
)
entry["number_of_appearances_who_niaid_ecdc"] = sum(
1 for x in ["WHO", "NIAID", "ECDC"] if x in prioritized_by
)
entry["number_of_appearances_who_niaid_ecdc_africacdc"] = sum(
1 for x in ["WHO", "NIAID", "ECDC", "AFRICACDC"] if x in prioritized_by
)
return entry
def apply_inclusion_criteria(entry):
"""Apply inclusion criteria to determine if a pathogen entry should be included in the final JSON output.
Parameters
----------
entry
dict
A dictionary representing a pathogen entry from the Google Sheet.
The dictionary contains the following keys:
- "prioritized_by"
- "number_of_priority_lists"
- "Pathogen Type"
Returns
-------
bool
True if the entry meets the inclusion criteria, False otherwise.
The inclusion criteria are as follows:
- The pathogen must be prioritized by at least 2 organizations (WHO, NIAID, ECDC)
- The pathogen must be listed in at least 2 priority lists
"""
n_appearances = entry.get("number_of_appearances_who_niaid_ecdc", 0)
n_priority_types = entry.get("number_of_priority_lists", 0)
return n_appearances >= 2 and n_priority_types >= 2
def add_scoring(entry):
"""Add scoring to the pathogen entry based on its attributes.
Parameters
----------
entry
dict
A dictionary representing a pathogen entry from the Google Sheet.
The dictionary contains the following keys:
- "number_of_appearances_who_niaid_ecdc"
- "number_of_priority_lists"
- "WasteWater"
- "Pathogen Type"
Returns
-------
dict
The input dictionary with additional keys:
- "Priority score (computed)"
- "Highest priority"
The "Priority score (computed)" key contains the computed score of the pathogen.
The "Highest priority" key is a boolean indicating if the score is greater than or equal to 12.
"""
score = compute_score(entry)
entry["priority_score"] = score
entry["highest_priority"] = score >= 12
return entry
def assign_priority_order(taxa, score_field="Priority score (computed)"):
"""Assign a priority order based on the score field.
Parameters
----------
taxa : list of dict
The list of pathogen entries.
score_field : str
The key to use for sorting entries (default: Priority score (computed)).
Returns
-------
list of dict
The same list, with each entry updated with a new key: Priority order (computed).
"""
sorted_taxa = sorted(taxa, key=lambda x: x.get(score_field, 0), reverse=True)
for i, entry in enumerate(sorted_taxa, start=1):
entry["priority_order"] = i
return sorted_taxa
def main():
gc = gspread.service_account(filename=CREDENTIALS_PATH)
spreadsheet = gc.open_by_url(SHEET_URL)
patho_df = load_pathogen_data(spreadsheet)
active_orgs = load_organizations(spreadsheet)
priority_sources = active_orgs["Acronym"].dropna().drop_duplicates().tolist()
source_urls = build_source_urls(active_orgs)
complete_taxa = []
for _, row in patho_df.iterrows():
entry = transform_pathogen_row(row, priority_sources)
entry = add_scoring(entry)
complete_taxa.append(entry)
complete_json = {
"full_name": "Complete Pathogen Priority List",
"source_urls": source_urls,
"last_updated": datetime.now().strftime("%Y-%m-%d"),
"expected_number_of_taxa": len(complete_taxa),
"taxa": complete_taxa,
}
# Write complete list
os.makedirs(os.path.dirname(CSV_PATH), exist_ok=True)
patho_df.to_csv(CSV_PATH, index=False)
complete_json["taxa"] = assign_priority_order(complete_json["taxa"])
with open(JSON_PATH, "w", encoding="utf-8") as f:
json.dump(complete_json, f, indent=2, ensure_ascii=False)
# Apply inclusion criteria
pdn_taxa = [e for e in complete_json["taxa"] if apply_inclusion_criteria(e)]
pdn_taxa = assign_priority_order(pdn_taxa)
pdn_json = {
"full_name": "PDN Pathogen Priority List",
"source_urls": source_urls,
"last_updated": datetime.now().strftime("%Y-%m-%d"),
"expected_number_of_taxa": len(pdn_taxa),
"taxa": pdn_taxa,
}
with open(PDN_JSON_PATH, "w", encoding="utf-8") as f:
json.dump(pdn_json, f, indent=2, ensure_ascii=False)
print(f"✅ CSV saved to {CSV_PATH}")
print(f"✅ JSON saved to {JSON_PATH}")
print(f"✅ PDN JSON saved to {PDN_JSON_PATH}")
if __name__ == "__main__":
main()