def write_relative_abundance(rel_abd, biomf, out_fn, sort_by=None):
with open(out_fn, '<STR_LIT:w>') as out_f:<EOL><INDENT>sids = sorted(set(biomf.ids()), key=sort_by)<EOL>out_f.write('<STR_LIT>'.format('<STR_LIT:\t>'.join(sids)))<EOL>for otuid in biomf.ids(axis="<STR_LIT>"):<EOL><INDENT>otuName = oc.otu_name(biomf.metadata(otuid, "<STR_LIT>")<EOL>["<STR_LIT>"])<EOL>sabd = [str(rel_abd[sid][otuid])<EOL>if sid in rel_abd and otuid in rel_abd[sid] else '<STR_LIT:0>'<EOL>for sid in sids]<EOL>out_f.write('<STR_LIT>'.format(otuName, '<STR_LIT:\t>'.join(sabd)))<EOL><DEDENT><DEDENT>
Given a BIOM table, calculate per-sample relative abundance for
each OTU and write out to a tab-separated file listing OTUs as
rows and Samples as columns.
:type biom: biom object
:param biom: BIOM-formatted OTU/Sample abundance data
:type out_fn: str
:param out_fn: The full path to the desired output file.
:type sort_by: function
:param sort_by: A function acting as a sorting key that will determine
                 the order in which the Sample IDs appear as columns in
                 the output file.