<s> '''<STR_LIT>''' <EOL> import sys <EOL> import CGAT . LCA as LCA <EOL> import CGAT . Experiment as E <EOL> import collections <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( version = "<STR_LIT>" , <EOL> usage = globals ( ) [ "<STR_LIT>" ] ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = E . Start ( parser , argv = argv ) <EOL> if options . output_map : <EOL> found = [ ] <EOL> options . stdout . write ( """<STR_LIT>""" ) <EOL> for lca in LCA . iterate ( options . stdin ) : <EOL> hierarchy = [ lca . kingdom , <EOL> lca . phylum , <EOL> lca . _class , <EOL> lca . order , <EOL> lca . family , <EOL> lca . genus , <EOL> lca . species ] <EOL> if hierarchy in found : <EOL> continue <EOL> else : <EOL> found . append ( hierarchy ) <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( hierarchy ) + "<STR_LIT:\n>" ) <EOL> return <EOL> if options . summarise == "<STR_LIT>" : <EOL> level_counts = collections . defaultdict ( set ) <EOL> total = <NUM_LIT:0> <EOL> nreads_kingdom = <NUM_LIT:0> <EOL> nreads_kingdom_plus = <NUM_LIT:0> <EOL> nreads_phylum = <NUM_LIT:0> <EOL> nreads_phylum_plus = <NUM_LIT:0> <EOL> nreads_class = <NUM_LIT:0> <EOL> nreads_class_plus = <NUM_LIT:0> <EOL> nreads_order = <NUM_LIT:0> <EOL> nreads_order_plus = <NUM_LIT:0> <EOL> nreads_family = <NUM_LIT:0> <EOL> nreads_family_plus = <NUM_LIT:0> <EOL> nreads_genus = <NUM_LIT:0> <EOL> nreads_genus_plus = <NUM_LIT:0> <EOL> nreads_species = <NUM_LIT:0> <EOL> nreads_species_plus = <NUM_LIT:0> <EOL> nreads_subspecies = <NUM_LIT:0> <EOL> nreads_subspecies_plus = <NUM_LIT:0> <EOL> c = E . Counter ( ) <EOL> for lca in LCA . iterate ( options . stdin ) : <EOL> total += <NUM_LIT:1> <EOL> if lca . kingdom != "<STR_LIT>" : <EOL> nreads_kingdom += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . kingdom ) <EOL> else : <EOL> c . kingdom_unmapped += <NUM_LIT:1> <EOL> if lca . kingdom_plus != "<STR_LIT>" : <EOL> nreads_kingdom_plus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . kingdom_plus ) <EOL> else : <EOL> c . kingdom_plus_unmapped += <NUM_LIT:1> <EOL> if lca . phylum != "<STR_LIT>" : <EOL> nreads_phylum += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . phylum ) <EOL> else : <EOL> c . phylum_unmapped += <NUM_LIT:1> <EOL> if lca . phylum_plus != "<STR_LIT>" : <EOL> nreads_phylum_plus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . phylum_plus ) <EOL> else : <EOL> c . phylum_plus_unmapped += <NUM_LIT:1> <EOL> if lca . _class != "<STR_LIT>" : <EOL> nreads_class += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT:class>" ] . add ( lca . _class ) <EOL> else : <EOL> c . class_unmapped += <NUM_LIT:1> <EOL> if lca . _class_plus != "<STR_LIT>" : <EOL> nreads_class_plus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . _class_plus ) <EOL> else : <EOL> c . class_plus_unmapped += <NUM_LIT:1> <EOL> if lca . order != "<STR_LIT>" : <EOL> nreads_order += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . order ) <EOL> else : <EOL> c . order_unmapped += <NUM_LIT:1> <EOL> if lca . order_plus != "<STR_LIT>" : <EOL> nreads_order_plus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . order_plus ) <EOL> else : <EOL> c . order_plus_unmapped += <NUM_LIT:1> <EOL> if lca . family != "<STR_LIT>" : <EOL> nreads_family += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . family ) <EOL> else : <EOL> c . family_unmapped += <NUM_LIT:1> <EOL> if lca . family != "<STR_LIT>" : <EOL> nreads_family_plus == <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . family_plus ) <EOL> else : <EOL> c . family_plus_unmapped += <NUM_LIT:1> <EOL> if lca . genus != "<STR_LIT>" : <EOL> nreads_genus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . genus ) <EOL> else : <EOL> c . genus_unmapped += <NUM_LIT:1> <EOL> if lca . genus_plus != "<STR_LIT>" : <EOL> nreads_genus_plus == <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . genus_plus ) <EOL> else : <EOL> c . genus_plus_unmapped += <NUM_LIT:1> <EOL> if lca . species != "<STR_LIT>" : <EOL> nreads_species += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . species ) <EOL> else : <EOL> c . species_unmapped += <NUM_LIT:1> <EOL> if lca . species_plus != "<STR_LIT>" : <EOL> nreads_species_plus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . species_plus ) <EOL> else : <EOL> c . species_plus_unmapped += <NUM_LIT:1> <EOL> if lca . subspecies != "<STR_LIT>" : <EOL> nreads_subspecies += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . subspecies ) <EOL> else : <EOL> c . subspecies_unmapped += <NUM_LIT:1> <EOL> if lca . subspecies_plus != "<STR_LIT>" : <EOL> nreads_subspecies_plus += <NUM_LIT:1> <EOL> level_counts [ "<STR_LIT>" ] . add ( lca . subspecies_plus ) <EOL> else : <EOL> c . subspecies_plus_unmapped += <NUM_LIT:1> <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) + "<STR_LIT:\n>" ) <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( map ( <EOL> str , [ len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT:class>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> len ( level_counts [ "<STR_LIT>" ] ) , <EOL> nreads_kingdom , <EOL> nreads_phylum , <EOL> nreads_phylum_plus , <EOL> nreads_class , <EOL> nreads_class_plus , <EOL> nreads_order , <EOL> nreads_order_plus , <EOL> nreads_family , <EOL> nreads_family_plus , <EOL> nreads_genus , <EOL> nreads_genus_plus , <EOL> nreads_species , <EOL> nreads_species_plus , <EOL> nreads_subspecies , <EOL> nreads_subspecies_plus ] ) ) + "<STR_LIT:\n>" ) <EOL> elif options . summarise == "<STR_LIT>" : <EOL> unmapped = collections . defaultdict ( int ) <EOL> total = <NUM_LIT:0> <EOL> taxa_counts = { "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT:class>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) , <EOL> "<STR_LIT>" : collections . defaultdict ( int ) } <EOL> c = E . Counter ( ) <EOL> for lca in LCA . iterate ( options . stdin ) : <EOL> total += <NUM_LIT:1> <EOL> if lca . kingdom != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . kingdom ] += <NUM_LIT:1> <EOL> else : <EOL> c . kingdom_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . kingdom_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . kingdom_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . kingdom_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . phylum != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . phylum ] += <NUM_LIT:1> <EOL> else : <EOL> c . phylum_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . phylum_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . phylum_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . phylum_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . _class != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT:class>" ] [ lca . _class ] += <NUM_LIT:1> <EOL> else : <EOL> c . class_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT:class>" ] += <NUM_LIT:1> <EOL> if lca . _class_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . _class_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . class_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . order != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . order ] += <NUM_LIT:1> <EOL> else : <EOL> c . order_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . order_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . order_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . order_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . family != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . family ] += <NUM_LIT:1> <EOL> else : <EOL> c . family_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . family_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . family_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . family_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . genus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . genus ] += <NUM_LIT:1> <EOL> else : <EOL> c . genus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . genus_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . genus_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . genus_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . species != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . species ] += <NUM_LIT:1> <EOL> else : <EOL> c . species_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . species_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . species_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . species_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . subspecies != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . subspecies ] += <NUM_LIT:1> <EOL> else : <EOL> c . subspecies_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> if lca . subspecies_plus != "<STR_LIT>" : <EOL> taxa_counts [ "<STR_LIT>" ] [ lca . subspecies_plus ] += <NUM_LIT:1> <EOL> else : <EOL> c . subspecies_plus_unmapped += <NUM_LIT:1> <EOL> unmapped [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> options . stdout . write ( "<STR_LIT>" ) <EOL> for level , taxa_count in taxa_counts . iteritems ( ) : <EOL> total_level = total - unmapped [ level ] <EOL> for taxa , count in taxa_count . iteritems ( ) : <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( <EOL> [ level , <EOL> taxa , <EOL> str ( count ) , <EOL> str ( float ( count ) / total_level ) , <EOL> str ( float ( count ) / ( float ( total_level ) / <NUM_LIT> ) ) ] ) + "<STR_LIT:\n>" ) <EOL> E . info ( c ) <EOL> elif options . summarise == "<STR_LIT>" : <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( [ "<STR_LIT:id>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:class>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) + "<STR_LIT:\n>" ) <EOL> for lca in LCA . iterate ( options . stdin ) : <EOL> options . stdout . write ( "<STR_LIT:\t>" . join ( [ lca . identifier , <EOL> lca . kingdom , <EOL> lca . kingdom_plus , <EOL> lca . phylum , <EOL> lca . phylum_plus , <EOL> lca . _class , <EOL> lca . _class_plus , <EOL> lca . order , <EOL> lca . order_plus , <EOL> lca . family , <EOL> lca . family_plus , <EOL> lca . genus , <EOL> lca . genus_plus , <EOL> lca . species , <EOL> lca . species_plus , <EOL> lca . subspecies , <EOL> lca . subspecies_plus ] ) + "<STR_LIT:\n>" ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
