Skip to content

Instantly share code, notes, and snippets.

@hashemi
Last active December 23, 2015 00:03
Show Gist options
  • Save hashemi/7b58899f75432d8b9ef7 to your computer and use it in GitHub Desktop.
Save hashemi/7b58899f75432d8b9ef7 to your computer and use it in GitHub Desktop.
# Takes a PubMed ID (PMID) and your email and returns
# a citation more or less in Vancouver style.
#
# Depends on biopython:
# $ pip install biopython
#
def citation(PMID, email):
from Bio import Entrez
Entrez.email = email
handle = Entrez.efetch(db="pubmed", id=PMID, retmode="xml")
data = Entrez.read(handle)
article_data = data[0]["MedlineCitation"]["Article"]
journal_data = article_data["Journal"]
journal_issue_data = journal_data["JournalIssue"]
authors = []
for author in article_data["AuthorList"]:
author_name = ''
try:
author_name = author["LastName"]
except KeyError:
continue
try:
author_name += ' ' + author["Initials"]
except KeyError:
pass
authors.append(author_name)
cite = {
"journal": str(journal_data["ISOAbbreviation"]),
"volume": str(journal_issue_data["Issue"]),
"issue": str(journal_issue_data["Volume"]),
"month": str(journal_issue_data["PubDate"]["Month"]),
"year": str(journal_issue_data["PubDate"]["Year"]),
# remove trailing period after a title if it already exists
"title": str(article_data["ArticleTitle"]).rstrip("."),
"authors": ", ".join(authors),
"pagination": str(article_data["Pagination"]["MedlinePgn"]),
}
return "{authors}. {title}. {journal}. {year} {month};{volume}({issue}):{pagination}.".format(**cite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment