#![warn(missing_docs)]
use std::io::Write as _;
use atomic_counter::{AtomicCounter, RelaxedCounter};
use anyhow::Context as _;
pub mod chunks;
pub mod cli;
pub mod consts;
pub mod count;
pub mod file_parsing;
pub mod kmer_counter;
pub mod kmer_hash;
pub mod matched_sequences;
pub mod sequence_normalizer;
use file_parsing::read_file_lines;
use crate::kmer_counter::KmerCounter;
#[allow(clippy::too_many_arguments)]
pub fn back_to_sequences<T: KmerCounter>(
in_fasta_reads: String,
in_fasta_kmers: String,
out_fasta_reads: String,
out_txt_kmers: String,
output_mapping_positions: bool,
kmer_size: usize,
counted_kmer_threshold: usize,
min_threshold: f32,
max_threshold: f32,
stranded: bool,
query_reverse: bool,
no_low_complexity: bool,
) -> anyhow::Result<()> {
if !in_fasta_reads.is_empty() {
cli::validate_non_empty_file(in_fasta_reads.clone())?;
}
cli::validate_non_empty_file(in_fasta_kmers.clone())?;
let (kmer_set, kmer_size) =
kmer_hash::index_kmers::<T>(in_fasta_kmers, kmer_size, stranded, no_low_complexity)
.context("Error indexing kmers: ")?;
if !out_fasta_reads.is_empty() {
if output_mapping_positions {
let (total_nucleotides, total_kmer, match_kmer) =
count::kmers_in_fasta_file_par::<_, matched_sequences::MatchedSequencePositional>(
in_fasta_reads,
&kmer_set,
kmer_size,
out_fasta_reads.clone(),
min_threshold,
max_threshold,
stranded,
query_reverse,
true, )?;
eprintln!(
"Filtered sequences with exact kmer count and mapping positions are in file {}",
out_fasta_reads
);
println!(
"Number of nucleotides seen {}",
total_nucleotides
);
println!(
"Number of kmer seen {}, number of kmer match {} kmer ratio {:.5}",
total_kmer,
match_kmer,
match_kmer as f64 / total_kmer as f64 * 100.0
);
} else {
let (total_nucleotides, total_kmer, match_kmer) =
count::kmers_in_fasta_file_par::<_, matched_sequences::MachedCount>(
in_fasta_reads,
&kmer_set,
kmer_size,
out_fasta_reads.clone(),
min_threshold,
max_threshold,
stranded,
query_reverse,
false, )?;
eprintln!(
"Filtered sequences with exact kmer count are in file {}",
out_fasta_reads
);
println!(
"Number of nucleotides seen {}",
total_nucleotides
);
println!(
"Number of kmer seen {}, number of kmer match {} ratio {:.5}",
total_kmer,
match_kmer,
match_kmer as f64 / total_kmer as f64 * 100.0
);
}
} else {
eprintln!("No output file provided, only the kmers with their count is output");
let (total_nucleotides, total_kmer, match_kmer) =
count::only_kmers_in_fasta_file_par::<_, matched_sequences::MachedCount>(
in_fasta_reads,
&kmer_set,
kmer_size,
stranded,
query_reverse,
)?;
println!(
"Number of nucleotides seen {}",
total_nucleotides
);
println!(
"Number of kmer seen {}, number of kmer match {} ratio {:.5}",
total_kmer,
match_kmer,
match_kmer as f64 / total_kmer as f64 * 100.0
);
}
if !out_txt_kmers.is_empty() {
(|| -> std::io::Result<_> {
let mut output = std::fs::File::create(&out_txt_kmers)?;
for (kmer, count) in kmer_set.iter() {
if count.get_count() >= counted_kmer_threshold {
output.write_all(kmer)?;
writeln!(output, " {}", count.to_string())?;
}
}
Ok(())
})()
.context("Error writing the kmers file")?;
eprintln!(
"kmers with their number of occurrences in the original sequences are in file {}",
out_txt_kmers
);
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub fn back_to_multiple_sequences(
in_fasta_filenames: String,
in_fasta_kmers: String,
out_fasta_filenames: String,
out_txt_kmers: String,
output_mapping_positions: bool,
kmer_size: usize,
counted_kmer_threshold: usize,
min_threshold: f32,
max_threshold: f32,
stranded: bool,
query_reverse: bool,
no_low_complexity: bool,
) -> anyhow::Result<()> {
if !in_fasta_filenames.is_empty() {
cli::validate_non_empty_file(in_fasta_filenames.clone())?;
}
cli::validate_non_empty_file(in_fasta_kmers.clone())?;
let input_files = read_file_lines(in_fasta_filenames.as_str())
.map_err(|e| eprintln!("Error reading file: {}", e))
.unwrap();
let output_files = read_file_lines(out_fasta_filenames.as_str())
.map_err(|e| eprintln!("Error reading file: {}", e))
.unwrap();
if input_files.len() != output_files.len() {
eprintln!("Error: the number of input files and output files must be the same");
std::process::exit(1);
}
let (kmer_set, kmer_size) = kmer_hash::index_kmers::<RelaxedCounter>(
in_fasta_kmers,
kmer_size,
stranded,
no_low_complexity,
)
.context("Error indexing kmers")?;
if output_mapping_positions {
for (in_f, out_f) in input_files.iter().zip(output_files.iter()) {
let (total_nucleotides, total_kmer, match_kmer) =
count::kmers_in_fasta_file_par::<_, matched_sequences::MatchedSequencePositional>(
in_f.to_string(),
&kmer_set,
kmer_size,
out_f.clone().to_string(),
min_threshold,
max_threshold,
stranded,
query_reverse,
true, )?;
eprintln!(
"Filtered sequences from {} with exact kmer count and mapping positions are in files specified at {}",
in_f, out_f
);
println!(
"Number of nucleotides seen {}",
total_nucleotides
);
println!(
"Number of kmer seen {}, number of kmer match {} ratio {:.5}",
total_kmer,
match_kmer,
match_kmer as f64 / total_kmer as f64 * 100.0
);
}
} else {
for (in_f, out_f) in input_files.iter().zip(output_files.iter()) {
let (total_nucleotides, total_kmer, match_kmer) =
count::kmers_in_fasta_file_par::<_, matched_sequences::MachedCount>(
in_f.to_string(),
&kmer_set,
kmer_size,
out_f.clone().to_string(),
min_threshold,
max_threshold,
stranded,
query_reverse,
false, )?;
eprintln!(
"Filtered sequences from {} with exact kmer count are in files specified at {}",
in_f, out_f
);
println!(
"Number of nucleotides seen {}",
total_nucleotides
);
println!(
"Number of kmer seen {}, number of kmer match {} ratio {:.5}",
total_kmer,
match_kmer,
match_kmer as f64 / total_kmer as f64 * 100.0
);
}
}
if !out_txt_kmers.is_empty() {
(|| -> std::io::Result<_> {
let mut output = std::fs::File::create(&out_txt_kmers)?;
for (kmer, count) in kmer_set.iter() {
if count.get() >= counted_kmer_threshold {
output.write_all(kmer)?;
writeln!(output, " {}", count.get())?;
}
}
Ok(())
})()
.context("Error writing the kmers file: {}")?;
eprintln!(
"kmers with their number of occurrences in the original sequences are in file {}",
out_txt_kmers
);
}
Ok(())
}