File: MappingMetrics.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 (297 lines) | stat: -rw-r--r-- 9,488 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <alignment/MappingMetrics.hpp>

#include <iomanip>

#include <unistd.h>
//In order to use clock_gettime in LINUX, add -lrt

#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
int my_clock_gettime_(clockid_t clk_id, struct timespec *tp)
{
    kern_return_t ret;
    clock_serv_t clk;
    clock_id_t clk_serv_id;
    mach_timespec_t tm;

    uint64_t start, end, delta, nano;

    //task_basic_info_data_t tinfo;
    //task_thread_times_info_data_t ttinfo;
    //mach_msg_type_number_t tflag;

    int retval = -1;
    switch (clk_id) {
        case CLOCK_REALTIME:
        case CLOCK_MONOTONIC:
            clk_serv_id = clk_id == CLOCK_REALTIME ? CALENDAR_CLOCK : SYSTEM_CLOCK;
            if (KERN_SUCCESS ==
                (ret = host_get_clock_service(mach_host_self(), clk_serv_id, &clk))) {
                if (KERN_SUCCESS == (ret = clock_get_time(clk, &tm))) {
                    tp->tv_sec = tm.tv_sec;
                    tp->tv_nsec = tm.tv_nsec;
                    retval = 0;
                }
            }
            if (KERN_SUCCESS != ret) {
                errno = EINVAL;
                retval = -1;
            }
            break;
        case CLOCK_PROCESS_CPUTIME_ID:
        case CLOCK_THREAD_CPUTIME_ID:
            start = mach_absolute_time();
            if (clk_id == CLOCK_PROCESS_CPUTIME_ID) {
                getpid();
            } else {
                sched_yield();
            }
            end = mach_absolute_time();
            delta = end - start;
            if (0 == __clock_gettime_inf.denom) {
                mach_timebase_info(&__clock_gettime_inf);
            }
            nano = delta * __clock_gettime_inf.numer / __clock_gettime_inf.denom;
            tp->tv_sec = nano * 1e-9;
            tp->tv_nsec = nano - (tp->tv_sec * 1e9);
            retval = 0;
            break;
        default:
            errno = EINVAL;
            retval = -1;
    }
    return retval;
}
#define clock_gettime my_clock_gettime_
#endif  // MAC_OS_X_VERSION_MAX_ALLOWED < 101200
#endif  // __APPLE__

Timer::Timer(std::string _header)
{
    keepHistogram = false;
    keepList = false;
    totalElapsedClock = 0;
    header = _header;
    elapsedClockMsec = 0;
    elapsedTime = 0.0;
}

int Timer::ListSize() { return msecList.size(); }

void Timer::PrintHeader(std::ostream &out)
{
    if (msecList.size() > 0) {
        out << header << " ";
    }
}

void Timer::PrintListValue(std::ostream &out, int index)
{
    if (msecList.size() > 0) {
        out << msecList[index] << " ";
    }
}
void Timer::Tick() { clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cpuclock[0]); }

void Timer::SetStoreElapsedTime(bool value) { keepList = value; }

void Timer::SetStoreHistgram(bool value) { keepHistogram = value; }

void Timer::Tock()
{
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cpuclock[1]);
    elapsedClockMsec = (cpuclock[1].tv_nsec - cpuclock[0].tv_nsec) / 1000;
    totalElapsedClock += elapsedClockMsec;
    elapsedTime = ((1.0) * elapsedClockMsec);
    if (keepHistogram) {
        // keep a histogram in number of milliseconds per operation
        if (histogram.find(elapsedClockMsec) == histogram.end()) {
            histogram[elapsedClockMsec] = 1;
        } else {
            histogram[elapsedClockMsec]++;
        }
    }
    if (keepList) {
        msecList.push_back(elapsedClockMsec);
    }
}

void Timer::Add(const Timer &rhs)
{
    elapsedClockMsec += rhs.elapsedClockMsec;
    elapsedTime += rhs.elapsedTime;
    totalElapsedClock += rhs.totalElapsedClock;
    msecList.insert(msecList.end(), rhs.msecList.begin(), rhs.msecList.end());
}

void Timer::SetHeader(std::string _header) { header = _header; }

void MappingClocks::AddCells(int nCells) { nCellsPerSample.push_back(nCells); }

void MappingClocks::AddBases(int nBases) { nBasesPerSample.push_back(nBases); }

int MappingClocks::GetSize() { return total.ListSize(); }

MappingClocks::MappingClocks()
{
    total.SetHeader("Total");
    findAnchors.SetHeader("FindAnchors");
    mapToGenome.SetHeader("MapToGenome");
    sortMatchPosList.SetHeader("SortMatchPosList");
    findMaxIncreasingInterval.SetHeader("FindMaxIncreasingInterval");
    alignIntervals.SetHeader("AlignIntervals");
}

void MappingClocks::PrintHeader(std::ostream &out)
{
    total.PrintHeader(out);
    findAnchors.PrintHeader(out);
    mapToGenome.PrintHeader(out);
    sortMatchPosList.PrintHeader(out);
    findMaxIncreasingInterval.PrintHeader(out);
    alignIntervals.PrintHeader(out);
}

void MappingClocks::PrintList(std::ostream &out, int index)
{

    total.PrintListValue(out, index);
    findAnchors.PrintListValue(out, index);
    mapToGenome.PrintListValue(out, index);
    sortMatchPosList.PrintListValue(out, index);
    findMaxIncreasingInterval.PrintListValue(out, index);
    alignIntervals.PrintListValue(out, index);
    if (nCellsPerSample.size() > 0) {
        out << nCellsPerSample[index] << " ";
    }
    if (nBasesPerSample.size() > 0) {
        out << nBasesPerSample[index] << " ";
    }
    out << std::endl;
}

