Skip to content

Commit

Permalink
Merge pull request #1419 from nextstrain/refactor/lint-unused
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-aksamentov authored Feb 16, 2024
2 parents 0a1d3f5 + 4b2b18d commit d7dda43
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 199 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ trivial_numeric_casts = "warn"

# Disable some of the lints enabled by default
# https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html
dead_code = "allow"
elided_lifetimes_in_paths = "allow"
unused_variables = "allow"

# Enable lints not enabled by default
# https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html
Expand Down Expand Up @@ -210,6 +208,7 @@ pattern_type_mismatch = "allow"
print_stderr = "allow"
print_stdout = "allow"
pub_use = "allow"
pub_with_shorthand = "allow"
question_mark_used = "allow"
self_named_module_files = "allow"
semicolon_inside_block = "allow"
Expand Down
4 changes: 1 addition & 3 deletions packages/nextclade-cli/src/cli/nextclade_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ pub fn nextclade_run(run_args: NextcladeRunArgs) -> Result<(), Report> {
});
}

let writer = s.spawn(move || {
s.spawn(move || {
let nextclade = &nextclade;

let AnalysisInitialData {
genome_size,
gene_map,
clade_node_attr_key_descs,
phenotype_attr_descs,
aa_motif_keys,
Expand Down
2 changes: 1 addition & 1 deletion packages/nextclade-cli/src/cli/nextclade_seq_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn run(args: &NextcladeSortArgs, minimizer_index: &MinimizerIndexJson, verbo
});
}

let writer = s.spawn(move || {
s.spawn(move || {
writer_thread(args, result_receiver, verbose).unwrap();
});
});
Expand Down
12 changes: 1 addition & 11 deletions packages/nextclade-cli/src/dataset/dataset_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ use nextclade::{make_error, make_internal_error, o};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader, Cursor, Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::path::Path;
use zip::ZipArchive;

const PATHOGEN_JSON: &str = "pathogen.json";

pub fn nextclade_get_inputs(
run_args: &NextcladeRunArgs,
cdses: &Option<Vec<String>>,
Expand Down Expand Up @@ -275,14 +273,6 @@ pub fn dataset_individual_files_load(
}
}

#[allow(clippy::struct_field_names)]
pub struct DatasetFilePaths<'a> {
input_ref: &'a Path,
input_tree: &'a Option<PathBuf>,
input_pathogen_json: &'a Option<PathBuf>,
input_annotation: &'a Option<PathBuf>,
}

pub fn read_from_path_or_url(
http: &HttpClient,
dataset: &Dataset,
Expand Down
13 changes: 2 additions & 11 deletions packages/nextclade/src/align/backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use crate::align::band_2d::{Band2d};
use crate::align::band_2d::Band2d;
use crate::align::score_matrix::{BOUNDARY, MATCH, QRY_GAP_EXTEND, QRY_GAP_MATRIX, REF_GAP_EXTEND, REF_GAP_MATRIX};
use crate::alphabet::letter::Letter;


use serde::{Deserialize, Serialize};


const fn index_to_shift(si: i32, band_width: i32, mean_shift: i32) -> i32 {
si - band_width + mean_shift
}

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema, PartialEq, Eq)]
pub struct AlignmentOutput<T> {
pub qry_seq: Vec<T>,
Expand All @@ -27,8 +20,6 @@ pub fn backtrace<T: Letter<T>>(
) -> AlignmentOutput<T> {
let num_cols = scores.num_cols();
let num_rows = scores.num_rows();
let qry_len = qry_seq.len() as i32;
let ref_len = ref_seq.len() as i32;

// max length of the alignment is the sum of query and reference length
let aln_capacity = scores.num_cols() + scores.num_rows();
Expand Down Expand Up @@ -108,7 +99,7 @@ mod tests {
use crate::align::band_2d::simple_stripes;
use crate::align::gap_open::{get_gap_open_close_scores_codon_aware, GapScoreMap};
use crate::align::params::AlignPairwiseParams;

use crate::alphabet::nuc::{to_nuc_seq, Nuc};
use crate::gene::gene_map::GeneMap;
use eyre::Report;
Expand Down
6 changes: 2 additions & 4 deletions packages/nextclade/src/align/band_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ pub fn simple_stripes(mean_shift: i32, band_width: usize, ref_len: usize, qry_le

pub fn full_matrix(ref_len: usize, qry_len: usize) -> Vec<Stripe> {
let mut stripes = Vec::<Stripe>::with_capacity(ref_len + 1);
let ref_len_i32 = ref_len.to_i32().unwrap();
let qry_len_i32 = qry_len.to_i32().unwrap();
for i in 0..=ref_len_i32 {
for _ in 0..=ref_len {
stripes.push(Stripe::new(0, qry_len + 1));
}
stripes
Expand All @@ -67,7 +65,7 @@ pub fn full_matrix(ref_len: usize, qry_len: usize) -> Vec<Stripe> {
///
/// The underlying storage is sparse - the row storage consists of `Stripe`s, each of a given size (`stripe.length`)
/// and shifted by a given amount (`stripe.begin`) relative to the left boundary of the matrix. In each row, the cells
/// which are outside of the corresponding stripe are not allocated and accessing them is illegal.
/// which are outside the corresponding stripe are not allocated and accessing them is illegal.
///
/// Stripe begins must increase monotonically
#[derive(Clone, PartialEq, Eq)]
Expand Down
3 changes: 1 addition & 2 deletions packages/nextclade/src/align/score_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub fn score_matrix<T: Letter<T>>(
let r_gap_extend: i32;
let r_gap_open: i32;
let q_gap_open: i32;
let tmp_match: i32;
let mut tmp_score: i32;

if qpos == 0 {
Expand Down Expand Up @@ -209,7 +208,7 @@ mod tests {
use super::*;
use crate::align::band_2d::simple_stripes;
use crate::align::gap_open::{get_gap_open_close_scores_codon_aware, GapScoreMap};

use crate::alphabet::nuc::{to_nuc_seq, Nuc};
use crate::gene::gene_map::GeneMap;
use eyre::Report;
Expand Down
112 changes: 46 additions & 66 deletions packages/nextclade/src/align/seed_alignment.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
use crate::align::band_2d::Stripe;
use crate::align::seed_match::SeedMatch2;
use crate::alphabet::letter::Letter;

use log::trace;
use num_traits::abs;
use num_traits::clamp;
use std::cmp::max;
use std::cmp::min;

/// generate a vector of query sequence positions that are followed by at least `seed_length`
/// valid characters. Positions in this vector are thus "good" positions to start a query k-mer.
fn get_map_to_good_positions<L: Letter<L>>(qry_seq: &[L], seed_length: usize) -> Vec<usize> {
let qry_len = qry_seq.len();

let mut map_to_good_positions = Vec::<usize>::with_capacity(qry_len);
let mut distance_to_last_bad_pos: i32 = 0;

for (i, letter) in qry_seq.iter().enumerate() {
// TODO: Exclude ambiguous letters
if letter.is_unknown() {
distance_to_last_bad_pos = 0;
} else if distance_to_last_bad_pos >= seed_length as i32 {
map_to_good_positions.push(i - seed_length);
}
distance_to_last_bad_pos += 1;
}

map_to_good_positions
}

#[derive(Debug, Clone, Copy)]
pub struct SeedMatch {
pub qry_pos: usize,
Expand Down Expand Up @@ -201,7 +177,6 @@ pub fn create_alignment_band(
});
}
}
// write_stripes_to_file(&stripes, "stripes.csv");

// trim stripes to reachable regions
regularize_stripes(stripes, qry_len as usize)
Expand Down Expand Up @@ -236,53 +211,58 @@ fn regularize_stripes(mut stripes: Vec<Stripe>, qry_len: usize) -> (Vec<Stripe>,
(stripes, band_area)
}

fn trace_stripe_stats(stripes: &[Stripe]) {
let mut stripe_lengths = Vec::new();
for stripe in stripes {
assert!(
stripe.begin <= stripe.end,
"Stripe begin must be <= stripe end for stripe {stripe:?}",
);
stripe_lengths.push(stripe.end - stripe.begin);
#[cfg(feature = "debug-seed-alignment")]
mod debug {
use crate::align::band_2d::Stripe;
use crate::align::seed_match::SeedMatch2;
use crate::alphabet::letter::Letter;
use log::trace;

fn trace_stripe_stats(stripes: &[Stripe]) {
let mut stripe_lengths = Vec::new();
for stripe in stripes {
assert!(
stripe.begin <= stripe.end,
"Stripe begin must be <= stripe end for stripe {stripe:?}",
);
stripe_lengths.push(stripe.end - stripe.begin);
}
stripe_lengths.sort_unstable();
let median = stripe_lengths[stripe_lengths.len() / 2];
let mean = stripe_lengths.iter().sum::<usize>() as f32 / stripe_lengths.len() as f32;
let max = stripe_lengths[stripe_lengths.len() - 1];
let min = stripe_lengths[0];
trace!("Stripe width stats: min: {min}, max: {max}, mean: {mean:.1}, median: {median}",);
}
stripe_lengths.sort_unstable();
let median = stripe_lengths[stripe_lengths.len() / 2];
let mean = stripe_lengths.iter().sum::<usize>() as f32 / stripe_lengths.len() as f32;
let max = stripe_lengths[stripe_lengths.len() - 1];
let min = stripe_lengths[0];
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!(
"Match {}: ref_pos: {}, qry_offset: {}, length: {}",
i,
seed.ref_pos,
-seed.offset,
seed.length,
);
fn trace_matches(matches: &[SeedMatch2]) {
for (i, seed) in matches.iter().enumerate() {
trace!(
"Match {}: ref_pos: {}, qry_offset: {}, length: {}",
i,
seed.ref_pos,
-seed.offset,
seed.length,
);
}
}
}

#[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();
writeln!(file, "ref,begin,end").unwrap();
for (i, stripe) in stripes.iter().enumerate() {
writeln!(file, "{i},{begin},{end}", begin = stripe.begin, end = stripe.end).unwrap();
fn write_stripes_to_file(stripes: &[Stripe], filename: &str) {
use std::io::Write;
let mut file = std::fs::File::create(filename).unwrap();
writeln!(file, "ref,begin,end").unwrap();
for (i, stripe) in stripes.iter().enumerate() {
writeln!(file, "{i},{begin},{end}", begin = stripe.begin, end = stripe.end).unwrap();
}
}
}

#[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();
writeln!(file, "ref_pos,qry_pos,length").unwrap();
for match_ in matches {
writeln!(file, "{},{},{}", match_.ref_pos, match_.qry_pos, match_.length).unwrap();
pub fn write_matches_to_file(matches: &[SeedMatch2], filename: &str) {
use std::io::Write;
let mut file = std::fs::File::create(filename).unwrap();
writeln!(file, "ref_pos,qry_pos,length").unwrap();
for match_ in matches {
writeln!(file, "{},{},{}", match_.ref_pos, match_.qry_pos, match_.length).unwrap();
}
}
}

Expand Down
23 changes: 7 additions & 16 deletions packages/nextclade/src/analyze/divergence.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
use crate::analyze::nuc_sub::NucSub;
use crate::coord::range::NucRefGlobalRange;
use crate::tree::params::TreeBuilderParams;
use crate::tree::tree::DivergenceUnits;

pub struct NucMutsCounted<'a> {
muts: Vec<&'a NucSub>,
masked_muts: Vec<&'a NucSub>,
other_muts: Vec<&'a NucSub>,
pub struct NucMutsCounted {
n_muts: usize,
n_masked_muts: usize,
n_other_muts: usize,
}

pub fn count_nuc_muts<'a>(nuc_muts: &'a [NucSub], masked_ranges: &[NucRefGlobalRange]) -> NucMutsCounted<'a> {
pub fn count_nuc_muts<'a>(nuc_muts: &'a [NucSub], masked_ranges: &[NucRefGlobalRange]) -> NucMutsCounted {
// Split away non_acgt mutations
let (nuc_muts, other_muts): (Vec<_>, Vec<_>) = nuc_muts
let (nuc_muts, other_muts) = nuc_muts
.iter()
.partition(|m| m.ref_nuc.is_acgt() && m.qry_nuc.is_acgt());
.partition::<Vec<&'a NucSub>, _>(|m| m.ref_nuc.is_acgt() && m.qry_nuc.is_acgt());

// Split away masked mutations
let (masked_muts, muts): (Vec<_>, Vec<_>) = nuc_muts
let (masked_muts, muts) = nuc_muts
.iter()
.partition(|m| masked_ranges.iter().any(|range| range.contains(m.pos)));
.partition::<Vec<&'a NucSub>, _>(|m| masked_ranges.iter().any(|range| range.contains(m.pos)));

let n_muts = muts.len();
let n_masked_muts = masked_muts.len();
let n_other_muts = other_muts.len();

NucMutsCounted {
muts,
masked_muts,
other_muts,
n_muts,
n_masked_muts,
n_other_muts,
Expand All @@ -47,7 +40,6 @@ pub fn calculate_branch_length(
n_muts,
n_masked_muts,
n_other_muts,
..
} = count_nuc_muts(nuc_muts, masked_ranges);

let mut this_div = n_muts as f64;
Expand All @@ -65,12 +57,11 @@ pub fn calculate_branch_length(
}

/// Calculate nuc mut score
pub fn score_nuc_muts(nuc_muts: &[NucSub], masked_ranges: &[NucRefGlobalRange], params: &TreeBuilderParams) -> f64 {
pub fn score_nuc_muts(nuc_muts: &[NucSub], masked_ranges: &[NucRefGlobalRange]) -> f64 {
let NucMutsCounted {
n_muts,
n_masked_muts,
n_other_muts,
..
} = count_nuc_muts(nuc_muts, masked_ranges);
let mut score = n_muts as f64;
// modify the score by sub-integer amounts for masked and other mutations. this effectively means
Expand Down
2 changes: 1 addition & 1 deletion packages/nextclade/src/features/feature_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PartialOrd for FeatureGroup {
}

impl FeatureGroup {
pub fn new(id: &str, features: &[Feature]) -> Self {
pub fn new(features: &[Feature]) -> Self {
let index = features
.iter()
.map(|feature| feature.index)
Expand Down
4 changes: 2 additions & 2 deletions packages/nextclade/src/features/feature_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn read_gff3_feature_tree_str(content: impl AsRef<str>) -> Result<Vec<SequenceRe
ranges
.into_iter()
.enumerate()
.map(|(index,(content, content_begin, content_end))| {
.map(|(index,(content, content_begin, _))| {
let content = content.trim();

// Extract '##sequence-region' header line
Expand Down Expand Up @@ -228,7 +228,7 @@ fn build_hierarchy_of_features(features: &[Feature]) -> Result<Vec<FeatureGroup>
.iter()
.group_by(|feature| feature.id.clone())
.into_iter()
.map(|(id, features)| FeatureGroup::new(&id, &features.cloned().collect_vec()))
.map(|(_, features)| FeatureGroup::new(&features.cloned().collect_vec()))
.collect_vec();

// Find top-level features (having no parents)
Expand Down
5 changes: 1 addition & 4 deletions packages/nextclade/src/features/feature_tree_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ fn format_sequence_region_feature<W: Write>(
Ok(())
}

const PASS_ICON: &str = "│ ";
const FORK_ICON: &str = "├──";
const IMPASSE_ICON: &str = "└──";
const INDENT: usize = 2;

fn format_feature_groups<W: Write>(w: &mut W, feature_map: &[FeatureGroup], max_name_len: usize) -> Result<(), Report> {
Expand All @@ -103,7 +100,7 @@ fn format_feature_groups_recursive<W: Write>(
feature_groups
.iter()
.enumerate()
.try_for_each(|(height, feature_group)| -> Result<(), Report> {
.try_for_each(|(_, feature_group)| -> Result<(), Report> {
format_feature_group(w, feature_group, max_name_len, depth)?;
format_feature_groups_recursive(w, &feature_group.children, max_name_len, depth + 1)?;
Ok(())
Expand Down
Loading

0 comments on commit d7dda43

Please sign in to comment.