Skip to content

Commit

Permalink
feat: Write metrics tables as outputs (#5)
Browse files Browse the repository at this point in the history
* feat: Write metrics tables as outputs

Signed-off-by: jvfe <[email protected]>

* style: Black files

Signed-off-by: jvfe <[email protected]>

---------

Signed-off-by: jvfe <[email protected]>
  • Loading branch information
jvfe authored May 2, 2024
1 parent af6e0af commit 063c6e7
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 10 deletions.
6 changes: 4 additions & 2 deletions microview/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 32 additions & 8 deletions microview/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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"},
Expand All @@ -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",
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_data/microview_tables/abund_diversity.tsv
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/test_data/microview_tables/beta_pcoa.tsv
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tests/test_data/microview_tables/classified_reads.tsv
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions tests/test_data/microview_tables/common_taxas.tsv
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/test_data/microview_tables/pcoa_variance_explained.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PC Variance Explained
PC1 0.9862442839054677
PC2 0.013755716094532197
PC3 0.0
PC4 0.0

0 comments on commit 063c6e7

Please sign in to comment.