

##################################################################################################pvalue between genomic DNA Col and Van


 setwd ("final")
 load ("gDNA.sc.nq.RData")
 gDNA <- gDNA.nq[, 1:8]
 pval <- apply(gDNA, 1, function(x) t.test(x[1:4], x[5:8], alternative="less")$p.value)
 save(pval, file="gDNA.pval.RData")

 q("no")




##################################################################################################segmentation of pvalue 



 segbin <- function(yn, probeanno, chrom, chrom.len, bin, bin.num, bin.size, baseperseg) {
 
    
      cat(sprintf("Running 'segment' on chromosome %s bin %s", chrom, bin) )

      if (bin == 1) range <- c(1, bin*bin.size  )
      if (bin == bin.num) range <- c(  (bin-1)*bin.size+1, chrom.len )
      if (bin !=1 & bin !=bin.num) range <- c(  (bin-1)*bin.size+1, bin*bin.size) 
    
      
      prbs <- probeanno[ probeanno$chr== chrom & probeanno$mid >= range[1] & probeanno$mid <= range[2], ]          
      
    
      y <- yn[prbs$index] 

      maxkm <- min(5000, length(y)) #the maximum segment length in probe numbers
      nsegs <- as.integer(round ( (range[2]-range[1]+1)/baseperseg )  )
     
      
      #maxseg is the maximum number of segments, maxk is the maximum length of a segment  
      s <- segment(y, maxseg = nsegs, maxk = maxkm)      
      s@x <- prbs$mid 
                  
      cat(" ... complete\n")
      
      save(s, file = paste( "segment/chr", chrom, ".", bin, ".s.RData", sep=""), compress=T)
         
      }



 setwd ("final")

 library(tilingArray)


 load ("attile1V7anno.RData")
 load ("gDNA.sc.nq.RData")


 load ("gDNA.pval.RData")
  

 #the 5% weakest probes
 col.mean <- rowMeans(gDNA.nq[, 1:4]); rm(gDNA.nq); gc()
 weak.cut <- quantile(col.mean, 0.05)
 weak.cut <- which(col.mean < weak.cut)

 attile1 <- attile1[-weak.cut,]
 pval <- pval[-weak.cut]  


 probeanno <- data.frame(chr=attile1$chr, mid=attile1$bpstart-12, index=c(1:nrow(attile1) ) ); rm(attile1); gc() 



 #divide each chromosome to 1Mb blocks so that memory usage is not huge 
 bin.size <- 1000000
 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 



 for (chrom in 1:5)
 { 
 chrom.len <- chr.len[[chrom]]
 bin.num <- trunc( chrom.len/bin.size ) +1
 for (k in 1:bin.num) segbin (yn=pval, probeanno, chrom=chrom, chrom.len, bin=k, bin.num, bin.size, baseperseg = 5000) 
 }

 q("no")








################################################################################################find indels 




 setwd ("final/segment")

 library(tilingArray)
 load ("../attile1V7anno.RData")


 bin.size <- 1000000
 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 



 col <- van <- vector("list", length=5)

 for (chrom in 1:5)
 {
  bin.num <- trunc( chr.len[[chrom]]/bin.size ) +1



  col.seg <- van.seg <- col.seg.bp <- van.seg.bp  <- matrix(numeric(), nc=2)     
  for (bin in 1:bin.num)
   {
   load ( paste( "chr", chrom, ".", bin, ".s.RData", sep="") )   
   x <- s@x
   y <- s@y
   bks <- s@breakpoints [[ which.max( logLik(s, penalty="AIC") ) ]]   
   #bks <- s@breakpoints [[ which.max( logLik(s, penalty="BIC") ) ]]   

   segind <- cbind( bks-1, bks )
   segind <- c(1, c(t(segind)), length(x) )    
   segind <- matrix(segind, byrow=T, nc=2) 
   fac <- rep( 1:nrow(segind), segind[,2]-segind[,1]+1 )
   pval <- tapply(y, fac, function(x) median(x) ) 
      
   #first pick segment signficant for differential signals 
   cseg <-  matrix( segind[ pval > 0.99 , ], nc=2 ) 
   vseg <-  matrix( segind[ pval < 0.02 , ], nc=2 )

   #remove those segments only have one probe  
   cseg <-  cseg[ which( cseg[,2]-cseg[,1]+1  > 1), ] 
   vseg <-  vseg[ which( vseg[,2]-vseg[,1]+1  > 1), ] 
  
   col.seg <- rbind( col.seg, cseg)
   van.seg <- rbind( van.seg, vseg)
  

   col.seg.bp <- rbind(col.seg.bp, matrix( t ( apply(matrix(cseg,nc=2), 1, function(p) c(x[p[1]], x[p[2]]) ) ), nc=2)  )
   van.seg.bp <- rbind(van.seg.bp, matrix( t ( apply(matrix(vseg,nc=2), 1, function(p) c(x[p[1]], x[p[2]]) ) ), nc=2)  )
   }


  #combine differential segments at the edge of chromosome chuncks
  mid <- (attile1$bpstart-12)[attile1$chr==chrom]
  cp <- c( t(col.seg.bp) )
  vp <- c( t(van.seg.bp) )
  cp <- which(mid %in% cp)
  vp <- which(mid %in% vp)
  cp <- which( diff(cp)==1 )
  vp <- which( diff(vp)==1 )
  if( length(cp)>0)  col.seg.bp <- c( t(col.seg.bp) ) [ - c(cp, cp+1) ]
  if( length(vp)>0)  van.seg.bp <- c( t(van.seg.bp) ) [ - c(vp, vp+1) ]
  col.seg.bp <- matrix(col.seg.bp, byrow=T, nc=2)
  van.seg.bp <- matrix(van.seg.bp, byrow=T, nc=2)

  if( length(cp)>0)  col.seg <- c( t(col.seg) ) [ -c(cp, cp+1) ]
  if( length(vp)>0)  van.seg <- c( t(van.seg) ) [ -c(vp, vp+1) ]
  col.seg <- matrix(col.seg, byrow=T, nc=2)
  van.seg <- matrix(van.seg, byrow=T, nc=2)
 

  #the number of probes interrogating the segment
  cindex <- (col.seg[,2]-col.seg[,1]+1)/(col.seg.bp[,2]-col.seg.bp[,1]+1)
  vindex <- (van.seg[,2]-van.seg[,1]+1)/(van.seg.bp[,2]-van.seg.bp[,1]+1) 


  #the segment require > 6 probes and 1kb average density
  col.seg.bp <- col.seg.bp[ ( col.seg[,2]-col.seg[,1]+1 > 6 & cindex > 0.001 ), ]     
  van.seg.bp <- van.seg.bp[ ( van.seg[,2]-van.seg[,1]+1 > 6 & vindex > 0.001 ), ]                                  



  col[[chrom]] <- col.seg.bp
  van[[chrom]] <- van.seg.bp

  cat(chrom, "\t", nrow(col.seg.bp), "\t", nrow(van.seg.bp), "\n" )
    
 }

 

 cat("total", "\t", sum( unlist( lapply(col, function(x) nrow(x) ) ) ), "\t", sum( unlist(lapply(van, function(x) nrow(x) ) ) ), "\n"  )




