Skip to content

Commit

Permalink
Merge pull request #1405 from nextstrain/chore/upgrade-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-aksamentov authored Feb 13, 2024
2 parents 3195eca + 95cf6d5 commit 6d3d7c5
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 85 deletions.
10 changes: 9 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ rustflags = [
"-Wclippy::zero_sized_map_values",
#
# Disable some of the clippy lints
"-Aclippy::absolute_paths",
"-Aclippy::allow_attributes",
"-Aclippy::arithmetic-side-effects",
"-Aclippy::arithmetic_side_effects",
"-Aclippy::as_conversions",
"-Aclippy::assign_op_pattern",
"-Aclippy::bool-to-int-with-if",
Expand All @@ -207,13 +210,14 @@ rustflags = [
"-Aclippy::implicit_return",
"-Aclippy::inconsistent_digit_grouping",
"-Aclippy::indexing_slicing",
"-Aclippy::integer_arithmetic",
"-Aclippy::integer_division",
"-Aclippy::iter_nth_zero",
"-Aclippy::large_digit_groups",
"-Aclippy::len_zero",
"-Aclippy::let_underscore_must_use",
"-Aclippy::manual-string-new",
"-Aclippy::match_same_arms",
"-Aclippy::min_ident_chars",
"-Aclippy::missing-assert-message",
"-Aclippy::missing_docs_in_private_items",
"-Aclippy::missing_errors_doc",
Expand All @@ -226,6 +230,8 @@ rustflags = [
"-Aclippy::modulo_arithmetic",
"-Aclippy::must_use_candidate",
"-Aclippy::needless_for_each",
"-Aclippy::needless_raw_string_hashes",
"-Aclippy::needless_raw_strings",
"-Aclippy::new_without_default",
"-Aclippy::non_ascii_literal",
"-Aclippy::option_if_let_else",
Expand All @@ -245,9 +251,11 @@ rustflags = [
"-Aclippy::shadow_unrelated",
"-Aclippy::should_implement_trait",
"-Aclippy::similar_names",
"-Aclippy::single_call_fn",
"-Aclippy::single_char_lifetime_names",
"-Aclippy::std-instead-of-alloc",
"-Aclippy::std_instead_of_core",
"-Aclippy::struct_field_names",
"-Aclippy::suboptimal_flops",
"-Aclippy::too_many_arguments",
"-Aclippy::too_many_lines",
Expand Down
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
allow-dbg-in-tests = true
allow-expect-in-tests = true
allow-unwrap-in-tests = true
avoid-breaking-exported-api = false
cognitive-complexity-threshold = 30
13 changes: 6 additions & 7 deletions packages/nextclade-cli/src/cli/nextclade_dataset_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ pub fn nextclade_dataset_get(
) -> Result<(), Report> {
let verbose = log::max_level() > LevelFilter::Info;

let mut http = HttpClient::new(server, proxy_config, verbose)?;
let dataset = dataset_http_get(&mut http, name, tag)?;
let http = HttpClient::new(server, proxy_config, verbose)?;
let dataset = dataset_http_get(&http, name, tag)?;

if let Some(output_dir) = &output_dir {
dataset_dir_download(&mut http, &dataset, output_dir)?;
dataset_dir_download(&http, &dataset, output_dir)?;
} else if let Some(output_zip) = &output_zip {
dataset_zip_download(&mut http, &dataset, output_zip)?;
} else {
dataset_zip_download(&http, &dataset, output_zip)?;
}

Ok(())
}

pub fn dataset_http_get(http: &mut HttpClient, name: impl AsRef<str>, tag: &Option<String>) -> Result<Dataset, Report> {
pub fn dataset_http_get(http: &HttpClient, name: impl AsRef<str>, tag: &Option<String>) -> Result<Dataset, Report> {
let name = name.as_ref();
let tag = tag.as_ref();

Expand Down Expand Up @@ -100,7 +99,7 @@ pub fn dataset_http_get(http: &mut HttpClient, name: impl AsRef<str>, tag: &Opti
}

pub fn dataset_file_http_get(
http: &mut HttpClient,
http: &HttpClient,
dataset: &Dataset,
filename: impl AsRef<str>,
) -> Result<String, Report> {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextclade-cli/src/cli/nextclade_dataset_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub fn nextclade_dataset_list(
) -> Result<(), Report> {
let verbose = log::max_level() > LevelFilter::Info;

let mut http = HttpClient::new(&server, &proxy_config, verbose)?;
let DatasetsIndexJson { collections, .. } = download_datasets_index_json(&mut http)?;
let http = HttpClient::new(&server, &proxy_config, verbose)?;
let DatasetsIndexJson { collections, .. } = download_datasets_index_json(&http)?;

let filtered = collections
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions packages/nextclade-cli/src/cli/nextclade_seq_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub fn nextclade_seq_sort(args: &NextcladeSortArgs) -> Result<(), Report> {
MinimizerIndexJson::from_path(input_minimizer_index_json)
} else {
// Otherwise fetch from dataset server
let mut http = HttpClient::new(server, proxy_config, verbose)?;
let index = download_datasets_index_json(&mut http)?;
let http = HttpClient::new(server, proxy_config, verbose)?;
let index = download_datasets_index_json(&http)?;
let minimizer_index_path = index
.minimizer_index
.iter()
Expand Down
36 changes: 16 additions & 20 deletions packages/nextclade-cli/src/dataset/dataset_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use nextclade::{make_error, make_internal_error, o};
use rayon::iter::ParallelIterator;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader, Read, Seek, Write};
use std::io::{BufReader, Cursor, Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use zip::ZipArchive;
Expand Down Expand Up @@ -50,19 +50,19 @@ pub fn nextclade_get_inputs(
}

#[inline]
pub fn download_datasets_index_json(http: &mut HttpClient) -> Result<DatasetsIndexJson, Report> {
pub fn download_datasets_index_json(http: &HttpClient) -> Result<DatasetsIndexJson, Report> {
let data_bytes = http.get("/index.json")?;
let data_str = String::from_utf8(data_bytes)?;
DatasetsIndexJson::from_str(data_str)
}

pub fn dataset_zip_fetch(http: &mut HttpClient, dataset: &Dataset) -> Result<Vec<u8>, Report> {
pub fn dataset_zip_fetch(http: &HttpClient, dataset: &Dataset) -> Result<Vec<u8>, Report> {
http
.get(&dataset.file_path("dataset.zip"))
.wrap_err_with(|| format!("When fetching zip file for dataset '{}'", dataset.path))
}

pub fn dataset_zip_download(http: &mut HttpClient, dataset: &Dataset, output_file_path: &Path) -> Result<(), Report> {
pub fn dataset_zip_download(http: &HttpClient, dataset: &Dataset, output_file_path: &Path) -> Result<(), Report> {
let mut file =
create_file_or_stdout(output_file_path).wrap_err_with(|| format!("When opening file {output_file_path:?}"))?;

Expand Down Expand Up @@ -127,9 +127,9 @@ pub fn dataset_zip_load(
})
}

pub fn dataset_dir_download(http: &mut HttpClient, dataset: &Dataset, output_dir: &Path) -> Result<(), Report> {
pub fn dataset_dir_download(http: &HttpClient, dataset: &Dataset, output_dir: &Path) -> Result<(), Report> {
let mut content = dataset_zip_fetch(http, dataset)?;
let mut reader = std::io::Cursor::new(content.as_mut_slice());
let mut reader = Cursor::new(content.as_mut_slice());
let mut zip = ZipArchive::new(&mut reader)?;

ensure_dir(output_dir).wrap_err_with(|| format!("When creating directory {output_dir:#?}"))?;
Expand Down Expand Up @@ -278,6 +278,7 @@ pub fn dataset_individual_files_load(
}
}

#[allow(clippy::struct_field_names)]
pub struct DatasetFilePaths<'a> {
input_ref: &'a Path,
input_tree: &'a Option<PathBuf>,
Expand All @@ -286,7 +287,7 @@ pub struct DatasetFilePaths<'a> {
}

pub fn read_from_path_or_url(
http: &mut HttpClient,
http: &HttpClient,
dataset: &Dataset,
filepath: &Option<impl AsRef<Path>>,
url: &Option<String>,
Expand All @@ -304,18 +305,18 @@ pub fn dataset_str_download_and_load(
cdses: &Option<Vec<String>>,
) -> Result<NextcladeParams, Report> {
let verbose = log::max_level() > LevelFilter::Info;
let mut http = HttpClient::new(&run_args.inputs.server, &ProxyConfig::default(), verbose)?;
let http = HttpClient::new(&run_args.inputs.server, &ProxyConfig::default(), verbose)?;

let name = run_args
.inputs
.dataset_name
.as_ref()
.expect("Dataset name is expected, but got 'None'");

let dataset = dataset_http_get(&mut http, name, &None)?;
let dataset = dataset_http_get(&http, name, &None)?;

let virus_properties = read_from_path_or_url(
&mut http,
&http,
&dataset,
&run_args.inputs.input_pathogen_json,
&Some(o!("pathogen.json")),
Expand All @@ -325,7 +326,7 @@ pub fn dataset_str_download_and_load(
.ok_or_else(|| eyre!("Required file not found in dataset: 'pathogen.json'. Please report it to dataset authors."))?;

let ref_record = read_from_path_or_url(
&mut http,
&http,
&dataset,
&run_args.inputs.input_ref,
&Some(dataset.files.reference.clone()),
Expand All @@ -334,7 +335,7 @@ pub fn dataset_str_download_and_load(
.wrap_err("When reading reference sequence from dataset")?;

let gene_map = read_from_path_or_url(
&mut http,
&http,
&dataset,
&run_args.inputs.input_annotation,
&dataset.files.genome_annotation,
Expand All @@ -344,14 +345,9 @@ pub fn dataset_str_download_and_load(
.map(|gene_map| filter_gene_map(gene_map, cdses))
.unwrap_or_default();

let tree = read_from_path_or_url(
&mut http,
&dataset,
&run_args.inputs.input_tree,
&dataset.files.tree_json,
)?
.map_ref_fallible(AuspiceTree::from_str)
.wrap_err("When reading reference tree from dataset")?;
let tree = read_from_path_or_url(&http, &dataset, &run_args.inputs.input_tree, &dataset.files.tree_json)?
.map_ref_fallible(AuspiceTree::from_str)
.wrap_err("When reading reference tree from dataset")?;

Ok(NextcladeParams {
ref_record,
Expand Down
2 changes: 1 addition & 1 deletion packages/nextclade-cli/src/dataset/dataset_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn format_dataset_table(filtered: &[Dataset]) -> String {

table.set_header([o!("name"), o!("attributes"), o!("versions")]);

for dataset in filtered.iter() {
for dataset in filtered {
let Dataset {
path,
shortcuts,
Expand Down
3 changes: 2 additions & 1 deletion packages/nextclade-cli/src/io/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use nextclade::utils::info::{this_package_name, this_package_version_str};
use reqwest::blocking::Client;
use reqwest::{Method, Proxy};
use std::str::FromStr;
use std::time::Duration;
use url::Url;

#[derive(Parser, Debug, Default)]
Expand Down Expand Up @@ -62,7 +63,7 @@ impl HttpClient {

let client = client_builder
.connection_verbose(verbose)
.connect_timeout(Some(std::time::Duration::from_secs(60)))
.connect_timeout(Some(Duration::from_secs(60)))
.user_agent(user_agent)
.build()?;

Expand Down
4 changes: 2 additions & 2 deletions packages/nextclade/benches/bench_seed_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use nextclade::align::params::AlignPairwiseParams;
use nextclade::align::seed_alignment::create_alignment_band;
use nextclade::align::seed_match::{get_seed_matches_maybe_reverse_complement, CodonSpacedIndex, SeedMatchesResult};
use nextclade::alphabet::nuc::to_nuc_seq;
use nextclade::alphabet::nuc::{Nuc, to_nuc_seq};

pub fn bench_seed_alignment(c: &mut Criterion) {
let params = AlignPairwiseParams::default();
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn bench_seed_alignment(c: &mut Criterion) {
group.finish();
}

fn sequence_from_path(path: PathBuf) -> Vec<nextclade::alphabet::nuc::Nuc> {
fn sequence_from_path(path: PathBuf) -> Vec<Nuc> {
black_box(to_nuc_seq(fs::read_to_string(path).unwrap().trim()).unwrap())
}

Expand Down
3 changes: 3 additions & 0 deletions packages/nextclade/src/align/score_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub fn score_matrix<T: Letter<T>>(
stripes: &[Stripe],
params: &AlignPairwiseParams,
) -> ScoreMatrixResult {
assert!(gap_open_close.len() > 0);
assert!(stripes.len() > 0);

let query_size = qry_seq.len();
let ref_len = ref_seq.len();
let n_rows = ref_len + 1;
Expand Down
4 changes: 3 additions & 1 deletion packages/nextclade/src/align/seed_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ fn extend_and_rewind(
minimal_bandwidth: isize,
ref_len: isize,
) -> RewindResult {
let current_seed_end = (current_seed.ref_pos + current_seed.length) as isize;
let mut current_band = current_band;
let mut look_back_length = look_back_length;
// generate new current trapezoid for the body of the current seed
Expand Down Expand Up @@ -254,6 +253,7 @@ fn trace_stripe_stats(stripes: &[Stripe]) {
trace!("Stripe width stats: min: {min}, max: {max}, mean: {mean:.1}, median: {median}",);
}

#[cfg(feature = "debug-seed-alignment")]
fn trace_matches(matches: &[SeedMatch2]) {
for (i, seed) in matches.iter().enumerate() {
trace!(
Expand All @@ -266,6 +266,7 @@ fn trace_matches(matches: &[SeedMatch2]) {
}
}

#[cfg(feature = "debug-seed-alignment")]
fn write_stripes_to_file(stripes: &[Stripe], filename: &str) {
use std::io::Write;
let mut file = std::fs::File::create(filename).unwrap();
Expand All @@ -275,6 +276,7 @@ fn write_stripes_to_file(stripes: &[Stripe], filename: &str) {
}
}

#[cfg(feature = "debug-seed-alignment")]
pub fn write_matches_to_file(matches: &[SeedMatch2], filename: &str) {
use std::io::Write;
let mut file = std::fs::File::create(filename).unwrap();
Expand Down
14 changes: 4 additions & 10 deletions packages/nextclade/src/analyze/find_private_aa_mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,8 @@ pub fn find_private_aa_mutations_for_one_gene(
);

// Iterate over sequence deletions
let non_reversion_deletions = process_seq_deletions(
cds,
node_mut_map,
aa_deletions,
ref_peptide,
&mut seq_positions_mutated_or_deleted,
);
let non_reversion_deletions =
process_seq_deletions(cds, node_mut_map, aa_deletions, &mut seq_positions_mutated_or_deleted);

// Iterate over node substitutions and deletions and find reversions
let reversion_substitutions = find_reversions(
Expand All @@ -110,7 +105,7 @@ pub fn find_private_aa_mutations_for_one_gene(
aa_unknowns,
aa_unsequenced_ranges,
ref_peptide,
&mut seq_positions_mutated_or_deleted,
&seq_positions_mutated_or_deleted,
);

let mut private_substitutions = concat_to_vec(&reversion_substitutions, &non_reversion_substitutions);
Expand Down Expand Up @@ -200,7 +195,6 @@ fn process_seq_deletions(
cds: &Cds,
node_mut_map: &BTreeMap<AaRefPosition, Aa>,
deletions: &[&AaDel],
ref_seq: &[Aa],
seq_positions_mutated_or_deleted: &mut BTreeSet<AaRefPosition>,
) -> Vec<AaDel> {
let mut non_reversion_deletions = Vec::<AaDel>::new();
Expand Down Expand Up @@ -249,7 +243,7 @@ fn find_reversions(
aa_unknowns: &[&CdsAaRange],
aa_unsequenced_ranges: &[AaRefRange],
ref_peptide: &[Aa],
seq_positions_mutated_or_deleted: &mut BTreeSet<AaRefPosition>,
seq_positions_mutated_or_deleted: &BTreeSet<AaRefPosition>,
) -> Vec<AaSub> {
let mut reversion_substitutions = Vec::<AaSub>::new();

Expand Down
4 changes: 2 additions & 2 deletions packages/nextclade/src/analyze/find_private_nuc_mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn find_private_nuc_mutations(
alignment_range,
ref_seq,
non_acgtns,
&mut seq_positions_mutated_or_deleted,
&seq_positions_mutated_or_deleted,
);

let (_undeletions, reversion_substitutions) = reversion_substitutions_and_undeletions
Expand Down Expand Up @@ -263,7 +263,7 @@ fn find_reversions(
alignment_range: &NucRefGlobalRange,
ref_seq: &[Nuc],
non_acgtns: &[NucRange],
seq_positions_mutated_or_deleted: &mut BTreeSet<NucRefGlobalPosition>,
seq_positions_mutated_or_deleted: &BTreeSet<NucRefGlobalPosition>,
) -> Vec<NucSub> {
let mut reversion_substitutions = Vec::<NucSub>::new();

Expand Down
2 changes: 1 addition & 1 deletion packages/nextclade/src/analyze/phenotype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use num_traits::real::Real;
pub fn calculate_phenotype(phenotype_data: &PhenotypeData, aa_substitutions: &[AaSub]) -> f64 {
let aa_substitutions = aa_substitutions
.iter()
.filter_map(|sub| (sub.cds_name == phenotype_data.cds && phenotype_data.aa_range.contains(sub.pos)).then_some(sub))
.filter(|sub| (sub.cds_name == phenotype_data.cds && phenotype_data.aa_range.contains(sub.pos)))
.collect_vec();

let phenotype: f64 = phenotype_data
Expand Down
Loading

0 comments on commit 6d3d7c5

Please sign in to comment.