File: operation.d

package info (click to toggle)
sambamba 1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,528 kB
  • sloc: sh: 220; python: 166; ruby: 147; makefile: 103
file content (147 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download | duplicates (4)
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
module bio.std.hts.bam.md.operation;

import bio.core.base;
import bio.core.sequence;

import std.conv;
import std.traits;
import std.bitmanip;
import std.algorithm;

/// MD tag operation types
enum MdOperationType : ubyte {
    Match,
    Mismatch,
    Deletion
}

/// Single MD operation.
struct MdOperation {

    private {
        MdOperationType _type;
        union {
            uint _match;
            NucleotideSequence _deletion;
            Base16 _mismatch;
        }
    }

    /// Operation type
    MdOperationType type() @property const {
        return _type;
    }

    /// ditto
    void type(MdOperationType t) @property {
        _type = t;
    }

    /// Convenience methods
    bool is_deletion() @property const {
        return _type == MdOperationType.Deletion;
    }

    /// ditto
    bool is_match() @property const {
        return _type == MdOperationType.Match;
    }

    /// ditto
    bool is_mismatch() @property const {
        return _type == MdOperationType.Mismatch;
    }

    /// The number of matched bases
    ref uint match() @property {
        return _match;
    }

    /// Mismatched reference base
    Base16 mismatch() @property const {
        return _mismatch;
    }

    /// ditto
    void mismatch(Base16 base) @property {
        _mismatch = base;
    }

    /// Deleted sequence
    ref NucleotideSequence deletion() @property {
        return _deletion;
    }

    static MdOperation createMatch(uint match) {
        MdOperation m = void;
        m._type = MdOperationType.Match;
        m._match = match;
        return m;
    }

    static MdOperation createDeletion(string deletion) {
        MdOperation m = void;
        m._type = MdOperationType.Deletion;
        m._deletion = nucleotideSequence(sliceableString(deletion));
        return m;
    }

    static MdOperation createMismatch(char mismatch) {
        MdOperation m = void;
        m._type = MdOperationType.Mismatch;
        m._mismatch = Base16(mismatch);
        return m;
    }

    static MdOperation createDeletion(NucleotideSequence seq) {
        MdOperation m = void;
        m._type = MdOperationType.Deletion;
        m._deletion = seq;
        return m;
    }

    static MdOperation createMismatch(Base16 base) {
        MdOperation m = void;
        m._type = MdOperationType.Mismatch;
        m._mismatch = base;
        return m;
    }

    bool opEquals(ref const(MdOperation) other) const {

        if (type != other.type) {
            return false;
        }

        final switch (type) {
            case MdOperationType.Match:
                return _match == other._match;
            case MdOperationType.Mismatch:
                return mismatch == other.mismatch;
            case MdOperationType.Deletion:
                return equal(cast()_deletion, cast()other._deletion);
        }
    }

    string toString() const {
        final switch (type) {
            case MdOperationType.Match:
                return "Match(" ~ to!string(_match) ~ ")";
            case MdOperationType.Mismatch:
                return "Mismatch(" ~ to!string(_mismatch) ~ ")";
            case MdOperationType.Deletion:
                return "Deletion(" ~ to!string(_deletion) ~ ")";
        }
    }
}

/// Returns MD operation with reverse-complemented data
MdOperation reverseMdOp(MdOperation op) {
    if (op.is_deletion)
        return MdOperation.createDeletion(op.deletion.reverse);

    if (op.is_mismatch)
        return MdOperation.createMismatch(op.mismatch.complement);

    return op;
}