#aic
#1        435     26 
#2        253     15 
#3        317     36 
#4        241     18 
#5        399     41 
#total    1645    136 


#bic
#1        129     4 
#2        82      4 
#3        97      7 
#4        64      2 
#5        146     5 
#total    518     22 


 save(col, van, file="aic.RData", compress=T)
 #save(col, van, file="bic.RData", compress=T)




 #using pval 0.95/0.05
 #> summary(p.col)
 #   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 # 0.9500  0.9771  0.9922  0.9861  0.9977  1.0000
 #> summary(p.van)
 #     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
 #0.0001157 0.0072310 0.0204200 0.0221900 0.0363400 0.0498300

 #using pval 0.99/0.02
 #>  summary(p.col)
 #  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 #0.9900  0.9948  0.9974  0.9966  0.9989  1.0000
 #>  summary(p.van)
 #    Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
 #0.0001157 0.0032870 0.0071610 0.0084770 0.0135000 0.0198800




 q("no")



############################################################################################plotting along chromosome 

 setwd ("final")


 load ("attile1V7anno.RData")
 load ("segment/aic.RData")
 #load ("segment/bic.RData")

 load ("gDNA.sc.nq.RData")
 gDNA.nq <- gDNA.nq[, 1:8]
 gDNA.nq <- gDNA.nq - rowMeans(gDNA.nq) 



 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 
 size <- 20000


 for (chrom in 1:5)
 {

  col.seg.bp <- col[[chrom]]
  van.seg.bp <- van[[chrom]]


  attile <- attile1[ attile1$chr==chrom, ]
  attile$mid <- as.numeric(attile$bpstart)-12
  gene.range <- tapply( as.numeric(attile$mid), as.character(attile$gene), range)
  names <- names(gene.range)[- length(gene.range) ]
  gene.range <- matrix(unlist(gene.range), byrow=T, nc=2)
  gene.range <- gene.range[-nrow(gene.range), ]
  rownames(gene.range) <- names


  yn <- gDNA.nq[ attile1$chr==chrom, ]


  page <- trunc( chr.len[chrom] /size ) +1
  
  pdf ( paste("segment/chr", chrom, ".aic.pdf", sep="") )      
  #pdf ( paste("segment/chr", chrom, ".bic.pdf", sep="") )   



     for (i in 1:page) 
      {
       probes <- which ( attile$mid > (i-1)*size  &  attile$mid <=  i*size  )
       
         if(length(probes)>0)
               {
                   gene.ind <- which( gene.range [,2] > (i-1)*size & gene.range [,1] <= i*size )
                   if ( length(gene.ind) >0 )  
                       {gene.pos <- matrix( gene.range[gene.ind, ], nc=2 ); 
                        rownames(gene.pos) <- rownames(gene.range)[gene.ind];                    
                        if ( gene.pos[1, 1] <= (i-1)*size )  gene.pos [1, 1] <- (i-1)*size;
                        if ( gene.pos[ nrow(gene.pos), 2] > i*size ) gene.pos[ nrow(gene.pos),2] <- i*size}                     
                   n <- length(gene.ind)



                   col.ind <- which( col.seg.bp [,2] > (i-1)*size & col.seg.bp [,1] <= i*size )
                   if ( length(col.ind) >0 )  
                        {col.pos <- matrix( col.seg.bp[col.ind, ], nc=2 );                      
                        if ( col.pos[1, 1] <= (i-1)*size )  col.pos [1, 1] <- (i-1)*size;
                        if ( col.pos[ nrow(col.pos), 2] > i*size ) col.pos[ nrow(col.pos),2] <- i*size}
        
                   van.ind <- which( van.seg.bp [,2] > (i-1)*size & van.seg.bp [,1] <= i*size )
                   if ( length(van.ind) >0 )  
                        {van.pos <- matrix( van.seg.bp[van.ind, ], nc=2 );                      
                        if ( van.pos[1, 1] <= (i-1)*size )  van.pos [1, 1] <- (i-1)*size;
                        if ( van.pos[ nrow(van.pos), 2] > i*size ) van.pos[ nrow(van.pos),2] <- i*size}
       
      
                
       
                   matplot( matrix(attile$mid [probes], nc=1), matrix(yn[probes,], nc=8), pch=".",  xlim=c( size*(i-1), i*size ), ylim=c(-1.0, 1.0), xlab="bp", ylab="log intensity difference",  col=rep(c("red","black"), each=4) ) 
                   legend("topright", c("deletion", "duplication"), col=c("orange", "green"), pch=15, bty="n")
       
       
                   if ( length(col.ind) > 0)  rect(col.pos[,1], rep(-0.05, length(col.ind)), col.pos[,2], rep(0, length(col.ind)), col="orange", border=NA)         
                   if ( length(van.ind) > 0)  rect(van.pos[,1], rep(-0.05, length(van.ind)), van.pos[,2], rep(0, length(van.ind)), col="green", border=NA)
                   if ( n > 0) 
                         {rect(gene.pos[,1], rep(0, n), gene.pos[,2], rep(0.05, n), col="blue", border=NA)
                          label <- c( rownames(gene.range)[gene.ind] )                   
                          legend("topleft",label, bty="n", cex=0.6) }   

               }            
       }

  dev.off() 
  

  cat(chrom, "\n")
 }


 q("no")







       



######################################################################################distribution
### chromosome distribution with SFPs

 setwd ("final/segment")
 load ("aic.RData")
  

 load ("../SFP.samout.RData")
 library(siggenes)
 d <- sam.out@d
 names(d) <- c(1:length(d) )
 sfp.cind <- as.numeric(  names(sort(d))  [ (length(d)-104115):length(d) ]  )
 sfp.vind <- as.numeric(  names(sort(d))  [1:3820] )


 load ("../attile1V7anno.RData")
 sfp.col <- cbind( as.numeric(attile1$chr)[ sfp.cind], (as.numeric( attile1$bpstart)-12)[ sfp.cind] ) 
 sfp.van <- cbind( as.numeric(attile1$chr)[ sfp.vind], (as.numeric( attile1$bpstart)-12)[ sfp.vind] ) 


 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 
 size <- 100000
 cen <- matrix(scan("../CENs.txt", what=1, nline=5), byrow=T, nc=2 )

  
 pdf ("indelnsfp.pdf", width=4, height=4)
 
{
 par( mfrow=c(5,1),  omi=c(0.3, 0, 0.2, 0.62), mai=c(0, 0.82, 0, 0), yaxp=c(0, 1, 1), tck=-0.04, cex.axis=0.6 )

 

 for (chrom in 1:5)
 {
   sfpc <- sfp.col[,2][ sfp.col[,1]==chrom]
   sfpv <- sfp.van[,2][ sfp.van[,1]==chrom]


   col.seg.bp <- col[[chrom]]
   van.seg.bp <- van[[chrom]]
   
 
   page <- trunc( chr.len[chrom] /size ) 
   countc <- countv <- rep(NA, page)
   
 
   for (i in 1:page ) { ind <- which ( col.seg.bp[,2] > (i-1)*size  &  col.seg.bp[,1] <=  i*size  )
                        if( length(ind) >0 )
                          {col.pos <- matrix( col.seg.bp[ind, ], nc=2 );                      
                           if ( col.pos[1, 1] <= (i-1)*size )  col.pos [1, 1] <- (i-1)*size;
                           if ( col.pos[ nrow(col.pos), 2] > i*size ) col.pos[ nrow(col.pos),2] <- i*size;                        
                           countc[i] <- sum( col.pos[,2]-col.pos[,1]+1) }   
                                             

                        ind <- which ( van.seg.bp[,2] > (i-1)*size  &  van.seg.bp[,1] <=  i*size  )
                        if( length(ind) >0 )
                          {van.pos <- matrix( van.seg.bp[ind, ], nc=2 );                      
                           if ( van.pos[1, 1] <= (i-1)*size )  van.pos [1, 1] <- (i-1)*size;
                           if ( van.pos[ nrow(van.pos), 2] > i*size ) van.pos[ nrow(van.pos),2] <- i*size;                          
                           countv[i] <- sum( van.pos[,2]-van.pos[,1]+1) } 
                       }

                       
      
   if (chrom %in% c(1,2,4) ) {plot( seq(50000, page*size, 100000), (countc/100000), "h", col="orange", xlim=c(0, chr.len[1]), ylim=c(0,1.6), xaxt="n" , ylab="")
                              lines ( seq(50000, page*size, 100000), (countv/100000), "h", col="green")
                              points( sfpc, jitter( rep(1.3, length(sfpc) ), factor=10), pch=".", cex=0.1, col="orange")
                              points( sfpv, jitter( rep(1, length(sfpv) ), factor=1), pch=".", cex=0.1, col="green")
                              rect( cen[chrom,1], -0.05, cen[chrom, 2], 0.05)
                              legend( "right", paste("chr", chrom, sep=""), bty="n")
                              }
 
   if (chrom == 3)            {plot( seq(50000, page*size, 100000), (countc/100000), "h", col="orange", xlim=c(0, chr.len[1]), ylim=c(0,1.6), ylab="freq of indels", xaxt="n"   )
                              lines ( seq(50000, page*size, 100000), (countv/100000), "h", col="green") 
                              points( sfpc, jitter( rep(1.3, length(sfpc) ), factor=10), pch=".", cex=0.1, col="orange")
                              points( sfpv, jitter( rep(1, length(sfpv) ), factor=1),  pch=".", cex=0.1, col="green")
                              rect( cen[chrom,1], -0.05, cen[chrom, 2], 0.05)
                              legend( "right", paste("chr", chrom, sep=""), bty="n")
                              }
  

   if (chrom == 5)            {plot( seq(50000, page*size, 100000), (countc/100000), "h", col="orange", xlim=c(0, chr.len[1]), ylim=c(0,1.6), ylab="" )
                              lines ( seq(50000, page*size, 100000), (countv/100000), "h", col="green") 
                              points( sfpc, jitter( rep(1.3, length(sfpc) ), factor=10 ), pch=".", cex=0.1, col="orange")
                              points( sfpv, jitter( rep(1, length(sfpv) ), factor=1 ), pch=".", cex=0.1, col="green")
                              rect( cen[chrom,1], -0.05, cen[chrom, 2], 0.05)
                              legend( "right", paste("chr", chrom, sep=""), bty="n")
                              }
   
   
  }
 }
 dev.off()





