-
Notifications
You must be signed in to change notification settings - Fork 3
/
Interval.php
236 lines (185 loc) · 5 KB
/
Interval.php
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
<?php
class Interval
{
/** @var int $start
* Start of the interval
*/
protected $start;
/** @var int $end
* End of the interval
*/
protected $end;
/**
* @var int $length
* Interval Length
*/
protected $length;
/** @var double $mean
* Mean of the interval
*/
protected $mean;
/** @var double $std
* Standard Deviation of the interval
*/
protected $std;
/**
* @var int
* Median depth value for the interval
*/
protected $medianDepth;
/** @var double $entropy */
protected $entropy;
/** @var Interval[] $subIntervals */
protected $subIntervals;
/** ---- Superfluous Information ---- */
public $chromosome;
public $gene;
// ---- Helpful variables for entropy calculation ---- //
/** @var int $maxDepth
* Maximum Depth for this interval
*/
protected $maxDepth;
// ----- Helpful variable for median calculation ----- //
protected $medianRemainingCount;
/** @var int $subIntervalsSum */
protected $subIntervalsDepthSum;
public function __construct($start, $end)
{
$this->start = $start + 0; // Converts it to a number
$this->end = $end + 0; // Converts it to a number
$this->length = $this->getEnd() - $this->getStart();
$this->medianRemainingCount = floor($this->length / 2);
$this->mean = 0;
$this->std = 0;
$this->maxDepth = 0;
}
public function calculateMean($depth, $basesAtDepth, $intervalLength)
{
$meanForLine = ($depth * $basesAtDepth) / $intervalLength;
$this->mean += $meanForLine;
}
public function calculateStandardDeviation($depth, $basesAtDepth, $intervalLength)
{
$stdForLine = $basesAtDepth * (pow($depth - $this->mean,2)) / $intervalLength;
/*echo '#bases:'.$basesAtDepth.PHP_EOL;
echo 'Depth:'.$depth.PHP_EOL;
echo 'Mean:'.$this->mean.PHP_EOL;
echo ($depth - $this->mean).PHP_EOL;
echo (pow($depth - $this->mean, 2)).PHP_EOL;
echo 'Interval Length:'.$intervalLength.PHP_EOL.PHP_EOL;*/
$this->std += $stdForLine;
}
public function addSubInterval(Interval $interval)
{
$this->subIntervals[] = $interval;
}
// ---------------------- Median -------------------- //
public function calculateMedian($bases, $depth)
{
$this->medianRemainingCount -= $bases;
if($this->medianRemainingCount <= 0)
{
$this->medianDepth = $depth;
// Reset to high number so this doesn't get set again.
$this->medianRemainingCount = $this->getLength();
}
}
// ---------------------- Entropy ------------------- //
/**
* @param $depth
*
* Used in sub intervals to calculate the maximum depth
*/
public function calculateMaxDepth($depth)
{
$this->maxDepth = max($this->maxDepth, $depth);
}
/**
* Used in the parent interval to sum all sub intervals' maxDepth value
*/
public function calculateMaxDepthSum()
{
$sum = 0;
foreach ($this->getSubIntervals() as $key => $subInterval)
{
$sum += $subInterval->getDepth();
}
$this->subIntervalsDepthSum = $sum;
}
/**
* Used in the parent interval to set the sub Intervals mini entropy
*/
public function calculateMiniEntropy()
{
foreach ($this->getSubIntervals() as $key => $subInterval)
{
if($this->subIntervalsDepthSum == 0)
{
$subInterval->entropy = 0;
continue;
}
$p = $subInterval->getDepth() / $this->subIntervalsDepthSum;
if($p != 0)
{
$subInterval->entropy = -1 * ($p * log($p, 2));
}
else
{
$subInterval->entropy = 0;
}
}
}
/**
* Used in the parent interval to sum all of the sub intervals' mini entropy
*/
public function calculateEntropy()
{
foreach ($this->getSubIntervals() as $key => $subInterval)
{
$this->entropy += $subInterval->entropy;
}
}
/**
* @return int
*/
public function getStart()
{
return $this->start;
}
public function getEnd()
{
return $this->end;
}
public function getLength()
{
return $this->length;
}
public function getSubIntervals()
{
return $this->subIntervals;
}
public function getDepth()
{
return $this->maxDepth;
}
public function getMean()
{
return $this->mean;
}
public function getStandardDeviation()
{
return $this->std;
}
public function getEntropy()
{
return $this->entropy;
}
public function getMaxDepth()
{
return $this->maxDepth;
}
public function getMedianDepth()
{
return $this->medianDepth;
}
}