-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetOptimizationCharts.m
278 lines (213 loc) · 9.88 KB
/
getOptimizationCharts.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
function [powerPlot, excludedPlot, biasPlot, costPlot, allowedPlot, cheapestExperiment, mostPowerfulExperiment, experimentsEvaluated, pwrsCalc] = getOptimizationCharts(...
maxReps, probSequencedC, mVControl, vVControl, mVTest, vVTest, fc, pCut, costPerRepControl, costPerRepTest, ...
costPerMillionReads, totalBudget, minReadsPerRep, maxReadsPerRep, minPercUnBiasedGenes, minPercDetected, pwrBiasCutoff, nRepsOriginal )
%{
Gets the optimization charts
The program creates an optimization table that has the power
based on the variance and number of sequencing depths.
The probability of being sequenced is based on
taking a random sample of 250 probailities. This gives a
sample at a fairly evenly distributed interval without any
assumptions about the underlying distribution.
Because the table is representative of the total experiment the
mean of that table gives the power for the total experiment.
%}
out='Running optimization charts.'
costPerRead=costPerMillionReads/10^6;
nReps=2:maxReps;
nReadsAllowedPerRep=Inf;
if minReadsPerRep>maxReadsPerRep
minReadsPerRep=maxReadsPerRep/10;
end
inc=(maxReadsPerRep-minReadsPerRep)/9;
readsS=round(minReadsPerRep:inc:maxReadsPerRep);
%Set up dummy
pwrsCalcA=zeros(length(nReps), length(readsS));
pwrBiasesA=zeros(length(nReps), length(readsS));
%Sample the probabilities of being sequenced
%These are used to calculate a probability randomly distributed
%
seqProbs=randsample(probSequencedC,250); %same prob for each rep, perpetuates random sampling effects but prevents inconsistent results.
for j=1:length(nReps)
nReplicates=nReps(j);
nRepsControl=nReplicates;
nRepsTest=nReplicates;
parfor k=1:length(readsS)
readsPerRep=readsS(k);
seqDepths=seqProbs.*readsPerRep; %The random sequencing depths to test
seqDepths=seqDepths(seqDepths>(1/(nRepsOriginal)) ); %Only include seqDepths that would have been observed in the original dataset
[ powerByReadDepth] = getPowerByReadDepth( mVControl, vVControl, mVTest, vVTest, seqDepths, nRepsControl, nRepsTest, fc, pCut, 1 );
[ powerByReadDepthNoPoisson ] = getPowerByReadDepth( mVControl, vVControl, mVTest, vVTest, seqDepths, nRepsControl, nRepsTest, fc, pCut, 0 );
[ totalPwr ] = mean(mean(powerByReadDepth));
pwrBias=getPwrBias( powerByReadDepth,powerByReadDepthNoPoisson, pwrBiasCutoff );
pwrsCalcA(j,k)=totalPwr;
pwrBiasesA(j, k)=pwrBias;
end
end
pwrsCalc=pwrsCalcA*100;
pwrBiases=pwrBiasesA*100;
%Create cost matrix
for i=1:length(nReps)
for j=1:length(readsS)
costMatrix(i,j)=(costPerRepControl*nReps(i))+(costPerRepTest*nReps(i)) + readsS(j)*nReps(i)*2*costPerRead;
end
end
readsSX=readsS/10^6;
%======================================================
%Make exclusion plot
%======================================================
excludedPlot=figure;
optimizationExclusions=ones(size(pwrsCalc));
imagesc(readsSX,nReps,pwrsCalc)
colorbar
xlabel('Millions of Reads Aligned to Genes Per Replicate', 'FontSize', 14);
ylabel('Number Reps in Each Condition', 'FontSize', 14);
zlabel('Power')
set(gca,'XTick',readsSX)
colormap bone
hold on
%Add Optimization by total budget
% plot(nReadsAllowedPerRep/10^6, nReps, 'Color', [0 .5 0], 'LineWidth', 3, 'HandleVisibility', 'Off')
%Add PowerBias
plot( -1, -1, 's', 'MarkerFaceColor', [ 0.9137 0.5686 0.3647] , 'MarkerEdgeColor', [ 0.9137 0.5686 0.3647] , 'MarkerSize', 12);
if minPercUnBiasedGenes<Inf
failedPwr=0;
for i=1:length(nReps)
for j=1:length(readsS)
if pwrBiases(i,j)<minPercUnBiasedGenes %if too few genes are measured to good enough power
plot( readsSX(j),nReps(i), 's', 'MarkerFaceColor', [ 0.9137 0.5686 0.3647], 'MarkerEdgeColor', [ 0.9137 0.5686 0.3647] ,'MarkerSize', 12, 'HandleVisibility', 'Off');
optimizationExclusions(i,j)=0;
end
end
end
end
%Add BudgetConstraint
plot( -1, -1, '^','MarkerFaceColor', [0 .5 0], 'MarkerEdgeColor', [0 .5 0], 'MarkerSize', 10);
if totalBudget<Inf
for i=1:length(nReps)
for j=1:length(readsS)
if costMatrix(i,j)>totalBudget
plot(readsSX(j),nReps(i), '^', 'MarkerFaceColor', [0 .5 0],'MarkerEdgeColor', [0 .5 0], 'MarkerSize', 10,'HandleVisibility', 'Off');
optimizationExclusions(i,j)=0;
end
end
end
end
%Add Min Percent Detected
plot( -1, -1, 'o', 'MarkerFaceColor', 'r','MarkerEdgeColor', 'k');
if minPercDetected>0
for i=1:length(nReps)
for j=1:length(readsS)
if pwrsCalc(i,j)<=minPercDetected %if power isn't high enough
plot( readsSX(j),nReps(i), 'o', 'MarkerFaceColor', 'r','MarkerEdgeColor', 'k','HandleVisibility', 'Off');
optimizationExclusions(i,j)=0;
end
end
end
end
titleText=strcat({'% of Genes with a '}, num2str(fc), 'x Fold Change Detected (p<', num2str(pCut), ')');
title(titleText, 'FontSize', 14)
legend( 'Measurements Too Biased','Too Expensive', 'Insufficient Power', 'Location', 'SouthOutside')
set(gca,'YDir', 'normal')
powerPlot=figure;
imagesc(readsSX,nReps,pwrsCalc)
xlabel('Millions of Reads Aligned to Genes Per Replicate', 'FontSize', 14);
ylabel('Number Reps in Each Condition', 'FontSize', 14);
zlabel('Power')
colormap bone;
set(gca,'YDir', 'normal')
set(gca,'XTick',readsSX)
titleText=strcat({'% Genes with a '}, num2str(fc), 'x Fold Change Detected (p<', num2str(pCut), ')');
title(titleText, 'FontSize', 14)
colorbar
biasPlot=figure;
imagesc(readsSX,nReps,pwrBiases)
xlabel('Millions of Reads Aligned to Genes Per Replicate', 'FontSize', 14);
ylabel('Number Reps in Each Condition', 'FontSize', 14);
zlabel('Detection Bias')
colormap copper
set(gca,'YDir', 'normal')
set(gca,'XTick',readsSX)
titleText=strcat({'% of Genes with at Least '}, num2str(pwrBiasCutoff*100),'% of Maximum Power');
title(titleText, 'FontSize', 14)
colorbar
costPlot=figure;
imagesc(readsSX,nReps,costMatrix)
hold on
xlabel('Millions of Reads Aligned to Genes Per Replicate', 'FontSize', 14);
ylabel('Number Reps in Each Condition', 'FontSize', 14);
zlabel('Cost')
colormap(moneyscale)
set(gca,'YDir', 'normal')
set(gca,'XTick',readsSX)
title('Cost of Each Experimental Configuration', 'FontSize', 14)
cb=colorbar;
%=======================================================
%Get final optimization chart
%=======================================================
%Get best power
pwrsCalcSize=size(pwrsCalc)
bestPwr=max(pwrsCalc(optimizationExclusions==1));
if isempty(bestPwr)==0
[rp rd]=find(pwrsCalc==bestPwr);
bestReadDepth=readsS(rd(1));
bestNReps=nReps(rp(1));
else
bestReadDepth=[];
bestNReps=[];
end
mostPowerfulExperiment=[];
if isempty(bestReadDepth)==0
mostPowerfulExperiment(1)=bestReadDepth;
mostPowerfulExperiment(2)=bestNReps;
end
%Get cheapestAllowed
bestPwr=min(costMatrix(optimizationExclusions==1));
if isempty(bestPwr)==0
[rp rd]=find(costMatrix==bestPwr);
bestReadDepth=readsS(rd(1));
bestNReps=nReps(rp(1));
else
bestReadDepth=[];
bestNReps=[];
end
cheapestExperiment=[];
if isempty(bestReadDepth)==0
cheapestExperiment(1)=bestReadDepth;
cheapestExperiment(2)=bestNReps;
end
%Plot final allowed optimization
allowedPlot=figure;
imagesc(readsSX,nReps,optimizationExclusions)
hold on
if length(cheapestExperiment)==2 && length(mostPowerfulExperiment)==2
plot(readsSX(readsS==cheapestExperiment(1)), cheapestExperiment(2), '^', 'MarkerFaceColor',[0 .5 0],'MarkerEdgeColor',[0 .5 0], 'MarkerSize', 12 )
plot(readsSX(readsS==mostPowerfulExperiment(1)), mostPowerfulExperiment(2), 'o', 'MarkerFaceColor', [0 0 .9], 'MarkerEdgeColor',[0 0 .9], 'MarkerSize', 12)
legend('Cheapest Allowed Experiment', 'Most Powerful Allowed Experiment', 'Location', 'SouthOutside' );
end
xlabel('Millions of Reads Aligned to Genes Per Replicate', 'FontSize', 14);
ylabel('Number Reps in Each Condition', 'FontSize', 14);
zlabel('Cost')
colormap([ 1 0 0; 1 1 1])
if sum(sum(optimizationExclusions))==0
colormap([1 0 0]) ;%Paint it black if nothing is allowed
end
minX=min(readsSX)-.5*inc;
maxX=max(readsSX)+.5*inc;
minY=min(nReps)-.5;
maxY=max(nReps)+.5;
for i=1:length(readsSX)-1
x=(readsSX(i)+readsSX(i+1))/2;
plot([x x], [minY maxY],'Color',[0.8 0.8 0.8]);
end
for i=1:length(nReps)-1
y=nReps(i)+.5;
plot([minX maxX] ,[y y],'Color',[0.8 0.8 0.8]);
end
set(gca,'XTick',readsSX)
set(gca,'YDir', 'normal')
title('Allowed (white) and Excluded (red) Experimental Designs', 'FontSize', 14)
experimentsEvaluated=size(pwrsCalc,1)*size(pwrsCalc,2);
out='Experiments Evaluated'
experimentsEvaluated
end