From 22a62c94bcb9186046ba3d5ba603fa56e9670e6e Mon Sep 17 00:00:00 2001 From: Douwe Schulte Date: Mon, 30 Oct 2023 11:04:46 +0100 Subject: [PATCH] Slight refactorings --- src-tauri/build.rs | 18 +++---- src-tauri/src/main.rs | 7 +++ src/main.js | 106 +++++++++++++++++++++++------------------- 3 files changed, 75 insertions(+), 56 deletions(-) diff --git a/src-tauri/build.rs b/src-tauri/build.rs index b2efbfe..a46e64a 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -43,15 +43,6 @@ r#"

-    
- -
- - / - 0 -
- -
@@ -60,6 +51,15 @@ r#"
+
+ +
+ + / + 0 +
+ +

   
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 4f90a43..eeb4c9d 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -26,6 +26,12 @@ mod state; type ModifiableState<'a> = tauri::State<'a, std::sync::Mutex>; +#[tauri::command] +fn refresh(state: ModifiableState) -> (usize, usize) { + let state = state.lock().unwrap(); + (state.spectra.len(), state.peptides.len()) +} + #[tauri::command] async fn load_mgf<'a>(path: &'a str, state: ModifiableState<'a>) -> Result { match rustyms::rawfile::mgf::open(path) { @@ -404,6 +410,7 @@ fn main() { peptides: Vec::new(), })) .invoke_handler(tauri::generate_handler![ + refresh, load_mgf, load_identified_peptides, load_clipboard, diff --git a/src/main.js b/src/main.js index 61f11ca..8e031a2 100644 --- a/src/main.js +++ b/src/main.js @@ -12,8 +12,10 @@ async function select_mgf_file(e) { extensions: ['mgf', 'mgf.gz'], name: "*" }] }; - e.dataset.filepath = await window.__TAURI__.dialog.open(properties); - load_mgf(e); + window.__TAURI__.dialog.open(properties).then((result) => { + e.dataset.filepath = result; + load_mgf(e); + }) }; /** @@ -28,8 +30,10 @@ async function select_identified_peptides_file(e) { extensions: ["csv", "csv.gz", "psmtsv", "psmtsv.gz", "fasta", "fasta.gz"], name: "*" }] }; - e.dataset.filepath = await window.__TAURI__.dialog.open(properties); - load_identified_peptides(e); + window.__TAURI__.dialog.open(properties).then((result) => { + e.dataset.filepath = result; + load_identified_peptides(e); + }) }; /** @@ -76,48 +80,46 @@ async function load_clipboard() { navigator.clipboard .readText() .then(async (clipText) => { - try { - let result = await invoke("load_clipboard", { data: clipText }); + invoke("load_clipboard", { data: clipText }).then((result) => { document.querySelector("#spectrum-log").innerText = "Loaded " + result + " spectra"; document.querySelector("#loaded-path").classList.remove("error"); document.querySelector("#loaded-path").innerText = "Clipboard"; document.querySelector("#number-of-scans").innerText = result; spectrum_details(); - } catch (error) { + }).catch((error) => { + document.querySelector("#load-clipboard").classList.remove("loading") console.log(error); document.querySelector("#loaded-path").classList.add("error"); document.querySelector("#loaded-path").innerText = error; - } - document.querySelector("#load-clipboard").classList.remove("loading") + document.querySelector("#load-clipboard").classList.remove("loading") + }) }); } async function spectrum_details() { - try { - let result = await invoke("spectrum_details", { index: Number(document.querySelector("#details-spectrum-index").value) }); - document.querySelector("#spectrum-details").innerText = result; - } catch (error) { + invoke("spectrum_details", { index: Number(document.querySelector("#details-spectrum-index").value) }).then( + (result) => document.querySelector("#spectrum-details").innerText = result + ).catch((error) => { console.log(error); document.querySelector("#spectrum-error").classList.remove("hidden"); document.querySelector("#spectrum-error").innerText = error; document.querySelector("#spectrum-details").innerText = "ERROR"; - } + }) } let displayed_identified_peptide = undefined; async function identified_peptide_details() { - try { - let index = Number(document.querySelector("#details-identified-peptide-index").value); - if (displayed_identified_peptide != index) { - let result = await invoke("identified_peptide_details", { index: index }); + let index = Number(document.querySelector("#details-identified-peptide-index").value); + if (displayed_identified_peptide != index) { + invoke("identified_peptide_details", { index: index }).then((result) => { document.querySelector("#identified-peptide-details").innerHTML = result; displayed_identified_peptide = index; - } - } catch (error) { - console.log(error); - document.querySelector("#spectrum-error").classList.remove("hidden"); - document.querySelector("#spectrum-error").innerText = error; - document.querySelector("#identified-peptide-details").innerText = "ERROR"; + }).catch((error) => { + console.log(error); + document.querySelector("#spectrum-error").classList.remove("hidden"); + document.querySelector("#spectrum-error").innerText = error; + document.querySelector("#identified-peptide-details").innerText = "ERROR"; + }) } } @@ -140,20 +142,19 @@ async function search_peptide() { } async function load_identified_peptide() { - try { - let index = Number(document.querySelector("#details-identified-peptide-index").value); - let result = await invoke("load_identified_peptide", { index: index }); + let index = Number(document.querySelector("#details-identified-peptide-index").value); + invoke("load_identified_peptide", { index: index }).then((result) => { document.querySelector("#peptide").innerText = result.peptide; document.querySelector("#spectrum-charge").value = result.charge; document.querySelector("#spectrum-model").value = result.mode.toLowerCase(); document.querySelector("#details-spectrum-index").value = result.scan_index; console.log(result) - } catch (error) { + }).catch((error) => { console.log(error); document.querySelector("#spectrum-error").classList.remove("hidden"); document.querySelector("#spectrum-error").innerText = error; document.querySelector("#identified-peptide-details").innerText = "ERROR"; - } + }) } @@ -180,22 +181,21 @@ function get_location(id) { async function annotate_spectrum() { document.querySelector("#annotate-button").classList.add("loading"); document.querySelector("#peptide").innerText = document.querySelector("#peptide").innerText.trim(); - try { - var charge = document.querySelector("#spectrum-charge").value == "" ? null : Number(document.querySelector("#spectrum-charge").value); - var noise_threshold = document.querySelector("#noise-threshold").value == "" ? null : Number(document.querySelector("#noise-threshold").value); - var model = [ - [get_location("#model-a-location"), document.querySelector("#model-a-loss").value], - [get_location("#model-b-location"), document.querySelector("#model-b-loss").value], - [get_location("#model-c-location"), document.querySelector("#model-c-loss").value], - [get_location("#model-d-location"), document.querySelector("#model-d-loss").value], - [get_location("#model-v-location"), document.querySelector("#model-v-loss").value], - [get_location("#model-w-location"), document.querySelector("#model-w-loss").value], - [get_location("#model-x-location"), document.querySelector("#model-x-loss").value], - [get_location("#model-y-location"), document.querySelector("#model-y-loss").value], - [get_location("#model-z-location"), document.querySelector("#model-z-loss").value], - [get_location("#model-z-location"), document.querySelector("#model-precursor-loss").value], // First element is discarded - ]; - var result = await invoke("annotate_spectrum", { index: Number(document.querySelector("#details-spectrum-index").value), ppm: Number(document.querySelector("#spectrum-ppm").value), charge: charge, noise_threshold: noise_threshold, model: document.querySelector("#spectrum-model").value, peptide: document.querySelector("#peptide").innerText, cmodel: model }); + var charge = document.querySelector("#spectrum-charge").value == "" ? null : Number(document.querySelector("#spectrum-charge").value); + var noise_threshold = document.querySelector("#noise-threshold").value == "" ? null : Number(document.querySelector("#noise-threshold").value); + var model = [ + [get_location("#model-a-location"), document.querySelector("#model-a-loss").value], + [get_location("#model-b-location"), document.querySelector("#model-b-loss").value], + [get_location("#model-c-location"), document.querySelector("#model-c-loss").value], + [get_location("#model-d-location"), document.querySelector("#model-d-loss").value], + [get_location("#model-v-location"), document.querySelector("#model-v-loss").value], + [get_location("#model-w-location"), document.querySelector("#model-w-loss").value], + [get_location("#model-x-location"), document.querySelector("#model-x-loss").value], + [get_location("#model-y-location"), document.querySelector("#model-y-loss").value], + [get_location("#model-z-location"), document.querySelector("#model-z-loss").value], + [get_location("#model-z-location"), document.querySelector("#model-precursor-loss").value], // First element is discarded + ]; + invoke("annotate_spectrum", { index: Number(document.querySelector("#details-spectrum-index").value), ppm: Number(document.querySelector("#spectrum-ppm").value), charge: charge, noise_threshold: noise_threshold, model: document.querySelector("#spectrum-model").value, peptide: document.querySelector("#peptide").innerText, cmodel: model }).then((result) => { document.querySelector("#spectrum-results-wrapper").innerHTML = result[0]; document.querySelector("#spectrum-fragments").innerHTML = result[1]; document.querySelector("#spectrum-log").innerText = result[2]; @@ -203,7 +203,7 @@ async function annotate_spectrum() { document.querySelector("#spectrum-wrapper").classList.remove("hidden"); // Remove hidden class if this is the first run document.querySelector("#spectrum-error").classList.add("hidden"); SetUpSpectrumInterface(); - } catch (error) { + }).catch((error) => { console.log(error); document.querySelector("#spectrum-error").classList.remove("hidden"); document.querySelector("#spectrum-error").innerHTML = "

" + error.short_description + "

" + error.long_description + "

"; @@ -211,7 +211,7 @@ async function annotate_spectrum() { let Line = error.context.Line; document.querySelector("#peptide").innerHTML = Line.line.slice(0, Line.offset) + "" + Line.line.slice(Line.offset, Line.offset + Line.length) + "" + Line.line.slice(Line.offset + Line.length, Line.line.length); } - } + }) document.querySelector("#annotate-button").classList.remove("loading"); } @@ -250,4 +250,16 @@ window.addEventListener("DOMContentLoaded", () => { .addEventListener("focus", (event) => { event.target.innerHTML = event.target.innerText; }); + + // Refresh interface for hot reload + invoke("refresh").then((result) => { + document.querySelector("#number-of-scans").innerText = result[0]; + if (result[0] > 0) { + spectrum_details(); + } + document.querySelector("#number-of-identified-peptides").innerText = result[1]; + if (result[1] > 0) { + identified_peptide_details(); + } + }) });