Thanks for the pointer to Entrez Direct. I used the workflow you outlined to fetch all ids and then iterate through to find a match for the ID used in the list of aliases. Using code like
import json
from Bio import Entrez
Entrez.email = "myemail@myinstitution.org"
def main(gene_name):
most_likely_entry = Entrez.esearch(db="gene",term="{gene_name} [Preferred Symbol] AND 9606 [Taxonomy ID]".format(gene_name=gene_name),retmode="json")
most_likely_entry_json = json.loads(most_likely_entry.read())
my_ids = most_likely_entry_json['esearchresult']['idlist']
if my_ids == []:
all_ids_entries = Entrez.esearch(db="gene",term="{gene_name} AND 9606 [Taxonomy ID]".format(gene_name=gene_name),retmode="json")
all_ids_json = json.loads(all_ids_entries.read())
all_ids = all_ids_json['esearchresult']['idlist']
# print all_ids
for an_iden in all_ids:
record_with_aliases = Entrez.efetch(db="gene",id=an_iden,retmode="json")
aliases = []
for line in record_with_aliases:
if line.startswith("Official Symbol:"):
aliases.append(line.split("and Name")[0].split(":")[1])
if line.startswith("Other Aliases:"):
for x in [y.strip() for y in [x.strip() for x in line.split(":")[1:]][0].split(",")]:
aliases.append(x)
# print aliases
if gene_name in aliases:
return aliases[0]
elif gene_name.replace(" ","") in aliases:
return aliases[0]
elif gene_name.replace("-","") in aliases:
return aliases[0]
else:
continue
return "NOT FOUND"
else:
# We got the ID with the Preferred Symbol lookup
print "Got ID:",my_ids
for an_iden in my_ids:
record_with_aliases = Entrez.efetch(db="gene",id=an_iden,retmode="json")
for line in record_with_aliases:
if line.startswith("Other Aliases:"):
print line.split(":")[1:]
entry = record_with_aliases.read()
return entry