mgbam commited on
Commit
469e58f
·
verified ·
1 Parent(s): 359d26a

Create literature.py

Browse files
Files changed (1) hide show
  1. genesis/literature.py +69 -0
genesis/literature.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # genesis/literature.py
2
+ import os
3
+ import requests
4
+ from typing import List, Dict
5
+
6
+ NCBI_API_KEY = os.getenv("NCBI_API_KEY")
7
+ NCBI_EMAIL = os.getenv("NCBI_EMAIL")
8
+
9
+ def search_pubmed(query: str, max_results: int = 5, sort: str = "relevance") -> List[Dict]:
10
+ """
11
+ Search PubMed for research articles.
12
+
13
+ sort: "relevance", "pub date", or "most recent"
14
+ Returns: List of dicts with title, authors, journal, year, and link.
15
+ """
16
+ try:
17
+ # Step 1: Search PubMed for IDs
18
+ search_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
19
+ search_params = {
20
+ "db": "pubmed",
21
+ "term": query,
22
+ "retmax": max_results,
23
+ "api_key": NCBI_API_KEY,
24
+ "email": NCBI_EMAIL,
25
+ "sort": "relevance" if sort == "relevance" else "pub+date" if sort == "most recent" else "pub+date",
26
+ "retmode": "json"
27
+ }
28
+ r = requests.get(search_url, params=search_params, timeout=10)
29
+ r.raise_for_status()
30
+ ids = r.json().get("esearchresult", {}).get("idlist", [])
31
+ if not ids:
32
+ return []
33
+
34
+ # Step 2: Fetch summaries for IDs
35
+ summary_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
36
+ summary_params = {
37
+ "db": "pubmed",
38
+ "id": ",".join(ids),
39
+ "retmode": "json"
40
+ }
41
+ s = requests.get(summary_url, params=summary_params, timeout=10)
42
+ s.raise_for_status()
43
+ summaries = s.json().get("result", {})
44
+
45
+ papers = []
46
+ for pmid in ids:
47
+ paper = summaries.get(pmid, {})
48
+ authors = [a["name"] for a in paper.get("authors", [])]
49
+ papers.append({
50
+ "title": paper.get("title", "N/A"),
51
+ "authors": ", ".join(authors) if authors else "N/A",
52
+ "journal": paper.get("fulljournalname", "N/A"),
53
+ "year": paper.get("pubdate", "N/A"),
54
+ "pmid": pmid,
55
+ "url": f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/"
56
+ })
57
+ return papers
58
+ except Exception as e:
59
+ print(f"[PubMed] Failed: {e}")
60
+ return []
61
+
62
+ def format_literature_markdown(papers: List[Dict]) -> str:
63
+ """Format literature list into markdown table."""
64
+ if not papers:
65
+ return "No relevant papers found."
66
+ md = "| Title | Authors | Journal | Year |\n|-------|---------|---------|------|\n"
67
+ for p in papers:
68
+ md += f"| [{p['title']}]({p['url']}) | {p['authors']} | {p['journal']} | {p['year']} |\n"
69
+ return md