###chromosome distribution without SFPs
 setwd ("final/segment")
 load ("aic.RData")
  

 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 
 size <- 100000
 cen <- matrix(scan("../CENs.txt", what=1, nline=5), byrow=T, nc=2 )

  
 pdf ("chrom.dist.pdf", width=4, height=4)
 
{
 par( mfrow=c(5,1),  omi=c(0.3, 0, 0.2, 0.62), mai=c(0, 0.82, 0, 0), yaxp=c(0, 0.8, 1), tck=-0.04 )



 for (chrom in 1:5)
 {
 
   col.seg.bp <- col[[chrom]]
   van.seg.bp <- van[[chrom]]
   
 
   page <- trunc( chr.len[chrom] /size ) 
   countc <- countv <- rep(NA, page)
   
 
   for (i in 1:page ) { ind <- which ( col.seg.bp[,2] > (i-1)*size  &  col.seg.bp[,1] <=  i*size  )
                        if( length(ind) >0 )
                          {col.pos <- matrix( col.seg.bp[ind, ], nc=2 );                      
                           if ( col.pos[1, 1] <= (i-1)*size )  col.pos [1, 1] <- (i-1)*size;
                           if ( col.pos[ nrow(col.pos), 2] > i*size ) col.pos[ nrow(col.pos),2] <- i*size;                        
                           countc[i] <- sum( col.pos[,2]-col.pos[,1]+1) }   
                                             

                        ind <- which ( van.seg.bp[,2] > (i-1)*size  &  van.seg.bp[,1] <=  i*size  )
                        if( length(ind) >0 )
                          {van.pos <- matrix( van.seg.bp[ind, ], nc=2 );                      
                           if ( van.pos[1, 1] <= (i-1)*size )  van.pos [1, 1] <- (i-1)*size;
                           if ( van.pos[ nrow(van.pos), 2] > i*size ) van.pos[ nrow(van.pos),2] <- i*size;                          
                           countv[i] <- sum( van.pos[,2]-van.pos[,1]+1) } 
                       }

                       
      
   if (chrom %in% c(1,2,4) ) {plot( seq(50000, page*size, 100000), (countc/100000), "h", col="orange", xlim=c(0, chr.len[1]), ylim=c(0,1), xaxt="n" , ylab="", cex.axis=0.8, mgp=c(3, 0.1, 0))
                              lines ( seq(50000, page*size, 100000), (countv/100000), "h", col="blue")
                              rect( cen[chrom,1], -0.05, cen[chrom, 2], 0.05)
                              legend( "topright", paste("chr", chrom, sep=""), bty="n")
                              if( chrom == 1) legend( "topleft", c("deletion", "duplication"), col=c("orange", "blue"), lty=c(1,1), bty="n" ) 
                              }
 
   if (chrom == 3)            {plot( seq(50000, page*size, 100000), (countc/100000), "h", col="orange", xlim=c(0, chr.len[1]), ylim=c(0,1), ylab="freq of indels", xaxt="n" , cex.axis=0.8, mgp=c(1.5, 0.1, 0) )
                              lines ( seq(50000, page*size, 100000), (countv/100000), "h", col="blue") 
                              rect( cen[chrom,1], -0.05, cen[chrom, 2], 0.05)
                              legend( "topright", paste("chr", chrom, sep=""), bty="n")
                              }
  

   if (chrom == 5)            {plot( seq(50000, page*size, 100000), (countc/100000), "h", col="orange", xlim=c(0, chr.len[1]), ylim=c(0,1), ylab="", cex.axis=0.8, mgp=c(3, 0.1, 0) )
                              lines ( seq(50000, page*size, 100000), (countv/100000), "h", col="blue") 
                              rect( cen[chrom,1], -0.05, cen[chrom, 2], 0.05)
                              legend( "topright", paste("chr", chrom, sep=""), bty="n")
                              mtext( "bp", at=15000000, line=-7.4, cex=0.7)
                              }
  
   
   
  }
 }
 dev.off()






