Skip to content

Instantly share code, notes, and snippets.

@ianyfchang
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianyfchang/9bf7c6d49675eae0a782 to your computer and use it in GitHub Desktop.
Save ianyfchang/9bf7c6d49675eae0a782 to your computer and use it in GitHub Desktop.
A shell script generates R scripts to plot heat map of HMR
# 檢查 ggplot2 與 grid 套件是否安裝
echo 'list.of.packages <- c("ggplot2","grid")'
echo 'new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]'
echo 'if(length(new.packages)) install.packages(new.packages)'
# 載入 ggplot2 套件
echo 'library(ggplot2) '
# 將多張圖繪製成同一張圖的函式 (http://stackoverflow.com/questions/11721401/r-save-multiplot-to-file)
echo 'multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { '
echo ' require(grid) '
echo ' # Make a list from the ... arguments and plotlist '
echo ' plots <- c(list(...), plotlist) '
echo ' numPlots = length(plots) '
echo ' # If layout is NULL, then use cols to determine layout '
echo ' if (is.null(layout)) { '
echo ' # Make the panel '
echo ' # ncol: Number of columns of plots '
echo ' # nrow: Number of rows needed, calculated from # of cols '
echo ' layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), '
echo ' ncol = cols, nrow = ceiling(numPlots/cols)) '
echo ' } '
echo 'if (numPlots==1) { '
echo ' print(plots[[1]]) '
echo ' } else { '
echo ' # Set up the page '
echo ' grid.newpage() '
echo ' pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) '
echo ' # Make each plot, in the correct location '
echo ' for (i in 1:numPlots) { '
echo ' # Get the i,j matrix positions of the regions that contain this subplot '
echo ' matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) '
echo ' '
echo ' print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, '
echo ' layout.pos.col = matchidx$col)) '
echo ' } '
echo ' } '
echo '} '
# 將背景與格線清空
echo 'bg<-theme(panel.background = element_rect(fill = "transparent",colour = "black"),panel.grid.minor = element_line(color="gray"), panel.grid.major = element_line(color="#EEEEEE"),strip.text.x=element_text(size = rel(1.7)), strip.background = element_rect( fill = "transparent" )) '
# 輸入的檔案名稱
echo 'file="'$1'" '
# 讀檔
echo 'hmr=read.delim(file, sep="\t",header=F) '
# 設定欄位名稱
echo 'colnames(hmr)=c("chrom","start","end","binidx","tsscgi","methy","rowidx") '
#split into tss, cgi; ntss, cgi; tss, ncgi; ntss, ncgi
echo 'tsscgi = subset(hmr, hmr$tsscgi=="TSS, CGI") '
echo 'tssncgi = subset(hmr, hmr$tsscgi=="TSS, nCGI") '
echo 'ntsscgi = subset(hmr, hmr$tsscgi=="nTSS, CGI") '
echo 'ntssncgi = subset(hmr, hmr$tsscgi=="nTSS, nCGI") '
# 清除 X-軸、Y-軸的資訊
echo 'blankall=theme(axis.text.y = element_blank(),axis.text.x = element_blank(), axis.title.y = element_blank(),axis.title.x = element_blank(), axis.ticks.y = element_blank(),axis.ticks.x = element_blank(), legend.position="none") '
echo 'blanky=theme(axis.ticks.y = element_blank(),axis.title.y = element_blank(),axis.text.y = element_blank(),legend.position="none") '
# 繪圖
echo 'tsscgi_plot =ggplot(tsscgi ,aes(x=binidx,y=factor(rowidx),fill=methy))+geom_tile()+scale_fill_gradient2(low="#006600",high="#660000",mid="white",na.value="#CCCCCC",guide="colourbar")+blankall+bg+labs("TSS, CGI") '
echo 'tssncgi_plot =ggplot(tssncgi ,aes(x=binidx,y=factor(rowidx),fill=methy))+geom_tile()+scale_fill_gradient2(low="#006600",high="#660000",mid="white",na.value="#CCCCCC",guide="colourbar")+blankall+bg+labs("TSS, nCGI") '
echo 'ntsscgi_plot =ggplot(ntsscgi ,aes(x=binidx,y=factor(rowidx),fill=methy))+geom_tile()+scale_fill_gradient2(low="#006600",high="#660000",mid="white",na.value="#CCCCCC",guide="colourbar")+blankall+bg+labs("nTSS, CGI") '
echo 'ntssncgi_plot=ggplot(ntssncgi,aes(x=binidx,y=factor(rowidx),fill=methy))+geom_tile()+scale_fill_gradient2(low="#006600",high="#660000",mid="white",na.value="#CCCCCC",guide="colourbar")+blankall+bg+labs("nTSS, nCGI") '
# 輸出圖檔至 PDF 檔
echo 'pdf(paste(file,".pdf",sep=""),height=8,width=16) '
echo 'multiplot(tsscgi_plot, tssncgi_plot, ntsscgi_plot, ntssncgi_plot, cols=4); '
echo 'dev.off() '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment