1 ;;; pubmed.el --- Links and functions for Pubmed and NIH databases -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 John Kitchin
5 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
6 ;; Keywords: convenience
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; This document is an experiment at creating a literate program to provide functions for interacting with pubmed databases.
27 ;; This library provides links that go to pubmed resources, e.g.
33 ;; and nihmsid:NIHMS395714
35 ;; See http://www.ncbi.nlm.nih.gov/pmc/about/public-access-info/#p3 for details of these identifiers.
37 ;; For PMID there is one interactive function that inserts a bibtex entry: pubmed-insert-bibtex-from-pmid.
39 ;; This library is complementary to [[./doi-utils.org]].
43 ;; * PMID (from PubMed) link and functions
45 ;; A PMID is a number that identifies an entry in the Pubmed database. The PMID
46 ;; is a unique reference number for PubMed citations. The PMID is a distinctly
47 ;; different number from the PMCID and is used only for PubMed records.
52 (lambda (link-string) (browse-url (format "http://www.ncbi.nlm.nih.gov/pubmed/%s" link-string)))
54 (lambda (keyword desc format)
57 (format "<a href=\"http://www.ncbi.nlm.nih.gov/pmc/articles/mid/%s\">pmid:%s</a>" keyword keyword)); no output for html
59 ;; write out the latex command
60 (format "\\url{http://www.ncbi.nlm.nih.gov/pmc/articles/mid/%s}" keyword)))))
62 ;; ** Get MEDLINE metadata
64 ;; We can get bibliographic metadata from a pmid. Here we get the MEDLINE
65 ;; text. The website wraps the data in <pre></pre> tags.
67 (defun pubmed-get-medline (pmid)
68 "Get MEDLINE text for PMID as a string."
70 (url-retrieve-synchronously
71 (format "http://www.ncbi.nlm.nih.gov/pubmed/%s/?report=medline&format=text" pmid))
72 (goto-char (point-min))
73 (let ((p1 (search-forward "<pre>"))
74 (p2 (search-forward "</pre>")))
75 (buffer-substring (+ 1 p1) (- p2 6)))))
77 ;; ** Parse the PMID MEDLINE data
79 ;; We can parse this into a data structure.
81 (defun pubmed-parse-medline (pmid)
82 "Parse the medline text for PMID and return a list of cons cells."
88 (with-temp-buffer (insert (pubmed-get-medline pmid))
89 (goto-char (point-min))
90 (while (re-search-forward "\\(^[A-Z]\\{2,4\\}\\)\\s-*- " nil t)
91 (setq tag (match-string 1))
92 ;; point is at end of the search
95 (re-search-forward "\\(^[A-Z]\\{2,4\\}\\)\\s-*- " nil t)
96 (setq p2 (- (match-beginning 1) 1))
97 (setq value (buffer-substring p1 p2))
98 (setq data (append data (list (cons tag value))))
99 ;; now go back to last tag to get the next one
103 ;; ** PMID to bibtex entry
105 ;; The point of parsing the MEDLINE text is so we can make bibtex entries. We
106 ;; only support Journal articles for now.
108 (defun pubmed-pmid-to-bibtex (pmid)
109 "Convert a PMID to a bibtex entry."
110 (let* ((data (pubmed-parse-medline pmid))
111 (type (cdr (assoc "PT" data)))
112 (title (cdr (assoc "TI" data)))
113 (authors (mapconcat 'cdr
115 (string= (car x) "FAU"))
118 (abstract (cdr (assoc "AB" data)))
119 (volume (cdr (assoc "VI" data)))
120 (issue (cdr (assoc "IP" data)))
121 (journal (cdr (assoc "JT" data)))
122 (year (cdr (assoc "DP" data)))
123 (pages (cdr (assoc "PG" data)))
124 (aid (cdr (assoc "AID" data))))
127 ((string= type "JOURNAL ARTICLE")
129 author = {" authors "},
131 abstract = {" abstract "},
132 journal = {" journal "},
133 volume = {" volume "},
134 number = {" issue "},
135 year = {" (car (split-string year)) "},
137 doi = {" (replace-regexp-in-string " \\[doi\\]" "" aid) "},
140 (message "No conversion for type: %s" type)))))
142 ;; And we probably want to be able to insert a bibtex entry
144 (defun pubmed-insert-bibtex-from-pmid (pmid)
145 "Insert a bibtex entry at point derived from PMID.
146 You must clean the entry after insertion."
147 (interactive "sPMID: ")
148 (insert (pubmed-pmid-to-bibtex pmid)))
152 ;; We can also get xml of the MEDLINE data. The web page here also wraps the xml
153 ;; in a <pre> block and escapes the <> with < and >, which we have to
154 ;; undo. I have not used this code for anything, so I am not sure how good the
157 (defun pubmed-get-medline-xml (pmid)
158 "Get MEDLINE xml for PMID as a string."
161 (url-retrieve-synchronously
162 (format "http://www.ncbi.nlm.nih.gov/pubmed/%s/?report=xml&format=text" pmid))
163 (goto-char (point-min))
164 (while (search-forward "<" nil t)
166 (goto-char (point-min))
167 (while (search-forward ">" nil t)
169 (goto-char (point-min))
171 (let ((p1 (search-forward "<pre>"))
172 (p2 (search-forward "</pre>")))
173 (buffer-substring (+ 1 p1) (- p2 6)))))
175 ;; * Pubmed Central (PMC) link
177 ;; A PMCID starts with PMC and is followed by numbers. The PMCID is a unique
178 ;; reference number or identifier that is assigned to every article that is
179 ;; accepted into PMC. The PMCID is also used by recipients of NIH funding to
180 ;; demonstrate compliance with the NIH Public Access policy. The PMCID can be
181 ;; found in both PMC and PubMed.
183 ;; Here we define a new link. Clicking on it simply opens a webpage to the
189 (lambda (link-string) (browse-url (format "http://www.ncbi.nlm.nih.gov/pmc/articles/%s" link-string)))
191 (lambda (keyword desc format)
194 (format "<a href=\"http://www.ncbi.nlm.nih.gov/pmc/articles/%s\">pmcid:%s</a>" keyword keyword))
196 (format "\\url{http://www.ncbi.nlm.nih.gov/pmc/articles/%s}" keyword)))))
200 ;; The NIHMSID is a preliminary article identifier that applies only to
201 ;; manuscripts deposited through the NIHMS system. The NIHMSID is only valid for
202 ;; compliance reporting for 90 days after the publication date of an
203 ;; article. Once the Web version of the NIHMS submission is approved for
204 ;; inclusion in PMC and the corresponding citation is in PubMed, the article
205 ;; will also be assigned a PMCID.
210 (lambda (link-string) (browse-url (format "http://www.ncbi.nlm.nih.gov/pmc/articles/mid/%s" link-string)))
212 (lambda (keyword desc format)
215 (format "<a href=\"http://www.ncbi.nlm.nih.gov/pmc/articles/mid//%s\">nihmsid:%s</a>" keyword keyword))
217 ;; write out the latex command
218 (format "\\url{http://www.ncbi.nlm.nih.gov/pmc/articles/mid/%s}" keyword)))))
222 ;;; pubmed.el ends here