diff --git a/.cargo/config b/.cargo/config index d2621da5d..b2e93d64d 100644 --- a/.cargo/config +++ b/.cargo/config @@ -61,7 +61,6 @@ rustflags = [ "-A", "clippy::comparison_chain", # TODO: burn down these allow exceptions and then deny them "-A", "clippy::too_many_arguments", - "-A", "clippy::needless_range_loop", ] [target.x86_64-unknown-linux-gnu] diff --git a/dna/src/lib.rs b/dna/src/lib.rs index 002fb33e8..8d3a80d5e 100644 --- a/dna/src/lib.rs +++ b/dna/src/lib.rs @@ -91,12 +91,7 @@ pub fn tm_nearest_neighbor_full(s: &str, s_mol: f64, na_mol: f64, locked: &[bool for c in s.chars() { sx.push(c); } - let mut gc = 0; - for i in 0..sx.len() { - if sx[i] == 'G' || sx[i] == 'C' { - gc += 1; - } - } + let gc = sx.iter().filter(|c| **c == 'G' || **c == 'C').count(); let gc_fract = gc as f64 / sx.len() as f64; let ln_na_mol = na_mol.ln(); @@ -431,12 +426,12 @@ pub fn thermodynamic_sums_dna( sx.push(c); } let mut b = Vec::::new(); - for i in 0..sx.len() { - if sx[i] == 'A' { + for sx_i in &sx { + if *sx_i == 'A' { b.push(0); - } else if sx[i] == 'C' { + } else if *sx_i == 'C' { b.push(1); - } else if sx[i] == 'G' { + } else if *sx_i == 'G' { b.push(2); } else { b.push(3); @@ -451,8 +446,8 @@ pub fn thermodynamic_sums_dna( // Handle locked bases. let mut have_lock = false; - for i in 0..locked.len() { - if locked[i] { + for locked_i in locked { + if *locked_i { have_lock = true; } } diff --git a/fasta_tools/src/lib.rs b/fasta_tools/src/lib.rs index 6ad7f6ae3..06dea313d 100644 --- a/fasta_tools/src/lib.rs +++ b/fasta_tools/src/lib.rs @@ -131,9 +131,7 @@ pub fn read_fasta_contents_into_vec_dna_string_plus_headers( ) { let mut last: String = String::new(); let mut first = true; - let lines = f.split('\n').collect::>(); - for i in 0..lines.len() { - let s = &lines[i]; + for s in f.split('\n') { if first { if !s.starts_with('>') { panic!("fasta format failure reading {}", f); diff --git a/graph_simple/src/lib.rs b/graph_simple/src/lib.rs index 36cf68735..1dd89fdf6 100644 --- a/graph_simple/src/lib.rs +++ b/graph_simple/src/lib.rs @@ -174,8 +174,8 @@ where fn get_predecessors(&self, v: &[i32], x: &mut Vec) { let mut check: Vec = Vec::new(); let mut tov: HashSet = HashSet::new(); - for j in 0..v.len() { - let s: u32 = v[j] as u32; + for v_i in v { + let s: u32 = *v_i as u32; check.push(s); tov.insert(s); } @@ -205,8 +205,8 @@ where fn get_successors(&self, v: &[i32], x: &mut Vec) { let mut check: Vec = Vec::new(); let mut fromv: HashSet = HashSet::new(); - for j in 0..v.len() { - let s: u32 = v[j] as u32; + for v_i in v { + let s: u32 = *v_i as u32; check.push(s); fromv.insert(s); } @@ -275,23 +275,23 @@ where fn components_e(&self, comp: &mut Vec>) { self.components(comp); - for j in 0..comp.len() { + for comp_i in comp { let mut c = Vec::::new(); - for i in 0..comp[j].len() { - let v = comp[j][i]; + for comp_i_j in comp_i.iter() { + let v = *comp_i_j; let n = self.n_from(v as usize); for l in 0..n { c.push(self.e_from(v as usize, l) as u32); } } - comp[j] = c; + *comp_i = c; } } fn components_e_pos_sorted(&self, comp: &mut Vec>) { self.components_e(comp); - for u in 0..comp.len() { - comp[u].sort_by(|a, b| { + for comp_i in comp { + comp_i.sort_by(|a, b| { if a == b { return std::cmp::Ordering::Equal; } diff --git a/hyperbase/src/lib.rs b/hyperbase/src/lib.rs index 7dc8fb90a..5c268a871 100644 --- a/hyperbase/src/lib.rs +++ b/hyperbase/src/lib.rs @@ -116,9 +116,7 @@ pub fn debruijn_to_petgraph_hyperbasevector( // the natural equivalence relation. let mut eq: EquivRel = EquivRel::new(2 * edges.len() as i32); - for i in 0..adj.len() { - let left = adj[i].0; - let right = adj[i].1; + for (left, right) in adj { eq.join(2 * left + 1, 2 * right); } let mut reps = Vec::::new(); @@ -132,13 +130,13 @@ pub fn debruijn_to_petgraph_hyperbasevector( for i in 0..reps.len() { g_out.add_node(i as u32); } - for e in 0..edges.len() { + for (e, edge) in edges.into_iter().enumerate() { let v = bin_position(&reps, &eq.class_id((2 * e) as i32)); let w = bin_position(&reps, &eq.class_id((2 * e + 1) as i32)); g_out.add_edge( NodeIndex::::new(v as usize), NodeIndex::::new(w as usize), - edges[e].2.clone(), + edge.2, ); } } @@ -273,10 +271,9 @@ impl Hyper { pub fn print(&self) { let mut comp = Vec::>::new(); self.h.g.components_e(&mut comp); - for j in 0..comp.len() { + for (j, comp_j) in comp.into_iter().enumerate() { println!("\nCOMPONENT {}", j + 1); - for i in 0..comp[j].len() { - let e = comp[j][i]; + for e in comp_j { let v = self.h.g.to_left(e); let w = self.h.g.to_right(e); let b: DnaString = self.h.g[EdgeIndex::::new(e as usize)].clone(); @@ -304,11 +301,10 @@ impl Hyper { let mut comp = Vec::>::new(); self.h.g.components_e(&mut comp); let mut n = 0; - for j in 0..comp.len() { + for comp_j in comp { let mut have_ann = false; - for i in 0..comp[j].len() { - let e = comp[j][i] as usize; - if !ann[e].is_empty() { + for e in &comp_j { + if !ann[*e as usize].is_empty() { have_ann = true; } } @@ -317,8 +313,8 @@ impl Hyper { } n += 1; println!("\nCOMPONENT {n}"); - for i in 0..comp[j].len() { - let e = comp[j][i] as usize; + for e in comp_j { + let e = e as usize; let v = self.h.g.to_left(e as u32); let w = self.h.g.to_right(e as u32); let b: DnaString = self.h.g[EdgeIndex::::new(e)].clone(); @@ -422,22 +418,22 @@ impl Hyper { make_kmer_lookup_20_single(&edges, &mut kmers_plus); drop(edges); let mut maxread = 0; - for id in 0..reads.len() { - maxread = max(maxread, reads[id].len()); + for read in reads { + maxread = max(maxread, read.len()); } if maxread < k as usize { return; } let mut next_rpos: Vec = vec![0; reads.len()]; for pos in 0..maxread - (k as usize) + 1 { - for id in 0..reads.len() { - if pos + k as usize > reads[id].len() { + for (id, read) in reads.iter().enumerate() { + if pos + k as usize > read.len() { continue; } if pos < next_rpos[id] as usize { continue; } - let x: Kmer20 = reads[id].get_kmer(pos); + let x: Kmer20 = read.get_kmer(pos); let p = bin_position1_3(&kmers_plus, &x); if p < 0 { continue; @@ -448,7 +444,7 @@ impl Hyper { let mut epos = kmers_plus[p as usize].2 + k; self.ids[e as usize].push(id as u32); loop { - if rpos == reads[id].len() { + if rpos == read.len() { break; } let mut next = false; @@ -456,9 +452,7 @@ impl Hyper { let v = self.h.g.to_right(e as u32); for j in 0..self.h.g.n_from(v as usize) { let f = self.h.g.e_from(v as usize, j); - if self.h.g.edge_obj(f as u32).get((k - 1) as usize) - == reads[id].get(rpos) - { + if self.h.g.edge_obj(f as u32).get((k - 1) as usize) == read.get(rpos) { e = f as i32; self.ids[e as usize].push(id as u32); epos = k - 1; @@ -472,7 +466,7 @@ impl Hyper { } } if !next { - if reads[id].get(rpos) != self.h.g.edge_obj(e as u32).get(epos as usize) { + if read.get(rpos) != self.h.g.edge_obj(e as u32).get(epos as usize) { break; } rpos += 1; @@ -893,8 +887,8 @@ impl Hyper { let mut comp = Vec::>::new(); self.h.g.components_e(&mut comp); let mut sizes = Vec::::new(); - for j in 0..comp.len() { - sizes.push(comp[j].len()); + for c in comp { + sizes.push(c.len()); } reverse_sort(&mut sizes); print!("component sizes = ["); diff --git a/tables/src/lib.rs b/tables/src/lib.rs index 182c0c4be..221db24d9 100644 --- a/tables/src/lib.rs +++ b/tables/src/lib.rs @@ -73,27 +73,26 @@ pub fn print_tabular( }; let nrows = rows.len(); let mut ncols = 0; - for i in 0..nrows { - ncols = max(ncols, rows[i].len()); + for row in &rows[0..nrows] { + ncols = max(ncols, row.len()); } let mut maxcol = vec![0; ncols]; - for i in 0..rows.len() { - for j in 0..rows[i].len() { - maxcol[j] = max(maxcol[j], rows[i][j].chars().count()); + for row in rows { + for (j, item) in row.iter().enumerate() { + maxcol[j] = max(maxcol[j], item.chars().count()); } } - for i in 0..rows.len() { - for j in 0..rows[i].len() { - let x = rows[i][j].clone(); + for row in rows { + for (j, x) in row.iter().enumerate() { if j < just.len() && just[j] == b'r' { log.append(&mut vec![b' '; maxcol[j] - x.chars().count()]); log.append(&mut x.as_bytes().to_vec()); - if j < rows[i].len() - 1 { + if j < row.len() - 1 { log.append(&mut vec![b' '; sep]); } } else { log.append(&mut x.as_bytes().to_vec()); - if j < rows[i].len() - 1 { + if j < row.len() - 1 { log.append(&mut vec![b' '; maxcol[j] - x.chars().count() + sep]); } } @@ -177,14 +176,14 @@ pub fn print_tabular_vbox( let mut rrr = rows.to_owned(); let nrows = rrr.len(); let mut ncols = 0; - for i in 0..nrows { - ncols = max(ncols, rrr[i].len()); + for item in &rrr[0..nrows] { + ncols = max(ncols, item.len()); } let mut vert = vec![false; ncols]; let mut just = Vec::::new(); let mut count = 0_isize; - for i in 0..justify.len() { - if justify[i] == b'|' { + for item in justify { + if *item == b'|' { assert!(count > 0); if count >= ncols as isize { eprintln!("\nposition of | in justify string is illegal"); @@ -193,7 +192,7 @@ pub fn print_tabular_vbox( assert!(count < ncols as isize); vert[(count - 1) as usize] = true; } else { - just.push(justify[i]); + just.push(*item); count += 1; } } @@ -205,27 +204,22 @@ pub fn print_tabular_vbox( just.len() ); eprintln!("justify = {}", strme(justify)); - for i in 0..rows.len() { - eprintln!( - "row {} = {} = {}", - i + 1, - rows[i].len(), - rows[i].iter().format(",") - ); + for (i, row) in rows.iter().enumerate() { + eprintln!("row {} = {} = {}", i + 1, row.len(), row.iter().format(",")); } assert_eq!(just.len(), ncols); } let mut maxcol = vec![0; ncols]; let mut ext = vec![0; ncols]; - for i in 0..rrr.len() { - for j in 0..rrr[i].len() { - if j < rrr[i].len() - 1 && rrr[i][j + 1] == *"\\ext" { + for row in &rrr { + for (j, item) in row.iter().enumerate() { + if j < row.len() - 1 && row[j + 1] == "\\ext" { continue; } - if rrr[i][j] == *"\\ext" || rrr[i][j] == *"\\hline" { + if item == "\\ext" || item == "\\hline" { continue; } - maxcol[j] = max(maxcol[j], visible_width(&rrr[i][j])); + maxcol[j] = max(maxcol[j], visible_width(item)); } } if debug_print { @@ -278,11 +272,11 @@ pub fn print_tabular_vbox( ext[k - 1] += need - have; } let mut m = 0; - for u in 0..rrr.len() { - if j >= rrr[u].len() { + for (u, row) in rrr.iter().enumerate() { + if j >= row.len() { eprintln!("\nProblem with line {u}, not enough fields.\n"); } - if rrr[u][j] != *"\\ext" { + if row[j] != *"\\ext" { m = max(m, visible_width(&rrr[u][j])); } } @@ -318,26 +312,26 @@ pub fn print_tabular_vbox( // Go through the rows. - for i in 0..nrows { + for (i, row) in rrr[0..nrows].iter().enumerate() { if debug_print { - println!("now row {} = {}", i, rrr[i].iter().format(",")); + println!("now row {} = {}", i, row.iter().format(",")); println!("0 - pushing │ onto row {i}"); } log.push(verty); - for j in 0..min(ncols, rrr[i].len()) { + for j in 0..min(ncols, row.len()) { // Pad entries according to justification. let mut x = String::new(); - if j >= rrr[i].len() { + if j >= row.len() { for _ in 0..maxcol[j] { x.push(' '); } - } else if rrr[i][j] == *"\\hline" { + } else if row[j] == *"\\hline" { for _ in 0..maxcol[j] { x.push(dash); } } else { - let r = rrr[i][j].clone(); + let r = row[j].clone(); let rlen = visible_width(&r); let mut xlen = 0; if r != *"\\ext" { @@ -347,7 +341,7 @@ pub fn print_tabular_vbox( xlen += 1; } } - if j < rrr[i].len() { + if j < row.len() { x += &r; xlen += visible_width(&r); } @@ -371,18 +365,18 @@ pub fn print_tabular_vbox( // Add separations and separators. let mut add_sep = true; - if j + 1 < rrr[i].len() && rrr[i][j + 1] == *"\\ext" { + if j + 1 < row.len() && row[j + 1] == *"\\ext" { add_sep = false; } let mut jp = j; - while jp + 1 < rrr[i].len() { - if rrr[i][jp + 1] != *"\\ext" { + while jp + 1 < row.len() { + if row[jp + 1] != *"\\ext" { break; } jp += 1; } if add_sep && jp < ncols - 1 { - if rrr[i][j] == *"\\hline" { + if row[j] == *"\\hline" { for _ in 0..sep { log.push(dash); } @@ -392,12 +386,12 @@ pub fn print_tabular_vbox( } } } - if vert[j] && rrr[i][j + 1] != "\\ext" { + if vert[j] && row[j + 1] != "\\ext" { if debug_print { println!("1 - pushing {verty} onto row {i}, j = {j}"); } log.push(verty); - if rrr[i][j + 1] == *"\\hline" { + if row[j + 1] == *"\\hline" { for _ in 0..sep { log.push(dash); } @@ -458,8 +452,8 @@ pub fn print_tabular_vbox( if !z.is_empty() { all.push(z); } - for i in 0..all.len() { - mat.push(package_characters_with_escapes_char(&all[i])); + for chars in all { + mat.push(package_characters_with_escapes_char(&chars)); } } @@ -502,10 +496,10 @@ pub fn print_tabular_vbox( // Output matrix. log.clear(); - for i in 0..mat.len() { - for j in 0..mat[i].len() { - for k in 0..mat[i][j].len() { - log.push(mat[i][j][k]); + for x in mat { + for y in x { + for z in y { + log.push(z); } } log.push('\n'); diff --git a/vdj_ann/src/annotate.rs b/vdj_ann/src/annotate.rs index e76bd4ce2..192510362 100644 --- a/vdj_ann/src/annotate.rs +++ b/vdj_ann/src/annotate.rs @@ -85,22 +85,17 @@ pub fn chain_type(b: &DnaString, rkmers_plus_full_20: &[(Kmer20, i32, i32)], rty brc.get_kmer(l) }; let low = lower_bound1_3(rkmers_plus_full_20, &x) as usize; - for j in low..rkmers_plus_full_20.len() { - if rkmers_plus_full_20[j].0 != x { + for kmer in &rkmers_plus_full_20[low..] { + if kmer.0 != x { break; } - let t = rkmers_plus_full_20[j].1 as usize; + let t = kmer.1 as usize; if rtype[t] >= 0 { is_type[z + rtype[t] as usize] = true; } } } - let mut nt = 0; - for l in 0..2 * N { - if is_type[l] { - nt += 1; - } - } + let nt = (is_type[0..2 * N]).iter().filter(|t| **t).count(); if nt == 1 { for l in 0..2 * N { if is_type[l] { @@ -307,12 +302,12 @@ pub fn annotate_seq_core( for l in 0..(b.len() - K + 1) { let x: Kmer12 = b.get_kmer(l); let low = lower_bound1_3(rkmers_plus, &x) as usize; - for r in low..rkmers_plus.len() { - if rkmers_plus[r].0 != x { + for kmer in &rkmers_plus[low..] { + if kmer.0 != x { break; } - let t = rkmers_plus[r].1 as usize; - let p = rkmers_plus[r].2 as usize; + let t = kmer.1 as usize; + let p = kmer.2 as usize; if l > 0 && p > 0 && b_seq[l - 1] == refs[t].get(p - 1) { continue; } @@ -376,8 +371,8 @@ pub fn annotate_seq_core( // least 150 bases aligned. let mut offsets = Vec::<(i32, i32)>::new(); - for i in 0..perf.len() { - offsets.push((perf[i].0, perf[i].1)); + for p in &perf { + offsets.push((p.0, p.1)); } unique_sort(&mut offsets); const MM_START: i32 = 150; @@ -418,8 +413,8 @@ pub fn annotate_seq_core( let len = lx - l; if (MM..20).contains(&len) { let mut known = false; - for k in 0..tig_starts.len() { - if l == tig_starts[k] { + for tig_start in &tig_starts { + if l == *tig_start { known = true; break; } @@ -503,12 +498,12 @@ pub fn annotate_seq_core( // Extend backwards and then forwards. - for i in 0..semi.len() { - let t = semi[i].0; - let off = semi[i].1; - let mut l = semi[i].2; - let mut len = semi[i].3; - let mut mis = semi[i].4.clone(); + for s in &mut semi { + let t = s.0; + let off = s.1; + let mut l = s.2; + let mut len = s.3; + let mut mis = s.4.clone(); while l > MIN_PERF_EXT as i32 && l + off > MIN_PERF_EXT as i32 { let mut ok = true; for j in 0..MIN_PERF_EXT { @@ -555,12 +550,12 @@ pub fn annotate_seq_core( len += 1; } } - semi[i].2 = l; - semi[i].3 = len; - semi[i].4 = mis; + s.2 = l; + s.3 = len; + s.4 = mis; } - for i in 0..semi.len() { - semi[i].4.sort_unstable(); + for s in &mut semi { + s.4.sort_unstable(); } // Add some 40-mers with the same offset having <= 6 mismatches. @@ -625,12 +620,12 @@ pub fn annotate_seq_core( if allow_weak { let max_mis = 5; - for i in 0..semi.len() { - let t = semi[i].0; - let off = semi[i].1; - let l = semi[i].2; - let mut len = semi[i].3; - let mut mis = semi[i].4.clone(); + for s in &mut semi { + let t = s.0; + let off = s.1; + let l = s.2; + let mut len = s.3; + let mut mis = s.4.clone(); let mut mis_count = 0; while l + len < b_seq.len() as i32 && l + len + off < refs[t as usize].len() as i32 { if b_seq[(l + len) as usize] != refs[t as usize].get((l + off + len) as usize) { @@ -640,16 +635,16 @@ pub fn annotate_seq_core( len += 1; } if mis_count <= max_mis && l + len + off == refs[t as usize].len() as i32 { - semi[i].3 = len; - semi[i].4 = mis; + s.3 = len; + s.4 = mis; } } - for i in 0..semi.len() { - let t = semi[i].0; - let off = semi[i].1; - let mut l = semi[i].2; - let mut len = semi[i].3; - let mut mis = semi[i].4.clone(); + for s in &mut semi { + let t = s.0; + let off = s.1; + let mut l = s.2; + let mut len = s.3; + let mut mis = s.4.clone(); let mut mis_count = 0; while l > 0 && l + off > 0 { if b_seq[(l - 1_i32) as usize] != refs[t as usize].get((l + off - 1_i32) as usize) { @@ -660,13 +655,13 @@ pub fn annotate_seq_core( len += 1; } if mis_count <= max_mis && l + off == 0 { - semi[i].2 = l; - semi[i].3 = len; - semi[i].4 = mis; + s.2 = l; + s.3 = len; + s.4 = mis; } } - for i in 0..semi.len() { - semi[i].4.sort_unstable(); + for s in &mut semi { + s.4.sort_unstable(); } } report_semis(verbose, "SEMI ALIGNMENTS", &semi, &b_seq, refs, log); @@ -822,8 +817,8 @@ pub fn annotate_seq_core( // Make sure that mismatches are unique sorted. - for i in 0..semi.len() { - unique_sort(&mut semi[i].4); + for s in &mut semi { + unique_sort(&mut s.4); } report_semis( verbose, @@ -898,14 +893,14 @@ pub fn annotate_seq_core( } let j1 = next_diff_pre_annotation(&annx, i1 as i32); let mut min_imp = 1000000000; - for k in i1..j1 as usize { - let imp = min(annx[k].match_len, annx[k].ref_id); + for a in &annx[i1..j1 as usize] { + let imp = min(a.match_len, a.ref_id); min_imp = min(imp, min_imp); } const MAX_IMP: i32 = 60; if min_imp > MAX_IMP { - for k in i1..j1 as usize { - to_delete[k] = true; + for d in &mut to_delete[i1..j1 as usize] { + *d = true; } } i1 = j1 as usize; @@ -966,8 +961,8 @@ pub fn annotate_seq_core( if verbose { fwriteln!(log, "\nALIGNMENTS ONE\n"); - for i in 0..annx.len() { - print_alignx(log, &annx[i], refdata); + for a in &annx { + print_alignx(log, a, refdata); } } @@ -1010,12 +1005,12 @@ pub fn annotate_seq_core( let mut over = 0_i64; let mut offsets1 = Vec::::new(); let mut offsets2 = Vec::::new(); - for k1 in i1..j1 { - let u1 = ts[k1].1; + for t in &ts[i1..j1] { + let u1 = t.1; offsets1.push(annx[u1].tig_start - annx[u1].ref_start); } - for k2 in i2..j2 { - let u2 = ts[k2].1; + for t in &ts[i2..j2] { + let u2 = t.1; offsets2.push(annx[u2].tig_start - annx[u2].ref_start); } offsets1.sort_unstable(); @@ -1026,8 +1021,8 @@ pub fn annotate_seq_core( let u1 = ts[k1].1; let l1 = annx[u1].tig_start; let len1 = annx[u1].match_len; - for k2 in i2..j2 { - let u2 = ts[k2].1; + for t in &ts[i2..j2] { + let u2 = t.1; let l2 = annx[u2].tig_start; let len2 = annx[u2].match_len; let start = max(l1, l2); @@ -1076,9 +1071,9 @@ pub fn annotate_seq_core( r1, r2 ); - for k in i1..j1 { - let t = ts[k].0; - let u = ts[k].1; + for item in &ts[i1..j1] { + let t = item.0; + let u = item.1; let l = annx[u].tig_start; let len = annx[u].match_len; let p = annx[u].ref_start; @@ -1095,9 +1090,9 @@ pub fn annotate_seq_core( ); } fwriteln!(log, "beats this alignment"); - for k in i2..j2 { - let t = ts[k].0; - let u = ts[k].1; + for item in &ts[i2..j2] { + let t = item.0; + let u = item.1; let l = annx[u].tig_start; let len = annx[u].match_len; let p = annx[u].ref_start; @@ -1129,8 +1124,8 @@ pub fn annotate_seq_core( if verbose { fwriteln!(log, "\nALIGNMENTS TWO\n"); - for i in 0..annx.len() { - print_alignx(log, &annx[i], refdata); + for a in &annx { + print_alignx(log, a, refdata); } } @@ -1187,6 +1182,7 @@ pub fn annotate_seq_core( if tot1 == tot2 && aligns[t] == 2 { let mut mis = annx[i1].mismatches.clone(); + #[allow(clippy::needless_range_loop)] for p in l1 + len1..l2 { if b_seq[p] != refs[t].get((p as i32 + off1) as usize) { mis.push(p as i32); @@ -1409,8 +1405,8 @@ pub fn annotate_seq_core( if verbose { fwriteln!(log, "\nALIGNMENTS THREE\n"); - for i in 0..annx.len() { - print_alignx(log, &annx[i], refdata); + for a in &annx { + print_alignx(log, a, refdata); } } @@ -1429,8 +1425,8 @@ pub fn annotate_seq_core( // ◼ Really should have ho_interval here. let mut combo = Vec::<(String, i32, usize)>::new(); - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for (i, a) in annx.iter().enumerate() { + let t = a.ref_id as usize; if !rheaders[t].contains("segment") { combo.push(( refdata.name[t].clone() + "." + &refdata.transcript[t], @@ -1442,6 +1438,7 @@ pub fn annotate_seq_core( combo.sort(); // cov mis locs rstarts mis_nutr let mut data = Vec::<(Vec<(usize, usize)>, usize, Vec, Vec, usize)>::new(); + let mut i = 0; while i < combo.len() { let j = next_diff1_3(&combo, i as i32) as usize; @@ -1463,6 +1460,7 @@ pub fn annotate_seq_core( data.push((cov, mis, locs, rstarts, mis_nutr)); i = j; } + let mut to_delete = vec![false; annx.len()]; let mut deleted = vec![false; data.len()]; for i1 in 0..data.len() { @@ -1530,8 +1528,8 @@ pub fn annotate_seq_core( let t = annx[data[i1].2[j]].ref_id; if utr2 || !refdata.is_u(t as usize) { let x = &data[i1].0[j]; - for m in x.0..x.1 { - cov1[m] = true; + for c in &mut cov1[x.0..x.1] { + *c = true; } } } @@ -1539,22 +1537,15 @@ pub fn annotate_seq_core( let t = annx[data[i2].2[j]].ref_id; if utr1 || !refdata.is_u(t as usize) { let x = &data[i2].0[j]; - for m in x.0..x.1 { - cov2[m] = true; + for c in &mut cov2[x.0..x.1] { + *c = true; } } } - let (mut total1, mut total2) = (0, 0); - for l in 0..n { - if cov1[l] { - total1 += 1; - } - } - for l in 0..n { - if cov2[l] { - total2 += 1; - } - } + let count_true = |bools: &[bool]| bools.iter().filter(|c| **c).count(); + + let total1 = count_true(&cov1[0..n]); + let total2 = count_true(&cov2[0..n]); // Same as above but always exclude UTRs. @@ -1563,8 +1554,8 @@ pub fn annotate_seq_core( let t = annx[data[i1].2[j]].ref_id; if !refdata.is_u(t as usize) { let x = &data[i1].0[j]; - for m in x.0..x.1 { - cov1_nu[m] = true; + for c in &mut cov1_nu[x.0..x.1] { + *c = true; } } } @@ -1572,22 +1563,14 @@ pub fn annotate_seq_core( let t = annx[data[i2].2[j]].ref_id; if !refdata.is_u(t as usize) { let x = &data[i2].0[j]; - for m in x.0..x.1 { - cov2_nu[m] = true; + for c in &mut cov2_nu[x.0..x.1] { + *c = true; } } } - let (mut total1_nu, mut total2_nu) = (0, 0); - for l in 0..n { - if cov1_nu[l] { - total1_nu += 1; - } - } - for l in 0..n { - if cov2_nu[l] { - total2_nu += 1; - } - } + + let total1_nu = count_true(&cov1_nu[0..n]); + let total2_nu = count_true(&cov2_nu[0..n]); // Compute amount shared. @@ -1798,32 +1781,32 @@ pub fn annotate_seq_core( } i = j; } + erase_if(&mut annx, &to_delete); // Extend some alignments. // { ( sequence start, match length, ref tig, ref tig start, {mismatches} ) }. let mut aligns = vec![0; refs.len()]; - for i in 0..annx.len() { - aligns[annx[i].ref_id as usize] += 1; + for a in &annx { + aligns[a.ref_id as usize] += 1; } - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; - let len = annx[i].match_len as usize; + for a in &mut annx { + let t = a.ref_id as usize; + let len = a.match_len as usize; if aligns[t] == 1 - && annx[i].ref_start == 0 + && a.ref_start == 0 && len < refs[t].len() && len as f64 / refs[t].len() as f64 >= 0.75 - && (refs[t].len() as i32 + annx[i].tig_start - annx[i].ref_start) as usize - <= b_seq.len() + && (refs[t].len() as i32 + a.tig_start - a.ref_start) as usize <= b_seq.len() { for p in len..refs[t].len() { - let q = p as i32 + annx[i].tig_start - annx[i].ref_start; + let q = p as i32 + a.tig_start - a.ref_start; if b_seq[q as usize] != refs[t].get(p) { - annx[i].mismatches.push(q); + a.mismatches.push(q); } } - annx[i].match_len = refs[t].len() as i32; + a.match_len = refs[t].len() as i32; } } @@ -1831,8 +1814,8 @@ pub fn annotate_seq_core( if verbose { fwriteln!(log, "\nALIGNMENTS FOUR\n"); - for i in 0..annx.len() { - print_alignx(log, &annx[i], refdata); + for a in &annx { + print_alignx(log, a, refdata); } } @@ -1840,9 +1823,9 @@ pub fn annotate_seq_core( // is aligned a lot further, it wins. let mut lens = vec![0; refdata.refs.len()]; - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; - lens[t] += annx[i].ref_start + annx[i].match_len; + for a in &annx { + let t = a.ref_id as usize; + lens[t] += a.ref_start + a.match_len; } let mut to_delete: Vec = vec![false; annx.len()]; for i1 in 0..annx.len() { @@ -1885,8 +1868,8 @@ pub fn annotate_seq_core( let mut igc = -1_i32; const J_TOT: i32 = 20; const J_MIS: i32 = 5; - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for a in &annx { + let t = a.ref_id as usize; if rheaders[t].contains("segment") { continue; } @@ -1897,11 +1880,11 @@ pub fn annotate_seq_core( } else if refdata.segtype[t] == "J" { igj = true; } else if refdata.segtype[t] == "C" - && annx[i].ref_start == 0 - && annx[i].tig_start >= J_TOT + && a.ref_start == 0 + && a.tig_start >= J_TOT && refs[t].len() >= J_TOT as usize { - igc = annx[i].tig_start; + igc = a.tig_start; } } } @@ -1963,8 +1946,8 @@ pub fn annotate_seq_core( let t1 = annx[i1].ref_id as usize; if !rheaders[t1].contains("segment") && refdata.segtype[t1] == "D" { let mut have_v = false; - for i2 in 0..annx.len() { - let t2 = annx[i2].ref_id as usize; + for a in &annx { + let t2 = a.ref_id as usize; if !rheaders[t2].contains("segment") && refdata.segtype[t2] == "V" && refdata.rtype[t1] == refdata.rtype[t2] @@ -2045,8 +2028,8 @@ pub fn annotate_seq_core( erase_if(&mut results, &to_delete); if results.solo() || results[0].0 < results[1].0 { let mut best_matches = 0; - for i in 0..results.len() { - best_matches = max(best_matches, results[i].1); + for result in &results { + best_matches = max(best_matches, result.1); } if results[0].1 == best_matches { let t = results[0].2; @@ -2069,8 +2052,8 @@ pub fn annotate_seq_core( if verbose { fwriteln!(log, "\nALIGNMENTS FIVE\n"); - for i in 0..annx.len() { - print_alignx(log, &annx[i], refdata); + for a in &annx { + print_alignx(log, a, refdata); } } @@ -2184,8 +2167,8 @@ pub fn annotate_seq_core( // Pick between V segments starting at zero. And favor zero. let mut nv = 0; - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for a in &annx { + let t = a.ref_id as usize; if rheaders[t].contains("segment") { continue; } @@ -2248,8 +2231,8 @@ pub fn annotate_seq_core( let mut to_delete: Vec = vec![false; annx.len()]; let (mut u, mut v) = (Vec::::new(), Vec::::new()); - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for a in &annx { + let t = a.ref_id as usize; if !rheaders[t].contains("segment") { let name = rheaders[t].after("|").between("|", "|"); if rheaders[t].contains("UTR") { @@ -2261,13 +2244,13 @@ pub fn annotate_seq_core( } } v.sort(); - for i in 0..u.len() { - if !bin_member(&v, &u[i]) { + for item in &u { + if !bin_member(&v, item) { for j in 0..annx.len() { let t = annx[j].ref_id as usize; if !rheaders[t].contains("segment") { let name = rheaders[t].after("|").between("|", "|"); - if rheaders[t].contains("UTR") && u[i] == name { + if rheaders[t].contains("UTR") && item == name { to_delete[j] = true; } } @@ -2280,8 +2263,8 @@ pub fn annotate_seq_core( if verbose { fwriteln!(log, "\nALIGNMENTS SIX\n"); - for i in 0..annx.len() { - print_alignx(log, &annx[i], refdata); + for a in &annx { + print_alignx(log, a, refdata); } } @@ -2291,8 +2274,8 @@ pub fn annotate_seq_core( let mut to_delete: Vec = vec![false; annx.len()]; let mut vs = Vec::<(usize, usize)>::new(); - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for (i, a) in annx.iter().enumerate() { + let t = a.ref_id as usize; if !rheaders[t].contains("V-REGION") { continue; } @@ -2379,8 +2362,8 @@ pub fn annotate_seq_core( let mut to_delete: Vec = vec![false; annx.len()]; let (mut j1, mut j2) = (false, false); - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for a in &annx { + let t = a.ref_id as usize; if rheaders[t].contains("TRBJ1") { j1 = true; } @@ -2388,8 +2371,8 @@ pub fn annotate_seq_core( j2 = true; } } - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for (i, a) in annx.iter().enumerate() { + let t = a.ref_id as usize; if j1 && !j2 && rheaders[t].contains("TRBC2") { to_delete[i] = true; } @@ -2481,8 +2464,8 @@ pub fn annotate_seq_core( let mut to_delete: Vec = vec![false; annx.len()]; let (mut u, mut v) = (Vec::::new(), Vec::::new()); - for i in 0..annx.len() { - let t = annx[i].ref_id as usize; + for a in &annx { + let t = a.ref_id as usize; if !rheaders[t].contains("segment") { let name = rheaders[t].after("|").between("|", "|"); if rheaders[t].contains("UTR") { @@ -2494,13 +2477,13 @@ pub fn annotate_seq_core( } } v.sort(); - for i in 0..u.len() { - if !bin_member(&v, &u[i]) { + for item in &u { + if !bin_member(&v, item) { for j in 0..annx.len() { let t = annx[j].ref_id as usize; if !rheaders[t].contains("segment") { let name = rheaders[t].after("|").between("|", "|"); - if rheaders[t].contains("UTR") && u[i] == name { + if rheaders[t].contains("UTR") && item == name { to_delete[j] = true; } } @@ -2564,19 +2547,19 @@ pub fn print_some_annotations( fwriteln!(log, ""); } let mut vstart = Vec::::new(); - for l in 0..ann.len() { - let estart = ann[l].tig_start; - let t = ann[l].ref_id as usize; - let tstart = ann[l].ref_start; + for a in ann { + let estart = a.tig_start; + let t = a.ref_id as usize; + let tstart = a.ref_start; if tstart == 0 && (rheaders[t].contains("V-REGION") || rheaders[t].contains("L+V")) { vstart.push(estart); } } - for l in 0..ann.len() { - let (estart, len) = (ann[l].tig_start, ann[l].match_len); - let t = ann[l].ref_id as usize; - let tstart = ann[l].ref_start; - let mis = ann[l].mismatches; + for a in ann { + let (estart, len) = (a.tig_start, a.match_len); + let t = a.ref_id as usize; + let tstart = a.ref_start; + let mis = a.mismatches; fwrite!( log, "{}-{} ==> {}-{} on {} [len={}] (mis={})", @@ -2718,30 +2701,20 @@ pub fn get_cdr3(contig: &DnaStringSlice<'_>) -> Vec { // otherwise continue with next possible CDR3 start position. if right_flank_score >= RIGHT_FLANK_MIN_SCORE { // Check if there is a stop codon in the CDR3. - let mut stop_codon = false; - for aa in amino_acid_seq + let stop_codon = (amino_acid_seq + [cdr3_start_pos + 1..right_motif_start_pos + 2]) .iter() - .take(right_motif_start_pos + 2) - .skip(cdr3_start_pos + 1) - { - if aa == &b'*' { - stop_codon = true; - } - } + .any(|aa| *aa == b'*'); // If there is no stop codon and there is room for the full left motif in the AA seq, match left flank. let ll = left_motifs[0].len(); if !stop_codon && cdr3_start_pos >= ll { let mut left_flank_score = 0; for left_motif_col in 0..ll { - let mut hit = false; - for left_motif_row in &left_motifs { - if amino_acid_seq[cdr3_start_pos - ll + left_motif_col] + let hit = left_motifs.iter().any(|left_motif_row| { + amino_acid_seq[cdr3_start_pos - ll + left_motif_col] == left_motif_row[left_motif_col] - { - hit = true; - } - } + }); if hit { left_flank_score += 1; } @@ -3015,10 +2988,9 @@ impl AnnotationUnit { let mut s = 0_i32; let t = ann[0].ref_id as usize; let r = &refdata.refs[t]; - for l in 0..na { - for i in 0..ann[l].match_len { - if b.get((ann[l].tig_start + i) as usize) == r.get((ann[l].ref_start + i) as usize) - { + for a in &ann[0..na] { + for i in 0..a.match_len { + if b.get((a.tig_start + i) as usize) == r.get((a.ref_start + i) as usize) { s += 2; } else { s -= 3; @@ -3036,9 +3008,9 @@ impl AnnotationUnit { let types = ["IGH", "IGK", "IGL", "TRA", "TRB", "TRD", "TRG"]; let mut chain_type = String::new(); - for i in 0..types.len() { - if refdata.rheaders[t].contains(types[i]) { - chain_type = types[i].to_string(); + for type_ in types { + if refdata.rheaders[t].contains(type_) { + chain_type = type_.to_string(); break; } } @@ -3186,10 +3158,10 @@ impl ContigAnnotation { jsupp: Option, // num reads, umis supporting junction ) -> ContigAnnotation { let mut vstart = -1_i32; - for i in 0..ann.len() { - let t = ann[i].ref_id as usize; - if refdata.is_v(t) && ann[i].ref_start == 0 { - vstart = ann[i].tig_start; + for a in ann { + let t = a.ref_id as usize; + if refdata.is_v(t) && a.ref_start == 0 { + vstart = a.tig_start; } } let mut aa = String::new(); @@ -3198,8 +3170,8 @@ impl ContigAnnotation { if vstart >= 0 { let y = nucleotide_to_aminoacid_sequence(&x, vstart as usize); aa = stringme(&y); - for i in 0..y.len() { - if y[i] == b'*' { + for (i, y_i) in y.iter().enumerate() { + if *y_i == b'*' { stop = vstart + 3 * (i as i32); break; } @@ -3216,15 +3188,15 @@ impl ContigAnnotation { let cdr3 = found_cdr3s.first().unwrap(); cdr3x = stringme(&cdr3.aa_seq); let start = cdr3.start_position_on_contig; - for i in start..start + 3 * cdr3x.len() { - cdr3x_dna.push(x[i] as char); + for x_i in &x[start..start + 3 * cdr3x.len()] { + cdr3x_dna.push(*x_i as char); } cdr3x_start = start as i32; cdr3x_stop = (start + 3 * cdr3x.len()) as i32; } let mut qp = q.to_vec(); - for i in 0..q.len() { - qp[i] += 33; + for item in &mut qp[0..q.len()] { + *item += 33; } let mut ann = ContigAnnotation { barcode: tigname.before("_").to_string(), @@ -3434,9 +3406,7 @@ pub fn make_annotation_units( if !locs.is_empty() { let (j, entries) = (locs[0].1, locs[0].2); let mut annx = Vec::::new(); - for k in j..j + entries { - annx.push(ann[k]); - } + annx.extend_from_slice(&ann[j..j + entries]); x.push(AnnotationUnit::from_annotate_seq(b, refdata, &annx)); } } diff --git a/vdj_ann/src/refx.rs b/vdj_ann/src/refx.rs index db3c51d6f..e264b072b 100644 --- a/vdj_ann/src/refx.rs +++ b/vdj_ann/src/refx.rs @@ -180,8 +180,8 @@ pub fn make_vdj_ref_data_core( refdata.segtype.push("?"); } } - for j in 0..types.len() { - if rheaders2[i].contains(types[j]) { + for (j, type_) in types.iter().enumerate() { + if rheaders2[i].contains(type_) { refdata.rtype[i] = j as i32; } } @@ -226,8 +226,8 @@ pub fn make_vdj_ref_data_core( // Fill in id. - for i in 0..rheaders.len() { - refdata.id.push(rheaders[i].between("|", "|").force_i32()); + for header in rheaders.iter() { + refdata.id.push(header.between("|", "|").force_i32()); } // Extend the reference. @@ -251,18 +251,18 @@ pub fn make_vdj_ref_data_core( // Determine which V segments have matching UTRs in the reference. - for t in 0..rheaders.len() { - if !rheaders[t].contains("segment") { - let name = rheaders[t].after("|").between("|", "|"); - if rheaders[t].contains("UTR") { + for header in rheaders.iter() { + if !header.contains("segment") { + let name = header.after("|").between("|", "|"); + if header.contains("UTR") { refdata.has_utr.insert(name.to_string(), true); } } } - for t in 0..rheaders.len() { - if !rheaders[t].contains("segment") { - let name = rheaders[t].after("|").between("|", "|"); - if rheaders[t].contains("V-REGION") { + for header in rheaders.iter() { + if !header.contains("segment") { + let name = header.after("|").between("|", "|"); + if header.contains("V-REGION") { refdata.has_utr.entry(name.to_string()).or_insert(false); } } diff --git a/vdj_ann/src/transcript.rs b/vdj_ann/src/transcript.rs index 349a6822b..565abbb6b 100644 --- a/vdj_ann/src/transcript.rs +++ b/vdj_ann/src/transcript.rs @@ -308,20 +308,19 @@ pub fn junction_supp_core( idj += 1; } let mut mm = Vec::<(i32, i32)>::new(); - for r in idi..idj { - let ida = ids[r]; - let b = &reads[ida as usize]; + for ida in &ids[idi..idj] { + let b = &reads[*ida as usize]; if b.len() < k { continue; } for j in 0..b.len() - k + 1 { let z: Kmer20 = b.get_kmer(j); let low = lower_bound1_3(&kmers_plus, &z) as usize; - for m in low..kmers_plus.len() { - if kmers_plus[m].0 != z { + for kmer in &kmers_plus[low..] { + if kmer.0 != z { break; } - let p = kmers_plus[m].2 as usize; + let p = kmer.2 as usize; if j > 0 && p > 0 && b.get(j - 1) == jseq.get(p - 1) { continue; } diff --git a/vdj_ann/src/vdj_features.rs b/vdj_ann/src/vdj_features.rs index 1897a9a63..a0b6207da 100644 --- a/vdj_ann/src/vdj_features.rs +++ b/vdj_ann/src/vdj_features.rs @@ -337,8 +337,8 @@ pub fn cdr3_start(aa: &[u8], _chain_type: &str, _verbose: bool) -> usize { for j in aa.len().saturating_sub(nm + reach)..=aa.len().saturating_sub(nm) { let mut score = 0; for k in 0..nm { - for l in 0..motif.len() { - if aa[j + k] == motif[l][k] { + for m in motif { + if aa[j + k] == m[k] { score += 1; if aa[j + k] == b'Q' { break; @@ -360,8 +360,8 @@ pub fn cdr3_score(aa: &[u8], _chain_type: &str, _verbose: bool) -> usize { for j in aa.len().saturating_sub(nm + REACH)..=aa.len().saturating_sub(nm) { let mut score = 0; for k in 0..nm { - for l in 0..motif.len() { - if aa[j + k] == motif[l][k] { + for m in motif { + if aa[j + k] == m[k] { score += 1; if aa[j + k] == b'Q' { break; diff --git a/vdj_ann_ref/src/bin/build_supp_ref.rs b/vdj_ann_ref/src/bin/build_supp_ref.rs index 02de2b913..4b8f12fe3 100644 --- a/vdj_ann_ref/src/bin/build_supp_ref.rs +++ b/vdj_ann_ref/src/bin/build_supp_ref.rs @@ -92,8 +92,8 @@ fn main() { let mut rheaders = Vec::::new(); read_fasta_into_vec_dna_string_plus_headers(fasta, &mut refs, &mut rheaders); let mut to_chr = HashMap::new(); - for i in 0..rheaders.len() { - let chr = rheaders[i].before(" "); + for (i, header) in rheaders.iter().enumerate() { + let chr = header.before(" "); to_chr.insert(chr.to_string(), i); } diff --git a/vdj_ann_ref/src/bin/build_vdj_ref.rs b/vdj_ann_ref/src/bin/build_vdj_ref.rs index c665712bc..da57eb84b 100644 --- a/vdj_ann_ref/src/bin/build_vdj_ref.rs +++ b/vdj_ann_ref/src/bin/build_vdj_ref.rs @@ -255,9 +255,9 @@ fn parse_gtf_file(gtf: &str, demangle: &HashMap, exons: &mut Vec // change it to CDS. let mut biotype = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with(" gene_biotype") { - biotype = fields8[i].between("\"", "\"").to_string(); + for field in &fields8 { + if field.starts_with(" gene_biotype") { + biotype = field.between("\"", "\"").to_string(); } } let mut cat = fields[2]; @@ -286,9 +286,9 @@ fn parse_gtf_file(gtf: &str, demangle: &HashMap, exons: &mut Vec // Get gene name and demangle. let mut gene = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with(" gene_name") { - gene = fields8[i].between("\"", "\"").to_string(); + for field in &fields8 { + if field.starts_with(" gene_name") { + gene = field.between("\"", "\"").to_string(); } } gene = gene.to_uppercase(); @@ -334,18 +334,18 @@ fn parse_gtf_file(gtf: &str, demangle: &HashMap, exons: &mut Vec // Get transcript name. let mut tr = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with(" transcript_name") { - tr = fields8[i].between("\"", "\"").to_string(); + for field in &fields8 { + if field.starts_with(" transcript_name") { + tr = field.between("\"", "\"").to_string(); } } // Get transcript id. let mut trid = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with(" transcript_id") { - trid = fields8[i].between("\"", "\"").to_string(); + for field in &fields8 { + if field.starts_with(" transcript_id") { + trid = field.between("\"", "\"").to_string(); } } @@ -1057,15 +1057,15 @@ fn main() { let fields8: Vec<&str> = fields[8].split_terminator(';').collect(); let (mut gene, mut gene2) = (String::new(), String::new()); let mut biotype = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with("Name=") { - gene = fields8[i].after("Name=").to_string(); + for field in &fields8 { + if field.starts_with("Name=") { + gene = field.after("Name=").to_string(); } - if fields8[i].starts_with("description=") { - gene2 = fields8[i].after("description=").to_string(); + if field.starts_with("description=") { + gene2 = field.after("description=").to_string(); } - if fields8[i].starts_with("biotype=") { - biotype = fields8[i].after("biotype=").to_string(); + if field.starts_with("biotype=") { + biotype = field.after("biotype=").to_string(); } } @@ -1187,8 +1187,8 @@ fn main() { // Find the chromosomes that we're using. let mut all_chrs = Vec::::new(); - for k in 0..exons.len() { - all_chrs.push(exons[k].2.clone()); + for exon in &exons { + all_chrs.push(exon.2.clone()); } unique_sort(&mut all_chrs); @@ -1234,8 +1234,8 @@ fn main() { refs.push(DnaString::from_dna_string(&last)); } let mut to_chr = HashMap::new(); - for i in 0..rheaders.len() { - to_chr.insert(rheaders[i].clone(), i); + for (i, header) in rheaders.iter().enumerate() { + to_chr.insert(header.clone(), i); } // Get the DNA sequences for the exons. @@ -1245,10 +1245,10 @@ fn main() { t.elapsed().as_secs_f64() ); let mut dna = Vec::::new(); - for i in 0..exons.len() { - let chr = &exons[i].2; + for exon in &exons { + let chr = &exon.2; let chrid = to_chr[chr]; - let (start, stop) = (exons[i].3, exons[i].4); + let (start, stop) = (exon.3, exon.4); let seq = refs[chrid].slice(start as usize, stop as usize).to_owned(); dna.push(seq); } @@ -1266,13 +1266,13 @@ fn main() { while i < exons.len() { let j = next_diff12_8(&exons, i as i32) as usize; let mut x = Vec::::new(); - for k in i..j { - x.push(dna[k].clone()); + for d in &dna[i..j] { + x.push(d.clone()); } if !exons[i].6 { x.reverse(); - for k in 0..x.len() { - x[k] = x[k].rc().clone(); + for item in &mut x { + *item = item.rc().clone(); } } dnas.push((x, i, j)); @@ -1302,8 +1302,8 @@ fn main() { } if matches { let (r, s) = (dnas[i - 1].1, dnas[i - 1].2); - for k in r..s { - to_delete[k] = true; + for item in &mut to_delete[r..s] { + *item = true; } } } @@ -1322,8 +1322,8 @@ fn main() { while i < exons.len() { let j = next_diff12_8(&exons, i as i32) as usize; let mut fws = Vec::::new(); - for k in i..j { - fws.push(exons[k].6); + for exon in &exons[i..j] { + fws.push(exon.6); } unique_sort(&mut fws); assert!(fws.len() == 1); @@ -1340,8 +1340,8 @@ fn main() { // ◼ NOT SURE WHAT THIS IS DOING NOW. let mut chrs = Vec::::new(); - for k in i..j { - chrs.push(exons[k].2.clone()); + for exon in &exons[i..j] { + chrs.push(exon.2.clone()); } unique_sort(&mut chrs); let chr = chrs[0].clone(); @@ -1353,12 +1353,12 @@ fn main() { let mut seq = DnaString::new(); let trid = &exons[i].7; - for k in i..j { - if exons[k].2 != chr { + for exon in &exons[i..j] { + if exon.2 != chr { continue; } - let (start, stop) = (exons[k].3, exons[k].4); - let cat = &exons[k].5; + let (start, stop) = (exon.3, exon.4); + let cat = &exon.5; if cat == "five_prime_utr" { let seqx = refs[chrid].slice(start as usize, stop as usize); for i in 0..seqx.len() { @@ -1384,12 +1384,12 @@ fn main() { { let mut seq = DnaString::new(); let mut ncodons = 0; - for k in i..j { - if exons[k].2 != chr { + for exon in &exons[i..j] { + if exon.2 != chr { continue; } - let (start, stop) = (exons[k].3, exons[k].4); - let cat = &exons[k].5; + let (start, stop) = (exon.3, exon.4); + let cat = &exon.5; if cat == "CDS" { ncodons += 1; let seqx = refs[chrid].slice(start as usize, stop as usize); @@ -1447,6 +1447,7 @@ fn main() { && gene != "IGHD" { let mut using = Vec::::new(); + #[allow(clippy::needless_range_loop)] for k in i..j { if exons[k].2 == chr && exons[k].5 != "five_prime_utr" { using.push(k); @@ -1497,11 +1498,11 @@ fn main() { gene = format!("TRG{}", gene.after("TRGC")); } let mut seq = DnaString::new(); - for k in i..j { - if exons[k].2 != chr { + for exon in &exons[i..j] { + if exon.2 != chr { continue; } - let (start, stop) = (exons[k].3, exons[k].4); + let (start, stop) = (exon.3, exon.4); let seqx = refs[chrid].slice(start as usize, stop as usize); for i in 0..seqx.len() { seq.push(seqx.get(i)); @@ -1531,46 +1532,46 @@ fn main() { "{:.1} seconds used, adding genes", t.elapsed().as_secs_f64() ); - for i in 0..added_genes.len() { + for gene in added_genes { add_gene( &mut out, - added_genes[i].0, + gene.0, &mut record, - added_genes[i].1, - added_genes[i].2, - added_genes[i].3, + gene.1, + gene.2, + gene.3, &to_chr, &refs, none, - added_genes[i].4, + gene.4, &source, ); } - for i in 0..added_genes2.len() { + for gene in added_genes2 { add_gene2( &mut out, - added_genes2[i].0, + gene.0, &mut record, - added_genes2[i].1, - added_genes2[i].2, - added_genes2[i].3, - added_genes2[i].4, - added_genes2[i].5, + gene.1, + gene.2, + gene.3, + gene.4, + gene.5, &to_chr, &refs, none, - added_genes2[i].6, + gene.6, &source, ); } - for i in 0..added_genes2_source.len() { - let gene = &added_genes2_source[i].0; - let start1 = added_genes2_source[i].1; - let stop1 = added_genes2_source[i].2; - let start2 = added_genes2_source[i].3; - let stop2 = added_genes2_source[i].4; - let fw = added_genes2_source[i].5; - let source = &added_genes2_source[i].6; + for added_gene in added_genes2_source { + let gene = &added_gene.0; + let start1 = added_gene.1; + let stop1 = added_gene.2; + let start2 = added_gene.3; + let stop2 = added_gene.4; + let fw = added_gene.5; + let source = &added_gene.6; let mut seq = DnaString::new(); load_genbank_accession(source, &mut seq); let seq1 = seq.slice(start1 - 1, stop1); @@ -1585,9 +1586,9 @@ fn main() { let header = header_from_gene(gene, false, &mut record, source); print_fasta(&mut out, &header, &seq.slice(0, seq.len()), none); } - for i in 0..added_genes_seq.len() { - let gene = &added_genes_seq[i].0; - let seq = DnaString::from_dna_string(added_genes_seq[i].1); + for added_gene in added_genes_seq { + let gene = added_gene.0; + let seq = DnaString::from_dna_string(added_gene.1); let header = header_from_gene(gene, false, &mut record, &source); print_fasta(&mut out, &header, &seq.slice(0, seq.len()), none); } diff --git a/vdj_ann_ref/src/bin/build_vdj_ref_exons.rs b/vdj_ann_ref/src/bin/build_vdj_ref_exons.rs index fcfafbe6c..994ffa48f 100644 --- a/vdj_ann_ref/src/bin/build_vdj_ref_exons.rs +++ b/vdj_ann_ref/src/bin/build_vdj_ref_exons.rs @@ -67,9 +67,9 @@ fn parse_gtf_file(gtf: &str, demangle: &HashMap, exons: &mut Vec // change it to CDS. [NOT] let mut biotype = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with(" gene_biotype") { - biotype = fields8[i].between("\"", "\"").to_string(); + for field in &fields8 { + if field.starts_with(" gene_biotype") { + biotype = field.between("\"", "\"").to_string(); } } let cat = fields[2]; @@ -89,9 +89,9 @@ fn parse_gtf_file(gtf: &str, demangle: &HashMap, exons: &mut Vec // Get gene name and demangle. let mut gene = ""; - for i in 0..fields8.len() { - if fields8[i].starts_with(" gene_name") { - gene = fields8[i].between("\"", "\""); + for field in &fields8 { + if field.starts_with(" gene_name") { + gene = field.between("\"", "\""); } } let gene = gene.to_uppercase(); @@ -139,18 +139,18 @@ fn parse_gtf_file(gtf: &str, demangle: &HashMap, exons: &mut Vec // Get transcript name. let mut tr = ""; - for i in 0..fields8.len() { - if fields8[i].starts_with(" transcript_name") { - tr = fields8[i].between("\"", "\""); + for field in &fields8 { + if field.starts_with(" transcript_name") { + tr = field.between("\"", "\""); } } // Get transcript id. let mut trid = ""; - for i in 0..fields8.len() { - if fields8[i].starts_with(" transcript_id") { - trid = fields8[i].between("\"", "\""); + for field in &fields8 { + if field.starts_with(" transcript_id") { + trid = field.between("\"", "\""); } } @@ -698,15 +698,15 @@ fn main() { let fields8: Vec<&str> = fields[8].split_terminator(';').collect(); let (mut gene, mut gene2) = (String::new(), String::new()); let mut biotype = String::new(); - for i in 0..fields8.len() { - if fields8[i].starts_with("Name=") { - gene = fields8[i].after("Name=").to_string(); + for field in &fields8 { + if field.starts_with("Name=") { + gene = field.after("Name=").to_string(); } - if fields8[i].starts_with("description=") { - gene2 = fields8[i].after("description=").to_string(); + if field.starts_with("description=") { + gene2 = field.after("description=").to_string(); } - if fields8[i].starts_with("biotype=") { - biotype = fields8[i].after("biotype=").to_string(); + if field.starts_with("biotype=") { + biotype = field.after("biotype=").to_string(); } } @@ -828,8 +828,8 @@ fn main() { // Find the chromosomes that we're using. let mut all_chrs = Vec::::new(); - for k in 0..exons.len() { - all_chrs.push(exons[k].2.clone()); + for exon in &exons { + all_chrs.push(exon.2.clone()); } unique_sort(&mut all_chrs); @@ -871,8 +871,8 @@ fn main() { refs.push(DnaString::from_dna_string(&last)); } let mut to_chr = HashMap::new(); - for i in 0..rheaders.len() { - to_chr.insert(rheaders[i].clone(), i); + for (i, header) in rheaders.iter().enumerate() { + to_chr.insert(header.clone(), i); } // Get the DNA sequences for the exons. Extended by ten in both directions. @@ -881,12 +881,12 @@ fn main() { let mut dna = Vec::::new(); let mut starts = Vec::::new(); let mut stops = Vec::::new(); - for i in 0..exons.len() { - let chr = &exons[i].2; + for exon in &exons { + let chr = &exon.2; let chrid = to_chr[chr]; - starts.push(exons[i].3 as usize); - stops.push(exons[i].4 as usize); - let (start, stop) = (exons[i].3 - EXT as i32, exons[i].4 + EXT as i32); + starts.push(exon.3 as usize); + stops.push(exon.4 as usize); + let (start, stop) = (exon.3 - EXT as i32, exon.4 + EXT as i32); let seq = refs[chrid].slice(start as usize, stop as usize).to_owned(); dna.push(seq); } diff --git a/vdj_ann_ref/src/lib.rs b/vdj_ann_ref/src/lib.rs index 9fc02f8ce..af4ede428 100644 --- a/vdj_ann_ref/src/lib.rs +++ b/vdj_ann_ref/src/lib.rs @@ -144,8 +144,8 @@ mod tests { let mut ann = Vec::::new(); annotate_seq(&seq, &refdata, &mut ann, true, false, true); let mut have_d = false; - for i in 0..ann.len() { - if refdata.is_d(ann[i].ref_id as usize) { + for a in ann { + if refdata.is_d(a.ref_id as usize) { have_d = true; } }