
########################################################################################################################################
########################################################################################################################################
##############################################################################################################ANOVA for variance partitioning







###########################################################################################################normalization



setwd ("final")



load ("attile1V7anno.RData")
load ("mRNA.sc.RData")


		
attile.rna <- attile1[ attile1$flank == "noflank" & attile1$RNA == "mRNA" & attile1$multiTranscript == "unique",]
write.table (attile.rna, file="table.csv")
attile.rna <- read.table( "table.csv", header=T)
save( attile.rna, file="fix/attile.rna.RData", compress=T)

attile.rna$gene <- as.character( attile.rna$gene )
attile.rna$tu <- as.character(attile.rna$tu)
attile.rna.exon <- attile.rna[grep( "tu", attile.rna$tu ), ]
maxclone <- tapply (attile.rna.exon$expressedClones, attile.rna.exon$gene, max )
attile.rna.exon$maxClone <- maxclone[ match( attile.rna.exon$gene, names(maxclone) ) ]
attile.rna.exon <- attile.rna.exon[ attile.rna.exon$expressedClones/attile.rna.exon$maxClone >= 0.5, ] 
write.table( attile.rna.exon, "table.csv")
attile.rna.exon <- read.table( "table.csv", header=T)
save( attile.rna.exon, file="fix/attile.rna.exon.RData", compress=T)






# totalClones=0 has expressedClones either=0.001, 0.002, 0.003 or 0.004, but not all expressedClones=0.001, 0.002, 0.003 and 0.004 has totalClones=0
# atV7: probes: 1683620 -  978189 (attile.rna) -   671745 (attile.rna.exon)



### do quantile normalization for attile.rna probes

library( affy)

mprobe.mean <- mprobe.mean[ which ( rownames( mprobe.mean) %in% rownames(attile.rna) ), ]

#quantile normalization
mRNA.nq <- normalize.quantiles( mprobe.mean )
rownames(mRNA.nq) <- rownames(mprobe.mean)
save (mRNA.nq, file="fix/mRNA.sc.nq.RData", compress=T)






