this is my code: fn pseudomap(consensus: &ConsensusTx, tx: &Transcript, bucket: ) { let…
- User
this is my code:
fn pseudomap(consensus: &ConsensusTx, tx: &Transcript, bucket: ) { let mut tx_exs = tx.2.iter().collect::<Vec<>>(); tx_exs.par_sort_unstable_by_key(|x| (x.0, x.1));
consensus.iter().any(|(start, end, exons)| { if tx.0 >= *start - BOUNDARY && tx.1 <= *end + BOUNDARY { // read is within the boundaries of a consensus group let mut acc = exons.to_owned(); let (status, matches) = cmp_exons(&mut acc, tx_exs.clone()); let cov = (matches.len() as f64 / acc.len() as f64) * 100.0; match status { Status::Pass => { if cov >= MIN_COVERAGE { bucket.entry(Pocket::Pass).push(tx.3); } else { bucket.entry(Pocket::Partial).push(tx.3); } } Status::Fail => { bucket.entry(Pocket::InRet).push(tx.3); } } true } else { // start/end of read is outside of consensus boundaries -> unmapped // println!("{:?} {}-{} could not be aligned", tx.3, tx.0, tx.1); bucket.entry(Pocket::Unmapped).push(tx.3); false } });}
fn main() { ...
let tracks = parse_tracks(&bed).unwrap(); let consensus = Arc::new(consensus(tracks)); let reads = parse_tracks(&isoseq).unwrap(); reads.par_iter().for_each(|(chr, txs)| { let cn = consensus.get(chr).unwrap(); txs.par_iter().for_each(|tx| { pseudomap(cn, tx, bucket); }); });}
where:
pub type Transcript = (u32, u32, HashSet<(u32, u32)>, String); pub type Chromosome = String; pub type ConsensusTx = Vec<(u32, u32, Vec<(u32, u32)>)>; pub type TranscriptMap = HashMap<Chromosome, Vec>; pub type ConsensusMap = HashMap<Chromosome, ConsensusTx>;
I want to pass to pseudomap a "bucket". Based on their Status (Pass or Fail) they will populate the four vec[]! (Pass, Partial, InRet, Unmapped). Any ideas on how to implement this? Needs to be the fastest and efficient way. Consider that the code uses rayon to iterate in parallel. You are free to use any crate, algorithm, trick, etc.
- Assistant
- User
this was supper inefficient. For 1 million reads I ended up consuming around 50Gb!!!
- Assistant
- User
ok it improved a the time but still to much time, any other idea to make it a lot faster? maybe you have another approach?
- Assistant
- User
look at this:
use hashbrown::HashSet;
use crate::config::*;
pub type Transcript = (u32, u32, HashSet<(u32, u32)>, String); pub type Chromosome = String;
#[derive(Debug, PartialEq, Clone)] pub struct Record { pub chrom: Chromosome, pub info: Transcript, }
impl Record { pub fn new(line: &str) -> Result<Record, &'static str> { let fields: Vec<&str> = line.split('\t').collect();
if fields.len() < MIN_BED_FIELDS { return Err("Bed line has less than 12 fields and cannot be parsed into a Record"); } let chrom = fields[0].to_string(); let tx_start = fields[1] .parse::<u32>() .map_err(|_| "Cannot parse tx_start")?; let tx_end = fields[2] .parse::<u32>() .map_err(|_| "Cannot parse tx_end")?; let id = fields[3].to_string(); let exon_start = fields[11] .split(',') .filter(|s| !s.is_empty()) .map(|x| x.parse::<u32>()) .collect::<Result<Vec<u32>, _>>(); let exon_end = fields[10] .split(',') .filter(|s| !s.is_empty()) .map(|x| x.parse::<u32>()) .collect::<Result<Vec<u32>, _>>(); let exon_start = exon_start.map_err(|_| "Cannot parse exon_start")?; let exon_end = exon_end.map_err(|_| "Cannot parse exon_end")?; if exon_start.len() != exon_end.len() { return Err("Exon start and end vectors have different lengths"); } let exon_starts: Vec<u32> = exon_start.iter().map(|&s| s + tx_start).collect(); let exon_ends: Vec<u32> = exon_end .iter() .enumerate() .map(|(i, &s)| s + exon_starts[i]) .collect(); let start_ends = exon_starts .iter() .zip(exon_ends.iter()) .map(|(&s, &e)| (s, e)) .collect::<HashSet<(u32, u32)>>(); Ok(Record { chrom: chrom.to_string(), info: (tx_start, tx_end, start_ends, id), }) } - Assistant
If your primary concern is minimizing memory usage and improving the efficiency of cloning the
idfield, you could consider the following techniques: - User
there is no ID duplication in my dataset, all are unique names
- Assistant