Skip to content

Instantly share code, notes, and snippets.

@mcfrank
Created October 7, 2015 22:08
Show Gist options
  • Save mcfrank/c1ec74df1427278cbe53 to your computer and use it in GitHub Desktop.
Save mcfrank/c1ec74df1427278cbe53 to your computer and use it in GitHub Desktop.
Example of using the Entrez API to get pubmed citations
## this little script shows off the use of the pubmed API through bioconductor
## requires installing Biopython (using pip)
## also requires installing the DTD files for each of the Entrez API calls,
## but the instructions for this are given when you run the script
## useful list of Entrez databases that can be queried through API
# pmc_pubmed PubMed citations for these articles
# pmc_refs_pubmed PubMed article citing PMC article
# pmc_pmc_cites PMC articles that given PMC article cites
# pmc_pmc_citedby PMC article citing given PMC article
# pubmed_pubmed Calculated set of PubMed citations similar to the selected article(s) retrieved using a word weight algorithm.
# pubmed_pubmed_refs Citation referenced in PubMed article. Only valid for PubMed citations that are also in PMC.
from Bio import Entrez
Entrez.email = "[email protected]"
def get_abstract(pmid):
handle = Entrez.efetch(db='pubmed', id=pmid, retmode='text', rettype='abstract')
return handle.read()
def get_links_id(pmid):
link_list = []
links = Entrez.elink(dbfrom="pubmed", id=pmid, linkname="pubmed_pubmed")
record = Entrez.read(links)
records = record[0][u'LinkSetDb'][0][u'Link']
for link in records:
link_list.append(link[u'Id'])
return link_list
def get_links_term(term):
links = Entrez.esearch(db="pubmed", retmax = 1000, term=term)
record = Entrez.read(links)
link_list = record[u'IdList']
return link_list
### MAIN -----------------------
print(get_links_term("Saffran JR[Author] "))
print "----------------------------"
print(get_abstract("26113833"))
print "----------------------------"
print(get_links_id("8943209"))
@rajputakhil
Copy link

How can I use this script to retrieve PMIDs for multiple 'terms'? Thanks a lot for the help.

@karlo-delacruz-ieds
Copy link

thank you for sharing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment