Skip to content

Commit

Permalink
Sped up build time file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
douweschulte committed Oct 23, 2023
1 parent a1f9a5a commit 7b6ebff
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 47 deletions.
38 changes: 20 additions & 18 deletions src/build/atomic_masses.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::OsString, fs, path::Path};
use std::{ffi::OsString, io::BufWriter, io::Write, path::Path};

use crate::Element;

Expand Down Expand Up @@ -107,25 +107,27 @@ pub fn build_atomic_masses(out_dir: &OsString, _debug: bool) -> Result<(), Strin
});

let dest_path = Path::new(&out_dir).join("elements.rs");
fs::write(
dest_path,
format!(
"#[allow(clippy::all, clippy::pedantic, clippy::nursery)]\n/// The data for all the elements (atomic_mass/monoisotopic mass, atomic_weight, isotopes: (num, mass, abundance))\npub const ELEMENTAL_DATA: &[(Option<f64>, Option<f64>, &[(u16, f64, f64)])] = &[
{}
];",
combined_data.fold(String::new(), |acc, element| format!(
"{}\n({:?}, {:?}, &[{}]),",
acc,
element.0,
element.1,
element.2.iter().fold(String::new(), |acc, i| format!(
"{}({},{:.1},{:.1}),",
acc, i.0, i.1, i.2
))
))
),
let file = std::fs::File::create(dest_path).unwrap();
let mut writer = BufWriter::new(file);
writeln!(
writer,
"#[allow(clippy::all, clippy::pedantic, clippy::nursery)]\n/// The data for all the elements (atomic_mass/monoisotopic mass, atomic_weight, isotopes: (num, mass, abundance))\npub const ELEMENTAL_DATA: &[(Option<f64>, Option<f64>, &[(u16, f64, f64)])] = &["
)
.unwrap();
for element in combined_data {
writeln!(
writer,
"({:?}, {:?}, &[{}]),",
element.0,
element.1,
element.2.iter().fold(String::new(), |acc, i| format!(
"{}({},{:.1},{:.1}),",
acc, i.0, i.1, i.2
))
)
.unwrap();
}
writeln!(writer, "];").unwrap();

Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions src/build/gnome.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{collections::HashMap, ffi::OsString, io::BufWriter, path::Path};
use std::{collections::HashMap, ffi::OsString, io::BufWriter, io::Write, path::Path};

use crate::{build::csv::parse_csv, glycan::GlycanStructure};
use std::io::Write;

use super::obo::OboOntology;

Expand Down
21 changes: 11 additions & 10 deletions src/build/psi_mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::OsString, fs, path::Path};
use std::{ffi::OsString, io::BufWriter, io::Write, path::Path};

use crate::{formula::MolecularFormula, ELEMENT_PARSE_LIST};

Expand All @@ -9,18 +9,19 @@ use super::{

pub fn build_psi_mod_ontology(out_dir: &OsString, debug: bool) {
let mods = parse_psi_mod(debug);

let dest_path = Path::new(&out_dir).join("psi-mod.rs");
fs::write(
dest_path,
format!(
"pub const PSI_MOD_ONTOLOGY: &[(usize, &str, Modification)] = &[
{}
];",
mods.iter()
.fold(String::new(), |acc, m| format!("{}\n{},", acc, m.to_code()))
),
let file = std::fs::File::create(dest_path).unwrap();
let mut writer = BufWriter::new(file);
writeln!(
writer,
"pub const PSI_MOD_ONTOLOGY: &[(usize, &str, Modification)] = &["
)
.unwrap();
for m in mods {
writeln!(writer, "{},", m.to_code()).unwrap();
}
writeln!(writer, "];").unwrap();
}

fn parse_psi_mod(_debug: bool) -> Vec<OntologyModification> {
Expand Down
21 changes: 11 additions & 10 deletions src/build/unimod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::OsString, fs, iter, path::Path};
use std::{ffi::OsString, io::BufWriter, io::Write, iter, path::Path};

use regex::Regex;

Expand All @@ -11,18 +11,19 @@ use super::{

pub fn build_unimod_ontology(out_dir: &OsString, debug: bool) {
let mods = parse_unimod(debug);

let dest_path = Path::new(&out_dir).join("unimod.rs");
fs::write(
dest_path,
format!(
"pub const UNIMOD_ONTOLOGY: &[(usize, &str, Modification)] = &[
{}
];",
mods.iter()
.fold(String::new(), |acc, m| format!("{}\n{},", acc, m.to_code()))
),
let file = std::fs::File::create(dest_path).unwrap();
let mut writer = BufWriter::new(file);
writeln!(
writer,
"pub const UNIMOD_ONTOLOGY: &[(usize, &str, Modification)] = &["
)
.unwrap();
for m in mods {
writeln!(writer, "{},", m.to_code()).unwrap();
}
writeln!(writer, "];").unwrap();
}

fn parse_unimod(_debug: bool) -> Vec<OntologyModification> {
Expand Down
14 changes: 7 additions & 7 deletions src/modification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,20 @@ fn parse_single_modification(
.with_long_description("The supplied PSI-MOD accession number is not an existing modification"))
}
("u", tail) => find_name_in_ontology(tail, UNIMOD_ONTOLOGY)
.map_err(|_| numerical_mod(tail))
.map_err(|()| numerical_mod(tail))
.flat_err()
.map(Some)
.map_err(|_| basic_error
.with_long_description("This modification cannot be read as a Unimod name or numerical modification")),
("m", tail) => find_name_in_ontology(tail, PSI_MOD_ONTOLOGY)
.map_err(|_| numerical_mod(tail))
.map_err(|()| numerical_mod(tail))
.flat_err()
.map(Some)
.map_err(|_| basic_error
.with_long_description("This modification cannot be read as a PSI-MOD name or numerical modification")),
("gno", tail) => find_name_in_ontology(tail, GNOME_ONTOLOGY)
.map(Some)
.map_err(|_| basic_error
.map_err(|()| basic_error
.with_long_description("This modification cannot be read as a GNO name")),
("formula", tail) => Ok(Some(Modification::Formula(
parse_molecular_formula_pro_forma(tail).map_err(|e| basic_error
Expand All @@ -251,10 +251,10 @@ fn parse_single_modification(
("obs", tail) => numerical_mod(tail).map(Some).map_err(|_| basic_error
.with_long_description("This modification cannot be read as a numerical modification")),
(_, _tail) => find_name_in_ontology(full.0, UNIMOD_ONTOLOGY)
.map_err(|_| find_name_in_ontology(full.0, PSI_MOD_ONTOLOGY))
.map_err(|()| find_name_in_ontology(full.0, PSI_MOD_ONTOLOGY))
.flat_err()
.map(Some)
.map_err(|_|
.map_err(|()|
CustomError::error(
"Invalid modification",
"Rustyms does not support these types of modification (yet)",
Expand All @@ -265,9 +265,9 @@ fn parse_single_modification(
Ok(None)
} else {
find_name_in_ontology(full.0, UNIMOD_ONTOLOGY)
.map_err(|_| find_name_in_ontology(full.0, PSI_MOD_ONTOLOGY))
.map_err(|()| find_name_in_ontology(full.0, PSI_MOD_ONTOLOGY))
.flat_err()
.map_err(|_| numerical_mod(full.0))
.map_err(|()| numerical_mod(full.0))
.flat_err()
.map(Some)
.map_err(|_|
Expand Down

0 comments on commit 7b6ebff

Please sign in to comment.