noodles_cram/record/
convert.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use std::{io, str};

use noodles_sam as sam;

use super::{Features, Flags, QualityScores, Record, Sequence};

impl Record {
    /// Converts an alignment record to a CRAM record.
    pub fn try_from_alignment_record<R>(header: &sam::Header, record: &R) -> io::Result<Self>
    where
        R: sam::alignment::Record + ?Sized,
    {
        let mut builder = Self::builder();

        let bam_flags = record.flags()?;
        builder = builder.set_bam_flags(bam_flags);

        let mut flags = Flags::default();

        if let Some(reference_sequence_id) = record.reference_sequence_id(header).transpose()? {
            builder = builder.set_reference_sequence_id(reference_sequence_id);
        }

        builder = builder.set_read_length(record.sequence().len());

        if let Some(alignment_start) = record.alignment_start().transpose()? {
            builder = builder.set_alignment_start(alignment_start);
        }

        let mut data = alignment_record_data_to_data_buf(record.data())?;

        if let Some(read_group_id) = get_read_group_id(header.read_groups(), &data)? {
            builder = builder.set_read_group_id(read_group_id);
        }

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

        // next mate bit flags

        if let Some(mate_reference_sequence_id) =
            record.mate_reference_sequence_id(header).transpose()?
        {
            builder = builder.set_next_fragment_reference_sequence_id(mate_reference_sequence_id);
        }

        if let Some(mate_alignment_start) = record.mate_alignment_start().transpose()? {
            builder = builder.set_next_mate_alignment_start(mate_alignment_start);
        }

        let template_length = record.template_length()?;
        builder = builder.set_template_size(template_length);

        // distance to next fragment

        if !data.is_empty() {
            use sam::alignment::record::data::field::Tag;
            data.remove(&Tag::READ_GROUP);
            builder = builder.set_tags(data);
        }

        let raw_bases: Vec<_> = record.sequence().iter().collect();
        let bases = Sequence::from(raw_bases);

        let quality_scores = if record.quality_scores().is_empty() {
            QualityScores::default()
        } else {
            if bam_flags.is_unmapped() {
                flags.insert(Flags::QUALITY_SCORES_STORED_AS_ARRAY);
            }

            let scores: Vec<_> = record.quality_scores().iter().collect::<io::Result<_>>()?;
            QualityScores::from(scores)
        };

        if !bam_flags.is_unmapped() {
            let cigar = record.cigar().iter().collect::<Result<_, _>>()?;
            let features = Features::from_cigar(flags, &cigar, &bases, &quality_scores);
            builder = builder.set_features(features);
        }

        if let Some(mapping_quality) = record.mapping_quality().transpose()? {
            builder = builder.set_mapping_quality(mapping_quality);
        }

        if !record.quality_scores().is_empty() {
            if bam_flags.is_unmapped() {
                flags.insert(Flags::QUALITY_SCORES_STORED_AS_ARRAY);
            }

            let scores: Vec<_> = record.quality_scores().iter().collect::<io::Result<_>>()?;
            let quality_scores = QualityScores::from(scores);
            builder = builder.set_quality_scores(quality_scores);
        }

        Ok(builder
            .set_flags(flags)
            .set_bases(bases)
            .set_quality_scores(quality_scores)
            .build())
    }

    /// Converts this CRAM record to an alignment record.
    pub fn try_into_alignment_record(
        self,
        header: &sam::Header,
    ) -> io::Result<sam::alignment::RecordBuf> {
        let mut builder = sam::alignment::RecordBuf::builder();

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

        builder = builder.set_flags(self.bam_flags);

        if let Some(reference_sequence_id) = self.reference_sequence_id {
            builder = builder.set_reference_sequence_id(reference_sequence_id);
        }

        if let Some(alignment_start) = self.alignment_start {
            builder = builder.set_alignment_start(alignment_start);
        }

        if let Some(mapping_quality) = self.mapping_quality {
            builder = builder.set_mapping_quality(mapping_quality);
        }

        if !self.bam_flags.is_unmapped() {
            let cigar = self
                .features
                .try_into_cigar(self.read_length)
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

            builder = builder.set_cigar(cigar);
        }

        if let Some(mate_reference_sequence_id) = self.mate_reference_sequence_id {
            builder = builder.set_mate_reference_sequence_id(mate_reference_sequence_id);
        }

        if let Some(mate_alignment_start) = self.mate_alignment_start {
            builder = builder.set_mate_alignment_start(mate_alignment_start);
        }

        builder = builder
            .set_template_length(self.template_length)
            .set_sequence(self.sequence)
            .set_quality_scores(self.quality_scores);

        let mut data = self.tags;
        maybe_insert_read_group(&mut data, header.read_groups(), self.read_group_id)?;
        builder = builder.set_data(data);

        Ok(builder.build())
    }
}