######################################################################### length distri


 setwd ("final/segment")
 load ("aic.RData")
 
 c <- v <- matrix( numeric(0), nc=2)
 for (chrom in 1:5)
 { c <- rbind(c, col[[chrom]])
   v <- rbind(v, van[[chrom]]) }

 
 a <-  c[,2]-c[,1]+1
 b <-  v[,2]-v[,1]+1

 a0 <- a [a > 10000]
 b0 <- b [b > 10000]
  
 a <- a[ a <=10000]
 b <- b[ b <=10000]

 a1 <- hist(a, breaks=50, plot=F)$mids 
 a1 <- c(a1, 11000)
 b1 <- hist(b, breaks=50, plot=F)$mids
 b1 <- c(b1, 11000) 
  
 a2 <-  c( hist(a, breaks=50, plot=F)$counts, length(a0))
 b2 <- c( hist(b, breaks=50, plot=F)$counts, length(b0))
 


 pdf ("indel.length.pdf") 
 
  xlim <- range(c(a1/1000, b1/1000))
  ylim <- range(c(a2, b2))
  plot( a1/1000, a2, "p", pch=15, cex=0.8, col="orange", xlim=xlim, ylim=ylim, xlab="indels length in kb", ylab="number of indels", cex.lab=1.2)
  points ( b1/1000, b2, "p", pch=15, cex=0.8, col="blue")
  lines( a1/1000, a2, "l", col="orange", lwd=2)
  lines( b1/1000, b2, "l", col="blue", lwd=2)

  legend( "topright", c("deletion", "duplication"), col=c("orange", "blue"), lty=1, lwd=2, bty="n", cex=1.2)
  axis(1, at=seq(0, 11, 1), labels= c(as.character( seq(0, 10, 1) ), ">10") )
 dev.off()






#####################################################################density difference
 
 
 setwd ("final/segment")

 load ("../gDNA.sc.nq.RData")
 load ("../attile1V7anno.RData")
 gDNA <- gDNA.nq [, 1:8]
 gDNA <- abs(rowMeans(gDNA[,1:4]) - rowMeans(gDNA[,5:8]) )
 

 load ("aic.RData")
 
 gc <- gv <- c()

 for (chrom in 1:5)
 { 
   bp.chr <- ( as.numeric(attile1$bpstart)-12)[ attile1$chr==chrom] 
   gDNA.chr <- gDNA[ attile1$chr==chrom]
   c <-  col[[chrom]]
   v <-  van[[chrom]]
  

   for (i in 1:nrow(c) )
    {probes <- which (bp.chr >= c[i,1] & bp.chr <= c[i,2])
    gc <- c(gc, median(gDNA.chr[probes])  )}

   for (i in 1:nrow(v) )
    {probes <- which (bp.chr >= v[i,1] & bp.chr <= v[i,2])
    gv <- c(gv, median(gDNA.chr[probes])  )}
 
  
 }


 his.c <- hist(gc, breaks=20, plot=F)
 his.v <- hist(gv, breaks=10, plot=F)
 his.n <- hist(gDNA, breaks=200, plot=F)
 xc <- his.c$mids
 xv <- his.v$mids
 xn <- his.n$mids
 yc <- his.c$density
 yv <- his.v$density
 yn <- his.n$density


 xlim=range(xc, xv, xn)
 ylim=range(yc, yv, yn)


 pdf ("indel.his.pdf")
   plot( xn, yn, "h", col="black", xlim=xlim, ylim=ylim, lwd=3, xlab="median probe log intensity difference", ylab="density")
   lines ( xv, yv, "h", col="blue", lwd=3)
   lines (xc, yc, "h", col="orange", lwd=3)
   legend ("topright", c("deletion", "duplication", "null"), col=c("orange", "blue", "black"), lwd=3, bty="n")
 dev.off()



 pdf("indel.density.pdf")
   plot( density(gDNA) , col="black", lwd=2,  main="", xlim=c(-0.1, 1.5), xlab="median probe log intensity difference", cex.lab=1.2)
   lines( density(gv), col="blue", lwd=2)
   lines( density(gc), col="orange", lwd=2)
   legend ("topright", c("deletion", "duplication", "null"), col=c("orange", "blue", "black"), lwd=2, bty="n", cex=1.2)
 dev.off()





