To fulfill your requirement, you can use a few Rust crates and programming techniques. Below is an approach that uses a combination of the itertools, hashbrown (optional, for faster HashMap), and optionally rayon for parallel processing if needed.
First, ensure that you have the itertools and rayon crates added to your Cargo.toml as dependencies:
[dependencies]
itertools = "0.10"
rayon = "1.5"
# hashbrown is a faster implementation of std Rust's HashMap, it can be used as drop-in replacement
# hashbrown = "0.11"
Next, we'll write the function to perform the aggregation. We'll first sort and deduplicate exons within each transcript, then merge transcripts that are within the same coordinates, and finally collect and deduplicate all the exons from the merged transcripts.
The Rust code could like this:
use itertools::Itertools; // For unique and sorted
use std::collections::HashMap;
// use hashbrown::HashMap; // Optional; use this for potentially faster performance
use rayon::prelude::*; // For parallel processing capabilities
// This function will remove duplicates and sort the exon coordinates.
fn merge_exons(exons: &mut Vec<(u32, u32)>) {
exons.sort_unstable();
exons.dedup();
}
// This function will process the transcripts and return the merged exons.
fn process_transcripts(transcripts: Vec<(u32, u32, Vec<(u32, u32)>)>) -> Vec<(u32, u32, Vec<(u32, u32)>)> {
let mut grouped_transcripts = transcripts
// Assuming transcripts are sorted by start and end.
.into_iter()
// Merge exons as we accumulate transcripts within the same coordinates
.coalesce(|(start_a, end_a, mut exons_a), (start_b, end_b, exons_b)| {
if start_b <= end_a {
exons_a.extend(exons_b);
merge_exons(&mut exons_a);
Ok((start_a, std::cmp::max(end_a, end_b), exons_a))
} else {
Err(((start_a, end_a, exons_a), (start_b, end_b, exons_b)))
}
})
.collect::<Vec<_>>();
// Deduplicate and sort exons for the final result
for (_, _, ref mut exons) in &mut grouped_transcripts {
merge_exons(exons);
}
grouped_transcripts
}
// Main function that will process the entire HashMap.
// In `transcript_map`, keys are chromosome names; values are transcripts' info on that chromosome.
fn merge_transcripts(mut transcript_map: HashMap<String, Vec<(u32, u32, Vec<(u32, u32)>)>>) -> HashMap<String, Vec<(u32, u32, Vec<(u32, u32)>)>> {
transcript_map.par_iter_mut().for_each(|(_key, transcripts)| {
*transcripts = process_transcripts(std::mem::take(transcripts));
});
transcript_map
}
fn main() {
// Here we would load the hashmap of transcripts. For this example, just creating a simple one.
let mut transcript_map: HashMap<String, Vec<(u32, u32, Vec<(u32, u32)>)>> = HashMap::new();
// Normally, you'll populate your `transcript_map` with the actual data.
// Process the transcripts.
let merged_transcript_map = merge_transcripts(transcript_map);
// Print result (or do something with the `merged_transcript_map`)
for (chromosome, transcripts) in merged_transcript_map {
println!("{}: {:?}", chromosome, transcripts);
}
}
This code makes use of parallel iterators from the rayon crate to process each chromosome's transcripts in parallel, which can help speed up the process if you have many chromosomes and each with a significant number of transcripts.
The merge_exons function merges exon ranges for a single transcript and process_transcripts processes a vector of transcripts, merging the overlapping or consecutive ones. Then the merge_transcripts function applies this process for each chromosome's transcripts and utilizes parallel processing.
Please note that this code is written without access to example data and under assumptions about the structure and behavior. Therefore, you should adapt this approach according to your data and test carefully. Consider special edge cases and the expected behavior when transcript coordinates exactly match, overlap partially, or are fully within another transcript.