library(MASS)
library(RColorBrewer)
rf <- colorRampPalette(rev(brewer.pal(11,'Spectral')))
r <- rf(32)

# Default call 
k <- kde2d(log2(1+(all_m0[,33])),log2(1+(all_m0[,17])))
image(k, col=r)



# Adjust binning (interpolate - can be computationally intensive for large datasets)
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
k <- kde2d(log2(1+(a$RPKM_KO_0h.y)),log2(1+(a$RPKM_KO_0h.x)),n=400,lims=c(0,7,0,7))
par(mfrow=c(2,1))
image(k, col=r)
contour(k, add = TRUE,vfont = c("sans serif", "plain"),labcex=0.65,levels=c(0.01,0.03,0.05))     # from base graphics package
k2 <- kde2d(log2(1+(a$RPKM_WT_0h.y)),log2(1+(a$RPKM_WT_0h.x)),n=400,lims=c(0,7,0,7))
image(k2, col=r)
contour(k2, add = TRUE,vfont = c("sans serif", "plain"),labcex=0.65,levels=c(0.01,0.03,0.05))     # from base graphics package
# Contour plot overlayed on heat map image of results
#contour(k, add = TRUE)     # from base graphics package

a2=a[ ((log2(1+a$RPKM_WT_m0.y))>0.5)|((log2(1+a$RPKM_WT_m0.y))<4),]


