]> git.donarmstrong.com Git - org-ref.git/blobdiff - doi-utils.org
space fix
[org-ref.git] / doi-utils.org
index e08e6013ab8b33b0eb7416acf4179b6429b22c3a..a96fc931fcd8e42c0942bc09be0e1ea1b592b292 100644 (file)
@@ -45,9 +45,9 @@ The principle commands you will use from here are:
 #+END_SRC
 
 * Getting pdf files from a DOI
-The idea here is simple. When you visit http://dx.doi.org/doi, you get redirected to the journal site. Once you have the url for the article, you can usually compute the url to the pdf, or find it in the page. Then you simply download it. 
+The idea here is simple. When you visit http://dx.doi.org/doi, you get redirected to the journal site. Once you have the url for the article, you can usually compute the url to the pdf, or find it in the page. Then you simply download it.
 
-There are some subtleties in doing this that are described here. To get the redirect, we have to use url-retrieve, and a callback function. The callback does not return anything, so we communicate through global variables. url-retrieve is asynchronous, so we have to make sure to wait for it to finish. 
+There are some subtleties in doing this that are described here. To get the redirect, we have to use url-retrieve, and a callback function. The callback does not return anything, so we communicate through global variables. url-retrieve is asynchronous, so we have to make sure to wait for it to finish.
 
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (defvar *doi-utils-waiting* t
@@ -76,8 +76,8 @@ To actually get the redirect we use url-retrieve like this.
   ;; we are going to wait until the url-retrieve is done
   (setq *doi-utils-waiting* t)
   ;; start with no redirect. it will be set in the callback.
-  (setq *doi-utils-redirect* nil) 
-  (url-retrieve 
+  (setq *doi-utils-redirect* nil)
+  (url-retrieve
    (format "http://dx.doi.org/%s" doi)
    'doi-utils-redirect-callback)
   ; I suspect we need to wait here for the asynchronous process to
@@ -168,7 +168,7 @@ we just change /abs/ to /pdf/.
     (replace-regexp-in-string "/abs/" "/pdf/" *doi-utils-redirect*)))
 #+END_SRC
 
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (acs-pdf-url  "http://pubs.acs.org/doi/abs/10.1021/nl500037x")
 #+END_SRC
 
@@ -190,7 +190,7 @@ we just change /abs/ to /pdf/.
     (concat (replace-regexp-in-string "/stable/" "/stable/pdfplus/" *doi-utils-redirect*) ".pdf")))
 #+END_SRC
 
