Search code examples
pythonbiopythonncbi

Make biopython Entrez.esearch loop through parameters


I'm trying to adapt a script (found here: https://gist.github.com/bonzanini/5a4c39e4c02502a8451d) to search and retrieve data from PubMed.

Here's what I have so far:

#!/usr/bin/env python

from Bio import Entrez
import datetime
import json

# Create dictionary of journals (the official abbreviations are not used here...)
GroupA=["Nature", "Science", "PNAS","JACS"]
GroupB=["E-life", "Mol Cell","Plos Computational","Nature communication","Cell"]
GroupC=["Nature Biotech", "Nature Chem Bio", "Nature Str Bio", "Nature Methods"]
Journals = {}
for item in GroupA:
    Journals.setdefault("A",[]).append(item)
for item in GroupB:
    Journals.setdefault("B",[]).append(item)
for item in GroupC:
    Journals.setdefault("C",[]).append(item)

# Set dates for search
today = datetime.datetime.today()
numdays = 15
dateList = []
for x in range (0, numdays):
    dateList.append(today - datetime.timedelta(days = x))
dateList[1:numdays-1] = []
today = dateList[0].strftime("%Y/%m/%d")
lastdate = dateList[1].strftime("%Y/%m/%d")
print 'Retreiving data from ' '%s to %s' % (lastdate,today)


for value in Journals['A']:
    Entrez.email = "email"
    handle = Entrez.esearch(db="pubmed",term="gpcr[TI] AND value[TA]",
sort="pubdate",retmax="10",retmode="xml",datetype="pdat",mindate=lastdate,maxdate=today) 
    record = Entrez.read(handle)
    print(record["IdList"])

I would like to use each "value" of the for loop (in this case journal titles) as a parameter for Entrez.search function. There is no built in parameter for this, so it would have to be inside the term parameter, but it doesn't work as shown.

Once I have an ID list, I will then use Entrez.fetch to retrieve and print the data that I want, but that's another question...

I hope this is clear enough, first question for me! Thanks!


Solution

  • If I understand you correctly, I think this is what you are looking for:

    term="gpcr[TI] AND {}[TA]".format(value)
    

    using this, the each term will be:

    "gpcr[TI] AND Nature[TA]"
    "gpcr[TI] AND Science[TA]"
    "gpcr[TI] AND PNAS[TA]"
    "gpcr[TI] AND JACS[TA]"