R/plotMX.R

Defines functions plotMX metaPlot

Documented in metaPlot plotMX

#' plotMX
#'
#' This function plots the matrix calculated by tssMap or peakMap.
#' @param mxls
#' A list of matrix generated by tssMap or peakMap,must be the same dimensions.
#' @param output
#' A PDF file name for image to save.
#' @param color
#' The color to be used in the image.
#' @param cutoff
#' Color saturate value. Default is 1. The color will change between 0 to 1 and saturate above 1.
#' @return PDF images of the matrix and color bar. For enrichment profile, color bar is useless.
#' @export

plotMX = function(mxls,output,cutoff=1,color=c("white","blue"))
{
	pal <- colorRampPalette(color)
	x=do.call(cbind,mxls)
	x=apply(x,2,rev)
	x=t(x)
	#increase size can make it smoother, save as pdf can make it like in java treeview
	pdf(output,width=length(mxls)*2,height=10)
		par(mar=c(1,1,10,1))
		image(x, col=pal(250),breaks=c(seq(0,cutoff,length=250),max(x)),useRaster=T,xaxt="n",yaxt="n")
		n=0.5/length(mxls)
		text(seq(n,1-n,by=n*2),par("usr")[2]+0.1,labels=names(mxls),srt=30,pos=1,xpd=T)
	dev.off()

	# color bar in separate figure
	maxcol=color[length(color)]
	col=pal(200)
	pdf(paste0("color_bar.",output),width=4,height=1.5)
		par(mar=c(4,1,1,1))
		barplot(rep(1,300),col=c(col,rep(maxcol,100)),space=0,border=F,axes=F,xlab="reads per million (RPM)")
		axis(side=1,at=c(0,50,100,150,200,300),labels=c(0,0.25,0.5,0.75,1,round(max(x))) )
	dev.off()
}

#' metaPlot
#'
#' This function plots the mean of each column to give a summary profile of the matrix, which is usually a hat shape.
#' @param mxls
#' A list of matrix generated by tssMap or peakMap,must be the same dimensions.
#' @param titles
#' Figure title and output file name
#' @param out.pdf
#' Save image as PDF or PNG. Default is PNG.
#' @param color
#' line colors
#' @param center
#' center type of the matrix, 'peak' or 'tss'. Default is 'peak'.
#' @param label_win
#' label of the window size. Default is '5 kb'.
#' @param ylim
#' y limit of the plot. Default is 0 to the maximum value of the matrix.
#' @return plot
#' @export

metaPlot=function(mxls,titles,out.pdf=F,color=NA,center="peak",label_win="5 kb",ylim=NA)
{
	ncols=ncol(mxls[[1]])
	meanls=lapply(mxls,colMeans)
	meanmx=do.call(cbind,meanls)

	if (is.na(color)) color=1:length(mxls)

	if (out.pdf==T) pdf(paste0("metaplot.",titles,".pdf")) else
	png(paste0("metaplot.",titles,".png"))
		if (is.na(ylim)) ylim=c(0,max(meanmx))
		matplot(1:ncols,meanmx,pch=20,type="l",lty=1,lwd=2,col=color,xlab="",ylab="", xaxt="n",ylim=ylim, main=titles)
		legend("topleft",names(mxls),col=color,lty=1,bty='n')

		if (center=="peak") axis(1, at=c(0,ncols/2,ncols),labels=c(paste0("-",label_win),"Peak center",label_win), col.axis="black", las=1) else
			axis(1, at=c(0,ncols/2,ncols),labels=c(paste0("-",label_win),"TSS",label_win), col.axis="black", las=1)
	dev.off()
}
xons/chipMap documentation built on May 4, 2019, 1:25 p.m.