# KeepNotes blog

Stay hungry, Stay Foolish.

0%

Co-mutation and exclusion analysis in R这篇文章中提到`maftools`包的`somaticInteractions()`函数可以做Co-mutation/exclusion分析及可视化

• 修改maftools包somaticInteractions函数的源码（但是缺少点base作图的技巧，结果不太理想）
• 利用ggplot2来作图（还是没有思路）
• 利用经典相关性可视化的corrplot包（调下参修改下，可能应该可以）

``````mutMat <- read.table(file = "../Desktop/mutMat.txt", sep = "\t", header = T)

interactions = sapply(1:ncol(mutMat), function(i)
sapply(1:ncol(mutMat), function(j) {
f<- try(fisher.test(mutMat[,i], mutMat[,j]), silent=TRUE)
if(class(f)=="try-error"){
NA
}else{
ifelse(f\$estimate>1, -log10(f\$p.val), log10(f\$p.val))
}
}))

oddsRatio <- oddsGenes <- sapply(1:ncol(mutMat), function(i)
sapply(1:ncol(mutMat), function(j) {
f<- try(fisher.test(mutMat[,i], mutMat[,j]), silent=TRUE)
if(class(f)=="try-error"){
NA
}else{
f\$estimate
}
}))

diag(interactions) <- NA
diag(oddsRatio) <- NA
colnames(oddsRatio) <- rownames(oddsRatio) <- colnames(interactions) <- rownames(interactions) <- colnames(mutMat)
oddsRatio[10^-abs(interactions) > 0.05] = 1
oddsRatio[oddsRatio < 0.1] = 0.1
oddsRatio[oddsRatio > 10] = 10

par(bty="n", mgp = c(2,.5,0), mar=rep(4,4)+.1, las=2, tcl=-.33)
m <- nrow(oddsRatio)
n <- ncol(oddsRatio)
r <- log10(oddsRatio)
r[lower.tri(r)] <- NA
image(x=1:n, y=1:m, r, col=brewer.pal(9,"PiYG"), breaks = c(seq(from = log10(0.1), to = 0-.Machine\$double.eps, length.out = 5),
seq(from = 0, to = log10(10), length.out = 5)),
xaxt="n", yaxt="n", xlab="",ylab="", xlim=c(0, n+3), ylim=c(0, n+3))

abline(h=0:n+.5, col="white", lwd=.5)
abline(v=0:n+.5, col="white", lwd=.5)

pvalue <- c(0.05, 0.01)
text(x=n/2, y=m+1, "Genetic interactions", pos=3, cex=1.3)
w0 <- arrayInd(which(as.vector(upper.tri(r)) == TRUE), rep(m, 2))
w = arrayInd(which(10^-abs(interactions) < min(pvalue)), rep(m,2))
points(dplyr::inner_join(data.frame(w0), data.frame(w)), pch="*", col="black")
w = arrayInd(which(10^-abs(interactions) < max(pvalue)), rep(m,2))
points(dplyr::inner_join(data.frame(w0), data.frame(w)), pch=".", col="black", cex = 2)

image(y = 3:10 +6, x=rep(n,2)+c(2,2.5), z=matrix(c(1:8), nrow=1), col=brewer.pal(8,"PiYG"), add=TRUE)
image(y = 3:10 +6, x=rep(n,2)+c(2,2.5)+1, z=matrix(c(1:8), nrow=1), col=brewer.pal(8,"PiYG"), add=TRUE)
axis(side = 4, at = seq(3,11) + 5.5,  tcl=-0.15, label=signif(10^seq(-1,1,length.out = 9), 2), las=1, lwd=.5, cex.axis=1)
mtext(side=4, at=12, "Odds ratio", las=3, line=3)
par(xpd=NA)
text(x=n+1.2, y=17.5, "Correlated", pos=4)
text(x=n+1.2, y=8-.7, "Exclusive", pos=4)
points(x=rep(n,2)+2, y=c(1,2.5), pch=c("*","."))