png("dens_m0_ko_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m0[,32])),log2(1+(all_m0[,16])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
#contour(k2, add = TRUE,labcex=1)     # from base graphics package
contour(k2, add = TRUE,vfont = c("sans serif", "plain"),labcex=0.65)     # from base graphics package
dev.off()

png("dens_m0_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m0[,33])),log2(1+(all_m0[,17])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
#contour(k2, add = TRUE,labcex=1)     # from base graphics package
contour(k2, add = TRUE,vfont = c("sans serif", "plain"),labcex=0.65)     # from base graphics package
dev.off()

png("dens_m1_ko_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m1[,32])),log2(1+(all_m1[,16])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()
png("dens_m1_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m1[,33])),log2(1+(all_m1[,17])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()

png("dens_m2_ko_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m2[,32])),log2(1+(all_m2[,16])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()
png("dens_m2_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m2[,33])),log2(1+(all_m2[,17])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()


png("dens_2h_ko_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_2h[,32])),log2(1+(all_2h[,16])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()
png("dens_2h_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_2h[,33])),log2(1+(all_2h[,17])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()

png("dens_6h_ko_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_6h[,32])),log2(1+(all_6h[,16])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()
png("dens_6h_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_6h[,33])),log2(1+(all_6h[,17])),n=200,lims=c(0,13,0,13))
k2=k
k2$z=log10(k$z+1e-3)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
#image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "bold"),labcex=0.65)     # from base graphics package
dev.off()




#@ diff of log2RPKM
png("dens_m0_diff_ko_minus_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m0[,32]))-log2(1+(all_m0[,33])),log2(1+(all_m0[,16]))-log2(1+(all_m0[,17])),n=200,lims=c(-3,3,-2,2))
k2=k
k2$z=log10(k$z+1e-8)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "plain"),labcex=0.65)     # from base graphics package
dev.off()

png("dens_m1_diff_ko_minus_wt_rna_vs_poly.png",width=1024,height=1024,pointsize = 22)
k <- kde2d(log2(1+(all_m1[,32]))-log2(1+(all_m1[,33])),log2(1+(all_m1[,16]))-log2(1+(all_m1[,17])),n=200,lims=c(-3,3,-2,2))
k2=k
k2$z=log10(k$z+1e-8)
image(k2, col=r,xlab="log2(RPKM_abundance)",ylab="log2(RPKM_translation)")
contour(k2, add = TRUE,vfont = c("sans serif", "plain"),labcex=0.65)     # from base graphics package
dev.off()

#ENSMUSG00000022150 -0.516267828715218 7.12196903296183 0.246853512009843 0.775919770432084 Dab2 2776.27 101.463217537883 130.091033543044 53.5945686645799 79.472002337196 36.5465958058412 46.8582067100981 19.3045232144496 28.6254587403948 41.7024012579696 23.9649909774222

g=all_m0[all_m0$Row.names=="ENSMUSG00000022150",]
print(c(log2(1+(g[,32]))-log2(1+(g[,33])),log2(1+(g[,16]))-log2(1+(g[,17]))))
print(c(log2(1+(g[,32])),log2(1+(g[,16])),log2(1+(g[,33])),log2(1+(g[,17])) ))
5.416245 3.114211 4.641834 2.778624

g=all_m0[all_m0$geneID.x=="Fcgr2b",]
1.651245 5.497498 2.977216 5.716757

# change in cor, RPKMs < 17
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)<4)&((log2(1+a$RPKM_KO_0h.y))<4)&(log2(1+a$RPKM_WT_0h.x)<4)&((log2(1+a$RPKM_WT_0h.y))<4),]
cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
	Pearson's product-moment correlation

data:  log2(1 + a2$RPKM_WT_0h.x) and log2(1 + a2$RPKM_WT_0h.y)
t = 0.50981, df = 8346, p-value = 0.6102
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.01587353  0.02702918
sample estimates:
        cor 
0.005580392 

> > 

	Pearson's product-moment correlation

data:  log2(1 + a2$RPKM_KO_0h.x) and log2(1 + a2$RPKM_KO_0h.y)
t = 0.51662, df = 8346, p-value = 0.6054
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.01579900  0.02710368
sample estimates:
        cor 
0.005654941 


#m1
a=all_m1[(!is.na(all_m1$RPKM_KO_m1.x))&(!is.na(all_m1$RPKM_KO_m1.y)),]
a2=a[ (log2(1+a$RPKM_WT_m1.x)<4)&((log2(1+a$RPKM_WT_m1.y))<4)&(log2(1+a$RPKM_KO_m1.x)<4)&((log2(1+a$RPKM_KO_m1.y))<4),]
cor.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y))
cor.test(log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
Pearson's product-moment correlation

data:  log2(1 + a2$RPKM_WT_m1.x) and log2(1 + a2$RPKM_WT_m1.y)
t = 0.34566, df = 8194, p-value = 0.7296
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.01783294  0.02546652
sample estimates:
        cor 
0.003818581

	Pearson's product-moment correlation

data:  log2(1 + a2$RPKM_KO_m1.x) and log2(1 + a2$RPKM_KO_m1.y)
t = 0.021911, df = 8194, p-value = 0.9825
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.02140810  0.02189198
sample estimates:
         cor 
0.0002420528 

#m2
a=all_m2[(!is.na(all_m2$RPKM_KO_m2.x))&(!is.na(all_m2$RPKM_KO_m2.y)),]
a2=a[ (log2(1+a$RPKM_WT_m2.x)<4)&((log2(1+a$RPKM_WT_m2.y))<4)&(log2(1+a$RPKM_KO_m2.x)<4)&((log2(1+a$RPKM_KO_m2.y))<4),]
cor.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y))
cor.test(log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
	Pearson's product-moment correlation

data:  log2(1 + a2$RPKM_WT_m2.x) and log2(1 + a2$RPKM_WT_m2.y)
t = 0.14771, df = 7808, p-value = 0.8826
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.02050772  0.02384942
sample estimates:
        cor 
0.001671674 

	Pearson's product-moment correlation

data:  log2(1 + a2$RPKM_KO_m2.x) and log2(1 + a2$RPKM_KO_m2.y)
t = 0.44245, df = 7808, p-value = 0.6582
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.01717340  0.02718275
sample estimates:
        cor 
0.005007139 


cor.diff.test = function(x1, x2, y1, y2, method="pearson") {
  cor1 = cor.test(x1, x2, method=method)
  cor2 = cor.test(y1, y2, method=method)

  r1 = cor1$estimate
  r2 = cor2$estimate
  n1 = sum(complete.cases(x1, x2))
  n2 = sum(complete.cases(y1, y2))
  fisher = ((0.5*log((1+r1)/(1-r1)))-(0.5*log((1+r2)/(1-r2))))/((1/(n1-3))+(1/(n2-3)))^0.5

  p.value = (2*(1-pnorm(abs(fisher))))

  result= list(
    "cor1" = list(
      "estimate" = as.numeric(cor1$estimate),
      "p.value" = cor1$p.value,
      "n" = n1
    ),
    "cor2" = list(
      "estimate" = as.numeric(cor2$estimate),
      "p.value" = cor2$p.value,
      "n" = n2
    ),
    "p.value.twosided" = as.numeric(p.value),
    "p.value.onesided" = as.numeric(p.value) / 2
  )
  cat(paste(sep="",
          "cor1: r=", format(result$cor1$estimate, digits=3), ", p=", format(result$cor1$p.value, digits=3), ", n=", result$cor1$n, "\n",
          "cor2: r=", format(result$cor2$estimate, digits=3), ", p=", format(result$cor2$p.value, digits=3), ", n=", result$cor2$n, "\n",
          "diffence: p(one-sided)=", format(result$p.value.onesided, digits=3), ", p(two-sided)=", format(result$p.value.twosided, digits=3), "\n"
  ))
  return(result);
}


cor.diff.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y),log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor1: r=0.00167, p=0.883, n=7810
cor2: r=0.00501, p=0.658, n=7810
diffence: p(one-sided)=0.417, p(two-sided)=0.835



#@RPKMs >2
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>=1)&((log2(1+a$RPKM_KO_0h.y))>=1)&(log2(1+a$RPKM_WT_0h.x)>=1)&((log2(1+a$RPKM_WT_0h.y))>=1),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.187, p=0, n=7185
cor2: r=0.161, p=0, n=7185
diffence: p(one-sided)=0.0531, p(two-sided)=0.106

#m1
a=all_m1[(!is.na(all_m1$RPKM_KO_m1.x))&(!is.na(all_m1$RPKM_KO_m1.y)),]
a2=a[ (log2(1+a$RPKM_WT_m1.x)>=1)&((log2(1+a$RPKM_WT_m1.y))>=1)&(log2(1+a$RPKM_KO_m1.x)>=1)&((log2(1+a$RPKM_KO_m1.y))>=1),]
#cor.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y))
#cor.test(log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
cor.diff.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y),log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
cor1: r=0.14, p=0, n=6981
cor2: r=0.136, p=0, n=6981
diffence: p(one-sided)=0.393, p(two-sided)=0.786

#m2
a=all_m2[(!is.na(all_m2$RPKM_KO_m2.x))&(!is.na(all_m2$RPKM_KO_m2.y)),]
a2=a[ (log2(1+a$RPKM_WT_m2.x)>=1)&((log2(1+a$RPKM_WT_m2.y))>=1)&(log2(1+a$RPKM_KO_m2.x)>=1)&((log2(1+a$RPKM_KO_m2.y))>=1),]
#cor.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y))
#cor.test(log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor.diff.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y),log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor1: r=0.106, p=0, n=7289
cor2: r=0.131, p=0, n=7289
diffence: p(one-sided)=0.0649, p(two-sided)=0.13




#@RPKMs >2 & <65
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>=1)&((log2(1+a$RPKM_KO_0h.y))>=1)&(log2(1+a$RPKM_WT_0h.x)>=1)&((log2(1+a$RPKM_WT_0h.y))>=1)&(log2(1+a$RPKM_KO_0h.x)<=6)&((log2(1+a$RPKM_KO_0h.y))<=6)&(log2(1+a$RPKM_WT_0h.x)<=6)&((log2(1+a$RPKM_WT_0h.y))<=6),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.0686, p=2.62e-07, n=5617
cor2: r=0.0518, p=0.000102, n=5617
diffence: p(one-sided)=0.186, p(two-sided)=0.372