#######################################################################################expression and deletion 



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

 library(affy)
 mRNA.nq <- normalize.quantiles(mprobe.mean); rm(mprobe.mean); gc()


 gene <- names(table( as.character(attile1$gene) ) )
 gene <- gene[ -length(gene)] 
 gene.mean <- matrix(NA, nr=length(gene), nc=2) 
 rownames(gene.mean) <- gene

 for (i in 1:length(gene) )
 {
  probes <- which( as.character(attile1$gene) %in% gene[i])
  gene.mean[i,] <- c( mean( mRNA.nq[probes,1:4]), mean( mRNA.nq[probes,5:8]) )
  if(i/100 == trunc(i/100) ) cat(i, "\n")
 }
 
 save(gene.mean, file="segment/gene.mean.RData", compress=T)




###for genes totally within indels

 setwd ("final")

 load ("attile1V7anno.RData") 
 load ("segment/gene.mean.RData")
 load ("segment/aic.RData")


 col.gene <- van.gene <- c()

 for (chrom in 1:5)
 {
  cgene <- vgene <- c()

  col.seg.bp <- col[[chrom]]
  van.seg.bp <- van[[chrom]]
  
  attile <- attile1[attile1$chr==chrom,]
  attile$mid <- as.numeric(attile$bpstart) - 12
  gene.range <- tapply( as.numeric(attile$mid), as.character(attile$gene), range)
  names <- names(gene.range)[- length(gene.range) ]
  gene.range <- matrix(unlist(gene.range), byrow=T, nc=2)
  gene.range <- gene.range[-nrow(gene.range), ]
  rownames(gene.range) <- names
  len <- tapply( as.numeric(attile$mid), as.character(attile$gene), length)
  len <- names(len)[which(len<3)]
  gene.range <- gene.range[ - which( rownames(gene.range) %in% len ), ]
 

  for (i in 1:nrow(col.seg.bp) )
   { gene <- which(col.seg.bp[i,1] <= gene.range[,1]  & col.seg.bp[i,2] >= gene.range[,2] )   
     if( length(gene) >0) cgene <- c(cgene, names(gene)) }


  for (i in 1:nrow(van.seg.bp) )
   { gene <- which(van.seg.bp[i,1] <= gene.range[,1] & van.seg.bp[i,2] >= gene.range[,2])   
     if( length(gene) >0) vgene <- c(vgene, names(gene)) }

  col.gene <- c(col.gene, cgene)
  van.gene <- c(van.gene, vgene)
 
 }


 pdf ("segment/del.col.expression.pdf")  
  null <- matrix (NA, nc=1000, nr=length(col.gene) )
     for (i in 1:1000)
     null[,i] <- sample( gene.mean[,1], length(col.gene))
     null <- apply(null, 2, sort)
     null <- rowMeans(null)
  plot(density( null), col="black", lwd=2, main="", xlab="mean gene log intensity in Col", ylab="density", ylim=c(0, 2), cex.lab=1.2 )
  lines(  density( gene.mean[,1][ which(rownames(gene.mean) %in% col.gene ) ]), col="orange", lwd=2)
  legend( "topright", c("genes deleted in Van", "null"), col=c("orange", "black"), lty=1, lwd=2, bty="n", cex=1.2)
 dev.off()
 




 cmean <-  cdif <- c()
 for (i in 1:length(col.gene) )
  { cmean <- c(cmean, gene.mean[which(rownames(gene.mean)==col.gene[i]),1])
    cdif <- c(cdif, (gene.mean[,1]-gene.mean[,2]) [which(rownames(gene.mean)==col.gene[i]) ]  ) 
  }

 vmean <- vdif <- c()
 for (i in 1:length(van.gene) )
  { vmean <- c(vmean, gene.mean[which(rownames(gene.mean)==van.gene[i]),1])
    vdif <- c(vdif, (gene.mean[,1]-gene.mean[,2]) [which(rownames(gene.mean)==van.gene[i]) ]  ) 
  }

 xlim <- range( c(cmean, vmean) )
 ylim <- range( c(cdif, vdif) )

 pdf ("segment/indel.expr.pdf") 
 plot(cmean, cdif, "p", xlab="col.gene.mean", ylab="col.gene.mean-van.gene.mean", pch=16, col="orange", cex=0.5, xlim=xlim, ylim=ylim )
 points(vmean, vdif, pch=16, col="green", cex=0.5)
 abline(h=0)
 legend( "topleft", c( "genes deleted in Van", "genes duplicated in Van"), col=c("orange", "green"), pch=16, bty="n")
 dev.off()







#####################################################################################plot indels and expression along chromosomes

 setwd ("final1")

 
 load ("attile1V7anno.RData")

 load ("hmm/gDNA.sc.nq.RData")
 gDNA.nq <- gDNA.nq[, 1:8]
 gDNA.nq <- gDNA.nq - rowMeans(gDNA.nq) 
 
 load ("hmm/gDNA/mRNA.nq.allg.RData")
 mRNA.nq <- mRNA.nq[, 1:8]
 mRNA.nq <- mRNA.nq - rowMeans(mRNA.nq)
 


