Question: Parse Ncbixml Output Into A Python List Of Hits
0
gravatar for Zach Powers
2.3 years ago by
Zach Powers270
Zach Powers270 wrote:

I would like to parse an NCBIXML file to obtain a list if the format:

known_results[i]=(query title, (hit_name,hit_name,hit_name....))

However I am having trouble getting the slice operator to work:

    knowns = "output.xml" 
    i=0
    for record in NCBIXML.parse(open(knowns)): 
        print record.query_id
        known_results[i] = record.query_id     
        known_results[i][1] = (align.title for  align in i.record.alignment)     
        i+=1

which results in:

list assignment index out of range

since i can do known_results[1]= "sample text" I think the problem is that I cannot use the slice method with a variable.

Can anyone suggest and alternative way to create this list?

thanks zach cp


crossposted with answer at StackOverflowlink text


There are two good answers on stackoverflow. The first uses list.append(), the second uses dictionaries. THe major problem with my construct is that you cannot assign values to parts of a list that have yet to be created.

ADD COMMENTlink modified 2.3 years ago by Damian Kao10k • written 2.3 years ago by Zach Powers270

Is known_results a list, or a list of lists?

ADD REPLYlink written 2.3 years ago by Niek De Klein2.0k

its a list of lists. the answer on the best ways to do this is on the StackExchange link.

ADD REPLYlink written 2.3 years ago by Zach Powers270
1
gravatar for Damian Kao
2.3 years ago by
Damian Kao10k
UK
Damian Kao10k wrote:

It's probably better to use a dictionary in this case:

knowns = "output.xml" 
known_results = {}
for record in NCBIXML.parse(open(knowns)): 
   print record.query_id
   known_results[record.query_id] = [align.title for align in i.record.alignment]

Now if you want to access your data, you can:

for queryID, alignments in known_results.items():
    print queryID
    for alignment in alignments:
        DO STUFF WITH ALIGNMENT
ADD COMMENTlink written 2.3 years ago by Damian Kao10k
Please log in to add an answer.

Help
Access
  • RSS
  • Stats
  • API

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.0.0
Traffic: 567 users visited in the last hour