-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_script.py
More file actions
400 lines (348 loc) · 15.4 KB
/
base_script.py
File metadata and controls
400 lines (348 loc) · 15.4 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import json
import requests
import sys
from datetime import datetime
import os
import requests
from jinja2 import Template
from collections import defaultdict
from datetime import datetime
import csv
def load_supplementary_metadata():
"""Loads supplementary metadata from supplementary.json."""
supplementary_file = "supplementary.json"
if os.path.exists(supplementary_file):
try:
with open(supplementary_file, "r", encoding="utf-8") as file:
return json.load(file)
except json.JSONDecodeError:
sys.stderr.write("ERROR: Failed to parse supplementary.json. Using default values.\n")
return {}
return {}
SUPPLEMENTARY_METADATA = load_supplementary_metadata()
def get_crossref_metadata(doi, index, total):
print(f"({index + 1} / {total}) Gathering metadata for {doi}")
#Initialize with supplementary.json
metadata = SUPPLEMENTARY_METADATA.get(doi, {})
metadata = {
"title": metadata.get("title"),
"year": metadata.get("year"),
"journal": metadata.get("journal"),
"date": metadata.get("date"),
"first_author": metadata.get("first author"),
"doi_link": f"https://doi.org/{doi}",
}
#Query CrossRef to fill missing values
url = f"https://api.crossref.org/works/{doi}"
response = requests.get(url)
if response.status_code == 200:
data = response.json().get("message", {})
if metadata["title"] is None:
metadata["title"] = data.get("title", [None])[0]
if metadata["year"] is None:
year_data = data.get("published-print", data.get("published-online", {})).get("date-parts", [[None]])
metadata["year"] = year_data[0][0] if isinstance(year_data[0][0], int) else None
if metadata["journal"] is None:
container_titles = data.get("container-title", [])
metadata["journal"] = container_titles[0] if container_titles else None
if metadata["date"] is None:
raw_date = data.get("created", {}).get("date-time", None)
if raw_date:
try:
metadata["date"] = datetime.strptime(raw_date, "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d")
except ValueError:
pass
if metadata["first_author"] is None:
first_author_field = data.get("author", [{}])[0]
first = first_author_field.get("given") or ""
last = first_author_field.get("family") or ""
metadata["first_author"] = first + " " + last
else:
sys.stderr.write(f"WARNING: CrossRef failed to fetch metadata for {doi}\n")
#Fallback defaults
metadata = {k: (v if v else get_default_value(k)) for k, v in metadata.items()}
return metadata
def get_default_value(field):
"""Returns default values for missing metadata fields."""
defaults = {
"title": "No Title Available",
"year": 0,
"journal": "No Journal",
"date": "0000-00-00",
"first_author": "N/A",
}
return defaults.get(field, "N/A")
# Fetch bibliography from Poseidon
def fetch_poseidon_bibliography(archive_name):
print(f"Fetching DOI data from {archive_name}...")
url = f"http://server.poseidon-adna.org:3000/bibliography?archive={archive_name}"
response = requests.get(url)
if response.status_code == 200:
return response.json().get("serverResponse", {}).get("bibEntries", [])
return []
# Load all Poseidon bibliography data into a dictionary
def load_poseidon_doi_map():
archives = ["community-archive", "minotaur-archive", "aadr-archive"]
poseidon_doi_map = defaultdict(set)
for archive in archives:
entries = fetch_poseidon_bibliography(archive)
for entry in entries:
doi = entry.get("bibDoi")
if doi:
poseidon_doi_map[doi.lower()].add(archive)
return poseidon_doi_map
# Preprocess DOIs
def preprocess_doi(doi):
return doi.replace("https://doi.org/", "").strip().lower()
# Check for duplicate DOIs
def check_for_duplicates(dois):
seen = set()
unique_dois_data = []
duplicates = []
for entry in dois:
doi = entry["doi"]
if doi in seen:
duplicates.append(doi) # Store duplicate for logging
else:
seen.add(doi)
unique_dois_data.append(entry) # Keep only unique DOIs
if duplicates:
print("\n WARNING: Duplicate DOIs found and removed:")
for duplicate in duplicates:
print(f"- {duplicate}")
print("\nProceeding with a cleaned DOI list.\n")
return unique_dois_data # Return unique dois
# Generate docs/index.html
def generate_html(papers):
print("Updating docs/index.html...")
output_file = "docs/index.html"
csv_file = "docs/paper_directory.csv"
stylesheet_file = "docs/pico.classless.blue.min.css"
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paper Directory</title>
<link rel="stylesheet" href="{{ stylesheet_filename }}">
<style>
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px; border: 1px solid #ddd; text-align: left; }
</style>
<script>
function filterTable() {
let input = document.getElementById("searchInput").value.toLowerCase();
let communityFilter = document.getElementById("communityFilter").value;
let aadrFilter = document.getElementById("aadrFilter").value;
let minotaurFilter = document.getElementById("minotaurFilter").value;
let table = document.getElementById("paperTable");
let rows = table.getElementsByTagName("tr");
let nrRows = 0;
for (let i = 1; i < rows.length; i++) {
let titleCell = rows[i].getElementsByTagName("td")[1]; // Title column
let authorCell = rows[i].getElementsByTagName("td")[4]; // Author column
let communityCell = rows[i].getElementsByTagName("td")[6]; // Community Archive column
let aadrCell = rows[i].getElementsByTagName("td")[7]; // AADR Archive column
let minotaurCell = rows[i].getElementsByTagName("td")[8]; // Minotaur Archive column
if (titleCell && authorCell && communityCell && aadrCell && minotaurCell) {
let titleText = titleCell.textContent.toLowerCase();
let authorText = authorCell.textContent.toLowerCase();
let communityText = communityCell.textContent.trim();
let aadrText = aadrCell.textContent.trim();
let minotaurText = minotaurCell.textContent.trim();
// Apply search filter (title or author must match)
let matchesSearch = titleText.includes(input) || authorText.includes(input);
// Apply archive filters using logical AND
let matchesFilters =
(communityFilter === "all" || (communityFilter === "✔" && communityText === "✔") || (communityFilter === "✘" && communityText === "✘")) &&
(aadrFilter === "all" || (aadrFilter === "✔" && aadrText === "✔") || (aadrFilter === "✘" && aadrText === "✘")) &&
(minotaurFilter === "all" || (minotaurFilter === "✔" && minotaurText === "✔") || (minotaurFilter === "✘" && minotaurText === "✘"));
// Show or hide row based on both search and filter conditions
rows[i].style.display = (matchesSearch && matchesFilters) ? "" : "none";
nrRows += matchesSearch && matchesFilters;
}
let nrRowsSpan = document.getElementById("nrRows");
nrRowsSpan.innerText = nrRows;
}
}
function resetFilters() {
document.getElementById('searchInput').value = "";
document.getElementById('communityFilter').value = 'all';
document.getElementById('aadrFilter').value = 'all';
document.getElementById('minotaurFilter').value = 'all';
filterTable();
}
window.addEventListener('load', function() {
resetFilters();
});
</script>
</head>
<body>
<main>
<nav>
<ul><li><strong>Poseidon paper directory</strong></li></ul>
<ul>
<li><a href="paper_directory.csv">⬇ Download as .csv</a></li>
<li><a href="https://github.com/poseidon-framework/paper-directory">Edit this list</a></li>
<li><a href="https://www.poseidon-adna.org">Poseidon?</a></li>
</ul>
</nav>
<h1>aDNA Paper Directory</h1>
<p>A list of ancient DNA papers, and their availability in the Poseidon archives.</p>
<!-- bubble chart begin -->
<hr>
<h6>Diachronic packed circle chart</h6>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
#chart {
width: 100%;
height: 300px;
display: block;
margin-bottom: 25px;
border: 1px solid #ddd;
}
.tooltip {
position: absolute;
background: white;
border: 1px solid #13171f;
padding: 6px;
font-size: 12px;
pointer-events: none;
color: #13171f;
}
</style>
<select id="colorMode">
<option value="community_archive">🟠 in Community Archive</option>
<option value="aadr_archive">🟠 in AADR Archive</option>
<option value="minotaur_archive">🟠 in Minotaur Archive</option>
<option value="none">No archive highlighted</option>
</select>
<svg id="chart"></svg>
<div class="tooltip" style="opacity:0"></div>
<script src="chart.js"></script>
<!-- bubble chart end -->
<hr>
<h6>Searchable list</h6>
<div>
<details>
<summary role="button">Filter by archive</summary>
<label for="communityFilter">Community Archive:</label>
<select id="communityFilter" onchange="filterTable()">
<option value="all">All</option>
<option value="✔">✔</option>
<option value="✘">✘</option>
</select>
<label for="aadrFilter">AADR Archive:</label>
<select id="aadrFilter" onchange="filterTable()">
<option value="all">All</option>
<option value="✔">✔</option>
<option value="✘">✘</option>
</select>
<label for="minotaurFilter">Minotaur Archive:</label>
<select id="minotaurFilter" onchange="filterTable()">
<option value="all">All</option>
<option value="✔">✔</option>
<option value="✘">✘</option>
</select>
<button onclick="resetFilters()">Reset Filters</button>
</details>
<input type="text" id="searchInput" onkeyup="filterTable()" placeholder="Type to search by title or author...">
</div>
<p><span id="nrRows">?</span> papers selected</p>
<table id="paperTable" style="font-size: 0.7em;">
<tr>
<th>DOI</th>
<th>Title</th>
<th>Year</th>
<th>Journal</th>
<th>First Author</th>
<th>Publication Date</th>
<th>Community Archive</th>
<th>AADR Archive</th>
<th>Minotaur Archive</th>
<th><em data-tooltip="Human WGS aDNA samples. May be inaccurate." data-placement="left"># aDNA samples</em></th>
</tr>
{% for paper in papers %}
<tr>
<td><a href="{{ paper.doi_link }}" target="_blank">{{ paper.doi }}</a></td>
<td>{{ paper.title }}</td>
<td>{{ paper.year }}</td>
<td>{{ paper.journal }}</td>
<td>{{ paper.first_author }}</td>
<td>{{ paper.date }}</td>
<td>{{ '✔' if 'community-archive' in paper.archives else '✘' }}</td>
<td>{{ '✔' if 'aadr-archive' in paper.archives else '✘' }}</td>
<td>{{ '✔' if 'minotaur-archive' in paper.archives else '✘' }}</td>
<td>{{ paper.nr_adna_samples }}</td>
</tr>
{% endfor %}
</table>
<footer style="border-top: 1px solid; padding: 1em; border-color: #727B8A;">
<div style="float: right; font-size: 0.7em;">
Built with <a href="https://picocss.com">pico CSS</a>
</div>
</footer>
</main>
</body>
</html>
"""
template = Template(html_template)
rendered_html = template.render(
papers = papers,
csv_filename = os.path.basename(csv_file),
stylesheet_filename = os.path.basename(stylesheet_file)
)
with open(output_file, "w", encoding="utf-8") as file:
file.write(rendered_html)
print("docs/index.html successfully updated!")
# Save CSV file
with open(csv_file, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([
"doi", "title", "year", "journal",
"first_author", "publication_date",
"community_archive", "aadr_archive", "minotaur_archive",
"nr_adna_samples"
])
for paper in papers:
writer.writerow([
paper["doi"],
paper["title"],
paper["year"],
paper["journal"],
paper["first_author"],
paper["date"],
"community-archive" in paper["archives"],
"aadr-archive" in paper["archives"],
"minotaur-archive" in paper["archives"],
paper["nr_adna_samples"]
])
print(f"{csv_file} successfully created!")
# Main Execution
dois_data = []
with open("list.csv", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
doi = preprocess_doi(row["doi"])
nr_samples = row.get("nr_adna_samples", "").strip()
dois_data.append({"doi": doi, "nr_adna_samples": nr_samples})
print(f"Processing {len(dois_data)} DOIs...")
# Check for duplicates and get a unique list
unique_dois_data = check_for_duplicates(dois_data)
# Get CrossRef metadata
metadata_map = {entry["doi"]: get_crossref_metadata(entry["doi"], index, len(unique_dois_data))
for index, entry in enumerate(unique_dois_data)}
# Get Poseidon DOI availability
poseidon_doi_map = load_poseidon_doi_map()
# Create structured paper data
papers = [{
"doi": entry["doi"],
"nr_adna_samples": entry["nr_adna_samples"],
**metadata_map[entry["doi"]],
"archives": poseidon_doi_map.get(entry["doi"], set())
} for entry in unique_dois_data]
# Sort papers by publication date (YYYY-MM-DD)
papers.sort(key=lambda x: x["date"], reverse=True)
# Generate HTML report
generate_html(papers)