annotate plot_distribution.r @ 3:f180c4ef9128 draft default tip

Uploaded
author ynewton
date Fri, 18 Jan 2013 12:18:45 -0500
parents cf8d0d54bc78
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
1 #!/usr/bin/Rscript
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
2
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
3 #usage, options and doc goes here
2
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
4 argspec <- c("plot_distribution.r - plots distribution of the value in the list or the matrix. Assumes the first line and the first column are annotations.
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
5 Usage:
2
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
6 plot_distribution.r input_matrix.tab output_file.pdf
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
7 Example:
2
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
8 Rscript plot_distribution.r input_matrix.tab output_file.pdf
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
9 Options:
2
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
10 input file name
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
11 output file name")
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
12
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
13 read_matrix <- function(in_file){
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
14 header <- strsplit(readLines(con=in_file, n=1), "\t")[[1]]
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
15 cl.cols<- 1:length(header) > 1
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
16 data_matrix.df <- read.delim(in_file, header=TRUE, row.names=NULL, stringsAsFactors=FALSE, na.strings="NA", check.names=FALSE)
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
17 data_matrix <- as.matrix(data_matrix.df[,cl.cols])
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
18 rownames(data_matrix) <- data_matrix.df[,1]
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
19 return(data_matrix)
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
20 }
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
21
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
22 main <- function(argv) {
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
23 in_file <- argv[1]
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
24 out_file <- argv[2]
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
25 sink('/dev/null')
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
26
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
27 input_data <- read_matrix(in_file)
2
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
28 input_data.df <- as.data.frame(input_data)
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
29 input_data.lst <- as.list(input_data.df)
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
30 input_data.unlst <- unlist(input_data.lst)
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
31 input_data.nona <- input_data.unlst[!is.na(input_data.unlst)]
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
32
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
33 pdf(out_file, bg="white")
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
34 par(mfrow=c(1,1))
2
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
35 hist(input_data.nona, col="lightblue", labels=TRUE, main="Histogram", xlab="")
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
36 plot(density(input_data.nona), type="l", col="blue", main="Density")
cf8d0d54bc78 Uploaded
ynewton
parents: 0
diff changeset
37 dev.off()
0
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
38 }
f91478b63ec6 Uploaded
ynewton
parents:
diff changeset
39 main(commandArgs(TRUE))