## Specify the colors for the different nucleotides (ACGTN) colors <- c("blue", "red", "green", "yellow", "pink") ## Define a custom function to tally base calls by position nucDistrFromFastx <- function(df){ ## Define the range of values for the plot window xlim <- c(0, nrow(df)+1) ylim <- c(0, max(df$count)) ## Plot the nucleotide distributions counts par(mar=c(5,4,2,2)+0.1, xaxs="i", yaxs="i") plot(xlim, ylim, type="n", xlab="Read position", ylab="Number of nucleotides") for (i in 1:nrow(df)){ ys <- c(0, df$A_Count[i], df$A_Count[i]+df$C_Count[i], df$A_Count[i]+df$C_Count[i]+df$G_Count[i], df$A_Count[i]+df$C_Count[i]+df$G_Count[i]+df$T_Count[i], df$A_Count[i]+df$C_Count[i]+df$G_Count[i]+df$T_Count[i]+df$N_Count[i] ) ## Apply colors for (j in 1:5){ plot_rect(i, ys[j], ys[j+1], colors[j]) } ## Generate legend legend("topright", legend=c("A", "C", "G", "T", "N"), fill=colors) } } plot_rect <- function(x, y1, y2, col){ polygon(x=c(x-0.4, x-0.4, x+0.4, x+0.4, x-0.4), y=c(y1, y2, y2, y1, y1), col=col) } ## Capture command line arguments args <- (commandArgs(TRUE)) ## Read in tab-delimited file as a table ## Use the first line as column headers data <- read.table(args[1], head=TRUE, as.is=TRUE) ## Apply user-defined output filename, if it was specified if (length(args) > 1){ outfile <- args[2] } else { outfile <- paste(args[1], ".png", sep="") } ## Initialize PNG output file and specify dimensions png(file=outfile, w=1000, h=500) ## Call custom function to generate plot nucDistrFromFastx(data) ## Close output file dev.off()