Skip to content

Commit

Permalink
fix: Header::to_hashmap skips @CO tags, add comments() method (#363)
Browse files Browse the repository at this point in the history
* `Header::to_hashmap` now skips processing of `@CO` tags, instead of crashing

* added `Header::comments()` to iterate over comments
  • Loading branch information
shohei-kojima authored Aug 24, 2022
1 parent 331fde5 commit c24a7f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/bam/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::bam::HeaderView;
use lazy_static::lazy_static;
use linear_map::LinearMap;
use regex::Regex;
use std::borrow::Cow;
use std::collections::HashMap;

/// A BAM header.
Expand Down Expand Up @@ -63,6 +64,9 @@ impl Header {
self.records.join(&b'\n')
}

/// This returns a header as a HashMap.
/// Comment lines starting with "@CO" will NOT be included in the HashMap.
/// Comment lines can be obtained by the `comments` function.
pub fn to_hashmap(&self) -> HashMap<String, Vec<LinearMap<String, String>>> {
let mut header_map = HashMap::default();

Expand All @@ -83,6 +87,9 @@ impl Header {
.unwrap()
.as_str()
.to_owned();
if record_type.eq("CO") {
continue;
}
let mut field = LinearMap::default();
for part in parts.iter().skip(1) {
let cap = TAG_RE.captures(part).unwrap();
Expand All @@ -97,6 +104,15 @@ impl Header {
}
header_map
}

/// Returns an iterator of comment lines.
pub fn comments(&self) -> impl Iterator<Item = Cow<str>> {
self.records.iter().flat_map(|r| {
r.split(|x| x == &b'\n')
.filter(|x| x.starts_with(b"@CO\t"))
.map(|x| String::from_utf8_lossy(&x[4..]))
})
}
}

/// Header record.
Expand Down
1 change: 1 addition & 0 deletions src/tbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ pub struct Records<'a, R: Read> {
impl<'a, R: Read> Iterator for Records<'a, R> {
type Item = Result<Vec<u8>>;

#[allow(clippy::read_zero_byte_vec)]
fn next(&mut self) -> Option<Result<Vec<u8>>> {
let mut record = Vec::new();
match self.reader.read(&mut record) {
Expand Down

0 comments on commit c24a7f6

Please sign in to comment.