Skip to content

BCF header records #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lazy_static = "0.2"
bitflags = "0.9"
serde = { version = "^1", optional = true }
regex = "0.2"
linear-map = "1.2.0"

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion src/bam/record_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ impl<'de> Deserialize<'de> for Record {

#[cfg(test)]
mod tests {
use bam::record::Record;
use bam::Read;
use bam::Reader;
use bam::record::Record;

use std::path::Path;

Expand Down
92 changes: 92 additions & 0 deletions src/bcf/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::str;

use htslib;

use linear_map::LinearMap;

pub type SampleSubset = Vec<i32>;

custom_derive! {
Expand Down Expand Up @@ -191,6 +193,38 @@ impl Drop for Header {
}
}

/// A header record.
#[derive(Debug)]
pub enum HeaderRecord {
/// A `FILTER` header record.
Filter {
key: String,
values: LinearMap<String, String>,
},
/// An `INFO` header record.
Info {
key: String,
values: LinearMap<String, String>,
},
/// A `FORMAT` header record.
Format {
key: String,
values: LinearMap<String, String>,
},
/// A `contig` header record.
Contig {
key: String,
values: LinearMap<String, String>,
},
/// A structured header record.
Structured {
key: String,
values: LinearMap<String, String>,
},
/// A generic, unstructured header record.
Generic { key: String, value: String },
}

#[derive(Debug)]
pub struct HeaderView {
pub inner: *mut htslib::bcf_hdr_t,
Expand Down Expand Up @@ -340,6 +374,64 @@ impl HeaderView {
};
key.to_bytes().to_vec()
}

/// Return structured `HeaderRecord`s.
pub fn header_records(&self) -> Vec<HeaderRecord> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would return a HashMap here, which assigns keys to HeaderRecord.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that will be enough. Rust's HashMap type is not insertion order (similar to the dict in Python <3.6). IMO we need to guarantee that (1) insertion order == order in BCF file and (2) vice versa. This is in particularly important for roundtrip processing of VCF files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. And moreover there might be duplicate keys I think. Returning a Vector is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, so should I just rebase or perform any changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, resolving the merge conflicts now FWIW, would have to be done anyway.

fn parse_kv(rec: &htslib::bcf_hrec_t) -> LinearMap<String, String> {
let mut result: LinearMap<String, String> = LinearMap::new();
for i in 0_i32..(rec.nkeys) {
let key = unsafe {
ffi::CStr::from_ptr(*rec.keys.offset(i as isize))
.to_str()
.unwrap()
.to_string()
};
let value = unsafe {
ffi::CStr::from_ptr(*rec.vals.offset(i as isize))
.to_str()
.unwrap()
.to_string()
};
result.insert(key, value);
}
result
}

let mut result: Vec<HeaderRecord> = Vec::new();
for i in 1_i32..unsafe { (*self.inner).nhrec } {
let rec = unsafe { &(**(*self.inner).hrec.offset(i as isize)) };
let key = unsafe { ffi::CStr::from_ptr(rec.key).to_str().unwrap().to_string() };
let record = match rec.type_ {
0 => HeaderRecord::Filter {
key,
values: parse_kv(rec),
},
1 => HeaderRecord::Info {
key,
values: parse_kv(rec),
},
2 => HeaderRecord::Format {
key,
values: parse_kv(rec),
},
3 => HeaderRecord::Contig {
key,
values: parse_kv(rec),
},
4 => HeaderRecord::Structured {
key,
values: parse_kv(rec),
},
5 => HeaderRecord::Generic {
key,
value: unsafe { ffi::CStr::from_ptr(rec.value).to_str().unwrap().to_string() },
},
_ => panic!("Unknown type: {}", rec.type_),
};
result.push(record);
}
result
}
}

impl Clone for HeaderView {
Expand Down
25 changes: 23 additions & 2 deletions src/bcf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ pub mod record;
use bcf::header::{HeaderView, SampleSubset};
use htslib;

pub use bcf::buffer::RecordBuffer;
pub use bcf::header::Header;
pub use bcf::header::{Header, HeaderRecord};
pub use bcf::record::Record;

/// Redefinition of corresponding `#define` in `vcf.h.`.
Expand Down Expand Up @@ -839,6 +838,28 @@ mod tests {
assert!(header.sample_to_id(b"three").is_err());
}

#[test]
fn test_header_records() {
let vcf = Reader::from_path(&"test/test_string.vcf")
.ok()
.expect("Error opening file.");
let records = vcf.header().header_records();
assert_eq!(records.len(), 9);

match &records[0] {
&HeaderRecord::Filter {
ref key,
ref values,
} => {
assert_eq!(key, "FILTER");
assert_eq!(values["ID"], "PASS");
}
_ => {
assert!(false);
}
}
}

// Helper function reading full file into string.
fn read_all<P: AsRef<Path>>(path: P) -> String {
let mut file = File::open(path.as_ref())
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ extern crate quick_error;
extern crate regex;
extern crate url;

extern crate linear_map;

#[cfg(feature = "serde")]
extern crate serde;

Expand Down
6 changes: 3 additions & 3 deletions src/sam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::path::Path;

use htslib;

use bam::HeaderView;
use bam::header;
use bam::record;
use bam::HeaderView;

/// SAM writer.
#[derive(Debug)]
Expand Down Expand Up @@ -111,10 +111,10 @@ quick_error! {

#[cfg(test)]
mod tests {
use bam::header;
use bam::record;
use bam::Read;
use bam::Reader;
use bam::header;
use bam::record;
use sam::Writer;

#[test]
Expand Down