-** AIP 
+** AIP
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (defun aip-pdf-url (*doi-utils-redirect*)
   (when (string-match "^http://scitation.aip.org" *doi-utils-redirect*)
@@ -295,7 +295,7 @@ http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf+html?with-ds=ye
        'aps-pdf-url
        'science-pdf-url
        'nature-pdf-url
-       'wiley-pdf-url       
+       'wiley-pdf-url
        'springer-pdf-url
        'acs-pdf-url
        'iop-pdf-url
@@ -317,7 +317,7 @@ http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf+html?with-ds=ye
 calculated. Loops through the functions in `doi-utils-pdf-url-functions'
 until one is found"
   (doi-utils-get-redirect doi)
-  
+
   (unless *doi-utils-redirect*
     (error "No redirect found for %s" doi))
   (message "applying functions")
@@ -335,14 +335,14 @@ until one is found"
 : doi-utils-get-pdf-url
 
 
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (doi-utils-get-pdf-url "10.1126/science.1158722")
 #+END_SRC
 
 #+RESULTS:
 : http://www.sciencemag.org/content/321/5890/792.full.pdf
 
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (doi-utils-get-pdf-url  "10.1021/nl500037x")
 #+END_SRC
 
@@ -350,7 +350,7 @@ until one is found"
 : http://pubs.acs.org/doi/pdf/10.1021/nl500037x
 
 
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (doi-utils-get-pdf-url  "10.1002/anie.201402680")
 #+END_SRC
 
@@ -369,11 +369,11 @@ page. you must have permission to access the pdf. We open the pdf
 at the end."
   (interactive)
   (save-excursion
-    (bibtex-beginning-of-entry) 
+    (bibtex-beginning-of-entry)
     (let (;; get doi, removing http://dx.doi.org/ if it is there.
          (doi (replace-regexp-in-string
                "http://dx.doi.org/" ""
-               (bibtex-autokey-get-field "doi")))             
+               (bibtex-autokey-get-field "doi")))
          (key)
          (pdf-url)
          (pdf-file)
@@ -398,7 +398,7 @@ at the end."
                      (message "%s" (buffer-string))
                      (delete-file pdf-file))
                  (message "%s saved" pdf-file)))
-       
+
              (when (file-exists-p pdf-file)
                (org-open-file pdf-file)))
          (message "No pdf-url found for %s at %s" doi *doi-utils-redirect* ))
@@ -411,20 +411,24 @@ I [[http://homepages.see.leeds.ac.uk/~eeaol/notes/2013/02/doi-metadata/][found]]
 
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (defun doi-utils-get-json-metadata (doi)
-  (let ((url-request-method "GET") 
-       (url-mime-accept-string "application/citeproc+json")
-       (json-object-type 'plist))
+
+  (let ((url-request-method "GET")
+       (url-mime-accept-string "application/citeproc+json")
+       (json-object-type 'plist)
+       (json-data))
     (with-current-buffer
        (url-retrieve-synchronously
         (concat "http://dx.doi.org/" doi))
-      (json-read-from-string (buffer-substring url-http-end-of-headers (point-max))))))       
+      (setq json-data (buffer-substring url-http-end-of-headers (point-max)))
+      (message "%s" json-data)
+      (json-read-from-string json-data))))
 #+END_SRC
 
 #+RESULTS:
 : doi-utils-get-json-metadata
 
 For example:
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (doi-utils-get-json-metadata "10.1103/PhysRevLett.99.016105")
 #+END_SRC
 
@@ -451,6 +455,7 @@ Now we define a function that fills in that template from the metadata.
        results
        author
        title
+       booktitle
        journal
        year
        volume
@@ -475,16 +480,30 @@ Now we define a function that fills in that template from the metadata.
     (cond
      ((or (string= type "journal-article") (string= type "article-journal"))
       (doi-utils-expand-template "@article{,
-  author =      {%{author}},
-  title =       {%{title}},
-  journal =     {%{journal}},
-  year =        {%{year}},
-  volume =      {%{volume}},
-  number =      {%{issue}},
-  pages =       {%{pages}},
+  author =      {%{author}},
+  title =       {%{title}},
+  journal =     {%{journal}},
+  year =        {%{year}},
+  volume =      {%{volume}},
+  number =      {%{issue}},
+  pages =       {%{pages}},
+  doi =          {%{doi}},
+  url =          {%{url}},
+}"))
+
+     ((string= type "proceedings-article")
+      (setq booktitle (plist-get results :container-title))
+      (doi-utils-expand-template "@inproceedings{,
+  author =      {%{author}},
+  title =       {%{title}},
+  booktitle =    {%{booktitle}},
+  year =        {%{year}},
+  month =       {%{month}},
+  pages =       {%{pages}},
   doi =          {%{doi}},
   url =          {%{url}},
 }"))
+
     (t (message-box "%s not supported yet." type)))))
 #+END_SRC
 
@@ -492,20 +511,20 @@ Now we define a function that fills in that template from the metadata.
 : doi-utils-doi-to-bibtex-string
 
 To see that in action:
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (doi-utils-doi-to-bibtex-string "10.1103/PhysRevLett.99.016105")
 #+END_SRC
 
 #+RESULTS:
 #+begin_example
 @article{,
-  author =      {F. Abild-Pedersen and J. Greeley and F. Studt and J. Rossmeisl and T. Munter and P. Moses and E. Skúlason and T. Bligaard and J. Nørskov},
-  title =       {Scaling Properties of Adsorption Energies for Hydrogen-Containing Molecules on Transition-Metal Surfaces},
-  journal =     {Phys. Rev. Lett.},
-  year =        {2007},
-  volume =      {99},
-  number =      {1},
-  pages =       {nil},
+  author =      {F. Abild-Pedersen and J. Greeley and F. Studt and J. Rossmeisl and T. Munter and P. Moses and E. Skúlason and T. Bligaard and J. Nørskov},
+  title =       {Scaling Properties of Adsorption Energies for Hydrogen-Containing Molecules on Transition-Metal Surfaces},
+  journal =     {Phys. Rev. Lett.},
+  year =        {2007},
+  volume =      {99},
+  number =      {1},
+  pages =       {nil},
   doi =          {10.1103/physrevlett.99.016105},
   url =          {http://dx.doi.org/10.1103/PhysRevLett.99.016105},
 }
@@ -532,13 +551,23 @@ org-ref, and tries to download the corresponding pdf."
 It may be you are in some other place when you want to add a bibtex entry. This next function will open the first entry in org-ref-default-bibliography go to the end, and add the entry. You can sort it later.
 
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
-(defun doi-utils-add-bibtex-entry-from-doi (doi)
-  "add entry to end of first entry in `org-ref-default-bibliography'."
-  (interactive "sDOI: ")
-  (find-file (car org-ref-default-bibliography))
-  (end-of-buffer)
-  (insert "\n\n")
-  (doi-utils-insert-bibtex-entry-from-doi doi))
+(defun doi-utils-add-bibtex-entry-from-doi (doi bibfile)
+  "add entry to end of a file in `org-ref-default-bibliography' or in the current directory ending with .bib."
+  (interactive
+   (list
+    (read-string "DOI: ")
+    (ido-completing-read
+     "Bibfile: "
+     (append org-ref-default-bibliography
+            (f-entries "." (lambda (f) (f-ext? f "bib")))))))
+  (find-file bibfile)
+  (goto-char (point-min))
+  (if (search-forward doi nil t)
+      (message "%s is already in this file" doi)
+    (end-of-buffer)
+    (insert "\n\n")
+    (doi-utils-insert-bibtex-entry-from-doi doi)
+    (save-buffer)))
 #+END_SRC
 
 It may be you want to just highlight a doi, and then add it. Here is that function.
@@ -549,9 +578,13 @@ It may be you want to just highlight a doi, and then add it. Here is that functi
   (interactive "r")
   (let ((doi (buffer-substring start end)))
     (find-file (car org-ref-default-bibliography))
-    (end-of-buffer)
-    (insert "\n")
-    (doi-utils-insert-bibtex-entry-from-doi doi)))
+    (goto-char (point-min))
+    (if (search-forward doi nil t)
+       (message "%s is already in this file" doi)
+      (end-of-buffer)
+      (insert "\n\n")
+      (doi-utils-insert-bibtex-entry-from-doi doi)
+      (save-buffer))))
 #+END_SRC
 
 #+RESULTS:
