Skip to content

Commit

Permalink
bed/feature/record_buf/convert: Add conversion from BED6+ feature record
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Aug 24, 2024
1 parent 2215def commit 54e6d70
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion noodles-bed/src/feature/record_buf/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl RecordBuf<4> {
}

impl RecordBuf<5> {
/// Converts a BED4+ feature record to a BED4+ record buffer.
/// Converts a BED5+ feature record to a BED5+ record buffer.
pub fn try_from_feature_record<R>(record: &R) -> io::Result<Self>
where
R: Record<5>,
Expand Down Expand Up @@ -80,3 +80,37 @@ impl RecordBuf<5> {
Ok(builder.build())
}
}

impl RecordBuf<6> {
/// Converts a BED6+ feature record to a BED6+ record buffer.
pub fn try_from_feature_record<R>(record: &R) -> io::Result<Self>
where
R: Record<6>,
{
let mut builder = RecordBuf::<6>::builder()
.set_reference_sequence_name(record.reference_sequence_name())
.set_feature_start(record.feature_start()?);

if let Some(feature_end) = record.feature_end().transpose()? {
builder = builder.set_feature_end(feature_end);
}

if let Some(Some(name)) = record.name() {
builder = builder.set_name(name);
}

if let Some(score) = record.score().transpose()? {
builder = builder.set_score(score);
}

if let Some(Some(strand)) = record.strand().transpose()? {
builder = builder.set_strand(strand);
}

let values: Vec<_> = record.other_fields().iter().map(Value::from).collect();
let other_fields = OtherFields::from(values);
builder = builder.set_other_fields(other_fields);

Ok(builder.build())
}
}

0 comments on commit 54e6d70

Please sign in to comment.