### 

 load ("hmm/gDNA/deldup.aic.RData")

 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 
 size <- 20000


 for (chrom in 1:5)
 {

  col.seg.bp <- col[[chrom]]-24
  van.seg.bp <- van[[chrom]]-24
  
  attile <- attile1[attile1$chr==chrom,]
  attile$mid <- as.numeric(attile$bpstart) - 12
  gene.range <- tapply(as.numeric(attile$mid), as.character(attile$gene), range)
  names <- names(gene.range)[- length(gene.range) ]
  gene.range <- matrix(unlist(gene.range), byrow=T, nc=2)
  gene.range <- gene.range[-nrow(gene.range), ]
  rownames(gene.range) <- names
 
 
  gDNA <- gDNA.nq[ attile1$chr==chrom, ]
  mRNA <- mRNA.nq[ attile1$chr==chrom, ]



  page <- trunc( chr.len[chrom] /size ) +1

  pdf( paste("hmm/gDNA/indelexpr.chr", chrom, ".pdf", sep=""), width=10, height=5 )
  par(mfrow=c(1,2) )
  
   for (i in 1:page) 
   {
    probes <- which ( attile$mid > (i-1)*size  &  attile$mid <=  i*size  )

    gene.ind <- which( gene.range [,2] > (i-1)*size & gene.range [,1] <= i*size )
    if ( length(gene.ind) >0 )  
                     {gene.pos <- matrix( gene.range[gene.ind, ], nc=2 ); 
                      rownames(gene.pos) <- rownames(gene.range)[gene.ind];                    
                      if ( gene.pos[1, 1] <= (i-1)*size )  gene.pos [1, 1] <- (i-1)*size;
                      if ( gene.pos[ nrow(gene.pos), 2] > i*size ) gene.pos[ nrow(gene.pos),2] <- i*size}                     
    n <- length(gene.ind)
    
 
   
    col.ind <- which( col.seg.bp [,2] > (i-1)*size & col.seg.bp [,1] <= i*size )
    if ( length(col.ind) >0 )  
                      {col.pos <- matrix( col.seg.bp[col.ind, ], nc=2 );                      
                      if ( col.pos[1, 1] <= (i-1)*size )  col.pos [1, 1] <- (i-1)*size;
                      if ( col.pos[ nrow(col.pos), 2] > i*size ) col.pos[ nrow(col.pos),2] <- i*size}
    

    
    van.ind <- which( van.seg.bp [,2] > (i-1)*size & van.seg.bp [,1] <= i*size )
    if ( length(van.ind) >0 )  
                      {van.pos <- matrix( van.seg.bp[van.ind, ], nc=2 );                      
                      if ( van.pos[1, 1] <= (i-1)*size )  van.pos [1, 1] <- (i-1)*size;
                      if ( van.pos[ nrow(van.pos), 2] > i*size ) van.pos[ nrow(van.pos),2] <- i*size}
       

     
    if ( length(probes) > 0)  
      { 
       matplot( matrix(attile$mid [probes], nc=1), matrix(gDNA[probes, ], nc=8), pch=".",  xlim=c( size*(i-1), i*size ), ylim=c(-1.0, 1.0), xlab="bp", ylab="gDNA log intensity difference",  col=rep(c("red","black"), each=4) ) 
       if ( length(col.ind) > 0 ) rect( col.pos[, 1], rep(-0.05, length(col.ind)), col.pos[, 2], rep(0, length(col.ind) ), col="orange")
       if ( length(van.ind) > 0 ) rect( van.pos[, 1], rep(-0.05, length(van.ind)), van.pos[, 2], rep(0, length(van.ind) ), col="green")
       if ( n > 0) 
            {rect(gene.pos[,1], rep(0, n), gene.pos[,2], rep(0.05, n), col="blue")}
                          

       matplot( matrix(attile$mid [probes], nc=1), matrix(mRNA[probes, ], nc=8), pch=".",  xlim=c( size*(i-1), i*size ), ylim=c(-1.0, 1.0), xlab="bp", ylab="mRNA log intensity difference",  col=rep(c("red","black"), each=4) ) 
       if ( length(col.ind) > 0 ) rect( col.pos[, 1], rep(-0.05, length(col.ind)), col.pos[, 2], rep(0, length(col.ind) ), col="orange")
       if ( length(van.ind) > 0 ) rect( van.pos[, 1], rep(-0.05, length(van.ind)), van.pos[, 2], rep(0, length(van.ind) ), col="green")
       if ( n > 0) 
            {rect(gene.pos[,1], rep(0, n), gene.pos[,2], rep(0.05, n), col="blue");
             label <- c( rownames(gene.range)[gene.ind] );                    
             legend("topleft",label, bty="n", cex=0.8)}             
      } 
   }
  dev.off()

 }