#@RPKMs >1 & <129
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>=1)&((log2(1+a$RPKM_KO_0h.y))>=1)&(log2(1+a$RPKM_WT_0h.x)>=1)&((log2(1+a$RPKM_WT_0h.y))>=1)&(log2(1+a$RPKM_KO_0h.x)<=7)&((log2(1+a$RPKM_KO_0h.y))<=7)&(log2(1+a$RPKM_WT_0h.x)<=7)&((log2(1+a$RPKM_WT_0h.y))<=7),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.0783, p=3.61e-10, n=6397
cor2: r=0.0577, p=3.94e-06, n=6397
diffence: p(one-sided)=0.121, p(two-sided)=0.241

#@RPKMs >2 & <129
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=7)&((log2(1+a$RPKM_KO_0h.y))<=7)&(log2(1+a$RPKM_WT_0h.x)<=7)&((log2(1+a$RPKM_WT_0h.y))<=7),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.0783, p=3.61e-10, n=6397
cor2: r=0.0577, p=3.94e-06, n=6397
diffence: p(one-sided)=0.121, p(two-sided)=0.241
(same)

#@RPKMs >2 & <33
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=5)&((log2(1+a$RPKM_KO_0h.y))<=5)&(log2(1+a$RPKM_WT_0h.x)<=5)&((log2(1+a$RPKM_WT_0h.y))<=5),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.0447, p=0.00336, n=4299
cor2: r=0.0289, p=0.0583, n=4299
diffence: p(one-sided)=0.231, p(two-sided)=0.462

