-
Notifications
You must be signed in to change notification settings - Fork 25
/
lengthdistributionanalysis.cpp
188 lines (144 loc) · 4.99 KB
/
lengthdistributionanalysis.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
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
#include "lengthdistributionanalysis.h"
LengthDistributionAnalysis::LengthDistributionAnalysis(QObject * parent)
:Analysis(parent)
{
setName(tr("Sequence Length Distribution"));
setDescription(tr("Shows the distribution of sequence length over all sequences"));
}
void LengthDistributionAnalysis::processSequence(const Sequence &sequence)
{
int seqLen = sequence.size();
if (seqLen + 2 > mLengthCounts.length())
mLengthCounts.resize(seqLen + 2);
++mLengthCounts[seqLen];
}
void LengthDistributionAnalysis::reset()
{
mLengthCounts.clear();
mGraphCounts.clear();
}
QWidget *LengthDistributionAnalysis::createResultWidget()
{
QChartView * view = new QChartView;
view->setRenderHint(QPainter::Antialiasing);
QLineSeries * serie = new QLineSeries;
if (mGraphCounts.isEmpty())
return view;
int yMax = 0;
auto graph_count_it = mGraphCounts.begin();
for (int i=mMin; i < mMax+mIntervale; i += mIntervale)
{
serie->append(i, *graph_count_it);
if(yMax < *graph_count_it)
yMax = *graph_count_it;
graph_count_it++;
}
QChart * chart = new QChart ;
chart->addSeries(serie);
/* Create fake series for set the chart dimension */
QLineSeries * fakeSerie = new QLineSeries();
fakeSerie->append(0, 0);
fakeSerie->append(mGraphCounts.size(), yMax);
fakeSerie->setVisible(false);
chart->addSeries(fakeSerie);
chart->createDefaultAxes();
chart->axisX()->setTitleText(tr("Position in read (bp)"));
chart->axisY()->setTitleText(tr("Sequence count"));
/* Set label of axis */
dynamic_cast<QValueAxis*>(chart->axisX())->setMin(mMin);
dynamic_cast<QValueAxis*>(chart->axisX())->setLabelFormat("%d");
dynamic_cast<QValueAxis*>(chart->axisY())->setLabelFormat("%d");
if (mGraphCounts.size() < 5)
dynamic_cast<QValueAxis*>(chart->axisX())->setTickCount(mGraphCounts.size());
chart->setTitle(tr("Distribution of sequence length over all sequences"));
chart->setAnimationOptions(QChart::NoAnimation);
// add Actions
view->setRubberBand(QChartView::HorizontalRubberBand);
QAction * zoomReset = new QAction(QFontIcon::icon(0xf002), tr("Zoom reset"), view);
QAction * zoomIn = new QAction(QFontIcon::icon(0xf00e), tr("Zoom in"), view);
QAction * zoomOut = new QAction(QFontIcon::icon(0xf010), tr("Zoom out"), view);
connect(zoomReset, &QAction::triggered, [chart](){chart->zoomReset();});
connect(zoomIn, &QAction::triggered, [chart](){chart->zoomIn();});
connect(zoomOut, &QAction::triggered, [chart](){chart->zoomOut();});
view->addAction(zoomReset);
view->addAction(zoomIn);
view->addAction(zoomOut);
view->setChart(chart);
return view;
}
void LengthDistributionAnalysis::after()
{
computeDistribution();
}
void LengthDistributionAnalysis::computeDistribution()
{
int maxLen = 0;
int minLen = -1;
// qDebug()<<mLengthCounts.length();
// Find the min and max lengths
for (int i=0;i<mLengthCounts.length();i++) {
if (mLengthCounts[i]>0) {
if (minLen < 0) {
minLen = i;
}
maxLen = i;
}
}
// We put one extra category either side of the actual size
if (minLen>0) minLen--;
maxLen++;
QVector<int> startAndInterval = sizeDistribution(minLen, maxLen);
// Work out how many categories we need
int categories = 0;
int currentValue = startAndInterval[0];
while (currentValue<= maxLen) {
++categories;
currentValue+= startAndInterval[1];
}
mGraphCounts.resize(categories);
for (int i=0;i<mGraphCounts.length();i++) {
int minValue = startAndInterval[0]+(startAndInterval[1]*i);
int maxValue = (startAndInterval[0]+(startAndInterval[1]*(i+1)))-1;
if (maxValue > maxLen) {
maxValue = maxLen;
}
for (int bp=minValue;bp<=maxValue;bp++) {
if (bp < mLengthCounts.length()) {
mGraphCounts[i] += mLengthCounts[bp];
}
}
if (mGraphCounts[i] > mMax) mMax = mGraphCounts[i];
}
mIntervale = startAndInterval[1];
mMin = minLen;
mMax = maxLen;
}
QVector<int> LengthDistributionAnalysis::sizeDistribution(int min, int max)
{
int base = 1;
while (base > (max-min)) {
base /= 10;
}
int interval =0;
int starting =0;
QVector<int> divisions ={1,2,5};
// Little hack to exit the while loop..
bool loop = true;
while (loop) {
for (int d=0;d<divisions.length();d++) {
int tester = base * divisions[d];
if (((max-min) / tester) <= 50) {
interval = tester;
loop = false;
break;
}
}
if (loop == true)
base *=10;
}
// // Now we work out the first value to be plotted
int basicDivision = (int)(min/interval);
int testStart = basicDivision * interval;
starting = testStart;
return {starting,interval};
}