File: HDFZMWWriter.cpp

package info (click to toggle)
pbseqlib 5.3.5%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,020 kB
  • sloc: cpp: 77,250; python: 331; sh: 103; makefile: 41
file content (232 lines) | stat: -rw-r--r-- 7,776 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
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
#include <LibBlasrConfig.h>

#ifdef USE_PBBAM

#include <hdf/HDFZMWWriter.hpp>

HDFZMWWriter::HDFZMWWriter(const std::string& filename, HDFGroup& parentGroup,
                           const bool inPulseCalls, const std::map<char, size_t>& baseMap)
    : HDFWriterBase(filename)
    , parentGroup_(parentGroup)
    , baseMap_(baseMap)
    , inPulseCalls_(inPulseCalls)
    , arrayLength_(0)
{
    if (not parentGroup.groupIsInitialized)
        PARENT_GROUP_NOT_INITIALIZED_ERROR(PacBio::GroupNames::zmw);
    else {
        parentGroup_.AddGroup(PacBio::GroupNames::zmw);

        if (zmwGroup_.Initialize(parentGroup_, PacBio::GroupNames::zmw) == 0)
            FAILED_TO_CREATE_GROUP_ERROR(PacBio::GroupNames::zmw);

        this->InitializeChildHDFGroups();
    }
}

HDFZMWWriter::HDFZMWWriter(const std::string& filename, HDFGroup& parentGroup)
    : HDFZMWWriter(filename, parentGroup, false, {})
{
}

HDFZMWWriter::~HDFZMWWriter(void)
{
    this->_WriteAttributes();
    this->Close();
}

bool HDFZMWWriter::InPulseCalls(void) const { return inPulseCalls_; }

bool HDFZMWWriter::HasBaseLineSigma(void) const
{
    return false;  // base line sigma won't be provided util 3.0.1 bug 29486
    //return InPulseCalls() and baseLineSigmaArray_.IsInitialized();
}

bool HDFZMWWriter::WriteOneZmw(const SMRTSequence& read)
{
    _WriteNumEvent(read.length);
    _WriteHoleNumber(read.HoleNumber());
    _WriteHoleXY(static_cast<int16_t>(read.HoleX()), static_cast<int16_t>(read.HoleY()));
    _WriteHoleStatus(read.HoleStatus());
    arrayLength_++;
    return Errors().empty();
}

bool HDFZMWWriter::WriteOneZmw(const PacBio::BAM::BamRecord& read)
{
    if (InPulseCalls()) {
        if (not read.HasPulseCall()) {
            AddErrorMessage(std::string("PulseCall absent in read ") + read.FullName());
        } else {
            _WriteNumEvent(read.PulseCall().size());
        }
    } else {
        _WriteNumEvent(read.Sequence().size());
    }
    arrayLength_++;

    uint32_t hn_ = read.HoleNumber();
    _WriteHoleNumber(hn_);
    _WriteHoleXY(static_cast<int16_t>(hn_ >> 16), static_cast<int16_t>(hn_ & 0x0000FFFF));
    _WriteHoleStatus(PacBio::AttributeValues::ZMW::HoleStatus::sequencingzmw);
    _WriteBaseLineSigma(read);
    return Errors().empty();
}

bool HDFZMWWriter::WriteFakeDataSets(void)
{
    // Fake BaselineLevel, BaselineSigma, SignalLevel, SignalSigma
    //std::vector<float> buffer(arrayLength_);
    //std::fill(buffer.begin(), buffer.end(), 0.0);
    return __WriteFake2DDataSet<float>(zmwGroup_, PacBio::GroupNames::baselinelevel, arrayLength_,
                                       4, 0) and
           __WriteFake2DDataSet<float>(zmwGroup_, PacBio::GroupNames::baselinesigma, arrayLength_,
                                       4, 0) and
           __WriteFake2DDataSet<float>(zmwGroup_, PacBio::GroupNames::signallevel, arrayLength_, 4,
                                       0) and
           __WriteFake2DDataSet<float>(zmwGroup_, PacBio::GroupNames::signalsigma, arrayLength_, 4,
                                       0);
}

bool HDFZMWWriter::_WriteNumEvent(const uint32_t numEvent)
{
    numEventArray_.Write(&numEvent, 1);
    return true;
}

bool HDFZMWWriter::_WriteHoleNumber(const uint32_t holeNumber)
{
    holeNumberArray_.Write(&holeNumber, 1);
    return true;
}

bool HDFZMWWriter::_WriteHoleXY(const int16_t holeX, const int16_t holeY)
{
    int16_t xy[2] = {holeX, holeY};
    holeXYArray_.WriteRow(xy, 2);
    return true;
}

bool HDFZMWWriter::_WriteHoleStatus(const unsigned char holeStatus)
{
    // NOTE that: We assume that all zmws in BAM are SEQUENCING zmws.
    holeStatusArray_.Write(&holeStatus, 1);
    return true;
}

