import pandas as pd
from Bio import Entrez
import sys
Entrez.email = 'td1515@ic.ac.uk'
Entrez.api_key = '41267f8592172caaa22ab00ec006c4330208'

#pfl = sys.argv[1] # PflA or PflB
#in_table = pd.read_csv('psiblast-nr-'+pfl+'.tsv', sep='\t') #open psiblast results table
#taxid = [i.split(';')[0] for i in in_table['taxid']] # get the taxid numbers (sometimes there are several sperated by a ';', for slightly differnet strains fo the same bacterium)

def prot_search(query):
	handle = Entrez.esearch(db='protein', term=query)
	record = Entrez.read(handle)
	handle.close()
	uid = record['IdList']
	return uid

def prot_summary(uid):
	handle = Entrez.esummary(db='protein', id=uid, report='full')
	record = Entrez.read(handle)
	handle.close
	return record[0]

def tax_search(query):
	handle = Entrez.esearch(db='taxonomy', term=query)
	record = Entrez.read(handle)
	handle.close()
	uid = record['IdList']
	return uid

def tax_efetch(uid):
	handle = Entrez.efetch(db='taxonomy', id=uid)
	record = Entrez.read(handle)
	handle.close()
	return record[0]

saccver = ['XP_024061949.1']

for ele in saccver:
	d = []
	uid = prot_search(ele)
	info = prot_summary(uid)
	d.append(info['TaxId']) # add taxid 
	d.append(info['Title']) # add name of protein 
	uid2 = tax_efetch(info['TaxId'])
	d.append(uid2['ScientificName']) # add species
	print(uid2['LineageEx'])
	print(uid2['Lineage'])
#	for i in range(len(uid2['LineageEx'])):
#		d.append(uid2['LineageEx'][i]['ScientificName']) # add taxonomy levels, one by one 
#	print(d)
#	s = pd.Series(d)
#	s = s.to_frame().transpose() # make the series a DF, so it will get written as a row
#	with open('psiblast-nr-'+pfl+'-saccver-tax.tsv', 'a+') as f:
#		s.to_csv(f, header=False, sep='\t', index=False) #write row in results table 
#	print(a)
#	a += 1




a = 0
#for ele in taxid:
#	try:
#		info = ent_summary(ele)
#		div = info['Division']
#		genus = info['Genus']
#		species = info['Species']
#		df = pd.Series({'taxid': ele, 'division': div, 'genus': genus, 'species': species})
#		df = df.to_frame().transpose()
#		with open('psiblast-nr-'+pfl+'-taxonomy.tsv', 'a+') as f:
#			df.to_csv(f, header=False, sep='\t')
#		print(a)
#		a += 1
#	except RuntimeError:
#		tx = 'txid'+ele+'[Name Tokens]'
#		uid = ent_search(tx)
#		info = ent_summary(uid)
#		div = info['Division']
#		genus = info['Genus']
#		species = info['Species']
#		df = pd.Series({'taxid': ele, 'division': div, 'genus': genus, 'species': species})
#		df = df.to_frame().transpose()
#		with open('psiblast-nr-'+pfl+'-taxonomy.tsv', 'a+') as f:
#			df.to_csv(f, header=False, sep='\t')
#		print(a)
#		a += 1

	


d3e482bb67488f7163f00f3cec639ff7138d6466
<mask token>


