diff --git a/microview/cli.py b/microview/cli.py index 158c637..b14f0be 100644 --- a/microview/cli.py +++ b/microview/cli.py @@ -70,9 +70,11 @@ def main(taxonomy: Path, csv_file: Path, output: Path) -> None: tax_results = get_tax_data(reports) # TODO: Improve this double check if parsed_result is not None: - tax_plots = generate_taxo_plots(tax_results, parsed_result["dataframe"]) + tax_plots = generate_taxo_plots( + tax_results, parsed_result["dataframe"], output_path=output + ) else: - tax_plots = generate_taxo_plots(tax_results) + tax_plots = generate_taxo_plots(tax_results, output_path=output) render_base(tax_plots=tax_plots, dir_path=data_source, output_path=output) console.print(f"\n Done!\n", style="bold green") except Exception: diff --git a/microview/plotting.py b/microview/plotting.py index 1d221cb..970df42 100644 --- a/microview/plotting.py +++ b/microview/plotting.py @@ -29,6 +29,17 @@ def export_to_html(fig: Figure, div_id: str) -> str: ) +def write_table(df, output_path, path): + """ + Write a dataframe to a file + """ + dirpath = Path(output_path).parent.resolve() / Path("microview_tables") + Path(dirpath).mkdir(exist_ok=True) + path = dirpath / path + + df.to_csv(path, sep="\t", index=False) + + def merge_with_contrasts(df, contrast_df, left_colname: Optional[str] = "index"): """ Merges a dataframe with the dataframe containing contrasts (or groups) @@ -40,10 +51,12 @@ def merge_with_contrasts(df, contrast_df, left_colname: Optional[str] = "index") return merged_df -def plot_common_taxas(common_taxas_df, **kwargs): +def plot_common_taxas(common_taxas_df, output_path, **kwargs): """ Generate bar plot with most common taxas """ + write_table(common_taxas_df, output_path, "common_taxas.tsv") + return bar( common_taxas_df.sort_values(by=["value", "variable"], ascending=[False, True]), x="index", @@ -60,10 +73,12 @@ def plot_common_taxas(common_taxas_df, **kwargs): ) -def plot_abund_div(abund_div_df, **kwargs): +def plot_abund_div(abund_div_df, output_path, **kwargs): """ Generate scatter plot of Pielou's Evenness and Shannon's Diversity (alpha) """ + write_table(abund_div_df, output_path, "abund_diversity.tsv") + return scatter( abund_div_df, x="Pielou Evenness", @@ -77,10 +92,12 @@ def plot_abund_div(abund_div_df, **kwargs): ) -def plot_beta_pcoa(beta_pcoa, **kwargs): +def plot_beta_pcoa(beta_pcoa, output_path, **kwargs): """ Generate scatter plot of two first coordinates of Beta Diversity PCoA """ + write_table(beta_pcoa, output_path, "beta_pcoa.tsv") + fig = scatter( beta_pcoa, x="PC1", @@ -94,7 +111,7 @@ def plot_beta_pcoa(beta_pcoa, **kwargs): return fig -def generate_taxo_plots(tax_data: Dict, contrast_df=None) -> Dict: +def generate_taxo_plots(tax_data: Dict, contrast_df=None, output_path=None) -> Dict: """ Get all taxonomy plots @@ -121,6 +138,7 @@ def generate_taxo_plots(tax_data: Dict, contrast_df=None) -> Dict: }, template="plotly_white", ) + write_table(tax_data["sample n reads"], output_path, "classified_reads.tsv") assigned.update_layout( xaxis={"categoryorder": "category ascending"}, @@ -146,6 +164,8 @@ def generate_taxo_plots(tax_data: Dict, contrast_df=None) -> Dict: .rename(columns={"index": "PC"}) ) + write_table(var_explained, output_path, "pcoa_variance_explained.tsv") + pcoa_var = line( var_explained, x="PC", @@ -162,25 +182,29 @@ def generate_taxo_plots(tax_data: Dict, contrast_df=None) -> Dict: ] merged_taxas_df = merge_with_contrasts(tax_data["common taxas"], contrast_df) - common_taxas = plot_common_taxas(merged_taxas_df, facet_col="group") + common_taxas = plot_common_taxas( + merged_taxas_df, output_path, facet_col="group" + ) common_taxas.update_xaxes(matches=None) abund_div = plot_abund_div( merge_with_contrasts(tax_data["abund and div"], contrast_df), + output_path, color="group", ) if plot_beta_div: betadiv_pcoa = plot_beta_pcoa( merge_with_contrasts(pcoa_embed, contrast_df, left_colname="sample"), + output_path, color="group", ) else: - common_taxas = plot_common_taxas(tax_data["common taxas"]) + common_taxas = plot_common_taxas(tax_data["common taxas"], output_path) - abund_div = plot_abund_div(tax_data["abund and div"]) + abund_div = plot_abund_div(tax_data["abund and div"], output_path) if plot_beta_div: - betadiv_pcoa = plot_beta_pcoa(pcoa_embed) + betadiv_pcoa = plot_beta_pcoa(pcoa_embed, output_path) common_taxas.update_traces(showlegend=False) common_taxas.update_layout( diff --git a/tests/test_data/microview_tables/abund_diversity.tsv b/tests/test_data/microview_tables/abund_diversity.tsv new file mode 100644 index 0000000..5becf87 --- /dev/null +++ b/tests/test_data/microview_tables/abund_diversity.tsv @@ -0,0 +1,5 @@ +index Shannon Diversity N Taxas Pielou Evenness +kaiju_test_2.txt 1.954647932741941 5 1.2144910453772946 +kaiju_test.txt 1.954647932741941 5 1.2144910453772946 +centrifuge_test.txt 0.029267352029729493 3 0.026640291876956372 +kraken_test.txt 3.0042033815854 181 0.5778984509401328 diff --git a/tests/test_data/microview_tables/beta_pcoa.tsv b/tests/test_data/microview_tables/beta_pcoa.tsv new file mode 100644 index 0000000..516c754 --- /dev/null +++ b/tests/test_data/microview_tables/beta_pcoa.tsv @@ -0,0 +1,5 @@ +sample PC1 PC2 +kaiju_test_2.txt -0.25049691655478007 -0.04160014143773811 +kaiju_test.txt -0.25049691655478046 -0.04160014143773799 +centrifuge_test.txt 0.7486472813642113 -0.00023745578005218948 +kraken_test.txt -0.24765344825465063 0.08343773865552835 diff --git a/tests/test_data/microview_tables/classified_reads.tsv b/tests/test_data/microview_tables/classified_reads.tsv new file mode 100644 index 0000000..8563cf2 --- /dev/null +++ b/tests/test_data/microview_tables/classified_reads.tsv @@ -0,0 +1,9 @@ +index variable value +kaiju_test_2.txt assigned 100.0 +kaiju_test.txt assigned 100.0 +centrifuge_test.txt assigned 0.2763703362505758 +kraken_test.txt assigned 100.0 +kaiju_test_2.txt unassigned 0.0 +kaiju_test.txt unassigned 0.0 +centrifuge_test.txt unassigned 99.72362966374942 +kraken_test.txt unassigned 0.0 diff --git a/tests/test_data/microview_tables/common_taxas.tsv b/tests/test_data/microview_tables/common_taxas.tsv new file mode 100644 index 0000000..c094384 --- /dev/null +++ b/tests/test_data/microview_tables/common_taxas.tsv @@ -0,0 +1,37 @@ +index variable value +kraken_test.txt unclassified +kraken_test.txt root +kraken_test.txt other 21.488 +kraken_test.txt Ustilaginoidea virens nonsegmented virus 2 9.13 +kraken_test.txt Tomato leaf curl alphasatellite 5.64 +kraken_test.txt Siphoviridae +kraken_test.txt Gordonia phage GTE6 38.56 +kraken_test.txt Cymbidium mosaic virus 15.54 +kraken_test.txt Circovirus-like genome SAR-B 9.64 +kaiju_test_2.txt unclassified +kaiju_test_2.txt root +kaiju_test_2.txt other 0.0 +kaiju_test_2.txt Ustilaginoidea virens nonsegmented virus 2 11.7 +kaiju_test_2.txt Tomato leaf curl alphasatellite 6.42 +kaiju_test_2.txt Siphoviridae +kaiju_test_2.txt Gordonia phage GTE6 49.61 +kaiju_test_2.txt Cymbidium mosaic virus 19.92 +kaiju_test_2.txt Circovirus-like genome SAR-B 12.35 +kaiju_test.txt unclassified +kaiju_test.txt root +kaiju_test.txt other 0.0 +kaiju_test.txt Ustilaginoidea virens nonsegmented virus 2 11.7 +kaiju_test.txt Tomato leaf curl alphasatellite 6.42 +kaiju_test.txt Siphoviridae +kaiju_test.txt Gordonia phage GTE6 49.61 +kaiju_test.txt Cymbidium mosaic virus 19.92 +kaiju_test.txt Circovirus-like genome SAR-B 12.35 +centrifuge_test.txt unclassified 99.72 +centrifuge_test.txt root 0.05 +centrifuge_test.txt other 0.0 +centrifuge_test.txt Ustilaginoidea virens nonsegmented virus 2 +centrifuge_test.txt Tomato leaf curl alphasatellite +centrifuge_test.txt Siphoviridae 0.23 +centrifuge_test.txt Gordonia phage GTE6 +centrifuge_test.txt Cymbidium mosaic virus +centrifuge_test.txt Circovirus-like genome SAR-B diff --git a/tests/test_data/microview_tables/pcoa_variance_explained.tsv b/tests/test_data/microview_tables/pcoa_variance_explained.tsv new file mode 100644 index 0000000..7766c31 --- /dev/null +++ b/tests/test_data/microview_tables/pcoa_variance_explained.tsv @@ -0,0 +1,5 @@ +PC Variance Explained +PC1 0.9862442839054677 +PC2 0.013755716094532197 +PC3 0.0 +PC4 0.0