File: EdnaEvaluator.hpp

package info (click to toggle)
consensuscore 1.1.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,492 kB
  • sloc: cpp: 38,945; python: 2,083; ansic: 543; sh: 184; makefile: 91; cs: 10
file content (238 lines) | stat: -rw-r--r-- 5,876 bytes parent folder | download
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
// Author: Patrick Marks

#pragma once

#define SIMDE_ENABLE_NATIVE_ALIASES
#include <simde/x86/sse3.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include <ConsensusCore/Edna/EdnaConfig.hpp>
#include <ConsensusCore/Features.hpp>
#include <ConsensusCore/LFloat.hpp>
#include <ConsensusCore/Read.hpp>
#include <ConsensusCore/Types.hpp>
#include <ConsensusCore/Utils.hpp>

#ifndef SWIG
using std::min;
using std::max;
#endif  // SWIG

#define NEG_INF -FLT_MAX

namespace ConsensusCore {
/// \brief An Evaluator for the Edna Edna model
class EdnaEvaluator
{
public:
    typedef ChannelSequenceFeatures FeaturesType;
    typedef EdnaModelParams ParamsType;

public:
    EdnaEvaluator(const ChannelSequenceFeatures& features, const std::string& tpl,
                  const std::vector<int> channelTpl, const EdnaModelParams& params)
        : features_(features)
        , params_(params)
        , tpl_(tpl)
        , channelTpl_(&(channelTpl[0]), tpl_.length())
        , pinStart_(true)
        , pinEnd_(true)
    {
    }

    ~EdnaEvaluator() {}

    std::string ReadName() const { return "(anonymous)"; }

    std::string Basecalls() const { return features_.Sequence(); }

    std::string Template() const { return tpl_; }

    void Template(std::string tpl) { tpl_ = tpl; }

    int ReadLength() const { return features_.Length(); }

    int TemplateLength() const { return tpl_.length(); }

    bool PinEnd() const { return pinEnd_; }

    bool PinStart() const { return pinStart_; }

    bool IsMatch(int i, int j) const
    {
        assert(0 <= i && i < ReadLength());
        assert(0 <= j && j < TemplateLength());
        return (features_.Channel[i] == channelTpl_[j]);
    }

    bool mergeable(int j) const
    {
        if (j < TemplateLength() - 1 && channelTpl_[j] == channelTpl_[j + 1]) return true;

        return false;
    }

    int templateBase(int j) const
    {
        if (j >= TemplateLength()) return 1;

        return channelTpl_[j];
    }

    float pStay(int j) const { return params_.pStay_[templateBase(j) - 1]; }

    float pMerge(int j) const
    {
        if (mergeable(j)) return params_.pMerge_[templateBase(j) - 1];

        return 0.0;
    }

    float moveDist(int obs, int j) const
    {
        int tplBase = templateBase(j) - 1;
        return params_.moveDists_[tplBase * 5 + obs];
    }

    float stayDist(int obs, int j) const
    {
        int tplBase = templateBase(j) - 1;
        return params_.stayDists_[tplBase * 5 + obs];
    }

    float Inc(int i, int j) const
    {
        assert(0 <= j && j < TemplateLength() && 0 <= i && i < ReadLength());

        float ps = pStay(j);
        float pm = (1.0f - ps) * pMerge(j);
        float trans = 1.0f - ps - pm;

        float em = moveDist(features_.Channel[i], j);
        return log(trans * em);
    }

    float Del(int i, int j) const
    {
        assert(0 <= j && j < TemplateLength() && 0 <= i && i <= ReadLength());
        if ((!PinStart() && i == 0) || (!PinEnd() && i == ReadLength())) {
            return 0.0f;
        } else {
            float ps = pStay(j);
            float pm = (1.0f - ps) * pMerge(j);
            float trans = 1.0f - ps - pm;

            float em = moveDist(0, j);
            return log(trans * em);
        }
    }

    float Extra(int i, int j) const
    {
        assert(0 <= j && j <= TemplateLength() && 0 <= i && i < ReadLength());

        float trans = pStay(j);
        float em = stayDist(features_.Channel[i], j);
        return log(trans * em);
    }

    float Merge(int i, int j) const
    {
        assert(0 <= j && j < TemplateLength() - 1 && 0 <= i && i < ReadLength());
        if (!(features_.Channel[i] == channelTpl_[j] &&
              features_.Channel[i] == channelTpl_[j + 1])) {
            return -FLT_MAX;
        } else {
            float ps = pStay(j);
            float pm = (1.0f - ps) * pMerge(j);
            return log(pm);
        }
    }

    float ScoreMove(int j1, int j2, int obs)
    {
        if (j1 == j2) {
            float trans = pStay(j1);
            float em = stayDist(obs, j1);
            return log(trans * em);
        } else if (j1 + 1 == j2) {
            float ps = pStay(j1);
            float pm = (1.0f - ps) * pMerge(j1);
            float trans = 1.0f - ps - pm;

            float em = moveDist(obs, j1);
            return log(trans * em);
        } else if (j1 + 2 == j2) {
            float ps = pStay(j1);
            float pm = (1.0f - ps) * pMerge(j1);

            if (obs == templateBase(j1))
                return log(pm);

            else
                return NEG_INF;
        }

        return NEG_INF;
    }

    float Burst(int, int, int) const
    {
        NotYetImplemented();
        return NEG_INF;
    }

    //
    // SSE
    //

    __m128 Inc4(int i, int j) const
    {
        __m128 res = _mm_setr_ps(Inc(i + 0, j), Inc(i + 1, j), Inc(i + 2, j), Inc(i + 3, j));
        return res;
    }

    __m128 Del4(int i, int j) const
    {
        __m128 res = _mm_setr_ps(Del(i + 0, j), Del(i + 1, j), Del(i + 2, j), Del(i + 3, j));
        return res;
    }

    __m128 Extra4(int i, int j) const
    {
        __m128 res =
            _mm_setr_ps(Extra(i + 0, j), Extra(i + 1, j), Extra(i + 2, j), Extra(i + 3, j));
        return res;
    }

    __m128 Merge4(int i, int j) const
    {
        __m128 res =
            _mm_setr_ps(Merge(i + 0, j), Merge(i + 1, j), Merge(i + 2, j), Merge(i + 3, j));
        return res;
    }

    __m128 Burst4(int, int, int) const
    {
        NotYetImplemented();
        return Zero4<lfloat>();
    }

protected:
    ChannelSequenceFeatures features_;
    EdnaModelParams params_;
    std::string tpl_;
    Feature<int> channelTpl_;
    bool pinStart_;
    bool pinEnd_;
};
}