@@ -563,7 +596,7 @@ I wrote this code because it is pretty common for me to copy bibtex entries from
 There is not bibtex set field function, so I wrote this one.
 
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
-(defun bibtex-set-field (field value)
+(defun bibtex-set-field (field value &optional nodelim)
   "set field to value in bibtex file. create field if it does not exist"
   (interactive "sfield: \nsvalue: ")
   (bibtex-beginning-of-entry)
@@ -574,7 +607,7 @@ There is not bibtex set field function, so I wrote this one.
          (goto-char (car (cdr found)))
          (when value
            (bibtex-kill-field)
-           (bibtex-make-field field)
+           (bibtex-make-field field nil nil nodelim)
            (backward-char)
            (insert value)))
       ;; make a new field
@@ -583,7 +616,7 @@ There is not bibtex set field function, so I wrote this one.
       (forward-line) (beginning-of-line)
       (bibtex-next-field nil)
       (forward-char)
-      (bibtex-make-field field)
+      (bibtex-make-field field nil nil nodelim)
       (backward-char)
       (insert value))))
 #+END_SRC
@@ -613,13 +646,13 @@ The updating function looks like this. We get all the keys from the json plist m
                       (elt
                        (elt
                         (plist-get
-                         (plist-get results :issued) :date-parts) 0) 0)))      
+                         (plist-get results :issued) :date-parts) 0) 0)))
        (volume (plist-get results :volume))
        (number (or (plist-get results :issue) ""))
        (pages (or (plist-get results :page) ""))
        (url (or (plist-get results :URL) ""))
        (doi (plist-get results :DOI)))
-    
+
     ;; map the json fields to bibtex fields. The code each field is mapped to is evaluated.
     (setq mapping '((:author . (bibtex-set-field "author" author))
                    (:title . (bibtex-set-field "title" title))
@@ -636,21 +669,152 @@ The updating function looks like this. We get all the keys from the json plist m
      (lambda (key)
        (eval (cdr (assoc key mapping))))
      (plist-get-keys results)))
-  
+
   ; reclean entry, but keep key if it exists.
   (if (bibtex-key-in-head)
       (org-ref-clean-bibtex-entry t)
     (org-ref-clean-bibtex-entry)))
 #+END_SRC
