File size: 6,264 Bytes
6d1bbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Fetch PubMed abstracts for IntAct publication PMIDs.

Stores title, abstract, publication year, and journal in
ppi_publication_abstracts table (created by migration 002).

Usage:
    PYTHONPATH=src python scripts_ppi/fetch_pmid_abstracts.py
    PYTHONPATH=src python scripts_ppi/fetch_pmid_abstracts.py --db data/negbiodb_ppi.db
"""

from __future__ import annotations

import argparse
import logging
import sys
import time
from pathlib import Path
from xml.etree import ElementTree

import requests

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
logger = logging.getLogger(__name__)

ROOT = Path(__file__).parent.parent

EFETCH_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
BATCH_SIZE = 50  # PubMed allows up to 200, but 50 is safer


def _parse_pubmed_xml(xml_text: str) -> list[dict]:
    """Parse PubMed efetch XML to extract article metadata."""
    root = ElementTree.fromstring(xml_text)
    articles = []

    for article_el in root.findall(".//PubmedArticle"):
        pmid_el = article_el.find(".//PMID")
        if pmid_el is None or pmid_el.text is None:
            continue
        pmid = int(pmid_el.text)

        # Title
        title_el = article_el.find(".//ArticleTitle")
        title = title_el.text if title_el is not None and title_el.text else None

        # Abstract
        abstract_parts = []
        for abs_el in article_el.findall(".//AbstractText"):
            label = abs_el.get("Label", "")
            text = "".join(abs_el.itertext()).strip()
            if label:
                abstract_parts.append(f"{label}: {text}")
            else:
                abstract_parts.append(text)
        abstract = " ".join(abstract_parts) if abstract_parts else None

        # Publication year
        pub_year = None
        for date_el in [
            article_el.find(".//ArticleDate"),
            article_el.find(".//PubDate"),
        ]:
            if date_el is not None:
                year_el = date_el.find("Year")
                if year_el is not None and year_el.text:
                    try:
                        pub_year = int(year_el.text)
                        break
                    except ValueError:
                        pass

        # Journal
        journal_el = article_el.find(".//Journal/Title")
        journal = journal_el.text if journal_el is not None else None

        if abstract:
            articles.append({
                "pmid": pmid,
                "title": title,
                "abstract": abstract,
                "publication_year": pub_year,
                "journal": journal,
            })

    return articles


def fetch_abstracts(pmids: list[int], session: requests.Session) -> list[dict]:
    """Fetch abstracts for a batch of PMIDs from PubMed."""
    params = {
        "db": "pubmed",
        "id": ",".join(str(p) for p in pmids),
        "rettype": "xml",
        "retmode": "xml",
    }

    for attempt in range(3):
        try:
            resp = session.get(EFETCH_URL, params=params, timeout=30)
            resp.raise_for_status()
            return _parse_pubmed_xml(resp.text)
        except requests.RequestException as e:
            if attempt < 2:
                logger.warning("Attempt %d failed: %s, retrying...", attempt + 1, e)
                time.sleep(2 ** attempt)
            else:
                raise


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description="Fetch PubMed abstracts for IntAct PMIDs.")
    parser.add_argument("--db", type=Path, default=ROOT / "data" / "negbiodb_ppi.db")
    args = parser.parse_args(argv)

    from negbiodb_ppi.ppi_db import get_connection

    conn = get_connection(args.db)

    # Get unique PMIDs from IntAct experiments
    rows = conn.execute(
        "SELECT DISTINCT pubmed_id FROM ppi_experiments "
        "WHERE source_db='intact' AND pubmed_id IS NOT NULL"
    ).fetchall()
    all_pmids = [int(r[0]) for r in rows if r[0] is not None]

    # Also include HuRI PMID
    huri_pmid = 32296183
    if huri_pmid not in all_pmids:
        all_pmids.append(huri_pmid)

    # Filter out already-fetched PMIDs
    existing = set(
        r[0] for r in conn.execute(
            "SELECT pmid FROM ppi_publication_abstracts"
        ).fetchall()
    )
    pmids = [p for p in all_pmids if p not in existing]
    logger.info("Total PMIDs: %d, already fetched: %d, to fetch: %d",
                len(all_pmids), len(existing), len(pmids))

    if not pmids:
        logger.info("All abstracts already fetched.")
        conn.close()
        return 0

    session = requests.Session()
    session.headers["User-Agent"] = "NegBioDB/1.0 (negbiodb@institution.edu)"

    total_fetched = 0
    for i in range(0, len(pmids), BATCH_SIZE):
        batch = pmids[i:i + BATCH_SIZE]
        try:
            articles = fetch_abstracts(batch, session)
        except Exception as e:
            logger.error("Failed batch %d-%d: %s", i, i + len(batch), e)
            continue

        for article in articles:
            conn.execute(
                "INSERT OR IGNORE INTO ppi_publication_abstracts "
                "(pmid, title, abstract, publication_year, journal) "
                "VALUES (?, ?, ?, ?, ?)",
                (article["pmid"], article["title"], article["abstract"],
                 article["publication_year"], article["journal"]),
            )
            total_fetched += 1

        conn.commit()
        logger.info(
            "Batch %d/%d: fetched %d abstracts (total: %d)",
            i // BATCH_SIZE + 1,
            (len(pmids) + BATCH_SIZE - 1) // BATCH_SIZE,
            len(articles), total_fetched,
        )
        time.sleep(0.5)  # NCBI rate limit: 3 requests/sec without API key

    # Summary
    year_dist = conn.execute(
        "SELECT publication_year, COUNT(*) FROM ppi_publication_abstracts "
        "GROUP BY publication_year ORDER BY publication_year"
    ).fetchall()
    logger.info("Publication year distribution:")
    for year, count in year_dist:
        logger.info("  %s: %d", year, count)

    conn.close()
    logger.info("Done. Fetched %d abstracts.", total_fetched)
    return 0


if __name__ == "__main__":
    sys.exit(main())