Skip to content

Commit

Permalink
bcf/writer/vcf_record: Use contig string map to find index of contig
Browse files Browse the repository at this point in the history
This previously used the position of the contig in the header, but BCF
allows records to override their positions using the `IDX` field,
requiring a contig string map.
  • Loading branch information
zaeleus committed Jan 11, 2022
1 parent f1c5d59 commit 9da12e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions noodles-bcf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
* bcf/writer: `Writer::write_vcf_record` now takes `StringMaps` instead of
`StringMap`.

* bcf/writer/vcf_record: Use contig string map to find index of contig.

This previously used the position of the contig in the header, but BCF
allows records to override their positions using the `IDX` field, requiring
a contig string map.

[#64]: https://github.com/zaeleus/noodles/issues/64

### Removed
Expand Down
2 changes: 1 addition & 1 deletion noodles-bcf/src/writer/vcf_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod tests {
.add_contig(vcf::header::Contig::new("sq0"))
.build();

let string_maps = StringMaps::default();
let string_maps = StringMaps::from(&header);

let record = vcf::Record::builder()
.set_chromosome("sq0".parse()?)
Expand Down
11 changes: 7 additions & 4 deletions noodles-bcf/src/writer/vcf_record/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use byteorder::{LittleEndian, WriteBytesExt};
use noodles_vcf as vcf;

use crate::{
header::{string_maps::StringStringMap, StringMaps},
header::{
string_maps::{ContigStringMap, StringStringMap},
StringMaps,
},
record::value::{Float, Value},
writer::value::write_value,
};
Expand All @@ -24,7 +27,7 @@ pub fn write_site<W>(
where
W: Write,
{
write_chrom(writer, header.contigs(), record.chromosome())?;
write_chrom(writer, string_maps.contigs(), record.chromosome())?;
write_pos(writer, record.position())?;

let end = record
Expand Down Expand Up @@ -56,7 +59,7 @@ where

fn write_chrom<W>(
writer: &mut W,
contigs: &vcf::header::Contigs,
contig_string_map: &ContigStringMap,
chromosome: &vcf::record::Chromosome,
) -> io::Result<()>
where
Expand All @@ -65,7 +68,7 @@ where
use vcf::record::Chromosome;

let chrom = match chromosome {
Chromosome::Name(name) => contigs
Chromosome::Name(name) => contig_string_map
.get_index_of(name)
.ok_or_else(|| {
io::Error::new(
Expand Down

0 comments on commit 9da12e7

Please sign in to comment.