################################################################################################################SFP effect for gene expression
 setwd ("final")

 load("SFP.samout.RData")
 library(siggenes)
 cut.low <- sort(sam.out@d)[6662]
 cut.high <- sort(sam.out@d)[1565240]
 sfp.cut <- as.character( which( sam.out@d <= cut.low | sam.out@d >= cut.high )  )
 


 setwd ("fix")

 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")

 mRNA.rna.exon <- mRNA.nq[ which( rownames( mRNA.nq) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.exon <- mRNA.rna.exon - rowMeans(mRNA.rna.exon)


 probes <- tapply(as.numeric(attile.rna.exon$bpstart), as.character(attile.rna.exon$gene), length) 
 gene.list <- names(probes)[ which(probes >= 3) ] #  25290



 aov.table <- vector("list", length(gene.list) )
 names(aov.table) <- gene.list
 for (i in 1:length(gene.list) )
 {
  probes <- which( as.character(attile.rna.exon$gene) %in% gene.list[i] )
  gene.mean <- mRNA.rna.exon [ probes, 1:8]
  n <- length(probes)

  add <- rep( c(1, -1), each=4*n)

  idx <- which( rownames(attile.rna.exon) [probes] %in% sfp.cut)
  sfp <- rep("nonSFP", n)
  sfp [idx] <- "SFP"
  sfp <- rep(sfp, 8)


  gframe <- data.frame( gene.mean = c(gene.mean), add, sfp )

 
  if(length(idx)>0 & length(idx) <n)  aov.table[i] <- summary( aov( gene.mean~ add*sfp, data=gframe) )
  if(length(idx)==n)  aov.table[i] <- summary( aov( gene.mean~ add, data=gframe) )


  if(i/100== trunc(i/100) ) cat(i, "\n")
 }




 aov.table <- aov.table [ ! unlist( lapply( aov.table, function(x) is.null(x) ) )  ]

 save(aov.table, file="aov.table.RData", compress=T)

 q("no")






###########################################################################################################SFP effect for exonic splicing



 setwd("final")
 
 load("SFP.samout.RData")
 library(siggenes)
 cut.low <- sort(sam.out@d)[6662]
 cut.high <- sort(sam.out@d)[1565240]
 sfp.cut <- as.character( which( sam.out@d <= cut.low | sam.out@d >= cut.high )  )



 setwd ("fix")

 load ("attile.rna.exon.RData")
 load ("resid.RData")

 gene.probe <- tapply( attile.rna.exon$bpstart, as.character( attile.rna.exon$gene), length )
 tu.num <- tapply( as.character( attile.rna.exon$tu), as.character( attile.rna.exon$gene), function(x)nlevels( factor(x)) )
 gene.list <- intersect( names(gene.probe) [gene.probe >=5], names(tu.num)[tu.num >=2 ] ) 

 tu.probe <- tapply( attile.rna.exon$bpstart, paste(attile.rna.exon$gene, attile.rna.exon$tu), length ) 
 tu.list <- names(tu.probe) [tu.probe >=2]  
 tu.matr <- sapply(tu.list, function(x) unlist( strsplit(x, " ") ) )
 tu.matr <- tu.matr[, which(tu.matr[1,] %in% gene.list) ]   
 tu.list <- paste(tu.matr[1,], tu.matr[2,]) 
 gene.list <- names( table( tu.matr[1,]) )  


 aov.table <- vector("list", length(tu.list) )
 names(aov.table) <- tu.list

 for (i in 1:length(tu.list) )
 {
  probes <- which( paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% tu.list[i])
  tmean <- resid[probes,1:8]
  n <- length(probes)

  add <- rep( c(1, -1), each=4*n)

  idx <- which( rownames(attile.rna.exon) [probes] %in% sfp.cut)
  sfp <- rep("nonSFP", n)
  sfp [idx] <- "SFP"
  sfp <- rep(sfp, 8)


  tframe <- data.frame( tmean = c(tmean), add, sfp )

 
  if(length(idx)>0 & length(idx) <n)  aov.table[i] <- summary( aov( tmean~ add*sfp, data=tframe) )
  if(length(idx)==n)  aov.table[i] <- summary( aov( tmean~ add, data=tframe) )

  if(i/100== trunc(i/100) ) cat(i, "\n")
 }




 aov.table <- aov.table [ ! unlist( lapply( aov.table, function(x) is.null(x) ) )  ]

 save(aov.table, file="esplice.aov.table.RData", compress=T)


 q("no")






###########################################################################################################SFP effect for intronic splicing



 setwd("final")
 
 load("SFP.samout.RData")
 library(siggenes)
 cut.low <- sort(sam.out@d)[6662]
 cut.high <- sort(sam.out@d)[1565240]
 sfp.cut <- as.character( which( sam.out@d <= cut.low | sam.out@d >= cut.high )  )



 setwd("fix")
 load ("attile.rna.RData")
 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")


 gene.probe <- tapply(attile.rna.exon$bpstart, as.character( attile.rna.exon$gene), length )
 tu.num <- tapply( as.character(attile.rna.exon$tu), as.character( attile.rna.exon$gene), function(x)nlevels( factor(x)) )
 gene.list <- intersect( names(gene.probe) [gene.probe >=3], names(tu.num)[tu.num >=2 ] ) 


 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.intron <- mRNA.nq[ which(rownames(mRNA.nq) %in% rownames(attile.rna.intron) ),]
 mRNA.rna.intron <- mRNA.rna.intron - rowMeans(mRNA.rna.intron)


 tu.probe <- tapply(attile.rna.intron$bpstart, paste(attile.rna.intron$gene, attile.rna.intron$tu), length) 
 tu.list <- names(tu.probe) [tu.probe >=2 ] 
 tu.matr <- sapply(tu.list, function(x) unlist( strsplit(x, " ") ) )
 tu.matr <- tu.matr[, which(tu.matr[1,] %in% gene.list) ]  
 tu.list <- paste(tu.matr[1,], tu.matr[2,]) 
 gene.list <- names( table( tu.matr[1,]) ) 


 aov.table <- vector("list", length(tu.list) )
 names(aov.table) <- tu.list

 for (i in 1:length(tu.list) )
 {
  probes <- which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% tu.list[i])
  imean <- mRNA.rna.intron[probes,1:8]
  n <- length(probes)

  add <- rep( c(1, -1), each=4*n)

  idx <- which( rownames(attile.rna.intron) [probes] %in% sfp.cut)
  sfp <- rep("nonSFP", n)
  sfp [idx] <- "SFP"
  sfp <- rep(sfp, 8)


  iframe <- data.frame( imean = c(imean), add, sfp )

  if(length(idx)>0 & length(idx) <n)  aov.table[i] <- summary( aov( imean~ add*sfp, data=iframe) )
  if(length(idx)==n)  aov.table[i] <- summary( aov( imean~ add, data=iframe)  )

  if(i/100== trunc(i/100) ) cat(i, "\n")
 }


 aov.table <- aov.table [ ! unlist( lapply( aov.table, function(x) is.null(x) ) )  ]

 save(aov.table, file="isplice.aov.table.RData", compress=T)


 q("no")





#############################################################################################################plotting



 setwd ("final/fix")


 library(geneplotter)


 pdf ("SFP.effect.pdf", width=12, height=4)
 par (mfrow=c(1,3), mai=c(0.7, 0.7, 0.2, 0.2), tck=-0.01, mgp=c(1.7, 0.5, 0) ) 


 load ("aov.table.RData")
 #ss <- lapply( aov.table, function(x) x[[2]])  #second component of the summary table is SS
 #len <- unlist(lapply( ss, function(x) length(x)) )
 #ss <- ss[ which(len==4) ]
 #ss.add <- unlist( lapply(ss, function(x) x[1]) )
 #ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 #ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 ss <- lapply( aov.table, function(x) x[[4]])  #fourth component of the summary table is F
 len <- unlist(lapply( ss, function(x) length(x)) )
 ss <- ss[ which(len==4) ]
 ss.add <- unlist( lapply(ss, function(x) x[1]) )
 ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 ss.itr <- unlist( lapply(ss, function(x) x[3]) )


 x <- sqrt(ss.add)
 y <- sqrt(ss.itr)
 #lim <- range( c(x, y))
 lim <- c(0, 30)


 #smoothScatter( x, y, xlim=lim, ylim=lim, xlab="sqrt (sum of squares genotype)", ylab="sqrt (sum of squares genotype:SFP)")
 smoothScatter( x, y, xlim=lim, ylim=lim, xlab="F statistic genotype", ylab="F statistic genotype x SFP")
 legend ("topleft", "gene expression", bty="n")





 load ("../tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    
 load ("esplice.aov.table.RData")
 idx <- match(qcut, names(aov.table) )
 aov.table <- aov.table[idx]
 #ss <- lapply( aov.table, function(x) x[[2]])  #second component of the summary table is SS
 #len <- unlist(lapply( ss, function(x) length(x)) )
 #ss <- ss[ which(len==4) ]
 #ss.add <- unlist( lapply(ss, function(x) x[1]) )
 #ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 #ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 ss <- lapply( aov.table, function(x) x[[4]])  #fourth component of the summary table is F
 len <- unlist(lapply( ss, function(x) length(x)) )
 ss <- ss[ which(len==4) ]
 ss.add <- unlist( lapply(ss, function(x) x[1]) )
 ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 x <- sqrt(ss.add)
 y <- sqrt(ss.itr)
 lim <- range( c(x, y))
 #smoothScatter( x, y, xlim=lim, ylim=lim, xlab="sqrt (sum of squares genotype)", ylab="sqrt (sum of genotype:SFP)")
 smoothScatter( x, y, xlim=lim, ylim=lim, xlab="F statistic genotype", ylab="F statistic genotype x SFP")
 legend ("topleft", "exon splicing", bty="n")




 load ("isplice.aov.table.RData")
 #ss <- lapply( aov.table, function(x) x[[2]])  #second component of the summary table is SS
 #len <- unlist(lapply( ss, function(x) length(x)) )
 #ss <- ss[ which(len==4) ]
 #ss.add <- unlist( lapply(ss, function(x) x[1]) )
 #ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 #ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 ss <- lapply( aov.table, function(x) x[[4]])  #fourth component of the summary table is F
 len <- unlist(lapply( ss, function(x) length(x)) )
 ss <- ss[ which(len==4) ]
 ss.add <- unlist( lapply(ss, function(x) x[1]) )
 ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 ss.itr <- unlist( lapply(ss, function(x) x[3]) )


 x <- sqrt(ss.add)
 y <- sqrt(ss.itr)
 #lim <- range( c(x, y))
 lim <-  c(0, 15)
 #smoothScatter( x, y, xlim=lim, ylim=lim, xlab="sqrt (sum of squares genotype)", ylab="sqrt (sum of genotype:SFP)")
 smoothScatter( x, y, xlim=lim, ylim=lim, xlab="F statistic genotype", ylab="F statistic genotype x SFP")
 legend ("topleft", "intron splicing", bty="n")


 dev.off()







###
 
 setwd ("final/fix")


 library(geneplotter)

 pdf ("SFP.effect2.pdf", width=12, height=4)
 par (mfrow=c(1,3), mai=c(0.7, 0.7, 0.2, 0.2), tck=-0.01, mgp=c(1.7, 0.5, 0) ) 


 load ("aov.table.RData")
 #ss <- lapply( aov.table, function(x) x[[2]])  #second component of the summary table is SS
 #len <- unlist(lapply( ss, function(x) length(x)) )
 #ss <- ss[ which(len==4) ]
 #ss.add <- unlist( lapply(ss, function(x) x[1]) )
 #ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 #ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 
 ss <- lapply( aov.table, function(x) x[[4]])  #fourth component of the summary table is F
 len <- unlist(lapply( ss, function(x) length(x)) )
 ss <- ss[ which(len==4) ]
 ss.add <- unlist( lapply(ss, function(x) x[1]) )
 ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 x <- sqrt(ss.add)
 y <- sqrt(ss.sfp)
 #lim <- range( c(x, y))
 lim <- c(0, 30)

 #smoothScatter( x, y, xlim=lim, ylim=lim, xlab="sqrt (sum of squares genotype)", ylab="sqrt (sum of squares SFP)")
 smoothScatter( x, y, xlim=lim, ylim=lim, xlab="F statistic genotype", ylab="F statistic SFP")
 legend ("topleft", "gene expression", bty="n")



 load ("../tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    
 load ("esplice.aov.table.RData")
 idx <- match(qcut, names(aov.table) )
 aov.table <- aov.table[idx]
 #ss <- lapply( aov.table, function(x) x[[2]])  #second component of the summary table is SS
 #len <- unlist(lapply( ss, function(x) length(x)) )
 #ss <- ss[ which(len==4) ]
 #ss.add <- unlist( lapply(ss, function(x) x[1]) )
 #ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 #ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 
 ss <- lapply( aov.table, function(x) x[[4]])  #fourth component of the summary table is F
 len <- unlist(lapply( ss, function(x) length(x)) )
 ss <- ss[ which(len==4) ]
 ss.add <- unlist( lapply(ss, function(x) x[1]) )
 ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 x <- sqrt(ss.add)
 y <- sqrt(ss.sfp)
 lim <- range( c(x, y))
 #smoothScatter( x, y, xlim=lim, ylim=lim, xlab="sqrt (sum of squares genotype)", ylab="sqrt (sum of squares SFP)")
 smoothScatter( x, y, xlim=lim, ylim=lim, xlab="F statistic genotype", ylab="F statistic SFP")
 legend ("topleft", "exon splicing", bty="n")




 load ("isplice.aov.table.RData")
 #ss <- lapply( aov.table, function(x) x[[2]])  #second component of the summary table is SS
 #len <- unlist(lapply( ss, function(x) length(x)) )
 #ss <- ss[ which(len==4) ]
 #ss.add <- unlist( lapply(ss, function(x) x[1]) )
 #ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 #ss.itr <- unlist( lapply(ss, function(x) x[3]) )

 ss <- lapply( aov.table, function(x) x[[4]])  #fourth component of the summary table is F
 len <- unlist(lapply( ss, function(x) length(x)) )
 ss <- ss[ which(len==4) ]
 ss.add <- unlist( lapply(ss, function(x) x[1]) )
 ss.sfp <- unlist( lapply(ss, function(x) x[2]) )
 ss.itr <- unlist( lapply(ss, function(x) x[3]) )


 x <- sqrt(ss.add)
 y <- sqrt(ss.sfp)
 #lim <- range( c(x, y))
 lim <-  c(0, 15)
 #smoothScatter( x, y, xlim=lim, ylim=lim, xlab="sqrt (sum of squares genotype)", ylab="sqrt (sum of squares SFP)")
 smoothScatter( x, y, xlim=lim, ylim=lim, xlab="F statistic genotype", ylab="F statistic SFP")
 legend ("topleft", "intron splicing", bty="n")


 dev.off()















#################################################################################################################################
#################################################################################################################################
###################################################################################analysis  by inclusion and exclusion of SFP probes



################################################################ normalization




setwd ("final")



load ("attile1V7anno.RData")
load ("mRNA.sc.RData")


		
attile.rna <- attile1[ attile1$flank == "noflank" & attile1$RNA == "mRNA" & attile1$multiTranscript == "unique",]
write.table (attile.rna, file="table.csv")
attile.rna <- read.table( "table.csv", header=T)
save( attile.rna, file="fix/attile.rna.RData", compress=T)

attile.rna$gene <- as.character( attile.rna$gene )
attile.rna$tu <- as.character(attile.rna$tu)
attile.rna.exon <- attile.rna[grep( "tu", attile.rna$tu ), ]
maxclone <- tapply (attile.rna.exon$expressedClones, attile.rna.exon$gene, max )
attile.rna.exon$maxClone <- maxclone[ match( attile.rna.exon$gene, names(maxclone) ) ]
attile.rna.exon <- attile.rna.exon[ attile.rna.exon$expressedClones/attile.rna.exon$maxClone >= 0.5, ] 
write.table( attile.rna.exon, "table.csv")
attile.rna.exon <- read.table( "table.csv", header=T)
save( attile.rna.exon, file="fix/attile.rna.exon.RData", compress=T)






# totalClones=0 has expressedClones either=0.001, 0.002, 0.003 or 0.004, but not all expressedClones=0.001, 0.002, 0.003 and 0.004 has totalClones=0
# atV7: probes: 1683620 -  978189 (attile.rna) -   671745 (attile.rna.exon)



### do quantile normalization for attile.rna probes

library( affy)

mprobe.mean <- mprobe.mean[ which ( rownames( mprobe.mean) %in% rownames(attile.rna) ), ]

#quantile normalization
mRNA.nq <- normalize.quantiles( mprobe.mean )
rownames(mRNA.nq) <- rownames(mprobe.mean)
save (mRNA.nq, file="fix/mRNA.sc.nq.RData", compress=T)





#######################################################################gene expression 





 setwd ("final/fix")

 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")

 
 mRNA.rna.exon <- mRNA.nq[ which( rownames( mRNA.nq) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.exon <- mRNA.rna.exon - rowMeans(mRNA.rna.exon)


 probes <- tapply(as.numeric(attile.rna.exon$bpstart), as.character(attile.rna.exon$gene), length) 
 gene.list <- names(probes)[ which(probes >= 3) ] #  25290


 #length( which( as.character(attile.rna.exon$gene) %in% gene.list)  )
 #[1]  670875


 matr <- matrix(NA, nc=4, nr=length(gene.list) )
 rownames(matr) <- gene.list
 expr.main <- list( add=matr, dom=matr, mat=matr)


 for (i in 1:length(gene.list) )
 {
  probes <- which( as.character(attile.rna.exon$gene) %in% gene.list[i] )
  gene.mean <- mRNA.rna.exon [ probes,]
  n <- length(probes)

  add <- rep( c(1, -1, 0, 0), each=4*n)
  dom <- rep( c(0, 0, 1, 1), each=4*n)
  mat <- rep( c(0, 0, -1, 1), each=4*n)

  gframe <- data.frame( gene.mean = c(gene.mean), add, dom, mat)

  #add+dom+mat to test additive and dominant
  fit <- summary( lm( gene.mean~ add + dom + mat, data=gframe) )$coef
  expr.main[[1]][i,] <- fit[2,]
  expr.main[[2]][i,] <- fit[3,]
  expr.main[[3]][i,] <- fit[4,]

  if(i/100== trunc(i/100) ) cat(i, "\n")
 }

 save(expr.main, file="expr.main.RData", compress=T)

 q("no")








#######################################################################permutation for gene expression



 k <- 1


 setwd("final/fix")
 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")


 mRNA.rna.exon <- mRNA.nq[ which( rownames( mRNA.nq) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.exon <- mRNA.rna.exon - rowMeans(mRNA.rna.exon)


 probes <- tapply(as.numeric(attile.rna.exon$bpstart), as.character(attile.rna.exon$gene), length) 
 gene.list <- names(probes)[ which(probes >= 3) ] 


 nperm <- 1000

 j <- 1
 gene.list <- gene.list[ 1:5000]


 matr <- matrix(NA, nr=length(gene.list), nc=nperm)
 expr.perm <- list(coef=matr, std=matr)


 nsample=16
 load ( paste("../samp.matrix", nsample, ".RData", sep="") )


 for (i in 1:length(gene.list) )
 {
  probes <- which( as.character(attile.rna.exon$gene) %in% gene.list[i] )
  gene.mean <- mRNA.rna.exon [ probes, ]
  n <- length(probes)

  add <- rep( c(1, -1, 0, 0), each=4*n)
  dom <- rep( c(0, 0, 1, 1), each=4*n)
  mat <- rep( c(0, 0, -1, 1), each=4*n)

  gframe <- data.frame( gmean = c(gene.mean), add, dom, mat)


	for (iperm in 1:nperm)
	{
	#leave one out 
	rfit <- lm( gmean~ as.matrix( gframe[, c(-1, -(k+1) ) ] ), data=gframe)
	resid <- matrix( rfit$resid, nr=n) [, samp.matrix[iperm,] ]
	predict <-  as.matrix( gframe[, c(-1,-(k+1) )]) %*% t( t( rfit$coef[c(2,3)] ) ) +  rfit$coef[1] +  c(resid) 
	pframe <- data.frame( predict, add, dom, mat)
	pfit <- summary ( lm( predict~ add+dom+mat, data=pframe) )$coef
	expr.perm[[1]][i, iperm] <- pfit[k+1,1]
	expr.perm[[2]][i, iperm] <- pfit[k+1,2]
	}


  if(i/100 == trunc(i/100) ) cat(i, "\n" )
 }

 save(expr.perm, file=paste("Main.expr.perm", k, ".set", j, ".RData", sep=""), compress=T)
 q("no")







###

 k <- 1


 setwd ("final/fix")

 load ( paste("Main.expr.perm", k, ".set1.RData", sep="") )
 coef <- expr.perm[[1]]
 std <- expr.perm[[2]]

 for (j in 2:6)
  {
  load ( paste("Main.expr.perm", k, ".set", j, ".RData", sep="") )
  coef <- rbind(coef, expr.perm[[1]])
  std <- rbind(std, expr.perm[[2]])
  }

 expr.perm <- list( coef, std)
 save(expr.perm, file=paste( "Main.expr.perm", k, ".RData", sep=""), compress=T )
 q("no")







#####################################################################################gene expression fdr



 setwd ("final")
 load ("expr.main.RData")
 gene <- rownames( expr.main[[1]])


 k <- 1
 setwd ("fix")
 load ("expr.main.RData")
 idx <- match(gene, rownames(expr.main[[k]] ) )


 load ( paste("Main.expr.perm", k, ".RData", sep="") )
 coef <- expr.perm[[1]][idx,]
 std <- expr.perm[[2]][idx,]
 s0 <- quantile( std, 0.5)

 main <- expr.main[[k]][idx,]
 main <- main[,1]/ ( main[,2]+s0)
 names(main) <- gene
 main <- sort(main)
 perm.mat <- coef/ (std+s0); rm(expr.perm); gc()


 perm.num <- 1000
 for (i in 1:perm.num) perm.mat[,i] <- sort(perm.mat[,i])
 null <- rowMeans(perm.mat)


 for (j in seq(0.5, 3, 0.1)) 
 { 
     real.vec <- main > null + j | main < null - j     
     threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
	for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] > null + j | perm.mat[,i] < null - j
     sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
     cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
 }



 for (j in seq(0.5, 3, 0.1)) 
 { 
     real.vec <- main > null + j 
     threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
	for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] > null + j 
     sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
     cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
 }



 for (j in seq(0.5, 3, 0.1)) 
 { 
     real.vec <-  main < null - j     
     threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
	for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] < null - j
     sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
     cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
 }









### compare the difference



 setwd ("final")

 load ("expr.main.RData")
 gene.list <- rownames( expr.main[[1]])


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("fix/attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which( as.character(attile.rna.exon$gene) %in% gene.list), ]
 gene <- as.character(attile.rna.exon$gene)
 sfp.per.gene <- rep(0, length(gene) )
 sfp.per.gene [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.gene <- tapply(sfp.per.gene, gene, function (x) sum(x) )
 sfp.gene <- names(sfp.per.gene)[ which( sfp.per.gene >=1) ]
 nonsfp.gene <- names(sfp.per.gene)[ which(sfp.per.gene ==0) ]
 

# table(sfp.per.gene)
#sfp.per.gene
#    0     1     2     3     4     5     6     7     8     9    10    11    12
#14469  5285  2120  1007   597   395   270   177   111    87    48    53    30
#   13    14    15    16    17    18    19    20    21    22    23    24    25
#   22    15    11    10     4     8     7     1     7     1     1     3     1
#   26    27    28    29    31    32    34    35    36    37    44    46
#    2     2     1     3     1     1     1     1     1     1     1     1




 k <- 1
 setwd ("fix")
 load ("expr.main.RData")
 idx <- match(gene.list, rownames(expr.main[[k]] ) )
 main <- expr.main[[k]][idx,]
 
 load ( paste("Main.expr.perm", k, ".RData", sep="") )
 coef  <- expr.perm[[1]][idx,] 
 std  <- expr.perm[[2]][idx,];   rm(expr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/(main[,2]+s0)
 names(main) <- gene.list


 tab <- scan("tab.txt", what=1)
 tab <- matrix(tab, byrow=T, nc=2)
 out.tab <- matrix(NA, nr=nrow(tab), 4)
 colnames(out.tab) <- c("sig.SFP", "nonsig.SFP", "sig.nonSFP", "nonsig.nonSFP")

 
 for (i in 1:nrow(tab) )
 {
  sig.gene <- names(sort(main)) [1:tab[i,2] ]
  sig.gene <- c(sig.gene, names(sort(main, decreasing=T)) [1:tab[i,1] ] )
  nonsig.gene <- gene.list[ - which(gene.list %in% sig.gene) ]

  out.tab[i,1] <- length( intersect(sig.gene, sfp.gene) )
  out.tab[i,2] <- length( intersect(nonsig.gene, sfp.gene) )
  out.tab[i,3] <- length( intersect(sig.gene, nonsfp.gene) )
  out.tab[i,4] <- length( intersect(nonsig.gene, nonsfp.gene) )
  
 }
 






############################################


 setwd ("final")

 load ("expr.main.RData")
 main <- expr.main[[1]]
 gene.list <- rownames(main)


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("fix/attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which( as.character(attile.rna.exon$gene) %in% gene.list), ]
 gene <- as.character(attile.rna.exon$gene)
 sfp.per.gene <- rep(0, length(gene) )
 sfp.per.gene [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.gene <- tapply(sfp.per.gene, gene, function (x) sum(x) )
 sfp.gene <- names(sfp.per.gene)[ which( sfp.per.gene >=1) ]
 nonsfp.gene <- names(sfp.per.gene)[ which(sfp.per.gene ==0) ]
 

 k <- 1
 load ( paste("Main.expr.perm", k, ".RData", sep="") )
 coef  <- expr.perm[[1]] 
 std  <- expr.perm[[2]];   rm(expr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/(main[,2]+s0)
 names(main) <- gene.list


 tab <- scan("fix/tab2.txt", what=1)
 tab <- matrix(tab, byrow=T, nc=2)
 out.tab <- matrix(NA, nr=nrow(tab), 4)
 colnames(out.tab) <- c("sig.SFP", "nonsig.SFP", "sig.nonSFP", "nonsig.nonSFP")

 
 for (i in 1:nrow(tab) )
 {
  sig.gene <- names(sort(main)) [1:tab[i,2] ]
  sig.gene <- c(sig.gene, names(sort(main, decreasing=T)) [1:tab[i,1] ] )
  nonsig.gene <- gene.list[ - which(gene.list %in% sig.gene) ]

  out.tab[i,1] <- length( intersect(sig.gene, sfp.gene) )
  out.tab[i,2] <- length( intersect(nonsig.gene, sfp.gene) )
  out.tab[i,3] <- length( intersect(sig.gene, nonsfp.gene) )
  out.tab[i,4] <- length( intersect(nonsig.gene, nonsfp.gene) )
  
 }
 








### compare coef and std distribution



 setwd ("final")


 load ("expr.main.RData")
 main <- expr.main[[1]]
 gene.list <- rownames(main)


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("fix/attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which( as.character(attile.rna.exon$gene) %in% gene.list), ]
 gene <- as.character(attile.rna.exon$gene)
 sfp.per.gene <- rep(0, length(gene) )
 sfp.per.gene [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.gene <- tapply(sfp.per.gene, gene, function (x) sum(x) )
 sfp.gene <- names(sfp.per.gene)[ which( sfp.per.gene >=1) ]
 nonsfp.gene <- names(sfp.per.gene)[ which(sfp.per.gene ==0) ]
 

 k <- 1
 load ( paste("Main.expr.perm", k, ".RData", sep="") )
 coef  <- expr.perm[[1]] 
 std  <- expr.perm[[2]];   rm(expr.perm); gc()
 s0 <- quantile(std, 0.5)


 main.c.nonsfp <- main[,1][ which( rownames(main) %in% nonsfp.gene) ]
 main.c.sfp <- main[,1][ which( rownames(main) %in% sfp.gene) ]
 main.s.nonsfp <- main[,2][ which( rownames(main) %in% nonsfp.gene) ]
 main.s.sfp <- main[,2][ which( rownames(main) %in% sfp.gene) ]
 main.d.nonsfp <- ( main[,1]/(main[,2]+s0) ) [ which ( rownames(main) %in% nonsfp.gene) ]
 main.d.sfp <- ( main[,1]/(main[,2]+s0) ) [ which ( rownames(main) %in% sfp.gene) ]



 load ( "fix/expr.main.RData")
 load ( paste("fix/Main.expr.perm", k, ".RData", sep="") )
 idx <- match(gene.list, rownames(expr.main[[1]]) )
 main2 <- expr.main[[1]][idx,]
 coef  <- expr.perm[[1]][idx,]
 std  <-  expr.perm[[2]][idx,];   rm(expr.perm); gc()
 s0 <- quantile(std, 0.5)


 main2.c.nonsfp <- main2[,1][ which( rownames(main2) %in% nonsfp.gene) ]
 main2.c.sfp <- main2[,1][ which( rownames(main2) %in% sfp.gene) ]
 main2.s.nonsfp <- main2[,2][ which( rownames(main2) %in% nonsfp.gene) ]
 main2.s.sfp <- main2[,2][ which( rownames(main2) %in% sfp.gene) ]
 main2.d.nonsfp <- ( main2[,1]/(main2[,2]+s0) ) [ which ( rownames(main2) %in% nonsfp.gene) ]
 main2.d.sfp <- ( main2[,1]/(main2[,2]+s0) ) [ which ( rownames(main2) %in% sfp.gene) ]



 pdf( "gene.comp.pdf", width=12, height=4)
 par( mfrow=c(1, 3))

 plot ( main.c.sfp, main2.c.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.c.nonsfp, main.c.sfp, main2.c.nonsfp, main2.c.sfp)), ylim=range( c(main.c.nonsfp, main.c.sfp, main2.c.nonsfp, main2.c.sfp)), xlab="coefficients from excluding SFP probes", ylab="coefficients from including SFP probes"  )
 points( main.c.nonsfp, main2.c.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("genes with SFPs", "genes without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline( 0,1)
 abline( v=0, h=0)
 

 plot (main.s.sfp, main2.s.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.s.nonsfp, main.s.sfp, main2.s.nonsfp, main2.s.sfp)), ylim=range( c(main.s.nonsfp, main.s.sfp, main2.s.nonsfp, main2.s.sfp)), xlab="standard deviations from excluding SFP probes", ylab="standard deviations from including SFP probes"      )
 points( main.s.nonsfp, main2.s.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("genes with SFPs", "genes without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline( 0, 1)
 

 plot (main.d.sfp, main2.d.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.d.nonsfp, main.d.sfp, main2.d.nonsfp, main2.d.sfp)), ylim=range( c(main.d.nonsfp, main.d.sfp, main2.d.nonsfp, main2.d.sfp)), xlab="d-scores from excluding SFP probes", ylab="d-scores from including SFP probes"    )
 points( main.d.nonsfp, main2.d.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("genes with SFPs", "genes without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline(0, 1)
 abline( v=0, h=0)


 dev.off()















##########################################################################################sfp per exon

 setwd ("final")


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]     


 setwd("fix")
 
 load ("attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which( paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% qcut), ]
 tu <- paste(attile.rna.exon$gene, attile.rna.exon$tu)
 sfp.per.exon <- rep(0, length(tu) )
 sfp.per.exon [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.exon <- tapply(sfp.per.exon, tu, function (x) sum(x) )

 table(sfp.per.exon)


    0     1     2     3     4     5     6     7     8     9    10
62742  4196   699   248    91    26    11     2     4     2     1


######################################################################################## exon splicing
###residuals from gene modeling




 setwd ("final/fix")

 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")


 mRNA.rna.exon <- mRNA.nq[ which( rownames( mRNA.nq) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.exon <- mRNA.rna.exon- rowMeans(mRNA.rna.exon)

 probes <- tapply(attile.rna.exon$bpstart, as.character(attile.rna.exon$gene), length) 
 gene.list <- names(probes)[ which(probes >= 3) ] 

 resid <- mRNA.rna.exon 


 for (i in 1:length(gene.list) )
 {
  probes <- which( as.character(attile.rna.exon$gene) %in% gene.list[i] )
  gene.mean <- mRNA.rna.exon [ probes,]
  n <- length(probes)

  add <- rep( c(1, -1, 0, 0), each=4*n)
  dom <- rep( c(0, 0, 1, 1), each=4*n)
  mat <- rep( c(0, 0, -1, 1), each=4*n)

  gframe <- data.frame( gene.mean = c(gene.mean), add, dom, mat)

  #add+dom+mat to test additive and dominant
  res <- summary( lm( gene.mean~ add + dom + mat, data=gframe) )$resid
  resid [probes, ] <- matrix(res, nr=length(probes) )


  if(i/100== trunc(i/100) ) cat(i, "\n")
 }

 
 save(resid, file="resid.RData", compress=T)

 q("no")





#######################################################################model residuals for each exon


 setwd("final/fix")
 
 load ("attile.rna.exon.RData")
 load ("resid.RData")

 gene.probe <- tapply( attile.rna.exon$bpstart, as.character( attile.rna.exon$gene), length )
 tu.num <- tapply( as.character( attile.rna.exon$tu), as.character( attile.rna.exon$gene), function(x)nlevels( factor(x)) )
 gene.list <- intersect( names(gene.probe) [gene.probe >=5], names(tu.num)[tu.num >=2 ] ) 

 tu.probe <- tapply( attile.rna.exon$bpstart, paste(attile.rna.exon$gene, attile.rna.exon$tu), length ) 
 tu.list <- names(tu.probe) [tu.probe >=2]  
 tu.matr <- sapply(tu.list, function(x) unlist( strsplit(x, " ") ) )
 tu.matr <- tu.matr[, which(tu.matr[1,] %in% gene.list) ]   
 tu.list <- paste(tu.matr[1,], tu.matr[2,]) 
 gene.list <- names( table( tu.matr[1,]) )  


 matr <- matrix(NA, nc=4, nr=length(tu.list))
 rownames(matr) <- tu.list
 texpr.main <- list(add=matr, dom=matr, mat=matr)


 for (i in 1:length(tu.list) )
 {
  probes <- which( paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% tu.list[i])
  tmean <- resid[probes,]
  n <- length(probes)


  add <- rep( c(1, -1, 0, 0), each=4*n)
  dom <- rep( c(0, 0, 1, 1), each=4*n)
  mat <- rep( c(0, 0, -1, 1), each=4*n)


  tframe <- data.frame( tmean = c(tmean), add, dom, mat)
  fit <- summary(lm(tmean~add+dom+mat, data=tframe))$coef
  texpr.main[[1]][i,] <- fit[2,]
  texpr.main[[2]][i,] <- fit[3,]
  texpr.main[[3]][i,] <- fit[4,]

  if(i/100== trunc(i/100) ) cat(i, "\n")
 }


 save(texpr.main, file="tmean.main.RData", compress=T)

 q("no")






########################################################################permutation for exon



 k <- 1

 setwd("final/fix")
 
 load ("resid.RData")
 res <- resid 


 load ("attile.rna.exon.RData")
 gene.probe <- tapply(attile.rna.exon$bpstart, as.character( attile.rna.exon$gene), length )
 tu.num <- tapply( as.character( attile.rna.exon$tu), as.character( attile.rna.exon$gene), function(x)nlevels( factor(x)) )
 gene.list <- intersect( names(gene.probe) [gene.probe >=5], names(tu.num)[tu.num >=2 ] ) 


 tu.probe <- tapply( attile.rna.exon$bpstart, paste(attile.rna.exon$gene, attile.rna.exon$tu), length ) 
 tu.list <- names(tu.probe) [tu.probe >=2]  
 tu.matr <- sapply(tu.list, function(x) unlist( strsplit(x, " ") ) )
 tu.matr <- tu.matr[, which(tu.matr[1,] %in% gene.list) ]  
 tu.list <- paste(tu.matr[1,], tu.matr[2,])  
 gene.list <- names( table( tu.matr[1,]) )  


 j <- 1 
 tu.list <- tu.list[1:10000] 


 nperm <- 1000
 nsample=16
 load ( paste("../samp.matrix", nsample, ".RData", sep="") )

 matr <- matrix(NA, nr=length(tu.list), nc=nperm)
 texpr.perm <- list(coef=matr, std=matr)


 system.time
 (
  for (i in 1:length(tu.list) )
    {
     probes <- which( paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% tu.list[i])
     tmean <- res[probes,]
     n <- length(probes)


     add <- rep( c(1, -1, 0, 0), each=4*n)
     dom <- rep( c(0, 0, 1, 1), each=4*n)
     mat <- rep( c(0, 0, -1, 1), each=4*n)


     tframe <- data.frame( tmean = c(tmean), add, dom, mat)
     rfit <- lm(tmean~ as.matrix( tframe[, c(-1, -(k+1) )] ), data=tframe) #leave one out

	for( iperm in 1:nperm) 
	{
	resid <- matrix( rfit$resid, nr=n) [, samp.matrix[iperm,] ]
	predict <-  as.matrix( tframe[, c(-1,-(k+1) )]) %*% t( t( rfit$coef[c(2,3)] ) ) +  rfit$coef[1] + c(resid) 
	pframe <- data.frame( predict, add, dom, mat)
	pfit <- summary ( lm( predict~ add+dom+mat, data=pframe) )$coef
	texpr.perm[[1]][i, iperm] <- pfit[k+1,1]
	texpr.perm[[2]][i, iperm] <- pfit[k+1,2]
	}

     if(i/100 == trunc(i/100) ) cat(i, "\n" )
     }
 ) 

 save(texpr.perm, file=paste("Main.tmean.perm", k, ".set", j, ".RData", sep=""), compress=T)
 q("no")








###combine permutation data

 setwd ("final/fix")

 k <- 1

 load ( paste("Main.tmean.perm", k, ".set1.RData", sep="") )
 coef <- texpr.perm[[1]]
 std <- texpr.perm[[2]]
 
 for (i in 2:10)
 {
  load ( paste("Main.tmean.perm", k, ".set", i, ".RData", sep=""))
  coef <- rbind(coef, texpr.perm[[1]])
  std <- rbind(std, texpr.perm[[2]])
 }

 texpr.perm <- list(coef, std)
 save(texpr.perm, file=paste("Main.tmean.perm", k, ".RData", sep=""), compress=T)

 q("no")






########################################################################## exon fdr with the tgr ratio

 

 setwd ("final")

 load ("tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    


 k <- 1
 setwd ("fix")

 load ("tmean.main.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main <- texpr.main[[k]] [idx, ]

 load ( paste("Main.tmean.perm", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]]; coef <- coef[idx,] 
 std  <- texpr.perm[[2]]; std <- std[idx,]
 s0 <- quantile(std, 0.5)

 main <- main[,1]/ (main[,2]+s0)
 main <- sort(main)

 perm.mat <- coef / (std +s0); rm(texpr.perm); gc()
 perm.num <- 1000
 for (i in 1:perm.num) perm.mat[,i] <- sort(perm.mat[,i])
 null <- rowMeans(perm.mat)

 for (j in seq(0.1, 1, 0.1)) 
   { 
    real.vec <- main > null + j | main < null - j     
    threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
    for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] > null + j | perm.mat[,i] < null - j
    sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
    cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
   }

 for (j in seq(0.1, 1, 0.1)) 
   { 
    real.vec <- main > null + j 
    threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
    for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] > null + j 
    sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
    cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
   } 

 for (j in seq(0.1, 1, 0.1)) 
   { 
    real.vec <-  main < null - j     
    threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
    for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] < null - j
    sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
    cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
   }
 



 

###
 

 setwd ("final")


 library(siggenes)


 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    
 

 setwd ("fix")
 load ("attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which(paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% qcut), ]
 exon <- paste(attile.rna.exon$gene, attile.rna.exon$tu)
 sfp.per.exon <- rep(0, length(exon) )
 sfp.per.exon [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.exon <- tapply(sfp.per.exon, exon, function (x) sum(x) )
#sfp.per.exon
#    0     1     2     3     4     5     6     7     8     9    10
#62742  4196   699   248    91    26    11     2     4     2     1



 sfp.exon <- names(sfp.per.exon)[ which( sfp.per.exon >=1) ]
 nonsfp.exon <- names(sfp.per.exon)[ which(sfp.per.exon ==0) ]




 

 k <- 1
 load ("tmean.main.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main.sfp <- texpr.main[[k]] [idx, ]
 load ( paste("Main.tmean.perm", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]]; coef <- coef[idx,] 
 std  <- texpr.perm[[2]]; std <- std[idx,]; rm(texpr.perm); gc()
 s0 <- quantile(std, 0.5)
 main.sfp <- main.sfp[,1]/ (main.sfp[,2]+s0)
 names(main.sfp) <- qcut


 tab <- scan("tab.txt", what=1)
 tab <- matrix(tab, byrow=T, nc=2)
 out.tab <- matrix(NA, nr=nrow(tab), 4)
 colnames(out.tab) <- c("sig.SFP", "nonsig.SFP", "sig.nonSFP", "nonsig.nonSFP")

 
 for (i in 1:nrow(tab) )
 {
  sig.exon <- names(sort(main.sfp)) [1:tab[i,2] ]
  sig.exon <- c(sig.exon, names(sort(main.sfp, decreasing=T)) [1:tab[i,1] ] )
  nonsig.exon <- qcut[ - which(qcut %in% sig.exon) ]

  out.tab[i,1] <- length( intersect(sig.exon, sfp.exon) )
  out.tab[i,2] <- length( intersect(nonsig.exon, sfp.exon) )
  out.tab[i,3] <- length( intersect(sig.exon, nonsfp.exon) )
  out.tab[i,4] <- length( intersect(nonsig.exon, nonsfp.exon) )
  
 }

 (out.tab[,1]/out.tab[,2]) / (out.tab[,3]/out.tab[,4])
 chi[i,5] <- chisq.test( matrix(out.tab[i,1:4], nc=2) )






##############################################################################

 setwd ("final")


 load ("tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    
 

 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR



 load ("fix/attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which(paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% qcut), ]
 exon <- paste(attile.rna.exon$gene, attile.rna.exon$tu)
 sfp.per.exon <- rep(0, length(exon) )
 sfp.per.exon [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.exon <- tapply(sfp.per.exon, exon, function (x) sum(x) )
 sfp.exon <- names(sfp.per.exon)[ which( sfp.per.exon >=1) ]
 nonsfp.exon <- names(sfp.per.exon)[ which(sfp.per.exon ==0) ]
 


 k <- 1
 load ("tmean.main.v2.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main <- texpr.main[[k]] [idx, ]
 load ( paste("Main.tmean.perm.v2.", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]]; coef <- coef[idx,] 
 std  <- texpr.perm[[2]]; std <- std[idx,]; rm(texpr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/ (main[,2]+s0)
 names(main) <- qcut


 tab <- scan("../tab2.txt", what=1)
 tab <- matrix(tab, byrow=T, nc=2)
 out.tab <- matrix(NA, nr=nrow(tab), 4)
 colnames(out.tab) <- c("sig.SFP", "nonsig.SFP", "sig.nonSFP", "nonsig.nonSFP")

 
 for (i in 1:nrow(tab) )
 {
  sig.exon <- names(sort(main)) [1:tab[i,2] ]
  sig.exon <- c(sig.exon, names(sort(main, decreasing=T)) [1:tab[i,1] ] )
  nonsig.exon <- qcut[ - which(qcut %in% sig.exon) ]

  out.tab[i,1] <- length( intersect(sig.exon, sfp.exon) )
  out.tab[i,2] <- length( intersect(nonsig.exon, sfp.exon) )
  out.tab[i,3] <- length( intersect(sig.exon, nonsfp.exon) )
  out.tab[i,4] <- length( intersect(nonsig.exon, nonsfp.exon) )
  
 }

 (out.tab[,1]/out.tab[,2]) / (out.tab[,3]/out.tab[,4])




###

 
 a <- scan("foldSFP.txt", what=1)
 a <- matrix( a, byrow=T, nc=2)
 gene <- a[1:20, ]
 exon <- a[21:40,]
 intron <- a[41:60,]


 pdf ("foldenrichment.pdf", width=12, height=4)
 par (mfrow=c(1,3) )

 plot( gene[1:10, 1], gene[1:10, 2], xlim=range(gene[,1]), ylim=range(gene[,2]), col="grey", "p", pch="+", xlab="number of significant genes", ylab="fold enrichment of SFP-genes in significant genes", cex.lab=1.2 )
 lines (gene[1:10, 1], gene[1:10, 2], col="grey")
 points (gene[11:20, 1], gene[11:20,2], col="black", pch="+")
 lines (gene[11:20, 1], gene[11:20,2], col="black")
 legend ("topright", c("excluding SFP probes", "including SFP probes"), col=c("black", "grey"), pch="+", bty="n", cex=1.2)
 
 plot( exon[1:10, 1], exon[1:10, 2], xlim=range(exon[,1]), ylim=range(exon[,2]), col="grey", "p", pch="+", xlab="number of significant exons", ylab="fold enrichment of SFP-exons in significant exons", cex.lab=1.2 )
 lines (exon[1:10, 1], exon[1:10, 2], col="grey")
 points (exon[11:20, 1], exon[11:20,2], col="black", pch="+")
 lines (exon[11:20, 1], exon[11:20,2], col="black")
 legend ("topright", c("excluding SFP probes", "including SFP probes"), col=c("black", "grey"), pch="+", bty="n", cex=1.2)

 plot( intron[1:10, 1], intron[1:10, 2], xlim=c(0, 10000), ylim=range(intron[,2]), col="grey", "p", pch="+", xlab="number of significant introns", ylab="fold enrichment of SFP-introns in significant introns", cex.lab=1.2 )
 lines (intron[1:10, 1], intron[1:10, 2], col="grey")
 points (intron[11:20, 1], intron[11:20,2], col="black", pch="+")
 lines (intron[11:20, 1], intron[11:20,2], col="black")
 legend ("topright", c("excluding SFP probes", "including SFP probes"), col=c("black", "grey"), pch="+", bty="n", cex=1.2)

 dev.off()














################################################################################################ see the distribution difference of coef and std





 setwd ("final")


 load ("tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    
 

 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR

 load ("fix/attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which(paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% qcut), ]
 exon <- paste(attile.rna.exon$gene, attile.rna.exon$tu)
 sfp.per.exon <- rep(0, length(exon) )
 sfp.per.exon [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.exon <- tapply(sfp.per.exon, exon, function (x) sum(x) )
 sfp.exon <- names(sfp.per.exon)[ which( sfp.per.exon >=1) ]
 nonsfp.exon <- names(sfp.per.exon)[ which(sfp.per.exon ==0) ]




 
 k <- 1
 load ("tmean.main.v2.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main <- texpr.main[[k]] [idx, ]
 load ( paste("Main.tmean.perm.v2.", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]][idx,] 
 std  <- texpr.perm[[2]][idx,]; rm(texpr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/ (main[,2]+s0)
 names(main) <- qcut
 
 sig.exon <- names(sort(main)) [1:190]
 sig.exon <- c(sig.exon, names(sort(main, decreasing=T)) [1:287] )
 nonsig.exon <- qcut[ - which(qcut %in% sig.exon) ]






 setwd ("fix")
 load ("tmean.main.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main <- texpr.main[[k]] [idx, ]
 load ( paste("Main.tmean.perm", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]][idx,] 
 std  <- texpr.perm[[2]][idx,]; rm(texpr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/ (main[,2]+s0)
 names(main) <- qcut
 
 sig.exon2 <- names(sort(main)) [1:279]
 sig.exon2 <- c(sig.exon2, names(sort(main, decreasing=T)) [1:206] )
 nonsig.exon2 <- qcut[ - which(qcut %in% sig.exon2 ) ]


 length( intersect( intersect(sig.exon, sfp.exon), intersect(sig.exon2, sfp.exon) ) )
 length( intersect( intersect(nonsig.exon, sfp.exon), intersect(nonsig.exon2, sfp.exon) )  )
 length( intersect( intersect(sig.exon, nonsfp.exon), intersect(sig.exon2, nonsfp.exon) ) )
 length( intersect( intersect(nonsig.exon, nonsfp.exon), intersect(nonsig.exon2, nonsfp.exon) ) )











#################################

 setwd ("final")


 load ("tmean.tgr.RData")
 qcut <- names(tgr)[ tgr <= 0.25 ]    
 

 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR

 load ("fix/attile.rna.exon.RData")
 attile.rna.exon <- attile.rna.exon[ which(paste(attile.rna.exon$gene, attile.rna.exon$tu) %in% qcut), ]
 exon <- paste(attile.rna.exon$gene, attile.rna.exon$tu)
 sfp.per.exon <- rep(0, length(exon) )
 sfp.per.exon [ which( rownames(attile.rna.exon) %in% sfpid )] <- 1
 sfp.per.exon <- tapply(sfp.per.exon, exon, function (x) sum(x) )
 sfp.exon <- names(sfp.per.exon)[ which( sfp.per.exon >=1) ]
 nonsfp.exon <- names(sfp.per.exon)[ which(sfp.per.exon ==0) ]


 
 k <- 1
 load ("tmean.main.v2.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main <- texpr.main[[k]] [idx, ]
 load ( paste("Main.tmean.perm.v2.", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]][idx,] 
 std  <- texpr.perm[[2]][idx,]; rm(texpr.perm); gc()
 s0 <- quantile(std, 0.5)
 s0
 #       50%
 #0.03317523
 


 main.c.nonsfp <- main[,1][ which( rownames(main) %in% nonsfp.exon) ]
 main.c.sfp <- main[,1][ which( rownames(main) %in% sfp.exon) ]
 main.s.nonsfp <- main[,2][ which( rownames(main) %in% nonsfp.exon) ]
 main.s.sfp <- main[,2][ which( rownames(main) %in% sfp.exon) ]
 main.d.nonsfp <- ( main[,1]/(main[,2]+s0) ) [ which ( rownames(main) %in% nonsfp.exon) ]
 main.d.sfp <- ( main[,1]/(main[,2]+s0) ) [ which ( rownames(main) %in% sfp.exon) ]




 setwd ("fix")
 load ("tmean.main.RData")
 idx <- match(qcut, rownames(texpr.main[[k]]) )
 main2 <- texpr.main[[k]] [idx, ]
 load ( paste("Main.tmean.perm", k, ".RData", sep="") )
 coef  <- texpr.perm[[1]][idx,] 
 std  <- texpr.perm[[2]][idx,]; rm(texpr.perm); gc()
 s0 <- quantile(std, 0.5)
 s0
 #      50%
 #0.03281458


 main2.c.nonsfp <- main2[,1][ which( rownames(main2) %in% nonsfp.exon) ]
 main2.c.sfp <- main2[,1][ which( rownames(main2) %in% sfp.exon) ]
 main2.s.nonsfp <- main2[,2][ which( rownames(main2) %in% nonsfp.exon) ]
 main2.s.sfp <- main2[,2][ which( rownames(main2) %in% sfp.exon) ]
 main2.d.nonsfp <- ( main2[,1]/(main2[,2]+s0) ) [ which ( rownames(main2) %in% nonsfp.exon) ]
 main2.d.sfp <- ( main2[,1]/(main2[,2]+s0) ) [ which ( rownames(main2) %in% sfp.exon) ]



 pdf( "exon.comp.pdf", width=12, height=4)
 par( mfrow=c(1, 3))

 plot ( main.c.sfp, main2.c.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.c.nonsfp, main.c.sfp, main2.c.nonsfp, main2.c.sfp)), ylim=range( c(main.c.nonsfp, main.c.sfp, main2.c.nonsfp, main2.c.sfp)), xlab="coefficients from excluding SFP probes", ylab="coefficients from including SFP probes"  )
 points( main.c.nonsfp, main2.c.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("exons with SFPs", "exons without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline( 0,1)
 abline( v=0, h=0)
 

 plot (main.s.sfp, main2.s.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.s.nonsfp, main.s.sfp, main2.s.nonsfp, main2.s.sfp)), ylim=range( c(main.s.nonsfp, main.s.sfp, main2.s.nonsfp, main2.s.sfp)), xlab="standard deviations from excluding SFP probes", ylab="standard deviations from including SFP probes"      )
 points( main.s.nonsfp, main2.s.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("exons with SFPs", "exons without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline( 0, 1)
 

 plot (main.d.sfp, main2.d.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.d.nonsfp, main.d.sfp, main2.d.nonsfp, main2.d.sfp)), ylim=range( c(main.d.nonsfp, main.d.sfp, main2.d.nonsfp, main2.d.sfp)), xlab="d-scores from excluding SFP probes", ylab="d-scores from including SFP probes"    )
 points( main.d.nonsfp, main2.d.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("exons with SFPs", "exons without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline(0, 1)
 abline( v=0, h=0)


 dev.off()







########################################################################################sfp per intron




 setwd ("final")

 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("iexpr.main.v2.RData")
 intron.list <- rownames(iexpr.main[[1]])



 setwd ("fix")
 load ("attile.rna.RData")
 load ("attile.rna.exon.RData")
 
 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]
 attile.rna.intron <- attile.rna.intron[ which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% intron.list), ]

 tu <- paste(attile.rna.intron$gene, attile.rna.intron$tu)
 sfp.per.intron <- rep(0, length(tu) )
 sfp.per.intron [ which( rownames(attile.rna.intron) %in% sfpid )] <- 1
 sfp.per.intron <- tapply(sfp.per.intron, tu, function (x) sum(x) )

 table(sfp.per.intron)






########################################################################################################3
#############################################################################for intron

 setwd("final/fix")
 load ("attile.rna.RData")
 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")


 gene.probe <- tapply(attile.rna.exon$bpstart, as.character( attile.rna.exon$gene), length )
 tu.num <- tapply( as.character(attile.rna.exon$tu), as.character( attile.rna.exon$gene), function(x)nlevels( factor(x)) )
 gene.list <- intersect( names(gene.probe) [gene.probe >=3], names(tu.num)[tu.num >=2 ] ) 


 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.intron <- mRNA.nq[ which(rownames(mRNA.nq) %in% rownames(attile.rna.intron) ),]
 mRNA.rna.intron <- mRNA.rna.intron - rowMeans(mRNA.rna.intron)


 tu.probe <- tapply(attile.rna.intron$bpstart, paste(attile.rna.intron$gene, attile.rna.intron$tu), length) 
 tu.list <- names(tu.probe) [tu.probe >=2 ] 
 tu.matr <- sapply(tu.list, function(x) unlist( strsplit(x, " ") ) )
 tu.matr <- tu.matr[, which(tu.matr[1,] %in% gene.list) ]  
 tu.list <- paste(tu.matr[1,], tu.matr[2,]) 

 gene.list <- names( table( tu.matr[1,]) ) 

 matr <- matrix(NA, nc=4, nr=length(tu.list))
 rownames(matr) <- tu.list
 iexpr.main <- list(add=matr, dom=matr, mat=matr)


 for (i in 1:length(tu.list) )
 {
 probes <- which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% tu.list[i])
 tmean <- mRNA.rna.intron[probes,]
 n <- length(probes)


 add <- rep( c(1, -1, 0, 0), each=4*n)
 dom <- rep( c(0, 0, 1, 1), each=4*n)
 mat <- rep( c(0, 0, -1, 1), each=4*n)

 tframe <- data.frame( tmean = c(tmean), add, dom, mat)

 fit <- summary(lm(tmean~add+dom+mat, data=tframe))$coef
 iexpr.main[[1]][i,] <- fit[2,]
 iexpr.main[[2]][i,] <- fit[3,]
 iexpr.main[[3]][i,] <- fit[4,]


 if(i/100== trunc(i/100) ) cat(i, "\n")
 }


 save(iexpr.main, file="iexpr.main.RData", compress=T)

 q("no")






####################################################################permutation for intron

 k <- 1

 setwd("final/fix")
 load ("attile.rna.RData")
 load ("attile.rna.exon.RData")
 load ("mRNA.sc.nq.RData")


 gene.probe <- tapply(attile.rna.exon$bpstart, as.character( attile.rna.exon$gene), length )
 tu.num <- tapply( as.character(attile.rna.exon$tu), as.character( attile.rna.exon$gene), function(x)nlevels( factor(x)) )
 gene.list <- intersect( names(gene.probe) [gene.probe >=3], names(tu.num)[tu.num >=2 ] ) 


 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]
 mRNA.rna.intron <- mRNA.nq[ which(rownames(mRNA.nq) %in% rownames(attile.rna.intron) ),]
 mRNA.rna.intron <- mRNA.rna.intron - rowMeans(mRNA.rna.intron)


 tu.probe <- tapply(attile.rna.intron$bpstart, paste(attile.rna.intron$gene, attile.rna.intron$tu), length) 
 tu.list <- names(tu.probe) [tu.probe >=2 ] 
 tu.matr <- sapply(tu.list, function(x) unlist( strsplit(x, " ") ) )
 tu.matr <- tu.matr[, which(tu.matr[1,] %in% gene.list) ]  
 tu.list <- paste(tu.matr[1,], tu.matr[2,]) 
 gene.list <- names( table( tu.matr[1,]) ) 

 nperm <- 1000
 nsample=16
 load ( paste("../samp.matrix", nsample, ".RData", sep="") )


 j <- 1 ###CHANGE
 tu.list <- tu.list[1:10000] ###CHANGE



 matr <- matrix(NA, nr=length(tu.list), nc=nperm)
 iexpr.perm <- list(coef=matr, std=matr)


 system.time
 (

  for (i in 1:length(tu.list) )
  {
  probes <- which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% tu.list[i])
  tmean <- mRNA.rna.intron[probes,]
  n <- length(probes)


  add <- rep( c(1, -1, 0, 0), each=4*n)
  dom <- rep( c(0, 0, 1, 1), each=4*n)
  mat <- rep( c(0, 0, -1, 1), each=4*n)

  tframe <- data.frame( tmean=c(tmean), add, dom, mat)

  rfit <- lm(tmean~ as.matrix( tframe[, c(-1, -(k+1) )] ), data=tframe) 

	for( iperm in 1:nperm) 
	{
	resid <- matrix( rfit$resid, nr=n) [, samp.matrix[iperm,] ]
	predict <-  as.matrix( tframe[, c(-1,-(k+1) )]) %*% t( t( rfit$coef[c(2,3)] ) ) +  rfit$coef[1] + c(resid) 
	
	pframe <- data.frame( predict, add, dom, mat)
	pfit <- summary ( lm( predict~ add+dom+mat, data=pframe) )$coef
	iexpr.perm[[1]][i, iperm] <- pfit[k+1,1]
	iexpr.perm[[2]][i, iperm] <- pfit[k+1,2]
	}


  if(i/100 == trunc(i/100) ) cat(i, "\n" )
  }
 )

 save(iexpr.perm, file=paste("Main.iexpr.perm", k, ".set", j, ".RData", sep=""), compress=T)
 q("no")







####combine permutation result


 setwd ("final/fix")

 k <- 1

 load ( paste("Main.iexpr.perm", k, ".set1", ".RData", sep="") )
 coef <- iexpr.perm[[1]]
 std <- iexpr.perm[[2]]
 
 for (i in 2:8)
 {
  load ( paste("Main.iexpr.perm", k, ".set", i, ".RData", sep=""))
  coef <- rbind(coef, iexpr.perm[[1]])
  std <- rbind(std, iexpr.perm[[2]])
 }

 iexpr.perm <- list(coef, std)
 save(iexpr.perm, file=paste("Main.iexpr.perm", k, ".RData", sep=""), compress=T)
 q("no")








##################################################################### intron fdr





 setwd ("final")



 load ("iexpr.main.v2.RData")
 intron.list <- rownames(iexpr.main[[1]])


 k <- 1
 setwd ("fix")

 load ("iexpr.main.RData")
 idx <- match( intron.list, rownames(iexpr.main[[k]]) )
 main <- iexpr.main[[k]][idx,]


 load ( paste("Main.iexpr.perm", k, ".RData", sep="") )
 coef <- iexpr.perm[[1]][idx,]
 std <- iexpr.perm[[2]][idx,]
 s0 <- quantile(std, 0.5)
 main <- main[,1]/ (main[,2]+s0)
 main <- sort(main)
 perm.mat <- coef/ (std+s0); rm(iexpr.perm); gc()


 perm.num <- 1000
 for (i in 1:perm.num) perm.mat[,i] <- sort(perm.mat[,i])
 null <- rowMeans(perm.mat)


 for (j in seq(0.1, 1, 0.1)) 
 { 
     real.vec <- main > null + j | main < null - j     
     threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
	for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] > null + j | perm.mat[,i] < null - j
     sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
     cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
 }



 for (j in seq(0.1, 1, 0.1)) 
 { 
     real.vec <- main > null + j 
     threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
	for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] > null + j 
     sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
     cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
 }



 for (j in seq(0.1, 1, 0.1)) 
 { 
     real.vec <-  main < null - j     
     threshold.mat <- matrix(FALSE, nr=length(main), nc=perm.num)
	for (i in 1:perm.num) threshold.mat[,i] <- perm.mat[,i] < null - j
     sam.tab <- cbind(table(real.vec), table(factor(threshold.mat ,c(FALSE,TRUE)))/perm.num) 
     cat(j,sam.tab[2,],sam.tab[2,1]-sam.tab[2,2],sam.tab[2,2]/sam.tab[2,1],"\n",sep="\t")
 }







##################################################################################################



 setwd ("final")


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("iexpr.main.v2.RData")
 intron.list <- rownames(iexpr.main[[1]])


 setwd("fix")
 load ("attile.rna.RData")
 load ("attile.rna.exon.RData")
 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]


 attile.rna.intron <- attile.rna.intron[  which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% intron.list), ]
 intron <- paste(attile.rna.intron$gene, attile.rna.intron$tu)
 sfp.per.intron <- rep(0, length(intron) )
 sfp.per.intron [ which( rownames(attile.rna.intron) %in% sfpid )] <- 1
 sfp.per.intron <- tapply(sfp.per.intron, intron, function (x) sum(x) )
 sfp.intron <- names(sfp.per.intron)[ which( sfp.per.intron >=1) ]
 nonsfp.intron <- names(sfp.per.intron)[ which(sfp.per.intron ==0) ]
 


 k <- 1
 load ("iexpr.main.RData")
 idx <- match(intron.list, rownames(iexpr.main[[k]]) )
 main <- iexpr.main[[k]] [idx, ]
 load ( paste("Main.iexpr.perm", k, ".RData", sep="") )
 coef  <- iexpr.perm[[1]][idx,] 
 std  <-  iexpr.perm[[2]][idx,]; rm(iexpr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/ (main[,2]+s0)
 names(main) <- intron.list


 tab <- scan("tab.txt", what=1)
 tab <- matrix(tab, byrow=T, nc=2)
 out.tab <- matrix(NA, nr=nrow(tab), 4)
 colnames(out.tab) <- c("sig.SFP", "nonsig.SFP", "sig.nonSFP", "nonsig.nonSFP")

 
 for (i in 1:nrow(tab) )
 {
  sig.intron <- names(sort(main)) [1:tab[i,2] ]
  sig.intron <- c(sig.intron, names(sort(main, decreasing=T)) [1:tab[i,1] ] )
  nonsig.intron <- intron.list[ - which(intron.list %in% sig.intron) ]

  out.tab[i,1] <- length( intersect(sig.intron, sfp.intron) )
  out.tab[i,2] <- length( intersect(nonsig.intron, sfp.intron) )
  out.tab[i,3] <- length( intersect(sig.intron, nonsfp.intron) )
  out.tab[i,4] <- length( intersect(nonsig.intron, nonsfp.intron) )
  
 }

 (out.tab[,1]/out.tab[,2]) / (out.tab[,3]/out.tab[,4])







###

 setwd ("final")


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("iexpr.main.v2.RData")
 intron.list <- rownames(iexpr.main[[1]])


 load ("fix/attile.rna.RData")
 load ("fix/attile.rna.exon.RData")
 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]


 attile.rna.intron <- attile.rna.intron[  which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% intron.list), ]
 intron <- paste(attile.rna.intron$gene, attile.rna.intron$tu)
 sfp.per.intron <- rep(0, length(intron) )
 sfp.per.intron [ which( rownames(attile.rna.intron) %in% sfpid )] <- 1
 sfp.per.intron <- tapply(sfp.per.intron, intron, function (x) sum(x) )
 sfp.intron <- names(sfp.per.intron)[ which( sfp.per.intron >=1) ]
 nonsfp.intron <- names(sfp.per.intron)[ which(sfp.per.intron ==0) ]
 

 k <- 1 
 main <- iexpr.main[[k]] 
 load ( paste("Main.iexpr.perm.v2.", k, ".RData", sep="") )
 coef  <- iexpr.perm[[1]]
 std  <-  iexpr.perm[[2]]; rm(iexpr.perm); gc()
 s0 <- quantile(std, 0.5)
 main <- main[,1]/ (main[,2]+s0)
 names(main) <- intron.list


 tab <- scan("fix/tab2.txt", what=1)
 tab <- matrix(tab, byrow=T, nc=2)
 out.tab <- matrix(NA, nr=nrow(tab), 4)
 colnames(out.tab) <- c("sig.SFP", "nonsig.SFP", "sig.nonSFP", "nonsig.nonSFP")

 
 for (i in 1:nrow(tab) )
 {
  sig.intron <- names(sort(main)) [1:tab[i,2] ]
  sig.intron <- c(sig.intron, names(sort(main, decreasing=T)) [1:tab[i,1] ] )
  nonsig.intron <- intron.list[ - which(intron.list %in% sig.intron) ]

  out.tab[i,1] <- length( intersect(sig.intron, sfp.intron) )
  out.tab[i,2] <- length( intersect(nonsig.intron, sfp.intron) )
  out.tab[i,3] <- length( intersect(sig.intron, nonsfp.intron) )
  out.tab[i,4] <- length( intersect(nonsig.intron, nonsfp.intron) )
  
 }

 (out.tab[,1]/out.tab[,2]) / (out.tab[,3]/out.tab[,4])







###########################################################################################################intron coef and std



 setwd ("final")


 library(siggenes)
 load ("attile1V7anno.RData")
 load ("SFP.samout.RData")
 d <- sam.out@d
 d.cut.h <- quantile(d, 1565240/length(d))
 d.cut.l <- quantile(d, 6662/length(d))
 sfpid <- rownames(attile1)[ which(d >= d.cut.h | d <= d.cut.l) ] #5%FDR


 load ("iexpr.main.v2.RData")
 intron.list <- rownames(iexpr.main[[1]])


 setwd("fix")
 load ("attile.rna.RData")
 load ("attile.rna.exon.RData")
 attile.rna.intron <- attile.rna[ - which( rownames(attile.rna) %in% rownames(attile.rna.exon) ), ]


 attile.rna.intron <- attile.rna.intron[  which( paste(attile.rna.intron$gene, attile.rna.intron$tu) %in% intron.list), ]
 intron <- paste(attile.rna.intron$gene, attile.rna.intron$tu)
 sfp.per.intron <- rep(0, length(intron) )
 sfp.per.intron [ which( rownames(attile.rna.intron) %in% sfpid )] <- 1
 sfp.per.intron <- tapply(sfp.per.intron, intron, function (x) sum(x) )
 sfp.intron <- names(sfp.per.intron)[ which( sfp.per.intron >=1) ]
 nonsfp.intron <- names(sfp.per.intron)[ which(sfp.per.intron ==0) ]
 


 k <- 1
 main <- iexpr.main[[k]] 
 load ( paste("../Main.iexpr.perm.v2.", k, ".RData", sep="") )
 coef  <- iexpr.perm[[1]]
 std  <-  iexpr.perm[[2]]; rm(iexpr.perm); gc()
 s0 <- quantile(std, 0.5)
 

 main.c.nonsfp <- main[,1][ which( rownames(main) %in% nonsfp.intron) ]
 main.c.sfp <- main[,1][ which( rownames(main) %in% sfp.intron) ]
 main.s.nonsfp <- main[,2][ which( rownames(main) %in% nonsfp.intron) ]
 main.s.sfp <- main[,2][ which( rownames(main) %in% sfp.intron) ]
 main.d.nonsfp <- ( main[,1]/(main[,2]+s0) ) [ which ( rownames(main) %in% nonsfp.intron) ]
 main.d.sfp <- ( main[,1]/(main[,2]+s0) ) [ which ( rownames(main) %in% sfp.intron) ]



 load ( "iexpr.main.RData")
 load ( paste("Main.iexpr.perm", k, ".RData", sep="") )
 idx <- match(intron.list, rownames(iexpr.main[[k]]) )
 main2 <- iexpr.main[[1]][idx,]
 coef  <- iexpr.perm[[1]][idx,]
 std  <-  iexpr.perm[[2]][idx,];   rm(iexpr.perm); gc()
 s0 <- quantile(std, 0.5)


 main2.c.nonsfp <- main2[,1][ which( rownames(main2) %in% nonsfp.intron) ]
 main2.c.sfp <- main2[,1][ which( rownames(main2) %in% sfp.intron) ]
 main2.s.nonsfp <- main2[,2][ which( rownames(main2) %in% nonsfp.intron) ]
 main2.s.sfp <- main2[,2][ which( rownames(main2) %in% sfp.intron) ]
 main2.d.nonsfp <- ( main2[,1]/(main2[,2]+s0) ) [ which ( rownames(main2) %in% nonsfp.intron) ]
 main2.d.sfp <- ( main2[,1]/(main2[,2]+s0) ) [ which ( rownames(main2) %in% sfp.intron) ]



 pdf( "intron.comp.pdf", width=12, height=4)
 par( mfrow=c(1, 3))

 plot ( main.c.sfp, main2.c.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.c.nonsfp, main.c.sfp, main2.c.nonsfp, main2.c.sfp)), ylim=range( c(main.c.nonsfp, main.c.sfp, main2.c.nonsfp, main2.c.sfp)), xlab="coefficients from excluding SFP probes", ylab="coefficients from including SFP probes"  )
 points( main.c.nonsfp, main2.c.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("introns with SFPs", "introns without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline( 0,1)
 abline( v=0, h=0)
 

 plot (main.s.sfp, main2.s.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.s.nonsfp, main.s.sfp, main2.s.nonsfp, main2.s.sfp)), ylim=range( c(main.s.nonsfp, main.s.sfp, main2.s.nonsfp, main2.s.sfp)), xlab="standard deviations from excluding SFP probes", ylab="standard deviations from including SFP probes"      )
 points( main.s.nonsfp, main2.s.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("introns with SFPs", "introns without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline( 0, 1)
 

 plot (main.d.sfp, main2.d.sfp, "p", pch=".", col="grey", cex=0.2, xlim=range( c(main.d.nonsfp, main.d.sfp, main2.d.nonsfp, main2.d.sfp)), ylim=range( c(main.d.nonsfp, main.d.sfp, main2.d.nonsfp, main2.d.sfp)), xlab="d-scores from excluding SFP probes", ylab="d-scores from including SFP probes"    )
 points( main.d.nonsfp, main2.d.nonsfp, pch=".", col="black", cex=0.2 )
 legend ( "topleft", c("introns with SFPs", "introns without SFPs"), col=c("grey", "black"), pch=15, bty="n")
 abline(0, 1)
 abline( v=0, h=0)


 dev.off()






























