File: HDFScanDataReader.cpp

package info (click to toggle)
pbseqlib 5.3.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,020 kB
  • sloc: cpp: 77,246; python: 331; sh: 103; makefile: 42
file content (278 lines) | stat: -rw-r--r-- 7,633 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
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
270
271
272
273
274
275
276
277
278
#include <hdf/HDFScanDataReader.hpp>

HDFScanDataReader::HDFScanDataReader()
{
    //
    // Assume the file is written without a movie name.  This is
    // flipped when a movie name is found.
    //
    Reset();
}

void HDFScanDataReader::Reset()
{
    useMovieName = false;
    useRunCode = false;
    useWhenStarted = false;
    fileHasScanData = false;
    movieName = "";
    runCode = "";
    platformId = NoPlatform;
    initializedAcqParamsGroup = initializedRunInfoGroup = false;
}

int HDFScanDataReader::InitializeAcqParamsAtoms()
{
    if (frameRateAtom.Initialize(acqParamsGroup.group, "FrameRate") == 0) {
        return 0;
    }
    if (numFramesAtom.Initialize(acqParamsGroup.group, "NumFrames") == 0) {
        return 0;
    }
    if (acqParamsGroup.ContainsAttribute("WhenStarted")) {
        if (whenStartedAtom.Initialize(acqParamsGroup.group, "WhenStarted") == 0) {
            return 0;
        }
        useWhenStarted = true;
    }
    return 1;
}

//
// This is created on top of a file that is already opened, so
// instead of initializing by opening a file, it is initialized by
// passing the root group that should contain the ScanData group.
// When shutting down, no file handle needs to be closed.
//
int HDFScanDataReader::Initialize(HDFGroup *pulseDataGroup)
{

    //
    // Initiailze groups for reading data.
    //

    initializedAcqParamsGroup = false;
    initializedRunInfoGroup = false;
    if (pulseDataGroup->ContainsObject("ScanData") == 0 or
        scanDataGroup.Initialize(pulseDataGroup->group, "ScanData") == 0) {
        return 0;
    }
    fileHasScanData = true;

    if (scanDataGroup.ContainsObject("DyeSet") == 0 or
        dyeSetGroup.Initialize(scanDataGroup.group, "DyeSet") == 0) {
        return 0;
    }

    if (scanDataGroup.ContainsObject("AcqParams") == 0 or
        acqParamsGroup.Initialize(scanDataGroup.group, "AcqParams") == 0) {
        return 0;
    }
    initializedAcqParamsGroup = true;

    if (scanDataGroup.ContainsObject("RunInfo") == 0 or
        runInfoGroup.Initialize(scanDataGroup.group, "RunInfo") == 0) {
        return 0;
    }
    initializedRunInfoGroup = true;
    if (InitializeAcqParamsAtoms() == 0) {
        return 0;
    }

    //
    // Read in the data that will be used later on either per read or
    // when the entire bas/pls file is read.
    //

    if (ReadPlatformId(platformId) == 0) {
        return 0;
    }

    if (runInfoGroup.ContainsAttribute("RunCode") and
        runCodeAtom.Initialize(runInfoGroup, "RunCode")) {
        useRunCode = true;
    }

    //
    // Load baseMap which maps bases (ATGC) to channel orders.
    // This should always be present.
    //
    if (LoadBaseMap(baseMap_) == 0) return 0;

    //
    // Attempt to load the movie name.  This is not always present.
    //
    LoadMovieName(movieName);

    return 1;
}

std::string HDFScanDataReader::GetMovieName()
{
    // If this object is correctly initialized, movieName
    // is guaranteed to be loaded if it exists, no need to reload.
    return movieName;
}

std::string HDFScanDataReader::GetRunCode() { return runCode; }

int HDFScanDataReader::Read(ScanData &scanData)
{
    // All parameters below are required.
    if (ReadPlatformId(scanData.platformId) == 0) return 0;
    LoadMovieName(scanData.movieName);
    LoadBaseMap(scanData.baseMap);

    if (useRunCode) {
        runCodeAtom.Read(scanData.runCode);
    }
    frameRateAtom.Read(scanData.frameRate);
    numFramesAtom.Read(scanData.numFrames);

    if (useWhenStarted) {
        whenStartedAtom.Read(scanData.whenStarted);
    }

    ReadSequencingKit(scanData.sequencingKit_);

    ReadBindingKit(scanData.bindingKit_);

    return 1;
}

