please check this code: fn main() { let start = std::time::Instant::now(); let file =…
- User
please check this code:
fn main() { let start = std::time::Instant::now();
codelet file = "/home/alejandro/Downloads/gencode.v44.annotation.gtf"; let contents = reader(file).unwrap(); let records = parallel_parse(&contents).unwrap(); let elapsed = start.elapsed(); println!("{:?}", records); println!("Elapsed: {:?}", elapsed);}
pub type ProtRecord = HashMap<String, InnerProtRecord>;
#[derive(Debug, PartialEq)] pub struct InnerProtRecord { pub chr: String, pub strand: char, pub start: u32, pub end: u32, }
impl Default for InnerProtRecord { fn default() -> Self { InnerProtRecord { chr: String::new(), strand: ' ', start: 0, end: 0, } } }
pub fn reader<P: AsRef + Debug>(file: P) -> io::Result { let mut file = File::open(file)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; Ok(contents) }
pub fn parallel_parse<'a>(s: &'a str) -> Result<ProtRecord, &'static str> { let x = s .par_lines() .filter(|line| !line.starts_with("#")) .filter_map(|line| Record::parse(line).ok()) .filter(|record| record.feat == "CDS") .fold( || HashMap::new(), |mut acc: ProtRecord, record| { // acc.entry(record.chr.clone()).or_default().push(record); let k = acc.entry(record.attr.protein_id).or_default(); if !k.chr.is_empty() { k.start = k.start.min(record.start); k.end = k.end.max(record.end); } else { InnerProtRecord { chr: record.chr, strand: record.strand, start: record.start, end: record.end, }; };
codeacc }, ) .reduce( || HashMap::new(), |mut acc, map| { for (k, v) in map { let x = acc.entry(k).or_default(); if !x.chr.is_empty() { x.start = x.start.min(v.start); x.end = x.end.max(v.end); } else { *x = v; } } acc }, ); Ok(x)}
why am I getting this output?:
{"ENSP00000503594.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000294168.3": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000502566.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000421719.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000359732.3": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000450699.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000490285.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000486606.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000492123.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000392760.2": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000475284.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000420359.2": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000426304.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000489204.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000495022.2": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000383896.1": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0 }, "ENSP00000380734.2": InnerProtRecord { chr: "", strand: ' ', start: 0, end: 0
- Assistant
It appears there is a mistake in the part of your code where you are trying to assign the
InnerProtRecordto theHashMap. Inside thefoldclosure, you are creating an instance, but not assigning it to the key. Instead, you're just creating a new instance with no effect, because you are not assigning it to the hashmap . - User
I have this type of structure:
"ENSP00000471568.1": InnerProtRecord { chr: "chr19", strand: '+', start: 40800572, end: 40800875 }, "ENSP00000394757.2": InnerProtRecord { chr: "chr18", strand: '-', start: 76362497, end: 76443054 }, "ENSP00000400383.1": InnerProtRecord { chr: "chr2", strand: '-', start: 217848883, end: 217905445 }, "ENSP00000472322.1": InnerProtRecord { chr: "chr19", strand: '-', start: 38815856, end: 38831761 }, "ENSP00000439042.1": InnerProtRecord { chr: "chr10", strand: '-', start: 124397941, end: 124408750 }}
and I need to write each one to a file in the form:
key\tchr\tstrand\tstart\end
what do you recommend? this needs to be the fastest way possible
- Assistant
Given the set of
InnerProtRecordinstances associated with keys (presumably protein IDs), you want to write this data to a file in a tab-delimited format. Here's a Rust function which accomplishes this at a high efficiency:rust