fn alignment_record_data_to_data_buf<D>(data: D) -> io::Result<sam::alignment::record_buf::Data>
where
    D: sam::alignment::record::Data,
{
    use sam::alignment::{
        record::data::field::{value::Array, Value},
        record_buf::data::field::{value::Array as ArrayBuf, Value as ValueBuf},
    };

    fn value_to_value_buf(value: Value<'_>) -> io::Result<ValueBuf> {
        match value {
            Value::Character(c) => Ok(ValueBuf::Character(c)),
            Value::Int8(n) => Ok(ValueBuf::Int8(n)),
            Value::UInt8(n) => Ok(ValueBuf::UInt8(n)),
            Value::Int16(n) => Ok(ValueBuf::Int16(n)),
            Value::UInt16(n) => Ok(ValueBuf::UInt16(n)),
            Value::Int32(n) => Ok(ValueBuf::Int32(n)),
            Value::UInt32(n) => Ok(ValueBuf::UInt32(n)),
            Value::Float(n) => Ok(ValueBuf::Float(n)),
            Value::String(s) => str::from_utf8(s)
                .map(|t| ValueBuf::String(t.into()))
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)),
            Value::Hex(s) => str::from_utf8(s)
                .map(|t| ValueBuf::Hex(t.into()))
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)),
            Value::Array(Array::Int8(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::Int8(vs))),
            Value::Array(Array::UInt8(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::UInt8(vs))),
            Value::Array(Array::Int16(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::Int16(vs))),
            Value::Array(Array::UInt16(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::UInt16(vs))),
            Value::Array(Array::Int32(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::Int32(vs))),
            Value::Array(Array::UInt32(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::UInt32(vs))),
            Value::Array(Array::Float(values)) => values
                .iter()
                .collect::<Result<_, _>>()
                .map(|vs| ValueBuf::Array(ArrayBuf::Float(vs))),
        }
    }

    let mut buf = sam::alignment::record_buf::Data::default();

    for result in data.iter() {
        let (tag, raw_value) = result?;
        let value = value_to_value_buf(raw_value)?;
        buf.insert(tag, value);
    }

    Ok(buf)
}

fn get_read_group_id(
    read_groups: &sam::header::ReadGroups,
    data: &sam::alignment::record_buf::Data,
) -> io::Result<Option<usize>> {
    use sam::alignment::{record::data::field::Tag, record_buf::data::field::Value};

    let Some(rg_value) = data.get(&Tag::READ_GROUP) else {
        return Ok(None);
    };

    let read_group_name = match rg_value {
        Value::String(s) => s,
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "invalid read group field value",
            ))
        }
    };

    read_groups
        .get_index_of(read_group_name)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "invalid read group name"))
        .map(Some)
}

fn maybe_insert_read_group(
    data: &mut sam::alignment::record_buf::Data,
    read_groups: &sam::header::ReadGroups,
    read_group_id: Option<usize>,
) -> io::Result<()> {
    use sam::alignment::{record::data::field::Tag, record_buf::data::field::Value};

    if let Some(id) = read_group_id {
        let name = read_groups
            .get_index(id)
            .map(|(name, _)| name)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "invalid read group ID"))?;

        data.insert(Tag::READ_GROUP, Value::String(name.clone()));
    }

    Ok(())
}