Skip to content

Instantly share code, notes, and snippets.

@hashemi
Last active December 23, 2015 00:03
Show Gist options
  • Select an option

  • Save hashemi/7b58899f75432d8b9ef7 to your computer and use it in GitHub Desktop.

Select an option

Save hashemi/7b58899f75432d8b9ef7 to your computer and use it in GitHub Desktop.

Revisions

  1. hashemi revised this gist Dec 23, 2015. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion fetch_pubmed_citation.py
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,18 @@ def citation(PMID, email):
    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"]),
    @@ -21,7 +33,7 @@ def citation(PMID, email):
    "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("{0[LastName]} {0[Initials]}".format(a) for a in article_data["AuthorList"]),
    "authors": ", ".join(authors),
    "pagination": str(article_data["Pagination"]["MedlinePgn"]),
    }
    return "{authors}. {title}. {journal}. {year} {month};{volume}({issue}):{pagination}.".format(**cite)
  2. hashemi created this gist Dec 16, 2015.
    27 changes: 27 additions & 0 deletions fetch_pubmed_citation.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # 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"]
    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("{0[LastName]} {0[Initials]}".format(a) for a in article_data["AuthorList"]),
    "pagination": str(article_data["Pagination"]["MedlinePgn"]),
    }
    return "{authors}. {title}. {journal}. {year} {month};{volume}({issue}):{pagination}.".format(**cite)