Skip to content

Commit

Permalink
feat: Add --annotation-field option to vcf-report (#211)
Browse files Browse the repository at this point in the history
* feat: Add --annotation-field option to vcf-report

* Add short parameter
  • Loading branch information
fxwiegand committed Dec 9, 2021
1 parent b7e4bcb commit 29719d5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/bcf/report/oncoprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn oncoprint(
max_cells: u32,
tsv_data_path: Option<&str>,
plot_info: Option<Vec<String>>,
annotation_field: &str,
) -> Result<()> {
let mut data = HashMap::new();
let mut gene_data = HashMap::new();
Expand Down Expand Up @@ -64,7 +65,7 @@ pub fn oncoprint(
for (sample, path) in sample_calls.iter().sorted() {
let bcf_reader = bcf::Reader::from_path(path)?;
let header_records = bcf_reader.header().header_records();
let ann_fields: Vec<_> = get_ann_description(header_records)?;
let ann_fields: Vec<_> = get_ann_description(header_records, annotation_field)?;
clin_sig_present.insert(
sample.to_owned(),
ann_fields.contains(&"CLIN_SIG".to_owned()),
Expand Down Expand Up @@ -95,7 +96,7 @@ pub fn oncoprint(
sample_names.push(String::from_utf8(s.to_owned())?);
}
let header_records = header.header_records();
let ann_fields: Vec<_> = get_ann_description(header_records)?;
let ann_fields: Vec<_> = get_ann_description(header_records, annotation_field)?;

for (i, field) in ann_fields.iter().enumerate() {
ann_indices.insert(field, i);
Expand Down Expand Up @@ -129,7 +130,7 @@ pub fn oncoprint(
.map(|s| s.to_vec())
.collect_vec();

let ann = record.info(b"ANN").string()?;
let ann = record.info(annotation_field.as_bytes()).string()?;
if let Some(ann) = ann {
for alt_allele in alt_alleles {
let variant = if alt_allele == b"<BND>" {
Expand Down
12 changes: 8 additions & 4 deletions src/bcf/report/table_report/create_report_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ pub(crate) fn make_table_report(
output_path: &str,
max_read_depth: u32,
js_files: Vec<String>,
annotation_field: &str,
) -> Result<()> {
// HashMap<gene: String, Vec<Report>>, Vec<ann_field_identifiers: String>
// let mut reports = HashMap::new();
let mut ann_indices = HashMap::new();
let mut vcf = rust_htslib::bcf::Reader::from_path(&vcf_path).unwrap();
let header = vcf.header().clone();
let header_records = header.header_records();
let ann_field_description: Vec<_> = get_ann_description(header_records)?;
let ann_field_description: Vec<_> = get_ann_description(header_records, annotation_field)?;
let samples: Vec<_> = header
.samples()
.iter()
Expand Down Expand Up @@ -186,7 +187,7 @@ pub(crate) fn make_table_report(
let mut alterations = Vec::new();
let mut hgvsgs = Vec::new();

if let Some(ann) = variant.info(b"ANN").string()? {
if let Some(ann) = variant.info(annotation_field.as_bytes()).string()? {
for entry in ann.iter() {
let fields = entry.split(|c| *c == b'|').collect_vec();

Expand Down Expand Up @@ -442,10 +443,13 @@ fn escape_hgvsg(hgvsg: &str) -> String {
hgvsg.replace(".", "_").replace(">", "_").replace(":", "_")
}

pub(crate) fn get_ann_description(header_records: Vec<HeaderRecord>) -> Result<Vec<String>> {
pub(crate) fn get_ann_description(
header_records: Vec<HeaderRecord>,
annotation_field: &str,
) -> Result<Vec<String>> {
for rec in header_records {
if let rust_htslib::bcf::HeaderRecord::Info { key: _, values } = rec {
if values.get("ID").unwrap() == "ANN" {
if values.get("ID").unwrap() == annotation_field {
let description = values.get("Description").unwrap();
let fields: Vec<_> = description.split('|').collect();
let mut owned_fields = Vec::new();
Expand Down
2 changes: 2 additions & 0 deletions src/bcf/report/table_report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn table_report(
format_strings: Option<Vec<String>>,
max_read_depth: u32,
js_files: Vec<String>,
annotation_field: &str,
) -> Result<()> {
let detail_path = output_path.to_owned() + "/details/" + sample;
fs::create_dir(Path::new(&detail_path)).context(WriteErr::CantCreateDir {
Expand All @@ -41,5 +42,6 @@ pub fn table_report(
output_path,
max_read_depth,
js_files,
annotation_field,
)
}
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub(crate) enum Command {

/// Creates report from a given VCF file including a visual plot
/// for every variant with the given BAM and FASTA file.
/// The VCF file has to be annotated with VEP, using the options --hgvs, --hgvsg and --vcf_info_field ANN.
/// The VCF file has to be annotated with VEP, using the options --hgvs and --hgvsg.
///
/// Examples:
/// With current directory as default ouput path:
Expand Down Expand Up @@ -306,6 +306,10 @@ pub(crate) enum Command {
#[structopt(long, default_value = "0")]
threads: usize,

/// Set the name of the annotation field generated by VEP.
#[structopt(long, short = "a", default_value = "ANN")]
annotation_field: String,

/// Relative output path for the report files. Default value is the current directory.
#[structopt(default_value = ".")]
output_path: String,
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn main() -> Result<()> {
custom_js_files,
tsv,
threads,
annotation_field,
output_path,
} => {
let mut sample_calls = HashMap::new();
Expand Down Expand Up @@ -202,6 +203,7 @@ fn main() -> Result<()> {
formats.clone(),
max_read_depth,
js_file_names.clone(),
&annotation_field,
)
.unwrap_or_else(|e| {
panic!("Failed building table report for sample {}. {}", sample, e)
Expand All @@ -214,6 +216,7 @@ fn main() -> Result<()> {
cells,
tsv.as_deref(),
plot_info,
&annotation_field,
)?
}
VcfSplit { input, output } => bcf::split::split(input, output.as_ref())?,
Expand Down

0 comments on commit 29719d5

Please sign in to comment.