check this code: fn main() { initwithlevel(Level::Info).unwrap(); let args: Args =…
- User
check this code:
fn main() { init_with_level(Level::Info).unwrap();
let args: Args = Args::parse(); args.check().unwrap_or_else(|e| { error!("{}", e); std::process::exit(1); }); info!("{:?}", args); rayon::ThreadPoolBuilder::new() .num_threads(args.threads) .build_global() .unwrap(); let start = std::time::Instant::now(); let bed = reader(&args.bed).unwrap(); let isoseq = reader(&args.reads).unwrap(); info!("Parsing records..."); let (tracks, pseudomap) = parse_tracks(&bed).unwrap(); let (reads, _) = parse_tracks(&isoseq).unwrap(); info!( "Parsed {} annotation tracks and {} reads and started pseudomapping...", tracks.len(), reads.len() ); let index = Arc::new(Track::new(tracks, pseudomap, reads)); let bucket = Arc::new(DashMap::<&String, Bucket>::new()); let pocket = Arc::new(DashMap::<Pocket, Vec<String>>::new()); index.reads.par_iter().for_each(|(read, record)| { index .pseudomap .get(&record.chrom) .unwrap() .par_iter() .for_each(|(start, end, id)| { if record.tx_start >= *start - BOUNDARY && record.tx_end <= *end + BOUNDARY { // a bucket needs to be of the form: // read: // [(start, end)] -> read_exons // [(start, end)] -> gene_exons // since a read could be pseudomapped to multiple transcripts, // the gene_exons vector will be extended with the exons of each // transcript. let mut b_acc = bucket.entry(read).or_insert(Bucket::default()); b_acc.id.push(id.clone()); b_acc .gene_exons .extend(index.tracks.get(id).unwrap().coords.clone()); if b_acc.read_exons.is_empty() { b_acc.read_exons = record.coords.clone(); } } // } else { // bucket.entry(read).or_insert(Bucket::default()); // } }); }); if bucket.len() == 0 { error!("No reads were pseudomapped to the provided annotation."); std::process::exit(1); } else if bucket.len() < index.reads.len() / 2 { info!( "Pseudomapped {} ({:.2}%) to the provided annotation.", bucket.len(), (bucket.len() as f64 / index.reads.len() as f64) * 100.0 ); warn!("Less than 50% of reads were pseudomapped to the provided annotation."); } else { info!( "Pseudomapped {} ({:.2}%) reads to the provided annotation.", bucket.len(), (bucket.len() as f64 / index.reads.len() as f64) * 100.0 ); } info!("Classifying reads into pockets..."); bucket.par_iter_mut().for_each(|mut entry| { entry.value_mut().gene_exons.par_sort_unstable(); entry.value_mut().gene_exons.dedup(); let read = entry.key(); let exons = entry.value(); if exons.read_exons.is_empty() { pocket .entry(Pocket::Unmapped) .or_insert(Vec::new()) .push(index.reads[*read].line.clone()); return; } let state = find_exon_matches(&exons); // fill pockets based on read state let id = if state { Pocket::Pass } else { Pocket::Fail }; let mut p_acc = pocket.entry(id).or_insert(Vec::new()); p_acc.push(index.reads[*read].line.clone()); }); // do we need a sorting step? // pocket.par_iter_mut().for_each(|mut entry| { // entry.value_mut().par_sort_unstable(); // }); pocket.iter().for_each(|entry| { info!( "Pocket: {:?} contains {} reads.", entry.key(), entry.value().len() ); }); write_objs(&pocket); let elapsed = start.elapsed(); info!("Elapsed: {:?}", elapsed); - Assistant
- User
- please implement the use of a HashSet instead of Vec
- show how would you parallelize i/o using async
- Assistant
- User
these are the functions I am using to parse files:
pub fn parse_tracks<'a>( contents: &'a str, ) -> Result< ( HashMap<String, Record>, Arc<DashMap<String, Vec<(u32, u32, String)>>>, ), &'static str,
{ let pseudomap: Arc<DashMap<String, Vec<(u32, u32, String)>>> = Arc::new(DashMap::new());
let tracks = contents .par_lines() .filter(|x| !x.starts_with("#")) .filter_map(|x| Record::new(x).ok()) .fold( || HashMap::new(), |mut acc, record| { let mut ps_acc = pseudomap.entry(record.chrom.clone()).or_insert(Vec::new()); ps_acc.push((record.tx_start, record.tx_end, record.id.clone())); acc.entry(record.id.clone()).or_insert(record); acc }, ) .reduce( || HashMap::new(), |mut acc, mut map| { acc.extend(map.drain()); acc }, ); Ok((tracks, pseudomap))}
pub fn reader<P: AsRef + Debug>(file: P) -> Result<String, Box> { let mut file = File::open(file)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; Ok(contents) }
implement your suggestion about async i/o here
- Assistant
- User
would you think using par_iter() and rayon is better than using tokio? which one would be faster?
- Assistant
- User
- Assistant
- User
do i still need this?:
fn write_read<T: Write, I, R>(writer: T, reads: I) where I: IntoIterator<Item = R>, R: Debug + Display, { let mut writer = BufWriter::new(writer); for read in reads { writeln!(writer, "{}", read).unwrap_or_else(|e| { panic!("Error writing to file: {}", e); }); } writer.flush().expect("Error flushing the buffer.") }
- Assistant
In the updated asynchronous version of
write_objsI provided in the previous answer, thewrite_readfunction is no longer needed. The reason is that within theasyncblock, we're performing the file-write operation directly using theAsyncWriteExttrait provided by Tokio, which includes methods like and that are designed for asynchronous operations. - User
this is the output of my program:
2024-02-17T17:49:08.925Z INFO [deintronize] Args { bed: "../test_data/hg38.query_annotation.bed", reads: "../test_data/5mill.bed", threads: 16 } 2024-02-17T17:49:09.571Z INFO [deintronize] Parsing records... 2024-02-17T17:49:17.435Z INFO [deintronize] Parsed 55658 annotation tracks and 5567700 reads and started pseudomapping... 2024-02-17T17:49:27.329Z INFO [deintronize] Pseudomapped 5567700 (100.00%) reads to the provided annotation. 2024-02-17T17:49:27.329Z INFO [deintronize] Classifying reads into pockets... 2024-02-17T17:49:36.675Z INFO [deintronize] Pocket: Pass contains 4991500 reads. 2024-02-17T17:49:36.675Z INFO [deintronize] Pocket: Fail contains 576200 reads. 2024-02-17T17:49:38.608Z INFO [deintronize] Elapsed: 29.681448435s
why after printing "Elapsed: ..." the program takes some additional seconds to finish?
- Assistant