|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +times<-Sys.time(); |
| 3 | +library('getopt'); |
| 4 | +options(bitmapType='cairo'); |
| 5 | +spec = matrix(c( |
| 6 | + 'base','b',0,'character', |
| 7 | + 'qual','q',0,'character', |
| 8 | + 'out','o',0,'character', |
| 9 | + 'help' , 'e', 0, 'logical' |
| 10 | + ), byrow=TRUE, ncol=4); |
| 11 | +opt = getopt(spec); |
| 12 | +print_usage <- function(spec=NULL){ |
| 13 | + cat(getopt(spec, usage=TRUE)); |
| 14 | + cat("Usage example: \n") |
| 15 | + cat(" |
| 16 | +Usage example: |
| 17 | + Rscript QC.r --base --qual --key --od |
| 18 | + |
| 19 | +Usage: |
| 20 | + --base base distribution file |
| 21 | + --qual base quality file |
| 22 | + --key sample ID |
| 23 | + --od output dir |
| 24 | + --help usage |
| 25 | +\n") |
| 26 | + q(status=1); |
| 27 | +} |
| 28 | + |
| 29 | +if ( !is.null(opt$help) ) { print_usage(spec) } |
| 30 | +if ( is.null(opt$base) ) { print_usage(spec) } |
| 31 | +if ( is.null(opt$qual) ) { print_usage(spec) } |
| 32 | +if ( is.null(opt$out) ) { print_usage(spec) } |
| 33 | + |
| 34 | +read1<-read.table(opt$base,header=FALSE) |
| 35 | +read2<-read.table(opt$qual,header=FALSE) |
| 36 | + |
| 37 | +read1<-read1[complete.cases(read1),] |
| 38 | +read2<-read2[complete.cases(read2),] |
| 39 | + |
| 40 | +num<-(length(read2[,1])/2) |
| 41 | +for (i in 1:(length(read2[,1])-1)) {if ((as.numeric(read2[i+1,1])-as.numeric(read2[i,1]))!=1) {num<-i}} |
| 42 | + |
| 43 | +quan<-read2[,ncol(read2)] |
| 44 | +quan<-10^(quan/10*-1) |
| 45 | +max_quan=ceiling(max(quan)/0.001)*0.001 |
| 46 | +Aper<-read1[,2] |
| 47 | +Cper<-read1[,5] |
| 48 | +Gper<-read1[,4] |
| 49 | +Tper<-read1[,3] |
| 50 | +Nper<-read1[,6] |
| 51 | +max_per=ceiling(mean(c(Aper,Cper,Gper,Tper,Nper))/0.25)*0.25*2 |
| 52 | +axix_at<-c(1,50,100,num,num+50,num+100,length(read2[,1])) |
| 53 | +axix_txt<-c(1,50,100,num,50,100,length(read2[,1])/2) |
| 54 | + |
| 55 | + |
| 56 | +pdf(file=paste(opt$od,"/",opt$out,".qual.pdf",sep="")) |
| 57 | +barplot(quan*100,col='springgreen2',space=0,ylab='Error rate(%)',border= NA,ylim=c(0,max_quan*100),xlab='Reads position(bp)',main='Quality distribution') |
| 58 | +axis(1,labels=axix_txt,at=axix_at) |
| 59 | +abline(v=num+0.5, lty=2,col='darkgray') |
| 60 | +box() |
| 61 | +dev.off() |
| 62 | +png(file=paste(opt$od,"/",opt$out,".qual.png",sep="")) |
| 63 | +barplot(quan*100,col='springgreen2',space=0,ylab='Error rate(%)',border= NA,ylim=c(0,max_quan*100),xlab='Reads position(bp)',main='Quality distribution') |
| 64 | +axis(1,labels=axix_txt,at=axix_at) |
| 65 | +abline(v=num+0.5, lty=2,col='darkgray') |
| 66 | +box() |
| 67 | +dev.off() |
| 68 | + |
| 69 | +pdf(file=paste(opt$od,"/",opt$out,".base.pdf",sep="")) |
| 70 | +plot(Aper,col='red',type = 'l',xlab='Reads position(bp)',ylab='Percent(%)',ylim=c(0,max_per),xaxt="n",lty=1,lwd=1.5,main="Base distribution") |
| 71 | +axis(1,labels=axix_txt,at=axix_at) |
| 72 | +abline(v=num+0.5, lty=2,col='darkgray') |
| 73 | +lines(Tper,col='orange',lty=1,lwd=1.5) |
| 74 | +lines(Gper,col='green',lty=1,lwd=1.5) |
| 75 | +lines(Cper,col='blue',lty=1,lwd=1.5) |
| 76 | +lines(Nper,col='black',lty=1,lwd=1.5) |
| 77 | +legend("topright",c("A","T","G","C","N"),lty=c(1,1,1,1,1),col=c("red","orange","green","blue","black")) |
| 78 | +dev.off() |
| 79 | + |
| 80 | +png(file=paste(opt$od,"/",opt$out,".base.png",sep="")) |
| 81 | +plot(Aper,col='red',type = 'l',xlab='Reads position(bp)',ylab='Percent(%)',ylim=c(0,max_per),xaxt="n",lty=1,lwd=1.5,main="Base distribution") |
| 82 | +axis(1,labels=axix_txt,at=axix_at) |
| 83 | +abline(v=num+0.5, lty=2,col='darkgray') |
| 84 | +lines(Tper,col='orange',lty=1,lwd=1.5) |
| 85 | +lines(Gper,col='green',lty=1,lwd=1.5) |
| 86 | +lines(Cper,col='blue',lty=1,lwd=1.5) |
| 87 | +lines(Nper,col='black',lty=1,lwd=1.5) |
| 88 | +legend("topright",c("A","T","G","C","N"),lty=c(1,1,1,1,1),col=c("red","orange","green","blue","black")) |
| 89 | +dev.off() |
| 90 | + |
| 91 | +escaptime=Sys.time()-times; |
| 92 | +print("Done!") |
| 93 | +print(escaptime) |
| 94 | +q() |
0 commit comments