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
use std::io::{self, Write};

use crate::{
    alignment::record::{
        cigar::{op::Kind, Op},
        Cigar,
    },
    io::writer::num,
};

/// Writes a SAM record CIGAR string.
///
/// # Examples
///
/// ```
/// use noodles_sam::{
///     alignment::{
///         record::cigar::{op::Kind, Op},
///         record_buf::Cigar,
///     },
///     io::writer::record::write_cigar,
/// };
///
/// let mut buf = Vec::new();
/// let cigar = Cigar::default();
/// write_cigar(&mut buf, &cigar)?;
/// assert_eq!(buf, b"*");
///
/// let mut buf = Vec::new();
/// let cigar: Cigar = [Op::new(Kind::Match, 4)].into_iter().collect();
/// write_cigar(&mut buf, &cigar)?;
/// assert_eq!(buf, b"4M");
/// Ok::<_, std::io::Error>(())
/// ```
pub fn write_cigar<W, C>(writer: &mut W, cigar: &C) -> io::Result<()>
where
    W: Write,
    C: Cigar,
{
    use super::MISSING;

    if cigar.is_empty() {
        writer.write_all(&[MISSING])?;
    } else {
        for result in cigar.iter() {
            let op = result?;
            write_op(writer, op)?;
        }
    }

    Ok(())
}

fn write_op<W>(writer: &mut W, op: Op) -> io::Result<()>
where
    W: Write,
{
    num::write_usize(writer, op.len())?;
    write_kind(writer, op.kind())?;
    Ok(())
}

fn write_kind<W>(writer: &mut W, kind: Kind) -> io::Result<()>
where
    W: Write,
{
    let c = match kind {
        Kind::Match => b'M',
        Kind::Insertion => b'I',
        Kind::Deletion => b'D',
        Kind::Skip => b'N',
        Kind::SoftClip => b'S',
        Kind::HardClip => b'H',
        Kind::Pad => b'P',
        Kind::SequenceMatch => b'=',
        Kind::SequenceMismatch => b'X',
    };

    writer.write_all(&[c])
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::alignment::record_buf::Cigar as CigarBuf;

    #[test]
    fn test_write_cigar() -> io::Result<()> {
        fn t(buf: &mut Vec<u8>, cigar: &CigarBuf, expected: &[u8]) -> io::Result<()> {
            buf.clear();
            write_cigar(buf, cigar)?;
            assert_eq!(buf, expected);
            Ok(())
        }

        let mut buf = Vec::new();

        let cigar = CigarBuf::default();
        t(&mut buf, &cigar, b"*")?;

        let cigar = [Op::new(Kind::Match, 4)].into_iter().collect();
        t(&mut buf, &cigar, b"4M")?;

        let cigar: CigarBuf = [Op::new(Kind::Match, 4), Op::new(Kind::HardClip, 2)]
            .into_iter()
            .collect();
        t(&mut buf, &cigar, b"4M2H")?;

        Ok(())
    }

    #[test]
    fn test_write_op() -> io::Result<()> {
        let mut buf = Vec::new();

        buf.clear();
        write_op(&mut buf, Op::new(Kind::Match, 1))?;
        assert_eq!(buf, b"1M");

        buf.clear();
        write_op(&mut buf, Op::new(Kind::Match, 1 << 28))?;
        assert_eq!(buf, b"268435456M");

        Ok(())
    }

    #[test]
    fn test_write_kind() -> io::Result<()> {
        fn t(buf: &mut Vec<u8>, kind: Kind, expected: &[u8]) -> io::Result<()> {
            buf.clear();
            write_kind(buf, kind)?;
            assert_eq!(buf, expected);
            Ok(())
        }

        let mut buf = Vec::new();

        t(&mut buf, Kind::Match, b"M")?;
        t(&mut buf, Kind::Insertion, b"I")?;
        t(&mut buf, Kind::Deletion, b"D")?;
        t(&mut buf, Kind::Skip, b"N")?;
        t(&mut buf, Kind::SoftClip, b"S")?;
        t(&mut buf, Kind::HardClip, b"H")?;
        t(&mut buf, Kind::Pad, b"P")?;
        t(&mut buf, Kind::SequenceMatch, b"=")?;
        t(&mut buf, Kind::SequenceMismatch, b"X")?;

        Ok(())
    }
}