bool HDFZMWWriter::_WriteBaseLineSigma(const PacBio::BAM::BamRecord& read)
{
    // FIXME: pbbam should provide APIs: HasBaseLineSigma and BaseLineSigma().
    if (HasBaseLineSigma()) {
        if (read.Impl().HasTag("bs")) {
            const PacBio::BAM::Tag& tag = read.Impl().TagValue("bs");
            std::vector<float> data = tag.ToFloatArray();
            if (data.size() != 4) {
                AddErrorMessage(
                    std::string("Tag BaseLineSigma must have 4 values per each record in read " +
                                read.FullName()));
            }
            float bls[4];
            bls[baseMap_['A']] = data[0];
            bls[baseMap_['C']] = data[1];
            bls[baseMap_['G']] = data[2];
            bls[baseMap_['T']] = data[3];
            baseLineSigmaArray_.WriteRow(bls, 4);
        } else {
            AddErrorMessage(std::string("Tag BaseLineSigma is absent in read ") + read.FullName());
        }
    } else {
        //BaseLineSigma won't be available uill v 3.0.1.
        //AddErrorMessage("BaseLineSigma array is not initialized.");
    }
    return Errors().empty();
}

void HDFZMWWriter::Flush(void)
{
    // Mandatory metrics
    numEventArray_.Flush();
    holeNumberArray_.Flush();
    holeStatusArray_.Flush();
    holeXYArray_.Flush();

    // Optional metrics
    if (HasBaseLineSigma()) baseLineSigmaArray_.Flush();
}

void HDFZMWWriter::Close(void)
{
    this->Flush();

    // Mandatory metrics
    numEventArray_.Close();
    holeNumberArray_.Close();
    holeStatusArray_.Close();
    holeXYArray_.Close();

    // Optional metrics
    if (HasBaseLineSigma()) baseLineSigmaArray_.Close();

    zmwGroup_.Close();
}

bool HDFZMWWriter::InitializeChildHDFGroups(void)
{
    // Mandatory metrics
    if (numEventArray_.Initialize(zmwGroup_, PacBio::GroupNames::numevent) == 0) {
        FAILED_TO_CREATE_GROUP_ERROR(PacBio::GroupNames::numevent);
    }

    if (holeNumberArray_.Initialize(zmwGroup_, PacBio::GroupNames::holenumber) == 0) {
        FAILED_TO_CREATE_GROUP_ERROR(PacBio::GroupNames::holenumber);
    }

    if (holeStatusArray_.Initialize(zmwGroup_, PacBio::GroupNames::holestatus) == 0) {
        FAILED_TO_CREATE_GROUP_ERROR(PacBio::GroupNames::holestatus);
    }

    if (holeXYArray_.Initialize(zmwGroup_, PacBio::GroupNames::holexy, 2) == 0) {
        FAILED_TO_CREATE_GROUP_ERROR(PacBio::GroupNames::holexy);
    }

    /* BaseLineSigma not available until 3.0.1
    if (InPulseCalls()) {
        if (baseLineSigmaArray_.Initialize(zmwGroup_, PacBio::GroupNames::baselinesigma, 4) == 0) {
            FAILED_TO_CREATE_GROUP_ERROR(PacBio::GroupNames::baselinesigma);
        }
    }
    */
    return Errors().empty();
}

void HDFZMWWriter::_WriteAttributes(void)
{
    // NumEvent has no attributes.

    if (holeNumberArray_.IsInitialized() and holeNumberArray_.size() > 0) {
        AddAttribute<std::string>(holeNumberArray_, PacBio::AttributeNames::Common::description,
                                  PacBio::AttributeValues::ZMW::HoleNumber::description);
    }

    if (holeStatusArray_.IsInitialized() and holeStatusArray_.size() > 0) {
        AddAttribute<std::string>(holeStatusArray_, PacBio::AttributeNames::Common::description,
                                  PacBio::AttributeValues::ZMW::HoleStatus::description);
        /*
        AddAttribute<std::vector<std::string>>(
            holeStatusArray_, PacBio::AttributeNames::ZMW::HoleStatus::lookuptable,
            PacBio::AttributeValues::ZMW::HoleStatus::lookuptable);
*/
    }

    if (holeXYArray_.IsInitialized() and holeXYArray_.GetNRows() > 0) {
        AddAttribute<std::string>(holeXYArray_, PacBio::AttributeNames::Common::description,
                                  PacBio::AttributeValues::ZMW::HoleXY::description);
    }

    if (HasBaseLineSigma() and baseLineSigmaArray_.GetNRows() > 0) {
        AddAttribute<std::string>(baseLineSigmaArray_, PacBio::AttributeNames::Common::description,
                                  PacBio::AttributeValues::ZMW::BaseLineSigma::description);
    }
}
#endif