Skip to content

Commit 605df7a

Browse files
committed
get error count for 2
1 parent 8a74ecd commit 605df7a

1 file changed

Lines changed: 180 additions & 1 deletion

File tree

errorAnalysis.r

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ computeEffectiveK <- function (prop){
1515
sumsq = sum(prop^2)
1616
if (sumsq == 0){
1717
return(0)
18-
} else {
18+
} else {
1919
return(1/sumsq)
2020
}
2121
}
@@ -177,3 +177,182 @@ dEploidOutError_3<-function(h.pair, h.pair.true, rel.cost.switch=2, do.plot=FAL
177177
op = op, drop.strain = drop.strain,
178178
possible.permn = possible.permn) )
179179
}
180+
181+
182+
dEploidOutError_2 <-function(h.pair, h.pair.true, rel.cost.switch=2, do.plot=FALSE) {
183+
l <- ncol(h.pair);
184+
n.hap <- nrow(h.pair)
185+
possible.permn = combinat::permn(1:n.hap)
186+
187+
possible.permn[[3]] = c(1,1)
188+
possible.permn[[4]] = c(2,2)
189+
190+
#possible.permn = list()
191+
#possible.permn[[1]] = c(1,1)
192+
#possible.permn[[2]] = c(2,2)
193+
#possible.permn[[3]] = c(1,2)
194+
#possible.permn[[4]] = c(2,1)
195+
196+
n.permn = length(possible.permn)
197+
v<-rep(0, n.permn);
198+
vn<-v;
199+
200+
tb<-array(0, c(n.permn, l));
201+
202+
for ( j in 1:n.permn){
203+
v[j] = sum(h.pair[,1]!=h.pair.true[possible.permn[[j]],1]);
204+
}
205+
206+
#rel.cost.drop.current = 0
207+
ee <- rep(0, n.permn)
208+
same.path <- rep(0, n.permn)
209+
210+
for (i in 2:l) {
211+
# rel.cost.drop.current = rel.cost.drop.current + 1
212+
for ( j in 1:n.permn){
213+
ee[j] = sum(h.pair[,i]!=h.pair.true[possible.permn[[j]],i]);
214+
215+
ones = rep(1, n.permn)
216+
ones[j] = 0
217+
drop.ones = rep(1, n.permn)
218+
drop.ones[j] = 0
219+
if (j<3){
220+
drop.ones[1:2] = 0
221+
} else {
222+
drop.ones[3:4] = 0
223+
}
224+
# tmp <- v + rel.cost.switch * ones
225+
tmp <- v + rel.cost.switch * ones + same.path *drop.ones
226+
227+
vn[j] <- min(tmp) + ee[j]
228+
transit.to = which.min(tmp)
229+
230+
if ( j == transit.to ){
231+
same.path = same.path + 1
232+
} else {
233+
same.path = 0
234+
}
235+
236+
tb[j, i] <- transit.to
237+
}
238+
239+
240+
# if ( rel.cost.drop.current > rel.cost.drop){
241+
# rel.cost.drop.current = 0
242+
# }
243+
v<-vn;
244+
# cat ("site ", i, " cost: ", v,"\n")
245+
}
246+
247+
#decode
248+
wm<-which.min(v);
249+
op<-array(0, c(1,l));
250+
n.s<-0;
251+
252+
if (wm!=0){
253+
n.gt<-sum(h.pair[,l]!=h.pair.true[possible.permn[[wm]],l]);
254+
}
255+
256+
257+
op[l]<-wm;
258+
for (i in l:2) {
259+
wmp<-tb[wm,i];
260+
if (wmp!=wm) n.s<-n.s+1;
261+
262+
if (wmp!=0){
263+
n.gt <- n.gt + sum(h.pair[,i-1] != h.pair.true[possible.permn[[wmp]],i-1]);
264+
}
265+
266+
wm<-wmp;
267+
op[i-1]<-wm;
268+
}
269+
270+
k.eff.permn = rep(0, n.permn)
271+
for (j in 1:n.permn){
272+
k.eff.permn[j] = length(unique(possible.permn[[j]]))
273+
}
274+
k.eff = k.eff.permn[op]
275+
276+
changed.permn.at = which(diff(op[1,]) != 0)
277+
changed.k.eff.at = which(diff(k.eff) != 0)
278+
279+
#print("changed.permn.at")
280+
#print(changed.permn.at)
281+
#print("changed.k.eff.at")
282+
#print(changed.k.eff.at)
283+
284+
285+
drop.strain.permn = rep(0, n.permn)
286+
for (j in 1:n.permn){
287+
dropped = which(!c(1,2) %in% possible.permn[[j]])
288+
if ( length(dropped) > 0 ){
289+
drop.strain.permn[j] = dropped
290+
}
291+
}
292+
drop.strain = drop.strain.permn[op]
293+
294+
# definite.switch = sum(!changed.permn.at %in% changed.k.eff.at)
295+
dropTimes = sum(diff(drop.strain) != 0) #length(changed.k.eff.at)
296+
dropError = sum(drop.strain != 0)
297+
cat("\nDecoding gives:\nNo. switches:\t", n.s - dropTimes, "\nNo. GT errs:\t", n.gt, "\nNo. Drop switches", dropTimes, "\nNo. Drop errs:\t", dropError,"\n");
298+
299+
300+
hap.pair.true.idx = array(unlist(possible.permn[op]), c(2, length(op)))
301+
genotype.error = rep(0, n.hap)
302+
switch.error = rep(0, n.hap)
303+
drop.error = rep(0, n.hap)
304+
for ( j in 1:n.hap ){
305+
wd1 = c()
306+
for ( i in 1:l ){
307+
if ( h.pair[j,i] != h.pair.true[hap.pair.true.idx[j,i],i] ){
308+
wd1 = c(wd1, i)
309+
}
310+
}
311+
genotype.error[j] = length(wd1)
312+
}
313+
314+
switch.error[1] = (sum(diff(hap.pair.true.idx[1,])!=0))
315+
switch.error[2] = (sum(diff(hap.pair.true.idx[2,])!=0))
316+
drop.error[1] = (sum(drop.strain == 1))
317+
drop.error[2] = (sum(drop.strain == 2))
318+
319+
if (do.plot) {
320+
# par(mfrow=c(2,1))
321+
layout(matrix(c(1,1,1,1,2,2), 3, 2, byrow = T))
322+
plot(0,0,type="n", xlab="Position", ylab="", yaxt="n", xlim=c(0,ncol(h.pair)), bty="n", ylim=c(-0.5,1.5));
323+
image(x=1:ncol(h.pair), z=t(rbind(h.pair, rep(0, ncol(h.pair)), h.pair.true)), col=c("white", "black"),
324+
add=T);
325+
del<-which(diff(op[1,])!=0);
326+
if (length(del)>0) {points(x=del, y=rep(0.5, length(del)), pch="|", col="red");}
327+
328+
329+
for ( j in 1:n.hap ){
330+
wd1 = c()
331+
for ( i in 1:l ){
332+
if ( h.pair[j,i] != h.pair.true[hap.pair.true.idx[j,i],i] ){
333+
wd1 = c(wd1, i)
334+
}
335+
}
336+
337+
if (length(wd1)>0) {
338+
points(x=wd1, y=rep(1.2, length(wd1)), pch=25, col=j+1, cex=0.5);
339+
# print(length(wd1))
340+
}
341+
342+
}
343+
plot(0,0,type="n", xlab="Position", ylab="", yaxt="n", xlim=c(0,ncol(h.pair)), bty="n", ylim=c(-0.5,1.5));
344+
# image(x=1:ncol(h.pair), z = t(rbind(op, array(drop.strain,c(1,l)))), col = heat.colors(4), add=T)
345+
image(x=1:ncol(h.pair), z = t(op), col = heat.colors(4), add=T)
346+
}
347+
348+
349+
return (list(switchError = n.s - dropTimes,
350+
mutError = n.gt,
351+
dropError = dropError,
352+
op = op, drop.strain = drop.strain,
353+
possible.permn = possible.permn, tb = tb,
354+
switch.error = switch.error,
355+
drop.error = drop.error,
356+
genotype.error = genotype.error )
357+
)
358+
}

0 commit comments

Comments
 (0)