help me with this please: fn pseudomap(consensus: &ConsensusTx, tx: &Transcript) ->…
- User
help me with this please:
fn pseudomap(consensus: &ConsensusTx, tx: &Transcript) -> (Arc, f64, Pocket) { let mut tx_exs = tx.2.iter().collect::<Vec<_>>(); tx_exs.par_sort_unstable_by_key(|x| (x.0, x.1));
let mapped_read = 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; let rs = match status { Status::Pass => { if cov >= MIN_COVERAGE { (tx.3.clone(), cov, Pocket::Pass) } else { (tx.3.clone(), cov, Pocket::Partial) } } Status::Fail => (tx.3.clone(), cov, Pocket::InRet), }; rs; true } else { // start/end of read is outside of consensus boundaries -> unmapped // println!("{:?} {}-{} could not be aligned", tx.3, tx.0, tx.1); (tx.3.clone(), 0.0, Pocket::Unmapped); false } });}
any() needs to return a bool but I want to return a tuple. Using for_each() is slower, any thoughts?
- Assistant
- User
ok thanks. this is my debug function with you impl:
fn pseudomap(consensus: &ConsensusTx, tx: &Transcript) { let mut tx_exs = tx.2.iter().collect::<Vec<_>>(); tx_exs.par_sort_unstable_by_key(|x| (x.0, x.1));
let mapped_read = consensus.iter().find_map(|(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; let rs = match status { Status::Pass => { if cov >= MIN_COVERAGE { Some((tx.3.as_bytes(), cov, Pocket::Pass)) } else { Some((tx.3.as_bytes(), cov, Pocket::Partial)) } } Status::Fail => Some((tx.3.as_bytes(), cov, Pocket::InRet)), }; println!("{:?}", rs); Some(rs) } else { // start/end of read is outside of consensus boundaries -> unmapped // println!("{:?} {}-{} could not be aligned", tx.3, tx.0, tx.1); let rs = Some((tx.3.as_bytes(), 0.0, Pocket::Unmapped)); println!("{:?}", rs); Some(rs) } }); - Assistant
- User
ok thanks for your help, this is my final function:
fn pseudomap(consensus: &ConsensusTx, tx: &Transcript) -> Option<(&[u8], f64, Pocket)> { let mut tx_exs = tx.2.iter().collect::<Vec<_>>(); tx_exs.par_sort_unstable_by_key(|x| (x.0, x.1));
let mapped_read = consensus .iter() .find_map(|(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; let rs = match status { Status::Pass => { if cov >= MIN_COVERAGE { Some((tx.3.as_bytes(), cov, Pocket::Pass)) } else { Some((tx.3.as_bytes(), cov, Pocket::Partial)) } } Status::Fail => Some((tx.3.as_bytes(), cov, Pocket::InRet)), }; rs } else { None } }) .unwrap_or_else(|| { // start/end of read is outside of consensus boundaries -> unmapped // println!("{:?} {}-{} could not be aligned", tx.3, tx.0, tx.1); (tx.3.as_bytes(), 0.0, Pocket::Unmapped) }); Some(mapped_read) - Assistant
- User
check this chunk of code:
reads.par_iter().for_each(|(chr, txs)| { let cn = consensus.get(chr).unwrap(); txs.par_iter().for_each(|tx| { if let Some((id, pocket)) = pseudomap(cn, tx) { match pocket { Pocket::Pass => { bucket.entry(pocket).or_default().push(id); } Pocket::Partial => { bucket.entry(pocket).or_default().push(id); } Pocket::InRet => { bucket.entry(pocket).or_default().push(id); } Pocket::Unmapped => { bucket.entry(pocket).or_default().push(id); } } } });any enhancement or make it look more elegant?
- Assistant
The code you've provided appears to map reads to a consensus and then categorize them into different pockets. The
matchstatement is used for pattern matching on thePocketenum and then inserting theidinto abuckethash map collection. Because you perform the same action for each variant, the statement is a bit redundant here. You can simplify it by getting rid of the and directly inserting the into the bucket based on the . - User
same with this:
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 = Arc::from(fields[3]); 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