-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpub-tool.py
More file actions
1220 lines (1067 loc) · 42.9 KB
/
pub-tool.py
File metadata and controls
1220 lines (1067 loc) · 42.9 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# build_pubmed_timeline_single_html.py
# Produces ONE self-contained HTML with:
# - Plotly interactive timeline (no graph title)
# - Yearly publication title tables INSIDE the timeline (annotations)
# - Horizontal tag bar (click to filter)
# - "Show all" / "Hide all" buttons
# - Fully offline HTML
# - Citation-only (gray dotted) bubbles connected by red dashed arrows
import requests
import plotly.graph_objects as go
import plotly.io as pio
import random
from datetime import datetime, timedelta
from collections import defaultdict, Counter
import re
import json
import xml.etree.ElementTree as ET
import textwrap
import os
import time
import argparse
# Load API key from .env
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("API_KEY")
if not API_KEY:
raise ValueError("API_KEY not found in environment. Please set it in .env or your system environment variables.")
# ---------------- CLI ARGUMENTS ----------------
parser = argparse.ArgumentParser(
description="Build a PubMed timeline HTML visualization for a search term."
)
parser.add_argument(
"term",
metavar="TERM",
type=str,
help="PubMed search term (required, e.g. 'hydra vulgaris')"
)
parser.add_argument(
"--retmax",
type=int,
default=30,
help="Maximum number of PubMed results to fetch (default: 30)"
)
args = parser.parse_args()
TERM = args.term
RETMAX = args.retmax
OUTPUT_HTML = "timeline_pubmed_ranked.html"
# ------------------------------------------------
# Layout tuning
SCALE_FACTOR = 5
MARGIN = 0.2
MIN_BUBBLE_SIZE = 26
AVG_CHARS_PER_YEAR = 125
TABLE_Y_OFFSET = -80
BORDER_COLOR = "transparent" #rgba(0,0,0,0.2)
# ----------------------------------------
CACHE_FILE = "citations_cache.json"
SUMMARY_CACHE_FILE = "summaries_cache.json"
XML_CACHE_FILE = "xml_cache.json"
def load_xml_cache():
"""Load cached XML records per PMID."""
if os.path.exists(XML_CACHE_FILE):
try:
with open(XML_CACHE_FILE, "r", encoding="utf-8") as f:
cache = json.load(f)
print(f" Loaded {len(cache)} cached XML records from {XML_CACHE_FILE}")
return cache
except Exception as e:
print(f" Failed to load XML cache: {e}")
return {}
def save_xml_cache(cache):
"""Save cached XML records."""
try:
with open(XML_CACHE_FILE, "w", encoding="utf-8") as f:
json.dump(cache, f, ensure_ascii=False, indent=2)
print(f" Saved {len(cache)} cached XML records to {XML_CACHE_FILE}")
except Exception as e:
print(f" Failed to save XML cache: {e}")
def load_summary_cache():
"""Load cached PubMed summaries."""
if os.path.exists(SUMMARY_CACHE_FILE):
try:
with open(SUMMARY_CACHE_FILE, "r", encoding="utf-8") as f:
cache = json.load(f)
print(f" Loaded {len(cache)} cached summaries from {SUMMARY_CACHE_FILE}")
return cache
except Exception as e:
print(f" Failed to load summary cache: {e}")
return {}
def save_summary_cache(cache):
"""Save cached PubMed summaries."""
try:
with open(SUMMARY_CACHE_FILE, "w", encoding="utf-8") as f:
json.dump(cache, f, ensure_ascii=False, indent=2)
print(f" Saved {len(cache)} cached summaries to {SUMMARY_CACHE_FILE}")
except Exception as e:
print(f" Failed to save summary cache: {e}")
def load_cache():
"""Load cached citation results from disk."""
if os.path.exists(CACHE_FILE):
try:
with open(CACHE_FILE, "r", encoding="utf-8") as f:
cache = json.load(f)
print(f" Loaded {len(cache)} cached citation entries from {CACHE_FILE}")
return cache
except Exception as e:
print(f" Failed to load cache: {e}")
return {}
def save_cache(cache):
"""Save citation cache to disk."""
try:
with open(CACHE_FILE, "w", encoding="utf-8") as f:
json.dump(cache, f, ensure_ascii=False, indent=2)
print(f" Saved {len(cache)} cached citation entries to {CACHE_FILE}")
except Exception as e:
print(f" Failed to save cache: {e}")
def fetch_elink(pmids, linkname):
"""Generic PubMed ELink fetcher."""
if not pmids:
return []
url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi"
params = {
"dbfrom": "pubmed",
"linkname": linkname,
"id": ",".join(pmids),
"api_key": API_KEY,
"retmode": "json",
}
r = requests.get(url, params=params, timeout=60)
r.raise_for_status()
data = r.json()
links = []
for ls in data.get("linksets", []):
for db in ls.get("linksetdbs", []):
if db.get("linkname") == linkname:
for l in db.get("links", []):
links.append(l["id"] if isinstance(l, dict) else l)
return links
def fetch_citations(pmid):
"""Return PMIDs that this PubMed article cites."""
refs = fetch_elink([pmid], "pubmed_pubmed_refs")
return refs
def parse_pubmed_date(info):
spd = (info.get("sortpubdate") or "").strip()
if spd:
try:
date_part = spd.split()[0]
ymd = [int(x) for x in date_part.split("/")]
y = ymd[0]
m = ymd[1] if len(ymd) >= 2 else 1
return datetime(y, m, 1)
except Exception:
pass
for key in ("epubdate", "pubdate"):
raw = info.get(key) or ""
if not raw:
continue
m = re.match(r"(\d{4})(?:/(\d{1,2}))?", raw)
if m:
y = int(m.group(1))
mo = int(m.group(2)) if m.group(2) else 1
return datetime(y, mo, 1)
m = re.search(r"(\d{4})", " ".join(info.values()))
if m:
return datetime(int(m.group(1)), 1, 1)
return datetime(1900, 1, 1)
def wrap_text(text, width=AVG_CHARS_PER_YEAR):
"""
Wrap visible text to a given width, ignoring HTML tags like <a href=...>.
Ensures <br> is only inserted between complete tokens, never inside tags.
"""
import re
# Split text into link and non-link parts
parts = re.split(r'(<a .*?>.*?</a>)', text)
lines = []
current_line = ""
visible_len = 0
for part in parts:
if part.startswith("<a "): # keep link as a single token
token = part
token_len = len(re.sub(r'<.*?>', '', token)) # visible text length only
else:
# Split non-HTML text into words
words = part.split()
for w in words:
w_len = len(w)
if visible_len + w_len + 1 > width:
lines.append(current_line)
current_line = w
visible_len = w_len
else:
if current_line:
current_line += " " + w
visible_len += w_len + 1
else:
current_line = w
visible_len = w_len
continue
# Handle full <a> block (count visible chars but don’t break inside)
if visible_len + token_len + 1 > width:
lines.append(current_line)
current_line = token
visible_len = token_len
else:
if current_line:
current_line += " " + token
visible_len += token_len + 1
else:
current_line = token
visible_len = token_len
if current_line:
lines.append(current_line)
return "<br>".join(lines)
def fetch_pubmed_ids(term, retmax):
esearch_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
params = {"db": "pubmed", "term": term, "retmax": retmax, "retmode": "json", "api_key": API_KEY}
r = requests.get(esearch_url, params=params, timeout=60)
r.raise_for_status()
return r.json()["esearchresult"]["idlist"]
def fetch_pubmed_summaries(ids, batch_size=100, cache=None):
"""Fetch PubMed summaries in batches, caching per PMID."""
if not ids:
return {}
all_results = {}
esummary_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
cached_ids = set(cache.keys()) if cache else set()
to_fetch = [pid for pid in ids if pid not in cached_ids]
print(f" {len(ids)} total IDs, {len(to_fetch)} to fetch, {len(cached_ids)} from cache.")
# Add cached ones directly
if cache:
for pid in ids:
if pid in cache:
all_results[pid] = cache[pid]
# Fetch remaining batches
for i in range(0, len(to_fetch), batch_size):
batch = to_fetch[i:i + batch_size]
params = {
"db": "pubmed",
"id": ",".join(batch),
"retmode": "json",
"api_key": API_KEY
}
try:
r = requests.get(esummary_url, params=params, timeout=120)
r.raise_for_status()
result = r.json().get("result", {})
result.pop("uids", None)
for pid, val in result.items():
all_results[pid] = val
if cache is not None:
cache[pid] = val
print(f"Fetched summaries for batch {i//batch_size + 1} ({len(batch)} PMIDs).")
except Exception as e:
print(f"Failed to fetch summaries for batch {i//batch_size + 1}: {e}")
continue
return all_results
def fetch_pubmed_xml(ids, batch_size=100, cache=None):
"""
Fetch PubMed XML data in batches and cache per PMID.
Cached XMLs are stored in JSON (PMID -> <PubmedArticle>...</PubmedArticle>).
"""
if not ids:
return "<PubmedArticleSet/>"
efetch_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
xml_parts = []
cached_ids = set(cache.keys()) if cache else set()
to_fetch = [pid for pid in ids if pid not in cached_ids]
print(f" {len(ids)} total IDs, {len(to_fetch)} to fetch, {len(cached_ids)} from cache.")
# Include already cached XMLs
for pid in ids:
if pid in cached_ids:
xml_parts.append(cache[pid])
# Fetch remaining XMLs
for i in range(0, len(to_fetch), batch_size):
batch = to_fetch[i:i + batch_size]
params = {
"db": "pubmed",
"id": ",".join(batch),
"retmode": "xml",
"api_key": API_KEY
}
try:
r = requests.get(efetch_url, params=params, timeout=180)
r.raise_for_status()
xml_text = r.text
# Split into individual <PubmedArticle> entries
articles = re.findall(r"<PubmedArticle>.*?</PubmedArticle>", xml_text, re.DOTALL)
for article_xml in articles:
pmid_match = re.search(r"<PMID.*?>(\d+)</PMID>", article_xml)
if pmid_match:
pmid = pmid_match.group(1)
cache[pmid] = article_xml
xml_parts.append(article_xml)
print(f"Fetched XML batch {i//batch_size + 1} ({len(batch)} PMIDs).")
time.sleep(0.3) # polite delay
except Exception as e:
print(f" Failed to fetch XML batch {i//batch_size + 1}: {e}")
continue
# Wrap everything into one combined XML
return "<PubmedArticleSet>\n" + "\n".join(xml_parts) + "\n</PubmedArticleSet>"
def extract_terms_from_xml_per_article(xml_text):
"""
Parse PubMed XML that may contain multiple <PubmedArticle> entries.
Extract terms article-by-article, so one bad record doesn't break everything.
"""
tags_by_pmid = defaultdict(set)
counts = Counter()
# Split by article tags instead of parsing the whole set at once
articles = re.findall(r"<PubmedArticle>.*?</PubmedArticle>", xml_text, re.DOTALL)
print(f" Found {len(articles)} articles in XML chunk")
for i, article_xml in enumerate(articles):
try:
root = ET.fromstring(article_xml)
except ET.ParseError as e:
print(f" Skipping malformed article #{i+1}: {e}")
continue
pmid_el = root.find(".//MedlineCitation/PMID")
pmid = pmid_el.text.strip() if pmid_el is not None and pmid_el.text else None
if not pmid:
continue
for mh in root.findall(".//MeshHeading/DescriptorName"):
if mh.text:
t = mh.text.strip()
counts[t] += 1
tags_by_pmid[pmid].add(t)
for kw in root.findall(".//KeywordList/Keyword"):
if kw.text:
t = kw.text.strip()
counts[t] += 1
tags_by_pmid[pmid].add(t)
return counts, tags_by_pmid
def build_annotations(data, min_year, max_year, citations_table=False, wrap_width=AVG_CHARS_PER_YEAR):
"""Return annotations (unused now) and an HTML mapping {year: html_content}."""
annotations = []
html_by_year = {}
for year in range(min_year, max_year + 1):
pubs = [d for d in data if d["Date"].year == year and d["IsCitationOnly"] == citations_table]
if not pubs:
continue
pubs.sort(key=lambda d: int(re.sub(r"[^0-9]", "", d["Rank"])))
lines = []
for p in pubs:
base = (
f"{p['Rank']}: {p['Title']} "
f"(<a href='https://pubmed.ncbi.nlm.nih.gov/{p['PMID']}/' target='_blank'>PubMed</a>)"
)
if p.get("Tags"):
tags_html = ", ".join(p["Tags"])
# Build PubMed AND search query for all tags
query = "+AND+".join(re.sub(r'[^A-Za-z0-9+]', '+', t) for t in p["Tags"])
pubmed_tag_url = f"https://pubmed.ncbi.nlm.nih.gov/?term={query}"
base += (
f"<br><small>Tags: {tags_html} "
f"(<a href='{pubmed_tag_url}' target='_blank'>PubMed</a>)</small>"
)
lines.append(base)
html_by_year[year] = "<br><br>".join(lines)
return annotations, html_by_year
def build_figure(data, min_year, max_year, citation_edges=None):
fig = go.Figure()
# Add vertical lines for each year
for year in range(min_year, max_year + 2):
fig.add_vline(
x=datetime(year, 1, 1),
line=dict(color="gray", dash="dot", width=1),
layer="below"
)
regular = [d for d in data if not d.get("IsCitationOnly")]
citations = [d for d in data if d.get("IsCitationOnly")]
# Regular bubbles
fig.add_trace(go.Scatter(
x=[d["Date"] for d in regular],
y=[d["y"] for d in regular],
mode="markers+text",
text=[d["Rank"] for d in regular],
textposition="middle center",
textfont=dict(size=10, color="black"),
marker=dict(
size=[max(MIN_BUBBLE_SIZE, d["Citations"]/SCALE_FACTOR) for d in regular],
color="skyblue",
line=dict(width=1, color="black"),
symbol="circle-open-dot"
),
hovertext=[
f"<b>{d['Title']}</b><br>PMID:{d['PMID']}"
for d in regular
],
hoverinfo="text"
))
# Citation-only bubbles (gray dotted)
if citations:
fig.add_trace(go.Scatter(
x=[d["Date"] for d in citations],
y=[d["y"] for d in citations],
mode="markers+text",
text=[d["Rank"] for d in citations],
textposition="middle center",
textfont=dict(size=10, color="orange"),
marker=dict(
size=[max(MIN_BUBBLE_SIZE, d["Citations"]/SCALE_FACTOR) for d in citations],
color="white",
line=dict(width=2, color="gray"),
symbol="circle"
),
hovertext=[
f"<b>{d['Title']}</b><br>PMID:{d['PMID']} (citation only)"
for d in citations
],
hoverinfo="text"
))
# Build annotations (title tables)
#annotations = build_annotations(data, min_year, max_year, citations_table=False)
#annotations += build_annotations(data, min_year, max_year, citations_table=True)
max_y = max(d["y"] for d in data)
bottom_margin = 600
buffer = 300
fig.update_layout(
#annotations=annotations,
margin=dict(l=80, r=50, t=40, b=0),
xaxis=dict(side="top", tickformat="%Y", dtick="M12",
range=[datetime(min_year, 1, 1) - timedelta(days=30),
datetime(max_year + 1, 1, 1)],
fixedrange=True),
yaxis=dict(visible=False, range=[-bottom_margin, max_y + buffer], fixedrange=True),
showlegend=False,
height=1000,
width=(max_year - min_year + 1) * 700,
)
return fig
def main():
print(f"Fetching PubMed results for: {TERM}")
ids = fetch_pubmed_ids(TERM, RETMAX)
# --- Load caches ---
summary_cache = load_summary_cache()
citation_cache = load_cache()
xml_cache = load_xml_cache()
# --- Summaries for initial search ---
summaries = fetch_pubmed_summaries(ids, cache=summary_cache)
save_summary_cache(summary_cache)
print(f"Initial search returned {len(ids)} PubMed IDs.")
# --- Fetch citations ---
citation_edges = []
all_pmids = set(ids)
total = len(ids)
for idx, pid in enumerate(ids, start=1):
try:
if pid in citation_cache:
refs = citation_cache[pid]
print(f"[{idx}/{total}] {pid} (cached) cites {len(refs)} papers.")
else:
refs = fetch_citations(pid)
citation_cache[pid] = refs
print(f"[{idx}/{total}] {pid} cites {len(refs)} papers.")
time.sleep(0.25)
for ref in refs:
citation_edges.append((pid, ref))
all_pmids.add(ref)
except Exception as e:
print(f"[{idx}/{total}] Failed to fetch citations for {pid}: {e}")
save_cache(citation_cache)
# --- Fetch missing summaries ---
new_pmids = list(all_pmids - set(ids))
if new_pmids:
print(f"Fetching {len(new_pmids)} cited papers not in main search...")
cited_summaries = fetch_pubmed_summaries(new_pmids, cache=summary_cache)
save_summary_cache(summary_cache)
summaries.update(cited_summaries)
else:
print("No additional cited papers found.")
# --- Build data entries ---
data = []
date_groups = defaultdict(list)
for pid in all_pmids:
info = summaries.get(pid, {})
title = info.get("title", f"Unknown Title ({pid})")
pubdate = info.get("pubdate", "1900")
xdate = parse_pubmed_date(info)
citations = random.randint(10, 300)
d = {
"Title": title,
"PubDate": pubdate,
"Date": xdate,
"Citations": citations,
"PMID": pid,
"y": None,
"Tags": [],
"IsCitationOnly": pid not in ids,
}
data.append(d)
date_groups[xdate].append(d)
# vertical stacking
for date, group in date_groups.items():
sorted_group = sorted(group, key=lambda d: -d["Citations"])
y_offset = 0
for item in sorted_group:
bubble_height = max(MIN_BUBBLE_SIZE, item["Citations"] / SCALE_FACTOR)
item["y"] = y_offset
y_offset += bubble_height * 8
years = sorted({d["Date"].year for d in data})
min_year, max_year = min(years), max(years)
# --- Assign ranks ---
main_pubs = [d for d in data if not d["IsCitationOnly"]]
main_pubs.sort(key=lambda d: -d["Citations"])
for i, pub in enumerate(main_pubs, start=1):
pub["Rank"] = f"#{i}"
citation_pubs = [d for d in data if d["IsCitationOnly"]]
citation_pubs.sort(key=lambda d: -d["Citations"])
for i, pub in enumerate(citation_pubs, start=1):
pub["Rank"] = f"#{i}"
# --- Extract MeSH / Keyword tags ---
xml_text = fetch_pubmed_xml(list(all_pmids), cache=xml_cache)
save_xml_cache(xml_cache)
_tag_counts_all, tags_by_pmid = extract_terms_from_xml_per_article(xml_text)
for dct in data:
if dct["PMID"] in tags_by_pmid:
dct["Tags"] = sorted(tags_by_pmid[dct["PMID"]])
# --- Tag frequencies per year ---
tags_by_year = defaultdict(Counter)
for d in data:
if d["PMID"] in tags_by_pmid and d["Date"].year >= 1900:
y = d["Date"].year
for tag in tags_by_pmid[d["PMID"]]:
tags_by_year[y][tag] += 1
tag_year_counts = Counter()
for y, counter in tags_by_year.items():
for t in counter:
tag_year_counts[t] += 1
# --- Build filtered_tags_by_year: dominant tags each year (not limited by number of years)
TOP_TAGS_PER_YEAR = 10 # display top 10 most frequently used tags every year
filtered_tags_by_year = {}
for y, counter in tags_by_year.items():
# vezmeme jen tagy s nejvyšší četností
top_tags = dict(sorted(counter.items(), key=lambda x: -x[1])[:TOP_TAGS_PER_YEAR])
filtered_tags_by_year[y] = top_tags
# --- Build "New Trends" and "New Dominant Trends" (First 3 Years, labeled as 1st / 2nd / 3rd) ---
TAG_DISPLAY_YEARS = 3
# zjisti první rok výskytu každého tagu
first_year_for_tag = {}
for y in sorted(tags_by_year.keys()):
for t in tags_by_year[y]:
if t not in first_year_for_tag:
first_year_for_tag[t] = y
# zjisti první rok výskytu dominantních tagů
first_year_for_filtered_tag = {}
for y in sorted(filtered_tags_by_year.keys()):
for t in filtered_tags_by_year[y]:
if t not in first_year_for_filtered_tag:
first_year_for_filtered_tag[t] = y
def year_suffix(offset):
return {0: "1st", 1: "2nd", 2: "3rd"}.get(offset, f"{offset+1}th")
# --- New Trends (all tags) ---
new_trends = defaultdict(dict)
for t, first_y in first_year_for_tag.items():
for offset in range(TAG_DISPLAY_YEARS):
y = first_y + offset
if y in tags_by_year and t in tags_by_year[y]:
new_trends[y][t] = tags_by_year[y][t]
# --- New Dominant Trends (from filtered tags) ---
new_dominant_trends = defaultdict(dict)
for t, first_y in first_year_for_filtered_tag.items():
for offset in range(TAG_DISPLAY_YEARS):
y = first_y + offset
if y in filtered_tags_by_year and t in filtered_tags_by_year[y]:
new_dominant_trends[y][t] = filtered_tags_by_year[y][t]
# Determine the first year each tag appeared
first_year_for_tag = {}
for y in sorted(tags_by_year.keys()):
for t in tags_by_year[y]:
if t not in first_year_for_tag:
first_year_for_tag[t] = y
# Keep tag visible only for first 3 years after it appears
TAG_DISPLAY_YEARS = 3
new_tag_trend = defaultdict(dict)
for t, first_y in first_year_for_tag.items():
for y in range(first_y, first_y + TAG_DISPLAY_YEARS):
if y in tags_by_year and t in tags_by_year[y]:
new_tag_trend[y][t] = tags_by_year[y][t]
# --- Count tags globally ---
main_pmids_set = {d["PMID"] for d in data if not d["IsCitationOnly"]}
filtered_counts = Counter()
for pmid, tags in tags_by_pmid.items():
if pmid in main_pmids_set:
for t in tags:
filtered_counts[t] += 1
# --- Build figure and tables ---
fig = build_figure(data, min_year, max_year, citation_edges)
_, pubs_by_year = build_annotations(data, min_year, max_year, citations_table=False)
_, cites_by_year = build_annotations(data, min_year, max_year, citations_table=True)
years = range(min_year, max_year + 1)
year_width = 697.5
left_margin = 135
total_width = left_margin + (max_year - min_year + 1) * year_width
# --- Build trend tag table ---
trend_table_html = [
"<h2>Trends by Year</h2>",
f"<div style='overflow-x:auto;'>",
f"<table style='border-collapse:collapse; width:{total_width}px; text-align:left; border:1px solid {BORDER_COLOR};'>",
"<tr>"
]
trend_table_html.append(f"<th style='width:{left_margin}px; border:1px solid {BORDER_COLOR};'></th>")
for y in years:
trend_table_html.append(
f"<th style='padding:6px; text-align:center; width:{year_width}px; font-weight:600; "
f"border:1px solid {BORDER_COLOR};'>{y}</th>"
)
trend_table_html.append("</tr><tr>")
trend_table_html.append(
f"<td style='width:{left_margin}px; border:1px solid {BORDER_COLOR}; font-weight:600;'>Tags</td>"
)
for y in years:
tag_counts = filtered_tags_by_year.get(y, {})
if not tag_counts:
cell_html = "<span style='color:#aaa;'>—</span>"
else:
counts = list(tag_counts.values())
min_c, max_c = min(counts), max(counts)
def scale_font(c):
if max_c == min_c:
return 14
return 10 + (c - min_c) / (max_c - min_c) * 16
tags_html = []
for t, c in sorted(tag_counts.items(), key=lambda x: -x[1]):
fs = scale_font(c)
if fs <= 10.5:
continue
tags_html.append(
f"<span class='tag' data-tag='{t}' style='font-size:{fs:.1f}px'>{t}</span>"
)
if tags_html:
cell_html = f"<div class='tagbar' data-year='{y}'>{' '.join(tags_html)}</div>"
else:
cell_html = "<span style='color:#aaa;'>—</span>"
trend_table_html.append(
f"<td style='vertical-align:top; padding:6px; width:{year_width}px; "
f"white-space:normal; word-wrap:break-word; border:1px solid {BORDER_COLOR};'>{cell_html}</td>"
)
trend_table_html.append("</tr></table></div>")
trend_table_html = "\n".join(trend_table_html)
def build_tag_table_html(title, tag_data):
"""Builds a trend table with rows for 1st, 2nd, 3rd years of tag appearance (non-consecutive years handled)."""
# Choose correct first-year map
first_year_map = (
first_year_for_filtered_tag
if tag_data is new_dominant_trends
else first_year_for_tag
)
# Build lookup: tag -> sorted list of years it appears
tag_years = defaultdict(list)
for y, tags in tag_data.items():
for t in tags:
tag_years[t].append(y)
for t in tag_years:
tag_years[t] = sorted(set(tag_years[t]))
rows = [
f"<h2>{title}</h2>",
f"<div style='overflow-x:auto;'>",
f"<table style='border-collapse:collapse; width:{total_width}px; text-align:left; border:1px solid {BORDER_COLOR};'>",
]
# Header: list of years
rows.append("<tr>")
rows.append(f"<th style='width:{left_margin}px; border:1px solid {BORDER_COLOR};'></th>")
for y in years:
rows.append(
f"<th style='padding:6px; text-align:center; width:{year_width}px; font-weight:600; "
f"border:1px solid {BORDER_COLOR};'>{y}</th>"
)
rows.append("</tr>")
# --- Separate rows for 1st / 2nd / 3rd year tags ---
for offset, label in enumerate(["1st year", "2nd year", "3rd year"][:TAG_DISPLAY_YEARS]):
rows.append("<tr>")
# left-side label column
rows.append(
f"<td style='width:{left_margin}px; border:1px solid {BORDER_COLOR}; "
f"font-weight:600; background:#f8f8f8;'>{label}</td>"
)
# fill columns per year
for y in years:
tag_counts = tag_data.get(y, {})
if not tag_counts:
rows.append(
f"<td style='vertical-align:top; padding:6px; width:{year_width}px; "
f"white-space:normal; border:1px solid {BORDER_COLOR}; color:#aaa;'>—</td>"
)
continue
# compute font scaling
counts = list(tag_counts.values())
min_c, max_c = min(counts), max(counts)
def scale_font(c):
if max_c == min_c:
return 14
return 10 + (c - min_c) / (max_c - min_c) * 16
tags_html = []
for t, c in sorted(tag_counts.items(), key=lambda x: -x[1]):
tag_year_list = tag_years.get(t, [])
if y not in tag_year_list:
continue
index = tag_year_list.index(y)
if index != offset:
continue
fs = scale_font(c)
if fs <= 10.5:
continue
tags_html.append(
f"<span class='tag' data-tag='{t}' style='font-size:{fs:.1f}px'>{t}</span>"
)
cell_html = (
f"<div class='tagbar' data-year='{y}'>{' '.join(tags_html)}</div>"
if tags_html else "<span style='color:#aaa;'>—</span>"
)
rows.append(
f"<td style='vertical-align:top; padding:6px; width:{year_width}px; "
f"white-space:normal; word-wrap:break-word; border:1px solid {BORDER_COLOR};'>{cell_html}</td>"
)
rows.append("</tr>")
rows.append("</table></div>")
return "\n".join(rows)
# --- Build both new tables dynamically based on TAG_DISPLAY_YEARS ---
new_trends_table_html = build_tag_table_html(
f"New Trends (First {TAG_DISPLAY_YEARS} Year{'s' if TAG_DISPLAY_YEARS > 1 else ''})",
new_trends
)
new_dominant_table_html = build_tag_table_html(
f"New Dominant Trends (First {TAG_DISPLAY_YEARS} Year{'s' if TAG_DISPLAY_YEARS > 1 else ''})",
new_dominant_trends
)
# --- Build first-time tag table (first 3 years of each tag) ---
new_tags_table_html = [
"<h2>New Tags (First 3 Years)</h2>",
f"<div style='overflow-x:auto;'>",
f"<table style='border-collapse:collapse; width:{total_width}px; text-align:left; border:1px solid {BORDER_COLOR};'>",
"<tr>"
]
new_tags_table_html.append(f"<th style='width:{left_margin}px; border:1px solid {BORDER_COLOR};'></th>")
for y in years:
new_tags_table_html.append(
f"<th style='padding:6px; text-align:center; width:{year_width}px; font-weight:600; "
f"border:1px solid {BORDER_COLOR};'>{y}</th>"
)
new_tags_table_html.append("</tr><tr>")
new_tags_table_html.append(
f"<td style='width:{left_margin}px; border:1px solid {BORDER_COLOR}; font-weight:600;'>Tags</td>"
)
for y in years:
tag_counts = new_tag_trend.get(y, {})
if not tag_counts:
cell_html = "<span style='color:#aaa;'>—</span>"
else:
counts = list(tag_counts.values())
min_c, max_c = min(counts), max(counts)
def scale_font(c):
if max_c == min_c:
return 14
return 10 + (c - min_c) / (max_c - min_c) * 16
tags_html = []
for t, c in sorted(tag_counts.items(), key=lambda x: -x[1]):
fs = scale_font(c)
if fs <= 10.5:
continue
tags_html.append(
f"<span class='tag' data-tag='{t}' style='font-size:{fs:.1f}px'>{t}</span>"
)
cell_html = f"<div class='tagbar' data-year='{y}'>{' '.join(tags_html)}</div>" if tags_html else "<span style='color:#aaa;'>—</span>"
new_tags_table_html.append(
f"<td style='vertical-align:top; padding:6px; width:{year_width}px; "
f"white-space:normal; word-wrap:break-word; border:1px solid {BORDER_COLOR};'>{cell_html}</td>"
)
new_tags_table_html.append("</tr></table></div>")
new_tags_table_html = "\n".join(new_tags_table_html)
# --- Build publication table ---
_, pubs_by_year = build_annotations(data, min_year, max_year, citations_table=False)
_, cites_by_year = build_annotations(data, min_year, max_year, citations_table=True)
table_html = [
"<h2>Publications by Year</h2>",
f"<div style='overflow-x:auto;'>",
f"<table style='border-collapse:collapse; width:{total_width}px; text-align:left; border:1px solid {BORDER_COLOR};'>",
"<tr>"
]
table_html.append(f"<th style='width:{left_margin}px; border:1px solid {BORDER_COLOR};'></th>")
for y in years:
table_html.append(
f"<th style='padding:6px; text-align:center; width:{year_width}px; font-weight:600; border:1px solid {BORDER_COLOR};'>{y}</th>"
)
table_html.append("</tr><tr>")
table_html.append(f"<td style='width:{left_margin}px; border:1px solid {BORDER_COLOR};'></td>")
for y in years:
pubs = pubs_by_year.get(y, "")
table_html.append(
f"<td style='vertical-align:top; padding:6px; font-size:13px; width:{year_width}px; white-space:normal; "
f"word-wrap:break-word; line-height:1.4em; border:1px solid {BORDER_COLOR};'>{pubs}</td>"
)
table_html.append("</tr>")
table_html.append("<tr class='citation-row' style='display:none;'>")
table_html.append(f"<td style='width:{left_margin}px; border:1px solid {BORDER_COLOR};'></td>")
for _ in years:
table_html.append(
f"<td style='vertical-align:top; padding:6px; font-size:13px; color:#555; width:{year_width}px; white-space:normal; "
f"word-wrap:break-word; line-height:1.4em; border:1px solid {BORDER_COLOR};'></td>"
)
table_html.append("</tr></table></div>")
tables_html = "\n".join(table_html)
# --- JSON for HTML ---
data_js = []
for dct in data:
dj = dict(dct)
dj["Date"] = dct["Date"].strftime("%Y-%m-%d")
data_js.append(dj)
pubs_json = json.dumps(data_js, ensure_ascii=False)
edges_json = json.dumps(citation_edges, ensure_ascii=False)
tag_counts_json = json.dumps(dict(filtered_counts.most_common(200)), ensure_ascii=False)
plot_div = pio.to_html(fig, include_plotlyjs="inline", full_html=False, config={"displaylogo": False})
main_count = sum(1 for d in data if not d["IsCitationOnly"])
citation_count = sum(1 for d in data if d["IsCitationOnly"])
# --- Write final HTML with fixed interactive behavior ---
html = build_final_html(
TERM, RETMAX, main_count, citation_count, plot_div,
trend_table_html, new_trends_table_html, new_dominant_table_html,
tables_html, pubs_json, edges_json, tag_counts_json, min_year, max_year
)
with open(OUTPUT_HTML, "w", encoding="utf-8") as f:
f.write(html)
print(f" Saved single-file HTML to {OUTPUT_HTML}")
def build_final_html(term, retmax, main_count, citation_count,
plot_div, trend_table_html, new_trends_table_html, new_dominant_table_html, tables_html,
pubs_json, edges_json, tag_counts_json,
min_year, max_year):
"""Return full HTML with independent multi-select yearly tags + global filters."""
return f"""<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>PubMed Timeline — {term}</title>
<style>
body {{
margin: 1.25rem;
font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}}
h1 {{ margin-bottom: 0.5rem; }}
.sub {{ color: #555; margin-bottom: 1rem; }}
.tagbar {{
display: flex;
flex-wrap: wrap;
gap: .5rem .75rem;
margin: .75rem 0 1rem;
user-select: none;
}}
.tag {{
cursor: pointer;
color: #075985;
border-bottom: 1px dotted rgba(2,132,199,.35);
}}
.tag.active {{
color: #b45309;
background: rgba(251,191,36,.15);
border-bottom-color: transparent;
border-radius: .25rem;
padding: .1rem .25rem;
}}
button {{
margin-right: .5rem;
padding: .2rem .5rem;
}}
</style>
<body>
<h1>PubMed Search: "{term}"</h1>
<div class="sub">{main_count} publications (limit {retmax}), {citation_count} associated publications</div>
<div id="timeline-container"
style="overflow-x:auto; white-space:nowrap; border:1px solid rgba(0,0,0,0.1); padding-bottom:1rem;">
<div style="display:inline-block; vertical-align:top;">
{plot_div}
<div id="tag-trends-table">
{trend_table_html}
{new_trends_table_html}
{new_dominant_table_html}
</div>
<div id="pub-tables" style="white-space:normal; width:100%;">