-
Notifications
You must be signed in to change notification settings - Fork 0
/
BamStatCalculator.cpp
70 lines (60 loc) · 1.46 KB
/
BamStatCalculator.cpp
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
#include "BamStatCalculator.h"
#include "error.h"
#include <numeric>
#include <cmath>
#include <algorithm>
using namespace std;
using namespace BamTools;
BamStatCalculator::BamStatCalculator(const string &filename) :
insertMean(-1), insertSd(-1)
{
if (!reader.Open(filename))
error("Could not open the input BAM file.");
loadInserts();
}
BamStatCalculator::~BamStatCalculator()
{
reader.Close();
}
int BamStatCalculator::getInsertMean()
{
if (insertMean == -1) {
insertMean = mean();
}
return insertMean;
}
int BamStatCalculator::getInsertSd()
{
if (insertSd == -1) {
insertSd = sd();
}
return insertSd;
}
void BamStatCalculator::loadInserts()
{
BamAlignment al;
size_t cnt = 0;
while (reader.GetNextAlignmentCore(al) && cnt < 10000)
{
if (al.IsProperPair() && al.MatePosition > al.Position)
{
uint64_t insert = al.MatePosition + al.Length - al.Position;
if (insert < 10000) {
inserts.push_back(insert);
cnt++;
}
}
}
}
int BamStatCalculator::mean()
{
return accumulate(inserts.begin(), inserts.end(), 0) / inserts.size();
}
int BamStatCalculator::sd()
{
int m = getInsertMean();
vector<int> temp;
transform(inserts.begin(), inserts.end(), back_inserter(temp), [](int x) { return x*x; });
uint32_t sum = accumulate(temp.begin(), temp.end(), 0);
return sqrt( sum / temp.size() - m * m);
}