#NEWLINE# def relative_abundance_iterator( infile ) : #NEWLINE# #INDENT# '\n    read in rel_ab outfile from metaphlan \n    and return relative abundances for\n    each taxonomic group\n    ' #NEWLINE# taxons = collections . defaultdict ( list ) #NEWLINE# abundances = collections . defaultdict ( list ) #NEWLINE# for line in infile . readlines ( ) : #NEWLINE# #INDENT# data = line [ : ( - 1 ) ] . split ( '\t' ) #NEWLINE# abundance = data [ 1 ] #NEWLINE# entries = data [ 0 ] . split ( '|' ) #NEWLINE# taxon = entries [ ( len ( entries ) - 1 ) ] #NEWLINE# taxon_names = taxon . split ( '__' ) #NEWLINE# if ( len ( taxon_names ) == 1 ) : #NEWLINE# #INDENT# taxons [ taxon_names [ 0 ] ] . append ( taxon_names [ 0 ] ) #NEWLINE# abundances [ taxon_names [ 0 ] ] . append ( abundance ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# taxons [ taxon_names [ 0 ] ] . append ( taxon_names [ 1 ] ) #NEWLINE# abundances [ taxon_names [ 0 ] ] . append ( abundance ) #NEWLINE# #UNINDENT# #UNINDENT# for group in taxons . keys ( ) : #NEWLINE# #INDENT# for abundance in zip ( taxons [ group ] , abundances [ group ] ) : #NEWLINE# #INDENT# if ( group == 'k' ) : #NEWLINE# #INDENT# group = 'kingdom' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( group == 'p' ) : #NEWLINE# #INDENT# group = 'phylum' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( group == 'c' ) : #NEWLINE# #INDENT# group = 'class' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( entries == 'o' ) : #NEWLINE# #INDENT# group = 'order' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( group == 'f' ) : #NEWLINE# #INDENT# group = 'family' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( group == 'g' ) : #NEWLINE# #INDENT# group = 'genus' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( group == 's' ) : #NEWLINE# #INDENT# group = 'species' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# group = group #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# ( yield RelativeAbundance ( ) . read ( group , abundance [ 0 ] , abundance [ 1 ] ) )