File size: 8,195 Bytes
66aa7dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fetch open-access full text from Europe PMC and save XML plus a plain-text rendering.

Usage: python3 -I scripts/fetch_fulltext.py PMC1234567 [PMC...]

Writes ../_raw/europepmc/<PMCID>.xml and <PMCID>.txt (a sibling folder of the repository).
The .txt keeps section titles, paragraphs, figure/table captions and table rows
(cells joined by " | "), so table values can be located by row.
Standard library only. The downloaded XML is treated as data, never executed.
"""
from __future__ import annotations

import pathlib
import html.parser
import re
import sys
import time
import urllib.request
import xml.etree.ElementTree as ET

RAW = pathlib.Path(__file__).resolve().parent.parent.parent / "_raw" / "europepmc"
URL = "https://www.ebi.ac.uk/europepmc/webservices/rest/{pmcid}/fullTextXML"
UA = "LifeFrom-open-build/1.0 (dataset build script)"


def _text(el: ET.Element) -> str:
    return re.sub(r"\s+", " ", "".join(el.itertext())).strip()


def render(xml_bytes: bytes) -> str:
    root = ET.fromstring(xml_bytes)
    out: list[str] = []

    front = root.find("front")
    if front is not None:
        t = front.find(".//article-title")
        if t is not None:
            out.append("TITLE: " + _text(t))
        doi = front.find(".//article-id[@pub-id-type='doi']")
        if doi is not None:
            out.append("DOI: " + _text(doi))
        ab = front.find(".//abstract")
        if ab is not None:
            out.append("\n== ABSTRACT ==")
            for p in ab.iter():
                if p.tag in ("title",):
                    out.append("[" + _text(p) + "]")
                elif p.tag == "p":
                    out.append(_text(p))

    def walk(el: ET.Element, depth: int = 0) -> None:
        for child in el:
            tag = child.tag
            if tag == "sec":
                t = child.find("title")
                if t is not None:
                    out.append("\n" + "#" * (depth + 2) + " " + _text(t))
                walk(child, depth + 1)
            elif tag == "p":
                # paragraphs may embed tables/figures; render text without them first
                inner_tables = child.findall(".//table-wrap")
                if inner_tables:
                    out.append(_text_excluding(child))
                    for tw in inner_tables:
                        render_table(tw)
                else:
                    out.append(_text(child))
            elif tag == "table-wrap":
                render_table(child)
            elif tag == "fig":
                lab = child.find("label")
                cap = child.find("caption")
                out.append(
                    "\nFIGURE " + (_text(lab) if lab is not None else "")
                    + ": " + (_text(cap) if cap is not None else "")
                )
            elif tag in ("title",):
                continue
            else:
                walk(child, depth)

    def render_table(tw: ET.Element) -> None:
        lab = tw.find("label")
        cap = tw.find("caption")
        out.append(
            "\nTABLE " + (_text(lab) if lab is not None else "")
            + ": " + (_text(cap) if cap is not None else "")
        )
        for tr in tw.iter("tr"):
            cells = [_text(c) for c in tr if c.tag in ("td", "th")]
            out.append(" | ".join(cells))
        foot = tw.find("table-wrap-foot")
        if foot is not None:
            out.append("TABLE FOOTNOTE: " + _text(foot))
        out.append("END TABLE")

    body = root.find("body")
    if body is not None:
        out.append("\n== BODY ==")
        walk(body)
    back = root.find("back")
    if back is not None:
        for tw in back.iter("table-wrap"):
            render_table(tw)
    return "\n".join(out) + "\n"


def _text_excluding(p: ET.Element) -> str:
    parts = [p.text or ""]
    for c in p:
        if c.tag != "table-wrap":
            parts.append("".join(c.itertext()))
        parts.append(c.tail or "")
    return re.sub(r"\s+", " ", "".join(parts)).strip()


PMC_HTML = "https://pmc.ncbi.nlm.nih.gov/articles/{pmcid}/"
# Articles whose full text Europe PMC does not serve as XML (publisher restriction) but which
# PubMed Central shows as a free public web page. Rendered from that page instead.
HTML_ONLY = {"PMC5818818", "PMC3069236", "PMC11254377", "PMC5134919", "PMC7467599", "PMC6818550"}


class _HTMLText(html.parser.HTMLParser):
    BLOCK = {"p", "h1", "h2", "h3", "h4", "tr", "li", "caption", "figcaption", "div", "section"}

    def __init__(self) -> None:
        super().__init__(convert_charrefs=True)
        self.lines: list[str] = []
        self.cur: list[str] = []
        self.cells: list[str] | None = None
        self.skip = 0
        self.meta: dict[str, str] = {}

    def flush(self) -> None:
        t = re.sub(r"\s+", " ", "".join(self.cur)).strip()
        if t:
            self.lines.append(t)
        self.cur = []

    def handle_starttag(self, tag, attrs):
        a = dict(attrs)
        if tag == "meta" and a.get("name", "").startswith("citation_") and a.get("name") not in self.meta:
            self.meta[a["name"]] = a.get("content", "")
        if tag in ("script", "style", "nav", "header", "footer"):
            self.skip += 1
        if self.skip:
            return
        if tag == "tr":
            self.flush()
            self.cells = []
        elif tag in ("td", "th") and self.cells is not None:
            self.cur = []
        elif tag in ("table",):
            self.flush()
            self.lines.append("TABLE START")
        elif tag in self.BLOCK:
            self.flush()
            if tag in ("h2", "h3", "h4"):
                self.cur.append("## ")

    def handle_endtag(self, tag):
        if tag in ("script", "style", "nav", "header", "footer"):
            self.skip = max(0, self.skip - 1)
            return
        if self.skip:
            return
        if tag in ("td", "th") and self.cells is not None:
            self.cells.append(re.sub(r"\s+", " ", "".join(self.cur)).strip())
            self.cur = []
        elif tag == "tr" and self.cells is not None:
            self.lines.append(" | ".join(self.cells))
            self.cells = None
            self.cur = []
        elif tag == "table":
            self.flush()
            self.lines.append("END TABLE")
        elif tag in self.BLOCK:
            self.flush()

    def handle_data(self, data):
        if not self.skip:
            self.cur.append(data)


def render_html(raw: str) -> str:
    p = _HTMLText()
    p.feed(raw)
    p.flush()
    head = [
        "TITLE: " + p.meta.get("citation_title", ""),
        "DOI: " + p.meta.get("citation_doi", ""),
        "SOURCE: PubMed Central public web page (Europe PMC XML not available for this article)",
    ]
    return "\n".join(head + p.lines) + "\n"


def fetch(pmcid: str, force: bool = False) -> pathlib.Path:
    RAW.mkdir(parents=True, exist_ok=True)
    xml_path = RAW / f"{pmcid}.xml"
    txt_path = RAW / f"{pmcid}.txt"
    if pmcid in HTML_ONLY:
        html_path = RAW / f"{pmcid}.html"
        if force or not html_path.exists():
            req = urllib.request.Request(PMC_HTML.format(pmcid=pmcid),
                                         headers={"User-Agent": "Mozilla/5.0 " + UA})
            with urllib.request.urlopen(req, timeout=60) as r:
                html_path.write_bytes(r.read())
        txt_path.write_text(render_html(html_path.read_text(encoding="utf-8")), encoding="utf-8")
        return txt_path
    if force or not xml_path.exists():
        req = urllib.request.Request(URL.format(pmcid=pmcid), headers={"User-Agent": UA})
        with urllib.request.urlopen(req, timeout=60) as r:
            data = r.read()
        if not data.strip().startswith(b"<"):
            raise RuntimeError(f"{pmcid}: no XML returned")
        xml_path.write_bytes(data)
        time.sleep(0.3)
    txt_path.write_text(render(xml_path.read_bytes()), encoding="utf-8")
    return txt_path


if __name__ == "__main__":
    for pid in sys.argv[1:]:
        try:
            p = fetch(pid.strip())
            print(f"ok {pid} -> {p} ({p.stat().st_size} bytes)")
        except Exception as e:  # noqa: BLE001
            print(f"FAIL {pid}: {e}")