Preprocessing:
 chipcenter -z -s 70 -c 10 /home/local/db/mga/hg19/barski07/H3K4me3.sga 2>/dev/null > /home/local/www-ccg/daily/H3K4me3_32733.TAR_sgaf_centered_in

Command pipeline:
 sort -s -m -k1,1 -k3,3n -k4,4 /home/local/db/mga/hg19/epd/Hs_EPDnew_003_hg19.sga /home/local/www-ccg/daily/H3K4me3_32733.TAR_sgaf_centered_in > /home/local/www-ccg/daily/TSS_H3K4me3_32733.sgaf_in; chipextract -A "TSS o" -B "H3K4me3 a" -b -1000 -e 1000 -w 20 -c 10 /home/local/www-ccg/daily/TSS_H3K4me3_32733.sgaf_in  2>/home/local/www-ccg/daily/chipextract_CMD_27162_32733.log |

ChIP-Extract output file:
 /home/local/www-ccg/daily/chipextract_27162_32733.out

R Script to generate the heat map and the AG Plot:
 png("chipextract_27162_32733_graph.png", width=10, height=14, pointsize=12, units="in", res=72)
 library(zoo)
 data <- as.matrix(read.table("chipextract_27162_32733.out", check.names=F))
 xaxis <- as.numeric(colnames(data))
 color <- colorRampPalette(c("white", "red"), space = "rgb")(100)
 color2 <- colorRampPalette(c("white", "darkgreen"), space = "rgb")(100)
 condense.matrix <- function(mat, rows=20){
   groups <- sort(rep(1:ceiling(dim(mat)[1]/rows), rows))[1:dim(mat)[1]]
   c.mat <- matrix(ncol=dim(mat)[2], nrow=max(groups))
   for (I in 1:dim(mat)[2]){
     c.mat[,I] <- tapply(mat[,I], groups, mean, na.rm=T)
   }
   colnames(c.mat) = colnames(mat)
   return(c.mat)
 }
 # K-means clustering
 k=3; km=kmeans(data, k);
 c=1:dim(data)[1]; for(i in k) {
     c[which(km$cluster == i)] = cov(t(data[which(km$cluster == i),]), km$center[i,])
 }
 order=order(km$cluster, c)
 # Condense matrix (average over groups of rows)
 w <- ceiling(dim(data)[1]/1000)
 x <- condense.matrix(data[order,], rows=w)
 # Adjust color range
 max <- sort(x, decreasing=T)[0.003*dim(x)[1]*dim(x)[2]]
 for(i in 1:dim(x)[1]) {for (j in 1:dim(x)[2]) {x[i,j]=min(x[i,j], max)}}
 layout(matrix(c(1,2), nrow=2, ncol=1), heights=c(3.0,1.5))
 par(mar=c(4,5,0,2), oma=c(0,0,2,0))
 image(t(x), col=color, xaxt="n", yaxt="n", bty="n")
 if (k == 1) {h <- 1} else
     {h <- km$size[1]; for (i in 2:k) {h <- c(h,h[i-1]+km$size[i])}; h <- h/h[k]; h <- c(0,h)}
 # Add vertical segments showing the clusters
 library(RColorBrewer)
 mypalette <- c(brewer.pal(9, "Set1"), brewer.pal(3,"Set3"))
 i <- 1
 for (j in 2:(k+1)) { segments(x0=0, y0=h[i], y1=h[j], col=mypalette[i], lwd=16, lend=1); text(0.015,  h[i]+ 0.005, i, cex =0.8); i <- i + 1}
 data2 <- as.matrix(read.table("chipextract_27162_32733.sga"))
 new_sga <- cbind(data2, km$cluster)
 write.table(new_sga, col.names=F, row.names=F, file="chipextract_27162_32733.new.sga", quote=F, sep="	")
 box(col="darkgrey")
 plot(xaxis, colMeans(data), type="l", lwd=4, ylab="Average counts", xlab="Relative distance to TSS", cex.axis=1.5, cex.lab=1.5, bty="n")
 dev.off()

Generate Ref BED and FPS Files
 sga2bed -r -e 6:4,7:7 /home/local/www-ccg/daily/chipextract_27162_32733.new.sga > /home/local/www-ccg/daily/chipextract_27162_32733.new.bed
 sga2fps.pl -f TSS -s hg19 /home/local/www-ccg/daily/chipextract_27162_32733.new.sga > /home/local/www-ccg/daily/chipextract_27162_32733.new.fps

Generate SGA Files for each class:
 awk '$7 == 1' /home/local/www-ccg/daily/chipextract_27162_32733.new.sga > /home/local/www-ccg/daily/chipextract_27162_32733.c1.sga
 awk '$7 == 2' /home/local/www-ccg/daily/chipextract_27162_32733.new.sga > /home/local/www-ccg/daily/chipextract_27162_32733.c2.sga
 awk '$7 == 3' /home/local/www-ccg/daily/chipextract_27162_32733.new.sga > /home/local/www-ccg/daily/chipextract_27162_32733.c3.sga
