From 21cb8ac18c64b35fc9f1487cd0a126667005f454 Mon Sep 17 00:00:00 2001 From: Zachary Charlop-Powers Date: Tue, 15 Oct 2024 17:33:49 -0400 Subject: [PATCH] refactor base plotting --- src/components/fasta_processing.js | 26 ++++++++ src/fastx/fasta_simple.md | 6 +- src/fastx/fastx_stats.md | 100 +++-------------------------- 3 files changed, 37 insertions(+), 95 deletions(-) diff --git a/src/components/fasta_processing.js b/src/components/fasta_processing.js index 4f16613..8b76cd7 100644 --- a/src/components/fasta_processing.js +++ b/src/components/fasta_processing.js @@ -1,6 +1,7 @@ import { open } from "@tauri-apps/plugin-dialog"; import { invoke } from "npm:@tauri-apps/api/core"; import { html } from "npm:htl"; +import * as Plot from "npm:@observablehq/plot"; function choosefasta() { const selected = open({ @@ -37,3 +38,28 @@ export async function getFastaStats() {

Maximum Length: ${stats.maxlength}

`; } + +export function stats_description(stats) { + return html` +

AVG Len: ${stats.avg_len}

+

Filename: ${stats.filename}

+

Format: ${stats.format}

+

MAX Len: ${stats.max_len}

+

MIN Len: ${stats.min_len}

+

Number of Records Len: ${stats.num_seqs}

+

Sum Len: ${stats.sum_len}

+ `; +} + +export function plot_stats(stats) { + return Plot.rectY( + { length: 10000 }, + Plot.binX({ y: "count" }, { x: stats.contig_lengths }), + ).plot(); +} + +export async function getFastaStatsSeqkit() { + const fasta = await choosefasta(); + const stats = await invoke("get_seqstats", { filename: fasta }); + return stats; +} diff --git a/src/fastx/fasta_simple.md b/src/fastx/fasta_simple.md index 7de9cde..9cdccb8 100644 --- a/src/fastx/fasta_simple.md +++ b/src/fastx/fasta_simple.md @@ -4,21 +4,19 @@ title: "Simple Fasta" Simplest example. uses Noodles to loop throught a fasta file and return the record count and maximul length in the file - ```js import {getFastaStats} from "../components/fasta_processing.js"; const fasta_stats = view( -Inputs.button("Get Fasta Stats: Part 2 ", { +Inputs.button("Get Fasta Stats", { value: null, reduce: () => getFastaStats() })); - ``` ```js +// if null there is a spinning arrow. if (fasta_stats != null) { display(fasta_stats) } - ``` diff --git a/src/fastx/fastx_stats.md b/src/fastx/fastx_stats.md index 5dbfbbb..c147ec4 100644 --- a/src/fastx/fastx_stats.md +++ b/src/fastx/fastx_stats.md @@ -2,74 +2,10 @@ title: "Fastx stats" --- - -```js -import { open } from "@tauri-apps/plugin-dialog"; -import { invoke } from "npm:@tauri-apps/api/core"; - - -// use the tauri open to get absolute file paths -async function choosefasta(){ - const selected = open({ - multiple: false, - filters: [{ - name: 'Fasta', - extensions: ['fa', 'fasta', 'fna']}] - }); - return selected -}; - -async function choosefastq(){ - const selected = open({ - multiple: false, - filters: [{ - name: 'Fastq', - extensions: ['fq', 'fastq']}] - }); - return selected -}; - -``` - - -```js - -// const fasta_stats_seqkit = view(Inputs.button( -// "Get Fasta Stats", -// { -// value: null, -// reduce: () => choosefasta().then((fname) => invoke("get_seqstats", {filename: fname.path})) -// })); - -const fasta_stats_seqkit = view(Inputs.button( - "Get Fasta Stats", - { - value: null, - reduce: () => choosefasta().then((selected) => { - if (selected) { - console.log("selected!"); - return invoke("get_seqstats", { filename: selected }).then(result => { - console.log("Stats received:", result); - return result; // This will update the button's value - }); - } else { - return Promise.reject("No file selected"); - } - }) - } -)); - -let fasta_stats_seqkit_realized = (fasta_stats_seqkit == null) ? "Click Above to Get Fasta Statistics" : fasta_stats_seqkit - -``` - - ```txt - https://downloads.pacbcloud.com/public/dataset/Sequel-IIe-202104/metagenomics/?utm_source=Website&utm_medium=webpage&utm_term=SQII-humanGut-microbiome-pooledStandards&utm_content=datasets&utm_campaign=0000-Website-Leads https://downloads.pacbcloud.com/public/dataset/AAV/2022-ssAAV-scAAV-mix/1-mapped-mixed/ - simple statistics of FASTA/Q files Columns: @@ -94,40 +30,22 @@ Columns: ``` - ## Basic Stats ```js +import {stats_description, plot_stats, getFastaStatsSeqkit} from "../components/fasta_processing.js"; -if (fasta_stats_seqkit_realized !== null) { - html`AVG Len: ${fasta_stats_seqkit_realized.avg_len}` - html`Filename: ${fasta_stats_seqkit_realized.filename}` - html`Format: ${fasta_stats_seqkit_realized.format}` - html`MAX Len: ${fasta_stats_seqkit_realized.max_len}` - html`MIN Len: ${fasta_stats_seqkit_realized.min_len}` - html`Number of Records Len: ${fasta_stats_seqkit_realized.num_seqs}` - html`Sum Len: ${fasta_stats_seqkit_realized.sum_len}` -} - -``` - - -## Plot Histogram +const fasta_stats = view( +Inputs.button("Get Fasta Stats", { + value: null, + reduce: () => getFastaStatsSeqkit() +})); -```js -if (fasta_stats_seqkit_realized !== null) { - display(fasta_stats_seqkit_realized) -} ``` - ```js -if (fasta_stats_seqkit_realized !== null) { - display( - Plot.rectY( - {length: 10000}, - Plot.binX( - {y: "count"}, - {x: fasta_stats_seqkit_realized.contig_lengths})).plot()); +if (fasta_stats != null) { + display(stats_description(fasta_stats)); + display(plot_stats(fasta_stats)); } ```