check this code, you are a rust expert: let mut layer: Vec<(String, i32, String,…
- User
check this code, you are a rust expert:
let mut layer: Vec<(String, i32, String, String)> = vec![]; let mut mapper: HashMap<String, Vec<String>> = HashMap::new(); let mut inner: HashMap<String, BTreeMap<Sort, String>> = HashMap::new(); let mut helper: HashMap<String, String> = HashMap::new(); log::info!("Sorting GTF file..."); for record in records { if record.chrom.is_empty() { writeln!(output, "{}", record.line).unwrap(); continue; } match record.feature() { "gene" => { layer.push(record.outer_layer()); } "transcript" => { let (gene, transcript, line) = record.gene_to_transcript(); mapper .entry(gene) .or_insert(Vec::new()) .push(transcript.clone()); helper.entry(transcript).or_insert(line); } "CDS" | "exon" | "start_codon" | "stop_codon" => { let (transcript, exon_number, line) = record.inner_layer(); inner .entry(transcript) .or_insert(BTreeMap::new()) .insert(Sort::new(exon_number.as_str()), line); } _ => { let (transcript, feature, line) = record.misc_layer(); inner .entry(transcript) .or_insert_with(|| BTreeMap::new()) .entry(Sort::new(feature.as_str())) .and_modify(|e| { e.push('\n'); e.push_str(&line); }) .or_insert(line); } }; }where:
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)] pub struct Record { pub chrom: String, feat: String, pos: i32, gene_id: String, transcript_id: String, exon_number: String, pub line: String, }
impl Record { pub fn parse(line: &str) -> Result<Self, &'static str> { if line.is_empty() { return Err("Empty line"); }
let fields: Vec<&str> = line.split("\t").collect(); let attributes = Attribute::parse(&fields[8].to_string()).unwrap(); Ok(Self { chrom: fields[0].to_string(), feat: fields[2].to_string(), pos: fields[3].parse().unwrap(), gene_id: attributes.gene_id().to_string(), transcript_id: attributes.transcript_id().to_string(), exon_number: attributes.exon_number().to_string(), line: fields.join("\t"), }) } pub fn outer_layer(&self) -> (String, i32, String, String) { ( self.chrom.clone(), self.pos, self.gene_id.clone(), self.line.clone(), ) } pub fn gene_to_transcript(&self) -> (String, String, String) { ( self.gene_id.clone(), self.transcript_id.clone(), self.line.clone(), ) } pub fn inner_layer(&self) -> (String, String, String) { let mut exon_number = self.exon_number.clone(); match self.feat.as_str() { "exon" => exon_number.push('a'), "CDS" => exon_number.push('b'), "start_codon" => exon_number.push('c'), "stop_codon" => exon_number.push('d'), _ => exon_number.push('e'), } (self.transcript_id.clone(), exon_number, self.line.clone()) } pub fn misc_layer(&self) -> (String, String, String) { ( self.transcript_id.clone(), self.feat.clone(), self.line.clone(), ) } pub fn feature(&self) -> &str { &self.feat } - Assistant
- User
what is your take on this?:
let records = parallel_parse(s)?;
let (layer, mapper, inner, helper) = records .into_par_iter() .fold( (Vec::new(), HashMap::new(), HashMap::new(), HashMap::new()), |(mut layer, mut mapper, mut inner, mut helper), record| { if record.chrom.is_empty() { writeln!(output, "{}", record.line).unwrap(); return (layer, mapper, inner, helper); }
match record.feature() { "gene" => { layer.push(record.outer_layer()); } "transcript" => { let (gene, transcript, line) = record.gene_to_transcript(); mapper .entry(gene) .or_insert(Vec::new()) .push(transcript.clone()); helper.entry(transcript).or_insert(line); } "CDS" | "exon" | "start_codon" | "stop_codon" => { let (transcript, exon_number, line) = record.inner_layer(); inner .entry(transcript) .or_insert(BTreeMap::new()) .insert(Sort::new(exon_number.as_str()), line); } _ => { let (transcript, feature, line) = record.misc_layer(); inner .entry(transcript) .or_insert_with(|| BTreeMap::new()) .entry(Sort::new(feature.as_str())) .and_modify(|e| { e.push('\n'); e.push_str(&line); }) .or_insert(line); } }; (layer, mapper, inner, helper) }, ); - Assistant