def convert(input, output): if not os.path.exists(os.path.dirname(output)): os.makedirs(os.path.dirname(output)) df = pd.read_csv(input, sep="\t", index_col=0) outdf = convert_to_relative_abundance(df) outdf.to_csv(output, sep='\t', float_format="%.5f", na_rep=0, index_label="#OTU ID")
def _convert_files_to_relative_abundances(files: list) -> list: outpath = os.path.dirname(files[0]) outfiles = [] for file in files: base = ".".join(os.path.basename(file).split(".")[:-1]) outfile = os.path.join(outpath, "%s.ra.txt" % base) df = pd.read_csv(file, sep="\t", index_col=0) outdf = convert_to_relative_abundance(df) outdf.to_csv(outfile, sep='\t', float_format="%.5f", na_rep=0, index_label="#OTU ID") outfiles.append(outfile) return outfiles
def test_convert_relative_abundance(self): df = pd.DataFrame([[1, 1, 0], [1, 0, 0], [1, 0, 0]]) df_expected = pd.DataFrame([[1. / 3, 1., np.nan], [1. / 3, 0., np.nan], [1. / 3, 0., np.nan]]) df_ra = convert_to_relative_abundance(df) pd.testing.assert_frame_equal(df_ra, df_expected)