bpy15. Writing a query function using Biopython

Using Bio.Entrez.egquery, that we used earlier in bpy12.py, we will encapsulate the table creation into a function, which will write that table to a csv file.

Additionally, we can give a boolean value to indicate, that if additionally, we want to print or not.

In later examples, we will use it as an import. However, it is good to have test code, should the module be run directly. The test code just prints the table returned for ‘asthma’ and saves result to count.csv. We will note that there are more Pubmed and Pubmed Central records than in our previous search.


# query.py

from __future__ import print_function, division
from Bio import Entrez

def query(term, fname, to_print):
Entrez.email = "[email protected]"
handle = Entrez.egquery(term=term)
record = Entrez.read(handle)
handle.close()
X=[]
Y=[]
out=[]
for row in record["eGQueryResult"]:
for i in row:
if i == 'DbName':
Y.append(row[i])
elif i=='Count':
X.append(row[i])
for i in range(len(X)):
if int(X[i])==0:
continue
out.append("%s,%sn" % (Y[i],X[i]))
if fname == None:
pass
else:
with open(fname+'.csv','w') as fout:
fout.writelines(out)
if to_print:
for i in out:
print(i, end = '')

if __name__ == '__main__':
query('asthma','count',True)

# pubmed,149164
# pmc,106784
# mesh,11
# books,7165
# pubmedhealth,1461
# omim,270
# ncbisearch,75
# nuccore,32446
# nucgss,91
# nucest,210
# protein,2559
# genome,1
# structure,390
# dbvar,9
# gene,1036
# sra,8245
# biosystems,125
# unigene,1
# cdd,22
# popset,7
# geoprofiles,731699
# gds,4417
# homologene,18
# pccompound,122
# pcsubstance,2125
# pcassay,3515
# nlmcatalog,2610
# probe,34
# gap,2387
# bioproject,211
# biosample,12542

Leave a Reply

Your email address will not be published. Required fields are marked *