void MappingClocks::SetStoreList(bool value)
{
    total.SetStoreElapsedTime(value);
    findAnchors.SetStoreElapsedTime(value);
    mapToGenome.SetStoreElapsedTime(value);
    sortMatchPosList.SetStoreElapsedTime(value);
    findMaxIncreasingInterval.SetStoreElapsedTime(value);
    alignIntervals.SetStoreElapsedTime(value);
}

void MappingClocks::AddClockTime(const MappingClocks &rhs)
{
    total.Add(rhs.total);
    findAnchors.Add(rhs.findAnchors);
    mapToGenome.Add(rhs.mapToGenome);
    sortMatchPosList.Add(rhs.sortMatchPosList);
    findMaxIncreasingInterval.Add(rhs.findMaxIncreasingInterval);
    alignIntervals.Add(rhs.alignIntervals);
}

MappingMetrics::MappingMetrics()
{
    numReads = 0;
    numMappedReads = 0;
    numMappedBases = 0;
    anchorsPerRead = 0;
    totalAnchorsForMappedReads = 0;
    totalAnchors = 0;
}
void MappingMetrics::StoreSDPPoint(int nBases, int nSDPAnchors, int nClock)
{
    sdpBases.push_back(nBases);
    sdpAnchors.push_back(nSDPAnchors);
    sdpClock.push_back(nClock);
}

void MappingMetrics::SetStoreList(bool value) { clocks.SetStoreList(value); }

void MappingMetrics::PrintSeconds(std::ostream &out, long sec) { out << sec << " Msec"; }

void MappingMetrics::PrintFraction(std::ostream &out, float frac)
{
    out << std::setprecision(2) << frac;
}

void MappingMetrics::RecordNumAlignedBases(int nBases) { mappedBases.push_back(nBases); }

void MappingMetrics::RecordNumCells(int nCells) { cellsPerAlignment.push_back(nCells); }

void MappingMetrics::Collect(MappingMetrics &rhs)
{
    clocks.AddClockTime(rhs.clocks);
    totalAnchors += rhs.totalAnchors;
    numReads += rhs.numReads;
    numMappedReads += rhs.numMappedReads;
    totalAnchorsForMappedReads += rhs.totalAnchorsForMappedReads;
    mappedBases.insert(mappedBases.end(), rhs.mappedBases.begin(), rhs.mappedBases.end());
    cellsPerAlignment.insert(cellsPerAlignment.end(), rhs.cellsPerAlignment.begin(),
                             rhs.cellsPerAlignment.end());
}

void MappingMetrics::CollectSDPMetrics(MappingMetrics &rhs)
{
    sdpAnchors.insert(sdpAnchors.end(), rhs.sdpAnchors.begin(), rhs.sdpAnchors.end());
    sdpBases.insert(sdpBases.end(), rhs.sdpBases.begin(), rhs.sdpBases.end());
    sdpClock.insert(sdpClock.end(), rhs.sdpClock.begin(), rhs.sdpClock.end());
}

void MappingMetrics::PrintSDPMetrics(std::ostream &out)
{
    out << "nbases ncells time" << std::endl;
    for (size_t i = 0; i < sdpAnchors.size(); i++) {
        out << sdpBases[i] << " " << sdpAnchors[i] << " " << sdpClock[i] << std::endl;
    }
}

void MappingMetrics::PrintFullList(std::ostream &out)
{
    //
    // Print the full header
    //
    clocks.PrintHeader(out);
    out << " MappedBases Cells " << std::endl;
    //
    // Print all values: clocks + bases and cells.
    //
    int i;
    for (i = 0; i < clocks.GetSize(); i++) {
        clocks.PrintList(out, i);
        //      out << mappedBases[i] << " " << cellsPerAlignment[i] << std::endl;
    }
}

void MappingMetrics::PrintSummary(std::ostream &out)
{
    out << "Examined " << numReads << std::endl;
    out << "Mapped   " << numMappedReads << std::endl;
    out << "Total mapping time\t";
    PrintSeconds(out, clocks.total.elapsedClockMsec);
    out << " \t";
    PrintSeconds(out, (1.0 * clocks.total.elapsedClockMsec) / numReads);
    out << " /read" << std::endl;
    out << "      find anchors\t";
    PrintSeconds(out, clocks.mapToGenome.elapsedClockMsec);
    out << " \t";
    PrintSeconds(out, (1.0 * clocks.mapToGenome.elapsedClockMsec) / numReads);
    out << std::endl;
    out << "      sort anchors\t";
    PrintSeconds(out, clocks.sortMatchPosList.elapsedClockMsec);
    out << " \t";
    PrintSeconds(out, (1.0 * clocks.sortMatchPosList.elapsedClockMsec) / numReads);
    out << std::endl;
    out << " find max interval\t";
    PrintSeconds(out, clocks.findMaxIncreasingInterval.elapsedClockMsec);
    out << " \t";
    PrintSeconds(out, (1.0 * clocks.findMaxIncreasingInterval.elapsedClockMsec) / numReads);
    out << std::endl;
    out << "Total anchors: " << totalAnchors << std::endl;
    out << "   Anchors per read: " << (1.0 * totalAnchors) / numReads << std::endl;
    out << "Total mapped: " << totalAnchorsForMappedReads << std::endl;
    out << "   Anchors per mapped read: " << (1.0 * totalAnchorsForMappedReads) / numMappedReads
        << std::endl;
}

void MappingMetrics::AddClock(MappingClocks &clocks) { clocks.AddClockTime(clocks); }