#@RPKMs >2 & <257
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=8)&((log2(1+a$RPKM_KO_0h.y))<=8)&(log2(1+a$RPKM_WT_0h.x)<=8)&((log2(1+a$RPKM_WT_0h.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.111, p=0, n=6810
cor2: r=0.0804, p=3.02e-11, n=6810
diffence: p(one-sided)=0.0367, p(two-sided)=0.0733
#sig

#m1
a=all_m1[(!is.na(all_m1$RPKM_KO_m1.x))&(!is.na(all_m1$RPKM_KO_m1.y)),]
a2=a[ (log2(1+a$RPKM_KO_m1.x)>1)&((log2(1+a$RPKM_KO_m1.y))>1)&(log2(1+a$RPKM_WT_m1.x)>1)&((log2(1+a$RPKM_WT_m1.y))>1)&(log2(1+a$RPKM_KO_m1.x)<=8)&((log2(1+a$RPKM_KO_m1.y))<=8)&(log2(1+a$RPKM_WT_m1.x)<=8)&((log2(1+a$RPKM_WT_m1.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y))
#cor.test(log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
cor.diff.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y),log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
cor1: r=0.0631, p=3.28e-07, n=6549
cor2: r=0.0546, p=9.98e-06, n=6549
diffence: p(one-sided)=0.313, p(two-sided)=0.626

#m2
a=all_m2[(!is.na(all_m2$RPKM_KO_m2.x))&(!is.na(all_m2$RPKM_KO_m2.y)),]
a2=a[ (log2(1+a$RPKM_KO_m2.x)>1)&((log2(1+a$RPKM_KO_m2.y))>1)&(log2(1+a$RPKM_WT_m2.x)>1)&((log2(1+a$RPKM_WT_m2.y))>1)&(log2(1+a$RPKM_KO_m2.x)<=8)&((log2(1+a$RPKM_KO_m2.y))<=8)&(log2(1+a$RPKM_WT_m2.x)<=8)&((log2(1+a$RPKM_WT_m2.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y))
#cor.test(log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor.diff.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y),log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor1: r=0.06, p=5.33e-07, n=6962
cor2: r=0.081, p=1.3e-11, n=6962
diffence: p(one-sided)=0.107, p(two-sided)=0.214




#@RPKMs >5 & <257
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>2)&((log2(1+a$RPKM_KO_0h.y))>2)&(log2(1+a$RPKM_WT_0h.x)>2)&((log2(1+a$RPKM_WT_0h.y))>2)&(log2(1+a$RPKM_KO_0h.x)<=8)&((log2(1+a$RPKM_KO_0h.y))<=8)&(log2(1+a$RPKM_WT_0h.x)<=8)&((log2(1+a$RPKM_WT_0h.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.119, p=1.14e-12, n=3548
cor2: r=0.0955, p=1.22e-08, n=3548
diffence: p(one-sided)=0.158, p(two-sided)=0.316

#m1
a=all_m1[(!is.na(all_m1$RPKM_KO_m1.x))&(!is.na(all_m1$RPKM_KO_m1.y)),]
a2=a[ (log2(1+a$RPKM_KO_m1.x)>2)&((log2(1+a$RPKM_KO_m1.y))>2)&(log2(1+a$RPKM_WT_m1.x)>2)&((log2(1+a$RPKM_WT_m1.y))>2)&(log2(1+a$RPKM_KO_m1.x)<=8)&((log2(1+a$RPKM_KO_m1.y))<=8)&(log2(1+a$RPKM_WT_m1.x)<=8)&((log2(1+a$RPKM_WT_m1.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y))
#cor.test(log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
cor.diff.test(log2(1+a2$RPKM_WT_m1.x),log2(1+a2$RPKM_WT_m1.y),log2(1+a2$RPKM_KO_m1.x),log2(1+a2$RPKM_KO_m1.y))
cor1: r=0.0631, p=3.28e-07, n=6549
cor2: r=0.0546, p=9.98e-06, n=6549
diffence: p(one-sided)=0.313, p(two-sided)=0.626

#m2
a=all_m2[(!is.na(all_m2$RPKM_KO_m2.x))&(!is.na(all_m2$RPKM_KO_m2.y)),]
a2=a[ (log2(1+a$RPKM_KO_m2.x)>2)&((log2(1+a$RPKM_KO_m2.y))>2)&(log2(1+a$RPKM_WT_m2.x)>2)&((log2(1+a$RPKM_WT_m2.y))>2)&(log2(1+a$RPKM_KO_m2.x)<=8)&((log2(1+a$RPKM_KO_m2.y))<=8)&(log2(1+a$RPKM_WT_m2.x)<=8)&((log2(1+a$RPKM_WT_m2.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y))
#cor.test(log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor.diff.test(log2(1+a2$RPKM_WT_m2.x),log2(1+a2$RPKM_WT_m2.y),log2(1+a2$RPKM_KO_m2.x),log2(1+a2$RPKM_KO_m2.y))
cor1: r=0.06, p=5.33e-07, n=6962
cor2: r=0.081, p=1.3e-11, n=6962
diffence: p(one-sided)=0.107, p(two-sided)=0.214


#@ RPKMs >3 &  <1025
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=10)&((log2(1+a$RPKM_KO_0h.y))<=10)&(log2(1+a$RPKM_WT_0h.x)<=10)&((log2(1+a$RPKM_WT_0h.y))<=10),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.168, p=0, n=7127
cor2: r=0.142, p=0, n=7127
diffence: p(one-sided)=0.0598, p(two-sided)=0.12

#@ RPKMs >3 &  <513
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=9)&((log2(1+a$RPKM_KO_0h.y))<=9)&(log2(1+a$RPKM_WT_0h.x)<=9)&((log2(1+a$RPKM_WT_0h.y))<=9),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.14, p=0, n=7025
cor2: r=0.113, p=0, n=7025
diffence: p(one-sided)=0.0539, p(two-sided)=0.108

#@RPKMs >2 & <257
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=8)&((log2(1+a$RPKM_KO_0h.y))<=8)&(log2(1+a$RPKM_WT_0h.x)<=7)&((log2(1+a$RPKM_WT_0h.y))<=7),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.0851, p=6.25e-12, n=6506
cor2: r=0.0603, p=1.14e-06, n=6506
diffence: p(one-sided)=0.0775, p(two-sided)=0.155

#@RPKMs >2 & <257
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=7)&((log2(1+a$RPKM_KO_0h.y))<=7)&(log2(1+a$RPKM_WT_0h.x)<=8)&((log2(1+a$RPKM_WT_0h.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.0916, p=1.65e-13, n=6461
cor2: r=0.0642, p=2.37e-07, n=6461
diffence: p(one-sided)=0.059, p(two-sided)=0.118

#@RPKMs >2 & <257
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>1)&((log2(1+a$RPKM_KO_0h.y))>1)&(log2(1+a$RPKM_WT_0h.x)>1)&((log2(1+a$RPKM_WT_0h.y))>1)&(log2(1+a$RPKM_KO_0h.x)<=8)&((log2(1+a$RPKM_KO_0h.y))<=8)&(log2(1+a$RPKM_WT_0h.x)<=9)&((log2(1+a$RPKM_WT_0h.y))<=9),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.113, p=0, n=6842
cor2: r=0.083, p=6.02e-12, n=6842
diffence: p(one-sided)=0.0371, p(two-sided)=0.0743

#@RPKMs >2 & <257
a=all_m0[(!is.na(all_m0$RPKM_KO_0h.x))&(!is.na(all_m0$RPKM_KO_0h.y)),]
a2=a[ (log2(1+a$RPKM_KO_0h.x)>3)&((log2(1+a$RPKM_KO_0h.y))>3)&(log2(1+a$RPKM_WT_0h.x)>3)&((log2(1+a$RPKM_WT_0h.y))>3)&(log2(1+a$RPKM_KO_0h.x)<=8)&((log2(1+a$RPKM_KO_0h.y))<=8)&(log2(1+a$RPKM_WT_0h.x)<=8)&((log2(1+a$RPKM_WT_0h.y))<=8),]
#cor.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y))
#cor.test(log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor.diff.test(log2(1+a2$RPKM_WT_0h.x),log2(1+a2$RPKM_WT_0h.y),log2(1+a2$RPKM_KO_0h.x),log2(1+a2$RPKM_KO_0h.y))
cor1: r=0.106, p=1.93e-05, n=1623
cor2: r=0.0762, p=0.00214, n=1623
diffence: p(one-sided)=0.197, p(two-sided)=0.394
