-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCoverageHistogram.m
304 lines (247 loc) · 15.1 KB
/
CoverageHistogram.m
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
298
299
300
301
302
303
304
//
// CoverageHistogram.m
// iGenomics
//
// Created by Stuckinaboot Inc. on 7/2/14.
//
//
#import "CoverageHistogram.h"
@implementation CoverageHistogram
- (void)createHistogramWithMaxCovVal:(int)maxCovVal andNumOfReads:(int)numOfReads andReadLen:(int)readLen andGenomeLen:(int)genomeLen {
self.view.backgroundColor = [UIColor whiteColor];
// Remove imgView if it previously existed
if (imgView != NULL) {
imgView.image = NULL;
[imgView removeFromSuperview];
}
// Create new image view
// Using kIPhonePopoverNavBarLandscapeHeight is a hacky way to ensure
// that the coverage histogram will be sized accounting for the nav bar
// in the view which it will be displayed in
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kIPhonePopoverNavBarLandscapeHeight)];
[self.view addSubview:imgView];
dnaColors = [[DNAColors alloc] init];
[dnaColors setUp];
maxCoverageVal = maxCovVal;
imgView.image = NULL;
CGRect rect = CGRectMake(0, 0, imgView.bounds.size.width, imgView.bounds.size.height);
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);//0.0 sets the scale factor to the scale of the device's main screen
// else
// UIGraphicsBeginImageContext(rect.size);
[imgView.image drawInRect:rect];
[self drawPlotInRect:rect];
[self drawAxisesInRect:rect];
[self drawNormalCurveInRect:rect];
imgView.image = UIGraphicsGetImageFromCurrentImageContext();//Might need to use drawRect for setting the img, depending on performance
UIGraphicsEndImageContext();
[self drawSideInformationWithNumOfReads:numOfReads andReadLen:readLen andGenomeLen:genomeLen];
}
- (void)drawSideInformationWithNumOfReads:(int)numOfReads andReadLen:(int)readLen andGenomeLen:(int)genomeLen {
NSArray *startStrings = [NSArray arrayWithObjects:kNumOfReadsLblStart, kReadLengthLblStart, kGenomeLengthLblStart, nil];
NSArray *numVals = [NSArray arrayWithObjects:[NSNumber numberWithInt:numOfReads], [NSNumber numberWithInt:readLen], [NSNumber numberWithInt:genomeLen], nil];
float y = 0;
for (int i = 0; i < [startStrings count]; i++) {
UILabel *lbl = [[UILabel alloc] init];
lbl.text = [NSString stringWithFormat:@"%@%i",[startStrings objectAtIndex:i],[[numVals objectAtIndex:i] intValue]];
lbl.font = [UIFont systemFontOfSize:kCoverageHistogramAxisFontSize];
CGSize size = [lbl.text sizeWithFont:lbl.font];
lbl.frame = CGRectMake(self.view.frame.size.width-size.width, y, size.width, size.height);
[self.view addSubview:lbl];
y += size.height;
}
}
- (void)drawNormalCurveInRect:(CGRect)rect {
float maxNormVal = [self normalCurveFormulaValueForPos:posOfHighestFrequency];
float x = kCoverageHistogramYAxisDistFromScreenLeft+kCoverageHistogramAxisWidth+boxWidth/2;
float y = rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom-kCoverageHistogramAxisWidth;
float i = 0;
float bw = (rect.size.width-kCoverageHistogramYAxisDistFromScreenLeft)/(maxCoverageVal+1);
boxWidth = bw;
int addFactor = boxWidth;
if (bw < 1) {
addFactor = (1.0f/bw);
boxWidth = 1;
}
// while (i*addFactor < rect.size.width) {
while (i < maxCoverageVal) {
float normalVal = [self normalCurveFormulaValueForPos:i];
float actualYVal = rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom-(normalVal/maxNormVal)*(rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom) * (float)freqAtPosOfHighestFrequency/highestFrequency;
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kCoverageHistogramThinLineWidth);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), x, y);
if (i > 0)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), kCoverageHistogramYAxisDistFromScreenLeft+i*addFactor+addFactor/2, actualYVal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
x = kCoverageHistogramYAxisDistFromScreenLeft+i*addFactor+addFactor/2;
y = actualYVal;
// [self drawRectangle:CGRectMake(kCoverageHistogramYAxisDistFromScreenLeft+i*boxWidth, actualYVal, 1.0f, 1.0f) withRGB:(double[3]){dnaColors.black.r, dnaColors.black.g, dnaColors.black.b}];
i += 1;//step;
}
}
- (float)normalCurveFormulaValueForPos:(float)x {
//Everything in here is following the normal distribution formula as defined here: http://upload.wikimedia.org/math/7/3/a/73ad15f79b11af99bd2477ff3ffc5a35.png
float sd = sqrtf(posOfHighestFrequency);
if (sd == 0)
sd = kCoverageHistogramSDDefault;
float mean = posOfHighestFrequency;
float formulaPart1 = 1/(sd*sqrtf(2*M_PI));//Formula 1st part
float formualaPart2 = pow(x-mean,2); //Formula 2nd part
float formulaPart3 = -1/(2*sd*sd);
return formulaPart1*exp(formualaPart2*formulaPart3);
}
- (void)drawAxisesInRect:(CGRect)rect {
[self drawRectangle:CGRectMake(kCoverageHistogramYAxisDistFromScreenLeft, 0, kCoverageHistogramAxisWidth, rect.size.height) withRGB:(double[3]){dnaColors.black.r,dnaColors.black.g,dnaColors.black.b}];
[self drawRectangle:CGRectMake(0, rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom, rect.size.width, kCoverageHistogramAxisWidth) withRGB:(double[3]){dnaColors.black.r,dnaColors.black.g,dnaColors.black.b}];
}
- (void)drawAxisLblsInRect:(CGRect)rect withCovFreqArr:(int[])covFreqArr andCovFreqArrMax:(int)covFreqArrMax {
//Easier to use UILabels
CGRect titleFrame = CGRectMake(0, 0, kCoverageHistogramLblWidth, kCoverageHistogramLblHeight);
UIFont *axisLblFont = [UIFont systemFontOfSize:kCoverageHistogramAxisFontSize];
UILabel *xTitle = [[UILabel alloc] initWithFrame:titleFrame];
xTitle.center = CGPointMake(kCoverageHistogramYAxisDistFromScreenLeft+((rect.size.width-kCoverageHistogramYAxisDistFromScreenLeft)/2)+xTitle.bounds.size.width/2,rect.size.height-kCoverageHistogramXAxisTitleDistFromScreenEdge);
[xTitle setFont:axisLblFont];
[xTitle setText:kCoverageHistogramAxisXTitle];
[self.view addSubview:xTitle];
UILabel *yTitle = [[UILabel alloc] initWithFrame:titleFrame];
yTitle.center = CGPointMake(kCoverageHistogramYAxisTitleDistFromScreenEdge, ((rect.size.height)/2)-kCoverageHistogramXAxisDistFromScreenBottom);
[yTitle setFont:axisLblFont];
[yTitle setText:kCoverageHistogramAxisYTitle];
[yTitle setTransform:CGAffineTransformMakeRotation(-(M_PI)/2)];
[self.view addSubview:yTitle];
if (posOfHighestFrequency > 0) {
UILabel *x0Lbl = [[UILabel alloc] initWithFrame:CGRectMake(kCoverageHistogramYAxisDistFromScreenLeft+kCoverageHistogramAxisWidth, rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom+kCoverageHistogramAxisWidth, kCoverageHistogramIntervalLblWidth, kCoverageHistogramIntervalLblHeight)];//Corresponds to the maxYLbl
[x0Lbl setText:[NSString stringWithFormat:kCoverageHistogram0IntervalTxt]];
[x0Lbl setAdjustsFontSizeToFitWidth:YES];
[self.view addSubview:x0Lbl];
}
// CGSize lblTxtSize = [kCoverageHistogram0IntervalTxt sizeWithFont:x0Lbl.font];
// UILabel *y0Lbl = [[UILabel alloc] initWithFrame:CGRectMake(kCoverageHistogramYAxisDistFromScreenLeft-kCoverageHistogramIntervalLblWidth, rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom-lblTxtSize.height, kCoverageHistogramIntervalLblWidth, kCoverageHistogramIntervalLblHeight)];//x0Lbl has same font so might as well just do it in the same line
// [y0Lbl setTextAlignment:NSTextAlignmentRight];
// [y0Lbl setText:[NSString stringWithFormat:kCoverageHistogram0IntervalTxt]];
// [y0Lbl setAdjustsFontSizeToFitWidth:YES];
// [self.view addSubview:y0Lbl];
float standardDeviation = sqrtf(posOfHighestFrequency);
BOOL usingDefaultSD = NO;
if (standardDeviation == 0) {
standardDeviation = kCoverageHistogramSDDefault;
usingDefaultSD = YES;
}
UILabel *xLbls[kCoverageHistogramNumOfIntervalLblsPerAxis];
UILabel *yLbls[kCoverageHistogramNumOfIntervalLblsPerAxis];
int currNumOfSD = -kCoverageHistogramNumOfIntervalLblsPerAxis/2.0f;
int x = xValOfPosOfHighestFrequency+(standardDeviation*currNumOfSD)*boxWidth+kCoverageHistogramAxisWidth;
for (int i = 0; i < kCoverageHistogramNumOfIntervalLblsPerAxis; i++, x += standardDeviation*boxWidth, currNumOfSD++) {
xLbls[i] = [[UILabel alloc] initWithFrame:CGRectMake(x, rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom+kCoverageHistogramAxisWidth, kCoverageHistogramIntervalLblWidth, kCoverageHistogramIntervalLblHeight)];//Corresponds to the maxYLbl
xLbls[i].center = CGPointMake(x, xLbls[i].center.y);
[xLbls[i] setTextAlignment:NSTextAlignmentCenter];
int pos = round(posOfHighestFrequency+standardDeviation*currNumOfSD);
[xLbls[i] setText:[NSString stringWithFormat:@"%i",pos]];
[xLbls[i] setAdjustsFontSizeToFitWidth:YES];
BOOL shouldAddX = YES;
if (i > 0) {
for (int j = 0; j < i; j++)
if (CGRectIntersectsRect(xLbls[j].frame, xLbls[i].frame)) {
shouldAddX = NO;
if (currNumOfSD == 0) {
shouldAddX = YES;
for (int l = 0; l < i; l++)
[xLbls[l] removeFromSuperview];
}
break;
}
}
if (shouldAddX) {
if (posOfHighestFrequency > 0)
[self.view addSubview:xLbls[i]];
else if (usingDefaultSD && maxCoverageVal != 0 && currNumOfSD >= 0)
[self.view addSubview:xLbls[i]];
}
UIFont *font = xLbls[i].font;
if (i <= ceilf(kCoverageHistogramNumOfIntervalLblsPerAxis/2.0f) && pos <= maxCoverageVal && pos >= 0) {//This if is here because at the same SD, there will probably/usually be different frequencies. Also, can't make labels for out of bounds positions
yLbls[i] = [[UILabel alloc] initWithFrame:CGRectMake(0, rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom-((covFreqArr[pos]/(float)highestFrequency)*(rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom)) + kCoverageHistogramIntervalLblHeight / 2, kCoverageHistogramIntervalLblWidth, kCoverageHistogramIntervalLblHeight)];
if (yLbls[i].frame.origin.y > 0)
yLbls[i].frame = CGRectMake(yLbls[i].frame.origin.x, yLbls[i].frame.origin.y-kCoverageHistogramIntervalLblHeight/2, yLbls[i].frame.size.width, yLbls[i].frame.size.height);
[yLbls[i] setText:[NSString stringWithFormat:@"%i",covFreqArr[pos]]];
CGSize size = [yLbls[i].text sizeWithFont:font];
yLbls[i].frame = CGRectMake(kCoverageHistogramYAxisDistFromScreenLeft-size.width, yLbls[i].frame.origin.y, size.width, size.height);
BOOL shouldAddY = YES;
if (i > 0) {
int intersectingLblIndex = -1;
for (int j = 0; j < i; j++)
if (CGRectIntersectsRect(yLbls[j].frame, yLbls[i].frame)) {
shouldAddY = NO;
intersectingLblIndex = j;
break;
}
if (!shouldAddY && currNumOfSD == 0 && CGRectIntersectsRect(yLbls[intersectingLblIndex].frame, yLbls[i].frame)) {
[yLbls[intersectingLblIndex] removeFromSuperview];
shouldAddY = YES;
}
}
if (shouldAddY) {
[yLbls[i] setTextAlignment:NSTextAlignmentLeft];
[yLbls[i] setAdjustsFontSizeToFitWidth:YES];
[self.view addSubview:yLbls[i]];
}
}
}
}
- (void)drawPlotInRect:(CGRect)rect {
int covFrequencyArr[maxCoverageVal+1];
for (int i = 0; i <= maxCoverageVal; i++) {
covFrequencyArr[i] = 0;
}
for (int i = 0; i < dgenomeLen; i++) {
int k = coverageArray[i]-posOccArray[kACGTLen+1][i];//Don't count insertions
covFrequencyArr[k]++;
}
int totalFreq = 0;
for (int i = 0; i < dgenomeLen; i++) {
totalFreq += coverageArray[i];
}
highestFrequency = 0;
for (int i = 0; i <= maxCoverageVal; i++) {
if (highestFrequency < covFrequencyArr[i]) {
highestFrequency = covFrequencyArr[i];
posOfHighestFrequency = i;
}
}
posOfHighestFrequency = round(((float)totalFreq) / dgenomeLen);
freqAtPosOfHighestFrequency = covFrequencyArr[posOfHighestFrequency];
float bw = (rect.size.width-kCoverageHistogramYAxisDistFromScreenLeft)/(maxCoverageVal+1);
boxWidth = bw;
int addFactor = 1;
if (bw < 1) {
addFactor = (1.0f/bw);
boxWidth = 1;
}
for (int i = 0, x = kCoverageHistogramYAxisDistFromScreenLeft; i <= maxCoverageVal; i += addFactor, x += boxWidth) {
float ratio = ((float)covFrequencyArr[i]/highestFrequency);
// float ratio = ((float)covFrequencyArr[i]/covFrequencyArr[posOfHighestFrequency]);
int height = ratio*(rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom);
[self drawRectangle:CGRectMake(x, rect.size.height-kCoverageHistogramXAxisDistFromScreenBottom-height, boxWidth, height) withRGB:(double[3]){dnaColors.mutHighlight.r,dnaColors.mutHighlight.g,dnaColors.mutHighlight.b}];
if (i + addFactor > posOfHighestFrequency && i - addFactor < posOfHighestFrequency)
xValOfPosOfHighestFrequency = x+boxWidth/2;
}
[self drawAxisLblsInRect:rect withCovFreqArr:covFrequencyArr andCovFreqArrMax:maxCoverageVal];
[self drawDashedLineOnHighestFreqOccInRect:rect];
}
- (void)drawDashedLineOnHighestFreqOccInRect:(CGRect)rect {
// CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kCoverageHistogramThinLineWidth);
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor blackColor] CGColor]);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), kCoverageHistogramYAxisDistFromScreenLeft+boxWidth*posOfHighestFrequency+boxWidth/2, 0);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), kCoverageHistogramYAxisDistFromScreenLeft+boxWidth*posOfHighestFrequency+boxWidth/2, rect.size.height-kCoverageHistogramAxisWidth-kCoverageHistogramXAxisDistFromScreenBottom);
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 0, (CGFloat[2]){kCoverageHistogramDashLength, kCoverageHistogramDashLength}, kCoverageHistogramThinLineWidth);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextRestoreGState(UIGraphicsGetCurrentContext());
}
- (void)drawRectangle:(CGRect)rect withRGB:(double[3])rgb {
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), rgb[0], rgb[1], rgb[2], 1.0f);
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
@end