this code reads a .bed file: use std::fs::File; use std::io::{self, Read}; fn…
- User
this code reads a .bed file:
use std::fs::File; use std::io::{self, Read};
fn filename_to_string(s: &str) -> io::Result { let mut file = File::open(s)?; let mut s = String::new(); file.read_to_string(&mut s)?; Ok(s) }
fn words_by_line<'a>(s: &'a str) -> Vec<Vec<&'a str>> { s.lines() .map(|line| line.split_whitespace().collect()) .collect() }
fn main() { let whole_file = filename_to_string( "/home/alejandro/Documents/unam/TOGA_old_versions/x/bed_gencode_tmp/5k.bed", ) .unwrap(); let wbyl = words_by_line(&whole_file); println!("{:?}", wbyl) }
producing this output:
["chr1", "35434120", "355 57599", "ENST00000697012.1", "0", "-", "35507125", "35554491", "0", "21", "9 61,92,91,123,143,160,139,152,149,133,124,229,133,93,88,98,102,524,118,170,50 ,", "0,6926,8125,8785,10040,14052,15746,16237,17507,19436,20241,21892,26184, 28500,32487,36742,40684,72491,104345,120229,123429,"], ["chr1", "35434120", "35557615", "ENST00000697013.1", "0", "-", "35479120", "35554491", "0", "20" , "961,92,91,123,143,160,139,152,149,133,124,229,133,93,88,98,102,247,170,66 ,", "0,6926,8125,8785,10040,14052,15746,16237,17507,19436,20241,21892,26184, 28500,32487,36742,40684,44845,120229,123429,"]]
is it possible to implement this to be run in parallel?
- Assistant
- User
can we avoid using BufReader?
- Assistant
Certainly, you can perform parallel processing without using
BufReader, thoughBufReaderis typically recommended for efficiency when dealing with files because it reduces the number of read system calls. However, if your use case does not require it or you prefer not to use it for some other reason, I will show you an alternative usingrayonand the file reading pattern we already had. - User
what would be the most fastest way to split a string? As I am aware, the rust split() function is not efficient
- Assistant
In Rust, the standard
str::splitmethod is actually quite efficient for most use cases. It lazily iterates over substrings of a string that are separated by a given pattern, allocating only the iterator itself and not the substrings.