def prot_search(query):
    handle = Entrez.esearch(db='protein', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def prot_summary(uid):
    handle = Entrez.esummary(db='protein', id=uid, report='full')
    record = Entrez.read(handle)
    handle.close
    return record[0]


def tax_search(query):
    handle = Entrez.esearch(db='taxonomy', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def tax_efetch(uid):
    handle = Entrez.efetch(db='taxonomy', id=uid)
    record = Entrez.read(handle)
    handle.close()
    return record[0]


<mask token>

<mask token>


def prot_search(query):
    handle = Entrez.esearch(db='protein', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def prot_summary(uid):
    handle = Entrez.esummary(db='protein', id=uid, report='full')
    record = Entrez.read(handle)
    handle.close
    return record[0]


def tax_search(query):
    handle = Entrez.esearch(db='taxonomy', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def tax_efetch(uid):
    handle = Entrez.efetch(db='taxonomy', id=uid)
    record = Entrez.read(handle)
    handle.close()
    return record[0]


<mask token>
for ele in saccver:
    d = []
    uid = prot_search(ele)
    info = prot_summary(uid)
    d.append(info['TaxId'])
    d.append(info['Title'])
    uid2 = tax_efetch(info['TaxId'])
    d.append(uid2['ScientificName'])
    print(uid2['LineageEx'])
    print(uid2['Lineage'])
<mask token>

<mask token>
Entrez.email = 'td1515@ic.ac.uk'
Entrez.api_key = '41267f8592172caaa22ab00ec006c4330208'


def prot_search(query):
    handle = Entrez.esearch(db='protein', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def prot_summary(uid):
    handle = Entrez.esummary(db='protein', id=uid, report='full')
    record = Entrez.read(handle)
    handle.close
    return record[0]


def tax_search(query):
    handle = Entrez.esearch(db='taxonomy', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def tax_efetch(uid):
    handle = Entrez.efetch(db='taxonomy', id=uid)
    record = Entrez.read(handle)
    handle.close()
    return record[0]


saccver = ['XP_024061949.1']
for ele in saccver:
    d = []
    uid = prot_search(ele)
    info = prot_summary(uid)
    d.append(info['TaxId'])
    d.append(info['Title'])
    uid2 = tax_efetch(info['TaxId'])
    d.append(uid2['ScientificName'])
    print(uid2['LineageEx'])
    print(uid2['Lineage'])
a = 0

import pandas as pd
from Bio import Entrez
import sys
Entrez.email = 'td1515@ic.ac.uk'
Entrez.api_key = '41267f8592172caaa22ab00ec006c4330208'


def prot_search(query):
    handle = Entrez.esearch(db='protein', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def prot_summary(uid):
    handle = Entrez.esummary(db='protein', id=uid, report='full')
    record = Entrez.read(handle)
    handle.close
    return record[0]


def tax_search(query):
    handle = Entrez.esearch(db='taxonomy', term=query)
    record = Entrez.read(handle)
    handle.close()
    uid = record['IdList']
    return uid


def tax_efetch(uid):
    handle = Entrez.efetch(db='taxonomy', id=uid)
    record = Entrez.read(handle)
    handle.close()
    return record[0]


saccver = ['XP_024061949.1']
for ele in saccver:
    d = []
    uid = prot_search(ele)
    info = prot_summary(uid)
    d.append(info['TaxId'])
    d.append(info['Title'])
    uid2 = tax_efetch(info['TaxId'])
    d.append(uid2['ScientificName'])
    print(uid2['LineageEx'])
    print(uid2['Lineage'])
a = 0

import pandas as pd
from Bio import Entrez
import sys
Entrez.email = 'td1515@ic.ac.uk'
Entrez.api_key = '41267f8592172caaa22ab00ec006c4330208'

#pfl = sys.argv[1] # PflA or PflB
#in_table = pd.read_csv('psiblast-nr-'+pfl+'.tsv', sep='\t') #open psiblast results table
#taxid = [i.split(';')[0] for i in in_table['taxid']] # get the taxid numbers (sometimes there are several sperated by a ';', for slightly differnet strains fo the same bacterium)

def prot_search(query):
	handle = Entrez.esearch(db='protein', term=query)
	record = Entrez.read(handle)
	handle.close()
	uid = record['IdList']
	return uid

def prot_summary(uid):
	handle = Entrez.esummary(db='protein', id=uid, report='full')
	record = Entrez.read(handle)
	handle.close
	return record[0]

def tax_search(query):
	handle = Entrez.esearch(db='taxonomy', term=query)
	record = Entrez.read(handle)
	handle.close()
	uid = record['IdList']
	return uid

def tax_efetch(uid):
	handle = Entrez.efetch(db='taxonomy', id=uid)
	record = Entrez.read(handle)
	handle.close()
	return record[0]

saccver = ['XP_024061949.1']

for ele in saccver:
	d = []
	uid = prot_search(ele)
	info = prot_summary(uid)
	d.append(info['TaxId']) # add taxid 
	d.append(info['Title']) # add name of protein 
	uid2 = tax_efetch(info['TaxId'])
	d.append(uid2['ScientificName']) # add species
	print(uid2['LineageEx'])
	print(uid2['Lineage'])
#	for i in range(len(uid2['LineageEx'])):
#		d.append(uid2['LineageEx'][i]['ScientificName']) # add taxonomy levels, one by one 
#	print(d)
#	s = pd.Series(d)
#	s = s.to_frame().transpose() # make the series a DF, so it will get written as a row
#	with open('psiblast-nr-'+pfl+'-saccver-tax.tsv', 'a+') as f:
#		s.to_csv(f, header=False, sep='\t', index=False) #write row in results table 
#	print(a)
#	a += 1




a = 0
#for ele in taxid:
#	try:
#		info = ent_summary(ele)
#		div = info['Division']
#		genus = info['Genus']
#		species = info['Species']
#		df = pd.Series({'taxid': ele, 'division': div, 'genus': genus, 'species': species})
#		df = df.to_frame().transpose()
#		with open('psiblast-nr-'+pfl+'-taxonomy.tsv', 'a+') as f:
#			df.to_csv(f, header=False, sep='\t')
#		print(a)
#		a += 1
#	except RuntimeError:
#		tx = 'txid'+ele+'[Name Tokens]'
#		uid = ent_search(tx)
#		info = ent_summary(uid)
#		div = info['Division']
#		genus = info['Genus']
#		species = info['Species']
#		df = pd.Series({'taxid': ele, 'division': div, 'genus': genus, 'species': species})
#		df = df.to_frame().transpose()
#		with open('psiblast-nr-'+pfl+'-taxonomy.tsv', 'a+') as f:
#			df.to_csv(f, header=False, sep='\t')
#		print(a)
#		a += 1

	