+* DOI functions for WOS
+I came across this API http://wokinfo.com/media/pdf/OpenURL-guide.pdf to make links to the things I am interested in here. Based on that document, here are three links based on a doi:10.1021/jp047349j that take you to different Web Of Science (WOS) pages.
+
+
+1. go to article in WOS: http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info:doi/10.1021/jp047349j
+2. citing articles: http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F10.1021/jp047349j&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.citing=yes
+3. related articles: http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F10.1021/jp047349j&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.related=yes
+
+These are pretty easy to construct, so we can write functions that will create them and open the url in our browser. There are some other options that could be considered, but since we usually have a doi, it seems like the best way to go for creating the links. Here are the functions.
+
+#+BEGIN_SRC emacs-lisp  :tangle doi-utils.el
+(defun doi-utils-wos (doi)
+  "Open Web of Science entry for DOI"
+  (interactive "sDOI: ")
+  (browse-url
+   (format
+    "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info:doi/%s" doi)))
+
+(defun doi-utils-wos-citing (doi)
+  "Open Web of Science citing articles entry. May be empty if none are found"
+  (interactive "sDOI: ")
+  (browse-url
+   (concat
+    "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F"
+    doi
+    "&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.citing=yes")))
+
+(defun doi-utils-wos-related (doi)
+  "Open Web of Science related articles page."
+  (interactive "sDOI: ")
+  (browse-url
+   (concat "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F"
+          doi
+          "&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.related=yes")))
+
+#+END_SRC
+
+* A new doi link for org-mode
+The idea is to add a menu to the doi link, so rather than just clicking to open the article, you can do other things.
+1. open doi
+2. open in wos
+3. open citing articles
+4. open related articles
+5. open bibtex entry
+6. get bibtex entry
+
+#+BEGIN_SRC emacs-lisp :tangle doi-utils.el :results silent
+(defun doi-utils-open (doi)
+ (interactive "sDOI: ")
+ (browse-url (concat "http://dx.doi.org/" doi)))
+
+
+(defun doi-utils-open-bibtex (doi)
+  "Search through `reftex-default-bibliography' for DOI."
+  (interactive "sDOI: ")
+  (catch 'file
+    (dolist (f reftex-default-bibliography)
+      (find-file f)
+      (when (search-forward doi (point-max) t)
+       (bibtex-beginning-of-entry)
+       (throw 'file t)))))
+
+
+(defun doi-utils-crossref (doi)
+  "Search DOI in CrossRef."
+  (interactive "sDOI: ")
+  (browse-url
+   (format
+    "http://search.crossref.org/?q=%s" doi)))
+
+
+(defun doi-utils-google-scholar (doi)
+  "Google scholar the word at point or selection."
+  (interactive "sDOI: ")
+  (browse-url
+   (format
+    "http://scholar.google.com/scholar?q=%s" doi)))
+
+
+(defun doi-utils-pubmed (doi)
+  "Pubmed the word at point or selection."
+  (interactive "sDOI: ")
+  (browse-url
+   (format
+    "http://www.ncbi.nlm.nih.gov/pubmed/?term=%s"
+    (url-hexify-string doi))))
+
+
+(defvar doi-link-menu-funcs '()
+ "Functions to run in doi menu. Each entry is a list of (key menu-name function).
+The function must take one argument, the doi.")
+
+(setq doi-link-menu-funcs
+      '(("o" "pen" doi-utils-open)
+       ("w" "os" doi-utils-wos)
+       ("c" "iting articles" doi-utils-wos-citing)
+       ("r" "elated articles" doi-utils-wos-related)
+        ("s" "Google Scholar" doi-utils-google-scholar)
+        ("f" "CrossRef" doi-utils-crossref)
+        ("p" "ubmed" doi-utils-pubmed)
+       ("b" "open in bibtex" doi-utils-open-bibtex)
+       ("g" "et bibtex entry" doi-utils-add-bibtex-entry-from-doi)))
+
+
+(defun doi-link-menu (link-string)
+   "Generate the link menu message, get choice and execute it.
+Options are stored in `doi-link-menu-funcs'."
+   (interactive)
+   (message
+   (concat
+    (mapconcat
+     (lambda (tup)
+       (concat "[" (elt tup 0) "]"
+              (elt tup 1) " "))
+     doi-link-menu-funcs "") ": "))
+   (let* ((input (read-char-exclusive))
+         (choice (assoc
+                  (char-to-string input) doi-link-menu-funcs)))
+     (when choice
+       (funcall
+       (elt
+        choice
+        2)
+       link-string))))
+
+(org-add-link-type
+ "doi"
+ 'doi-link-menu)
+#+END_SRC
+
+doi:10.1021/jp047349j
+
 * end of file
 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
 (provide 'doi-utils)
 #+END_SRC
 * load
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :tangle no
 (org-babel-load-file "doi-utils.org")
 #+END_SRC
 
 #+RESULTS:
 : Loaded doi-utils.el
-