Skip to content

Commit

Permalink
vcf/header/record/value/map/format: Change conversion from key value …
Browse files Browse the repository at this point in the history
…to a reference
  • Loading branch information
zaeleus committed Jan 11, 2023
1 parent 6b0f9c8 commit d45d9b9
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 35 deletions.
8 changes: 4 additions & 4 deletions noodles-bcf/src/header/string_maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl StringMaps {
/// let header = vcf::Header::builder()
/// .add_info(info::Key::TotalDepth, Map::<Info>::from(&info::Key::TotalDepth))
/// .add_filter("q10", Map::<Filter>::new("Quality below 10"))
/// .add_format(format::Key::ReadDepth, Map::<Format>::from(format::Key::ReadDepth))
/// .add_format(format::Key::ReadDepth, Map::<Format>::from(&format::Key::ReadDepth))
/// .add_contig("sq0".parse()?, Map::<Contig>::new())
/// .build();
///
Expand Down Expand Up @@ -78,7 +78,7 @@ impl StringMaps {
/// let header = vcf::Header::builder()
/// .add_info(info::Key::TotalDepth, Map::<Info>::from(&info::Key::TotalDepth))
/// .add_filter("q10", Map::<Filter>::new("Quality below 10"))
/// .add_format(format::Key::ReadDepth, Map::<Format>::from(format::Key::ReadDepth))
/// .add_format(format::Key::ReadDepth, Map::<Format>::from(&format::Key::ReadDepth))
/// .add_contig("sq0".parse()?, Map::<Contig>::new())
/// .build();
///
Expand Down Expand Up @@ -417,11 +417,11 @@ mod tests {
.add_filter("q10", Map::<Filter>::new("Quality below 10"))
.add_format(
format::Key::Genotype,
Map::<Format>::from(format::Key::Genotype),
Map::<Format>::from(&format::Key::Genotype),
)
.add_format(
format::Key::ReadDepth,
Map::<Format>::from(format::Key::ReadDepth),
Map::<Format>::from(&format::Key::ReadDepth),
)
.add_alternative_allele(del, Map::<AlternativeAllele>::new("Deletion"))
.build();
Expand Down
3 changes: 3 additions & 0 deletions noodles-vcf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

`Map<I>` no longer holds the record ID. Use the collection key instead.

* vcf/header/record/value/map/format: Change conversion from key value to a
reference (`&format::Key`).

* vcf/header/record/value/map/info: Change conversion from key value to a
reference (`&info::Key`).

Expand Down
4 changes: 2 additions & 2 deletions noodles-vcf/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Header {
/// };
///
/// let id = Key::Genotype;
/// let format = Map::<Format>::from(id.clone());
/// let format = Map::<Format>::from(&id);
///
/// let header = vcf::Header::builder()
/// .add_format(id, format.clone())
Expand All @@ -244,7 +244,7 @@ impl Header {
/// let mut header = vcf::Header::default();
///
/// let id = Key::Genotype;
/// let format = Map::<Format>::from(id.clone());
/// let format = Map::<Format>::from(&id);
/// header.formats_mut().insert(id, format.clone());
///
/// let formats = header.formats();
Expand Down
4 changes: 2 additions & 2 deletions noodles-vcf/src/header/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Builder {
/// };
///
/// let id = Key::Genotype;
/// let format = Map::<Format>::from(id.clone());
/// let format = Map::<Format>::from(&id);
///
/// let header = vcf::Header::builder()
/// .add_format(id, format.clone())
Expand Down Expand Up @@ -399,7 +399,7 @@ mod tests {
.add_filter("q10", Map::<Filter>::new("Quality below 10"))
.add_format(
FormatKey::Genotype,
Map::<Format>::from(FormatKey::Genotype),
Map::<Format>::from(&FormatKey::Genotype),
)
.add_alternative_allele(del, Map::<AlternativeAllele>::new("Deletion"))
.set_assembly("file:///assemblies.fasta")
Expand Down
14 changes: 7 additions & 7 deletions noodles-vcf/src/header/record/value/map/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ impl fmt::Display for Map<Format> {
}
}

impl From<Key> for Map<Format> {
fn from(key: Key) -> Self {
impl From<&Key> for Map<Format> {
fn from(key: &Key) -> Self {
use crate::header::format::key;

let number = key::number(&key).unwrap_or(Number::Count(1));
let ty = key::ty(&key).unwrap_or(Type::String);
let description = key::description(&key).map(|s| s.into()).unwrap_or_default();
let number = key::number(key).unwrap_or(Number::Count(1));
let ty = key::ty(key).unwrap_or(Type::String);
let description = key::description(key).map(|s| s.into()).unwrap_or_default();

Self {
inner: Format {
Expand Down Expand Up @@ -205,7 +205,7 @@ mod tests {

#[test]
fn test_fmt() {
let map = Map::<Format>::from(Key::Genotype);
let map = Map::<Format>::from(&Key::Genotype);
let expected = r#",Number=1,Type=String,Description="Genotype""#;
assert_eq!(map.to_string(), expected);
}
Expand All @@ -218,7 +218,7 @@ mod tests {
(String::from("Description"), String::from("Genotype")),
])?;

let expected = Map::<Format>::from(Key::Genotype);
let expected = Map::<Format>::from(&Key::Genotype);

assert_eq!(actual, expected);

Expand Down
8 changes: 4 additions & 4 deletions noodles-vcf/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ impl Record {
/// };
///
/// let header = vcf::Header::builder()
/// .add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
/// .add_format(Key::ConditionalGenotypeQuality, Map::<Format>::from(Key::ConditionalGenotypeQuality))
/// .add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
/// .add_format(Key::ConditionalGenotypeQuality, Map::<Format>::from(&Key::ConditionalGenotypeQuality))
/// .build();
///
/// let keys = "GT:GQ".parse()?;
Expand Down Expand Up @@ -832,10 +832,10 @@ mod tests {
};

let header = Header::builder()
.add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
.add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
.add_format(
Key::ConditionalGenotypeQuality,
Map::<Format>::from(Key::ConditionalGenotypeQuality),
Map::<Format>::from(&Key::ConditionalGenotypeQuality),
)
.build();

Expand Down
6 changes: 3 additions & 3 deletions noodles-vcf/src/record/genotypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Genotypes {
/// };
///
/// let header = vcf::Header::builder()
/// .add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
/// .add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
/// .build();
///
/// let actual = Genotypes::parse("GT:GQ\t0|0:13", &header)?;
Expand Down Expand Up @@ -232,10 +232,10 @@ mod tests {
};

let header = crate::Header::builder()
.add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
.add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
.add_format(
Key::ConditionalGenotypeQuality,
Map::<Format>::from(Key::ConditionalGenotypeQuality),
Map::<Format>::from(&Key::ConditionalGenotypeQuality),
)
.build();

Expand Down
14 changes: 7 additions & 7 deletions noodles-vcf/src/record/genotypes/genotype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ impl Genotype {
/// };
///
/// let header = vcf::Header::builder()
/// .add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
/// .add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
/// .add_format(
/// Key::ConditionalGenotypeQuality,
/// Map::<Format>::from(Key::ConditionalGenotypeQuality),
/// Map::<Format>::from(&Key::ConditionalGenotypeQuality),
/// )
/// .build();
///
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Genotype {
let field = if let Some(format) = formats.get(key) {
Field::from_str_format(raw_field, key, format).map_err(ParseError::InvalidField)?
} else {
let format = Map::<Format>::from(key.clone());
let format = Map::<Format>::from(key);
Field::from_str_format(raw_field, key, &format).map_err(ParseError::InvalidField)?
};

Expand All @@ -156,10 +156,10 @@ impl Genotype {
/// };
///
/// let header = vcf::Header::builder()
/// .add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
/// .add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
/// .add_format(
/// Key::ConditionalGenotypeQuality,
/// Map::<Format>::from(Key::ConditionalGenotypeQuality),
/// Map::<Format>::from(&Key::ConditionalGenotypeQuality),
/// )
/// .build();
///
Expand Down Expand Up @@ -268,10 +268,10 @@ mod tests {
#[test]
fn test_parse() -> Result<(), Box<dyn std::error::Error>> {
let header = crate::Header::builder()
.add_format(Key::Genotype, Map::<Format>::from(Key::Genotype))
.add_format(Key::Genotype, Map::<Format>::from(&Key::Genotype))
.add_format(
Key::ConditionalGenotypeQuality,
Map::<Format>::from(Key::ConditionalGenotypeQuality),
Map::<Format>::from(&Key::ConditionalGenotypeQuality),
)
.build();

Expand Down
10 changes: 5 additions & 5 deletions noodles-vcf/src/record/genotypes/genotype/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Field {
/// };
///
/// let key = Key::ConditionalGenotypeQuality;
/// let format = Map::<Format>::from(key.clone());
/// let format = Map::<Format>::from(&key);
///
/// assert_eq!(
/// Field::from_str_format("13", &key, &format),
Expand Down Expand Up @@ -176,25 +176,25 @@ mod tests {
#[test]
fn test_from_str_format() -> Result<(), ParseError> {
let key = Key::MappingQuality;
let format = Map::<Format>::from(key.clone());
let format = Map::<Format>::from(&key);
let actual = Field::from_str_format(".", &key, &format)?;
assert_eq!(actual.key(), &key);
assert_eq!(actual.value(), None);

let key = Key::ConditionalGenotypeQuality;
let format = Map::<Format>::from(key.clone());
let format = Map::<Format>::from(&key);
let actual = Field::from_str_format("13", &key, &format)?;
assert_eq!(actual.key(), &key);
assert_eq!(actual.value(), Some(&Value::Integer(13)));

let key = Key::GenotypeCopyNumberQuality;
let format = Map::<Format>::from(key.clone());
let format = Map::<Format>::from(&key);
let actual = Field::from_str_format("8.333", &key, &format)?;
assert_eq!(actual.key(), &key);
assert_eq!(actual.value(), Some(&Value::Float(8.333)));

let key = Key::Genotype;
let format = Map::<Format>::from(key.clone());
let format = Map::<Format>::from(&key);
let actual = Field::from_str_format("0|0", &key, &format)?;
assert_eq!(actual.key(), &key);
assert_eq!(actual.value(), Some(&Value::String(String::from("0|0"))));
Expand Down
2 changes: 1 addition & 1 deletion noodles-vcf/src/record/genotypes/genotype/field/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Value {
/// record::genotypes::genotype::field::Value,
/// };
///
/// let format = Map::<Format>::from(Key::ConditionalGenotypeQuality);
/// let format = Map::<Format>::from(&Key::ConditionalGenotypeQuality);
/// assert_eq!(Value::from_str_format("13", &format), Ok(Value::Integer(13)));
/// ```
pub fn from_str_format(s: &str, format: &Map<Format>) -> Result<Self, ParseError> {
Expand Down

0 comments on commit d45d9b9

Please sign in to comment.