#! /usr/bin/env python

import os
import os.path
import sys
import json

def _extractGenome(header):
    headers = header.split('|')
    genome = headers[4].strip()
    pos = genome.find(",")        
    if pos > 0:
        genome = genome[:pos]

    pos = genome.find(" chromosome")
    if pos > 0:
        genome = genome[:pos]
    return genome

def _parseClassified(path):
    res_dict = {}
    f = open(path)
    for line in f:
        j = json.loads(line)

        query = j['query']
        query_header = j['query_header']
        result = j['result']
        ctype = j['type']

        if ctype == "CLASSIFIED":
            r = result[0]
            #print "> " + ctype + "\t" + r['header'] + "\t" + str(r['score'])
            genome = _extractGenome(r['header'])
            if genome in res_dict:
                res_dict[genome] = res_dict[genome] + 1
            else:
                res_dict[genome] = 1

    f.close()

    for k in res_dict:
        print k + "\t" + str(res_dict[k])

def _parseVague(path):
    count=0
    f = open(path)
    for line in f:
        j = json.loads(line)

        query = j['query']
        query_header = j['query_header']
        result = j['result']
        ctype = j['type']

        if ctype == "VAGUE":
            print "> "
            for r in result:
                print ctype + "\t" + r['header'] + "\t" + str(r['score'])

    f.close()

def _parseUnknown(path):
    count=0
    f = open(path)
    for line in f:
        j = json.loads(line)

        query = j['query']
        query_header = j['query_header']
        result = j['result']
        ctype = j['type']

        if ctype == "UNKNOWN":
            print "> " + ctype + query_header

    f.close()

def _parse(path):
    if path.endswith(".result"):
        print "parsing", path
        _parseClassified(path);
        _parseVague(path);
        _parseUnknown(path);
        return True
    return False

def parse(path):
    if os.path.isdir(path):
        for p in os.listdir(path):
            parse(os.path.join(path, p))
    else:
        _parse(path)

def main():
    parse(sys.argv[1])

if __name__ == "__main__":
    main()

@@ -70,284 +70,8 @@
 on%0A%0A
-def _extractGenome(header):%0A    headers = header.split('%7C')%0A    genome = headers%5B4%5D.strip()%0A    pos = genome.find(%22,%22)        %0A    if pos %3E 0:%0A        genome = genome%5B:pos%5D%0A%0A    pos = genome.find(%22 chromosome%22)%0A    if pos %3E 0:%0A        genome = genome%5B:pos%5D%0A    return genome%0A%0A
 def 
@@ -299,33 +299,32 @@
 = j%5B'type'%5D%0A
-%0A
         
 if ctype == 
@@ -315,186 +315,165 @@
     
-if ctype == %22CLASSIFIED%22:%0A            r = result%5B0%5D%0A            #print %22%3E %22 + ctype + %22%5Ct%22 + r%5B'header'%5D + %22%5Ct%22 + str(r%5B'score'%5D)%0A            genome = _extractGenome(r%5B'header'%5D)
+taxon_rank = j%5B'taxon_rank'%5D%0A        taxon_name = j%5B'taxon_name'%5D%0A%0A        if ctype == %22CLASSIFIED%22:%0A            genome = taxon_name + %22(%22 + taxon_rank + %22)%22
 %0A   

Use taxon_rank and taxon_name