###################################################################################one example for deletion/expression



 setwd ("final1/hmm/gDNA")

 load ("../attile1V7anno.RData")
 load ("../gDNA.sc.nq.RData")
 load ("mRNA.nq.allg.RData")
 
 gDNA.nq <- gDNA.nq[,1:8]
 gDNA.nq <- gDNA.nq - rowMeans(gDNA.nq)

 mRNA.nq <- mRNA.nq[, 1:8]
 mRNA.nq <- mRNA.nq - rowMeans(mRNA.nq) 
 
 
 
 chr.len <-  c(30432563, 19705359, 23470805, 18585042, 26992728) 
 size <- 20000

 chrom <- 4
 i <- 466 

  
 attile <- attile1[ attile1$chr==chrom,]
 attile$mid <- as.numeric(attile$bpstart) - 12
 gene.range <- tapply( as.numeric(attile$mid), as.character(attile$gene), range)
 names <- names(gene.range)[- length(gene.range) ]
 gene.range <- matrix(unlist(gene.range), byrow=T, nc=2)
 gene.range <- gene.range[-nrow(gene.range), ]
 rownames(gene.range) <- names
 
 gDNA <- gDNA.nq[ attile1$chr==chrom, ]
 mRNA <- mRNA.nq[ attile1$chr==chrom, ]


 load ("deldup.aic.RData")
 col.seg.bp <- col[[chrom]]-24
 van.seg.bp <- van[[chrom]]-24



 pdf( paste("chr", chrom, ".page", i, ".pdf", sep=""), width=6, height=6 )
 par(mfrow=c(2,1), mai=c(0.65, 0.9, 0.2, 0.2)  )
  
   probes <- which ( attile$mid > (i-1)*size  &  attile$mid <=  i*size  )

   gene.ind <- which( gene.range [,2] > (i-1)*size & gene.range [,1] <= i*size )
   if ( length(gene.ind) >0 )  
                     {gene.pos <- matrix( gene.range[gene.ind, ], nc=2 ); 
                      rownames(gene.pos) <- rownames(gene.range)[gene.ind];                    
                      if ( gene.pos[1, 1] <= (i-1)*size )  gene.pos [1, 1] <- (i-1)*size;
                      if ( gene.pos[ nrow(gene.pos), 2] > i*size ) gene.pos[ nrow(gene.pos),2] <- i*size}                     
   n <- length(gene.ind)
    
 
   
   col.ind <- which( col.seg.bp [,2] > (i-1)*size & col.seg.bp [,1] <= i*size )
   if ( length(col.ind) >0 )  
                      {col.pos <- matrix( col.seg.bp[col.ind, ], nc=2 );                      
                      if ( col.pos[1, 1] <= (i-1)*size )  col.pos [1, 1] <- (i-1)*size;
                      if ( col.pos[ nrow(col.pos), 2] > i*size ) col.pos[ nrow(col.pos),2] <- i*size}
    

    
   van.ind <- which( van.seg.bp [,2] > (i-1)*size & van.seg.bp [,1] <= i*size )
   if ( length(van.ind) >0 )  
                      {van.pos <- matrix( van.seg.bp[van.ind, ], nc=2 );                      
                      if ( van.pos[1, 1] <= (i-1)*size )  van.pos [1, 1] <- (i-1)*size;
                      if ( van.pos[ nrow(van.pos), 2] > i*size ) van.pos[ nrow(van.pos),2] <- i*size}
       

     
   if ( length(probes) > 0)  
      { 
       matplot( matrix(attile$mid [probes], nc=1), matrix(gDNA[probes, ], nc=8), pch=".",  xlim=c( size*(i-1), i*size ), ylim=c(-1.0, 1.0), xlab="", ylab="log intensity difference",  col=rep(c("red","black"), each=4) ) 
       legend("topright", "gDNA", bty="n")
       if ( length(col.ind) > 0 ) rect( col.pos[, 1], rep(-0.06, length(col.ind)), col.pos[, 2], rep(0, length(col.ind) ), col="orange", border=NA)
       if ( length(van.ind) > 0 ) rect( van.pos[, 1], rep(-0.06, length(van.ind)), van.pos[, 2], rep(0, length(van.ind) ), col="green", border=NA)
       if ( n > 0) 
            {rect(gene.pos[,1], rep(0, n), gene.pos[,2], rep(0.05, n), col="blue", border=NA)}
                          

       matplot( matrix(attile$mid [probes], nc=1), matrix(mRNA[probes, ], nc=8), pch=".",  xlim=c( size*(i-1), i*size ), ylim=c(-1.0, 1.0), xlab="", ylab="log intensity difference",  col=rep(c("red","black"), each=4) ) 
       legend("topright", "mRNA", bty="n")
       mtext("bp", at=i*20000-10000, line=-14)
       if ( length(col.ind) > 0 ) rect( col.pos[, 1], rep(-0.05, length(col.ind)), col.pos[, 2], rep(0, length(col.ind) ), col="orange", border=NA)
       if ( length(van.ind) > 0 ) rect( van.pos[, 1], rep(-0.05, length(van.ind)), van.pos[, 2], rep(0, length(van.ind) ), col="green", border=NA)
       if ( n > 0) 
            {rect(gene.pos[,1], rep(0, n), gene.pos[,2], rep(0.05, n), col="blue", border=NA);
             label <- c( rownames(gene.range)[gene.ind] );                    
             legend("topleft",label, bty="n", cex=0.6)}             
       } 
   
  dev.off()









