Skip to content

Commit

Permalink
Update UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Mar 29, 2024
1 parent e88441b commit e4ce8d4
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 484 deletions.
8 changes: 4 additions & 4 deletions src-tauri/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Calculation {
pub samples_omitted: u64,
}

pub async fn aggregate(spreadsheets: &Vec<(PathBuf, u64)>) -> Result<Vec<Calculation>, String> {
pub async fn aggregate(spreadsheets: &Vec<(PathBuf, u64)>) -> Result<Vec<Calculation>, Box<dyn std::error::Error>> {
let mut calculations = vec![];

for spreadsheet in spreadsheets {
Expand All @@ -30,15 +30,15 @@ pub async fn aggregate(spreadsheets: &Vec<(PathBuf, u64)>) -> Result<Vec<Calcula
Ok(calculations)
}

async fn parse_calculations(spreadsheet: &(PathBuf, u64)) -> Result<Vec<Calculation>, String> {
let contents = fs::read(&spreadsheet.0).await.map_err(|err| format!("Error reading calculations file: {}", err))?;
async fn parse_calculations(spreadsheet: &(PathBuf, u64)) -> Result<Vec<Calculation>, Box<dyn std::error::Error>> {
let contents = fs::read(&spreadsheet.0).await?;
let mut rdr = ReaderBuilder::new()
.from_reader(Cursor::new(contents));

let mut calculations = vec![];

for result in rdr.records() {
let record = result.map_err(|err| format!("Error reading record: {}", err))?;
let record = result?;
let calculation = Calculation {
protein: record[0].to_string(),
peptide: record[1].trim().to_string(),
Expand Down
24 changes: 15 additions & 9 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub async fn install_dependencies(app_handle: tauri::AppHandle) -> Result<(), St
pub async fn process_data(
app_handle: tauri::AppHandle,
should_remove_na_calculations: bool,
tolerance_multiplier: f64,
input_file_path: String,
) -> Result<(), String> {
let data_dir = app_handle
Expand All @@ -59,21 +60,26 @@ pub async fn process_data(
.join("dependencies");
let input_file_path = Path::new(&input_file_path);

let (days, mice, labels, peptides) = parse(input_file_path).await.unwrap();
let (
days,
mice,
labels,
peptides
) = parse(input_file_path).await?;

let groups = group_by_peptides(peptides);
let groups = group_by_na_columns(groups);
let groups = group_by_na_columns(group_by_peptides(tolerance_multiplier, peptides));

let datasets = serialize(
should_remove_na_calculations,
&data_dir,
days,
mice,
labels,
groups,
).await.unwrap();

let calculations = analyze(&dependencies_dir, &data_dir, &datasets).await.unwrap();
let calculations = aggregate(&calculations).await.unwrap();
let calculations = analyze(&dependencies_dir, &data_dir, &datasets).await?;
let calculations = aggregate(&calculations).await.map_err(|e| e.to_string())?;

let input_file_name = input_file_path
.file_stem()
Expand All @@ -82,12 +88,12 @@ pub async fn process_data(
.into_owned();

let file_path = FileDialogBuilder::new()
.set_file_name(&format!("{input_file_name}.RateConst.csv"))
.add_filter("Output CSV File", &vec!["csv"])
.save_file();
.set_file_name(&format!("{input_file_name}.RateConst.csv"))
.add_filter("Output CSV File", &vec!["csv"])
.save_file();

if let Some(file_path) = file_path {
serialize_calculations(&file_path, &calculations).unwrap();
serialize_calculations(&file_path, &calculations).map_err(|e| e.to_string())?;
}


Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/grouper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct PeptideGroup {
pub na_columns: Vec<bool>,
}

pub fn group_by_peptides(peptides: Vec<Peptide>) -> Vec<PeptideGroup> {
pub fn group_by_peptides(tolerance_multiplier: f64, peptides: Vec<Peptide>) -> Vec<PeptideGroup> {
let mut sorted_peptides = peptides;
sorted_peptides.sort_by(|a, b| {
a.name.cmp(&b.name).then_with(|| {
Expand All @@ -24,7 +24,7 @@ pub fn group_by_peptides(peptides: Vec<Peptide>) -> Vec<PeptideGroup> {
current_group.push(peptide);
} else if peptide.name == current_group.last().unwrap().name {
// Dynamically determine the threshold based on the current group
let threshold = dynamic_threshold(&current_group);
let threshold = calc_std_deviation(&current_group) * tolerance_multiplier;
let last_ratio = current_group.last().unwrap().mass_charge_ratio;
if (peptide.mass_charge_ratio - last_ratio).abs() < threshold {
current_group.push(peptide);
Expand All @@ -47,7 +47,7 @@ pub fn group_by_peptides(peptides: Vec<Peptide>) -> Vec<PeptideGroup> {
}


fn dynamic_threshold(peptides: &[Peptide]) -> f64 {
fn calc_std_deviation(peptides: &[Peptide]) -> f64 {
if peptides.len() <= 1 {
return f64::INFINITY; // If there's only one peptide, no need for a threshold.
}
Expand Down
1 change: 0 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod commands;
pub mod processor;
pub mod parser;
pub mod grouper;
pub mod serializer;
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub struct Peptide {
pub async fn parse(spreadsheet: &Path)
-> Result<
(Vec<Day>, Vec<Mouse>, Vec<Label>, Vec<Peptide>),
Box<dyn std::error::Error>
String
>
{
let contents = fs::read(spreadsheet).await?;
let contents = fs::read(spreadsheet).await.map_err(|_| "Failed to read file")?;
let mut rdr = ReaderBuilder::new()
.has_headers(false)
.from_reader(Cursor::new(contents));
Expand All @@ -35,14 +35,14 @@ pub async fn parse(spreadsheet: &Path)
Ok((days, mice, labels, peptides))
}

fn extract_peptides(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<Vec<Peptide>, Box<dyn std::error::Error>> {
fn extract_peptides(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<Vec<Peptide>, String> {
let mut peptides = vec![];

for result in rdr.records() {
let record = result?;
let record = result.map_err(|_| "Failed to read row from spreadsheet")?;
let protein = record[0].to_string();
let name = record[1].to_string();
let charge_mass_ratio = record[2].parse::<f64>()?;
let charge_mass_ratio = record[2].parse::<f64>().map_err(|_| "Failed to parse charge/mass ratio")?;
let intensities = record.iter().skip(3).map(|value| {
if value == "#N/A" {
None
Expand All @@ -63,14 +63,14 @@ fn extract_peptides(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<Vec<Peptide>, B
Ok(peptides)
}

fn extract_headers(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<(Vec<Day>, Vec<Mouse>, Vec<Label>), Box<dyn std::error::Error>> {
fn extract_headers(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<(Vec<Day>, Vec<Mouse>, Vec<Label>), String> {
let mut non_empty_row_count = 0;
let mut days = vec![];
let mut mice = vec![];
let mut labels = vec![];

for row in rdr.records() {
let record = row?;
let record = row.map_err(|_| "Failed to read row from spreadsheet")?;
if record.iter().any(|field| !field.is_empty()) {
non_empty_row_count += 1;

Expand Down
41 changes: 0 additions & 41 deletions src-tauri/src/processor/aggregator.rs

This file was deleted.

47 changes: 0 additions & 47 deletions src-tauri/src/processor/executor.rs

This file was deleted.

47 changes: 0 additions & 47 deletions src-tauri/src/processor/helpers.rs

This file was deleted.

Loading

0 comments on commit e4ce8d4

Please sign in to comment.