类似于maga多readsSNP的图示R脚本

以下是对多readsSNP的图示

image.png
args <- commandArgs(TRUE)
input <- args[1]
outpdf <- args[2]
if(length(args)<2){
    print('Usages:Rscript XXX.R input.marker.xls output.marker.pdf')
    q()
}
options(stringsAsFactors=F)
df <- read.table(input,header=T,sep="\t",check.names=F)
if(nrow(df)>60) df = tail(head(df,n=96),n=60)
df <- df[nrow(df):1,]
data <- df[,-(1:6)]
col = rainbow(5,s=0.6,v=0.7) # A T G C -
col = c(col[-1],"grey")
color = data
color[data=="A"]=col[1]
color[data=="T"]=col[2]
color[data=="G"]=col[3]
color[data=="C"]=col[4]
color[data=="-"]=col[5]
draw <- function(data,color,outpdf,smnlabels){
    dx = 0.5  ## max 0.5
    dy = 0.5  ## max 0.5
    x1r = 1:ncol(data)-dx
    x2r = 1:ncol(data)+dx
    y1r = 1:nrow(data)-dy
    y2r = 1:nrow(data)+dy
    d = max(c(dx,dy))
    colorgene = rgb(0,0,0,alpha=seq(0.4,1,length=length(unique(smnlabels))))[as.numeric(as.factor(smnlabels))]
    pdf(outpdf)
    par(tck=0.01,mar=c(3,10,4,6),mgp=c(3,0,0),cex.axis=0.5)
    plot(1,xlim=c(d,ncol(data)+d),ylim=c(0,nrow(data)+d),type="n",axes=F,xaxt="n",yaxt="n",xlab="",ylab="")
    for(x in 1:nrow(data)){
        rect(xleft=x1r,ybottom=y1r[x],xright=x2r,ytop=y2r[x],col=unlist(color[x,]),border="grey90")
        text(1:ncol(data),x,unlist(data[x,]),cex=0.5,col="white")   ## labels
    }  
    par(xpd=T)
    text(d-0.1,1:nrow(data),df[,1],adj=1,cex=0.5)  ## CCS
    text(ncol(data)+d+0.1,1:nrow(data),smnlabels,cex=0.5,adj=0,col=colorgene) ## SMN
    axis(3,1:ncol(data),colnames(data),tick=F)
    dev.off()
}
smnlabels = df[,2]
draw(data,color,outpdf,smnlabels=smnlabels)
clustpdf = paste0(dirname(outpdf),"/clust.",basename(outpdf))
data = data[order(df[,2]),]
color = color[order(df[,2]),]
smnlabels = smnlabels[order(df[,2])]
draw(data,color,clustpdf,smnlabels=smnlabels)


你可能感兴趣的:(类似于maga多readsSNP的图示R脚本)