This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Biopython Parsing Of Blast Results - String Format Issue

Hi Biostar,

I have two fasta files that I have blasted against one another and I am trying to make a list (dicitonary) of the top hits in a simple format of Query:Hit using Biopython. I am running into a an error, however, with the string's format. Here is an the script:

test_dictionary={}
blast_records = NCBIXML.parse(open(outfile))
for blast_record in blast_records:
    for alignment in blast_record.alignments:
        for hsp in alignment.hsps:
          test_dictionary.update({blast_record.query:alignment.title})

and here is an example dictionary entry where the u'' surrounds the value:

u'HA9WEQA08JTIW5': u'gnl|BL_ORD_ID|100 PhosphataseA'

however if I use the print command the values appear correct:

print alignment.title
gnl|BL_ORD_ID|100 PhosphataseA

I am sure this is a simple problem and results in my lack of understanding of precisely how Biopython stores its information. But any suggestions would be appreciated.

thanks zach cp

Edit *** as per DK's answer I ended up using this formulation where I split the output and keep the gene name:

 test_dictionary.update[str(blast_record.query)] = str(alignment.title).split()[1]
biopython

You might not want to split it like that. If the gene name is multiple words, you'll only get the first word. I've edited my post to get just the gene name.

The strange u thing is to mark a Unicode string in Python 2

1 answer

I am not exactly sure what the problem is. Are you saying the entries that are getting inserted into the dictionary is showing up with u'' surrounding the string?

If you want a string representation of anything in biopython to be saved, always cast it as a String just to be safe.

So instead of:

test_dictionary.update({blast_record.query:alignment.title})

do this:

test_dictionary.update({str(blast_record.query):str(alignment.title)})

or you can really just do this:

test_dictionary[str(blast_record.query)] = str(alignment.title)

To get just the gene name:

test_dictionary[str(blast_record.query)] = ' '.join(str(alignment.title).split()[1:])

thanks DK. I am learning some basic programming backwards by writing scripts that are getting progressively better. Sometimes the things that are obvious to others are tough to figure out.

Log in to answer this question.