void HDFScanDataReader::ReadWhenStarted(std::string &whenStarted)
{
    whenStartedAtom.Read(whenStarted);
}

PlatformId HDFScanDataReader::GetPlatformId() { return platformId; }

int HDFScanDataReader::ReadPlatformId(PlatformId &pid)
{
    if (runInfoGroup.ContainsAttribute("PlatformId")) {
        if (platformIdAtom.Initialize(runInfoGroup, "PlatformId") == 0) {
            return 0;
        }
        platformIdAtom.Read((unsigned int &)pid);
    } else {
        pid = Astro;
    }
    return 1;
}

int HDFScanDataReader::ReadStringAttribute(std::string &attributeValue,
                                           const std::string &attributeName, HDFGroup &group,
                                           HDFAtom<std::string> &atom)
{

    if (group.ContainsAttribute(attributeName) and
        (atom.isInitialized or atom.Initialize(group, attributeName))) {
        atom.Read(attributeValue);
        return 1;
    } else {
        return 0;
    }
}

int HDFScanDataReader::ReadBindingKit(std::string &bindingKit)
{
    return ReadStringAttribute(bindingKit, "BindingKit", runInfoGroup, bindingKitAtom);
}

int HDFScanDataReader::ReadSequencingKit(std::string &sequencingKit)
{
    return ReadStringAttribute(sequencingKit, "SequencingKit", runInfoGroup, sequencingKitAtom);
}

int HDFScanDataReader::LoadMovieName(std::string &movieNameP)
{
    // Groups for building read names
    if (ReadStringAttribute(movieNameP, "MovieName", runInfoGroup, movieNameAtom) == 0) {
        // Internal analysis may manually edit the movie name and set STRSIZE to a value
        // which != movie name length. Handle this case.
        movieNameP = std::string(movieNameP.c_str());
        return 0;
    } else {
        useMovieName = true;
        int e = movieNameP.size() - 1;
        while (e > 0 and movieNameP[e] == ' ')
            e--;
        movieNameP = movieNameP.substr(0, e + 1);
        movieNameP = std::string(movieNameP.c_str());
        return 1;
    }
}

int HDFScanDataReader::LoadBaseMap(std::map<char, size_t> &baseMap)
{
    // Map bases to channel order in hdf pls file.
    if (dyeSetGroup.ContainsAttribute("BaseMap") and
        baseMapAtom.Initialize(dyeSetGroup, "BaseMap")) {
        std::string baseMapStr;
        baseMapAtom.Read(baseMapStr);
        if (baseMapStr.size() != 4) {
            std::cout << "ERROR, there are more than four types of bases "
                      << "according to /ScanData/DyeSet/BaseMap." << std::endl;
            std::exit(EXIT_FAILURE);
        }
        baseMap.clear();
        for (size_t i = 0; i < baseMapStr.size(); i++) {
            baseMap[toupper(baseMapStr[i])] = i;
        }
        this->baseMap_ = baseMap;
        return 1;
    }
    return 0;
}

void HDFScanDataReader::Close()
{
    if (useMovieName) {
        movieNameAtom.Close();
    }
    if (useRunCode) {
        runCodeAtom.Close();
    }
    if (useWhenStarted) {
        whenStartedAtom.Close();
    }

    baseMapAtom.Close();
    platformIdAtom.Close();
    frameRateAtom.Close();
    numFramesAtom.Close();
    sequencingKitAtom.Close();
    bindingKitAtom.Close();

    scanDataGroup.Close();
    dyeSetGroup.Close();
    acqParamsGroup.Close();
    runInfoGroup.Close();
    Reset();
}

std::string HDFScanDataReader::GetMovieName_and_Close(std::string &fileName)
{
    HDFFile file;
    file.Open(fileName, H5F_ACC_RDONLY);

    fileHasScanData = false;
    if (file.rootGroup.ContainsObject("ScanData") == 0 or
        scanDataGroup.Initialize(file.rootGroup, "ScanData") == 0) {
        return "";
    }
    fileHasScanData = true;

    initializedRunInfoGroup = false;
    if (scanDataGroup.ContainsObject("RunInfo") == 0 or
        runInfoGroup.Initialize(scanDataGroup.group, "RunInfo") == 0) {
        return "";
    }
    initializedRunInfoGroup = true;

    std::string movieName;
    LoadMovieName(movieName);
    Close();
    file.Close();
    return movieName;
}