-
Notifications
You must be signed in to change notification settings - Fork 3
/
aq-pca.in
114 lines (95 loc) · 3.23 KB
/
aq-pca.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env Rscript
options(error = quote(dump.frames("pca-debug", TRUE)))
#source("@prefix@/share/@PACKAGE@/biom.R")
pkgTest <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
}
pkgTest('getopt')
#Grab arguments
#Arguments required:
#-i input OTU table (tabular format ONLY, JSON libraries much too slow in R)
#-m mapping file
#-e mapping.extra file
#-t headers.txt file
#-o output dir
spec = matrix(c('input', 'i', 1, "character",
'mapping','m',1,"character",
'mapping.extra','e',1,"character",
'headers','t',1,'character',
'output','o',1,"character",
'help','h',2,"character"), byrow=TRUE, ncol=4)
opt = getopt(spec)
# if help was asked for print a friendly message
# and exit with a non-zero error code
if ( !is.null(opt$help) ) {
cat(getopt(spec, usage=TRUE))
q(status=1)
}
if ( is.null(opt$input) ) {
print("Input OTU table required.")
q(status=1)
}
if ( is.null(opt$mapping) ) {
print("Mapping file required.")
q(status=1)
}
if ( is.null(opt$mapping.extra) ) {
print("mapping.txt file required.")
q(status=1)
}
if ( is.null(opt$headers) ) {
print("headers.txt file required.")
q(status=1)
}
if ( is.null(opt$output) ) {
opt$output <- getwd()
}
outDir <- opt$output
otuTable <- opt$input
mappingFile <- opt$mapping
mappingExtra <- opt$mapping.extra
headersFile <- opt$headers
dir.create(outDir)
rawtable <- read.table(otuTable, skip = 1,
comment.char = "", header = TRUE, row.names = 1, sep = "\t")
otutable <- t(rawtable[1:(ncol(rawtable) - 1)])
otutable <- otutable[order(as.integer(sub("X","", rownames(otutable)))),]
print("Reading mapping");
mapping <- read.table(mappingFile, header = TRUE, comment.char = "", row.names = "X.SampleID", sep = "\t")
print("Reading mapping extra");
extra <- read.table(mappingExtra, header = TRUE,
comment.char = "", row.names = "X.SampleID", sep = "\t")
print("Reading header");
interest <- read.table(headersFile)[1,] == "TRUE";
print("Computing PCA");
for(i in 0 : length(interest)) {
if (interest[i] && length(levels(factor(as.matrix(mapping[, i])))) == 1) {
interest[i] <- FALSE
print(paste("Ignoring", colnames(mapping)[i], "because all values are identical."))
}
}
newmapping <- t(mapping[,interest]);
# For non-numeric sample names, comment out the following line
colnames(newmapping) <- paste("X", rownames(mapping), sep = "");
rownames(newmapping) <- colnames(mapping)[interest];
d <- rbind(t(otutable), newmapping);
p <- prcomp(t(d), scale = TRUE);
pdf(paste(outDir, "/pca-biplot.pdf",sep=""));
print("Making Scree plot");
plot(p);
print("Making MDS plot");
plot(p$x, asp = p$sdev[2]/p$sdev[1], col = as.matrix(extra[ ,"Colour"]));
text(x = p$x[, "PC1"], y = p$x[, "PC2"], labels = extra[ ,"Description"], col = as.matrix(extra[ ,"Colour"]));
print("Making biplot");
contrib <- apply(p$rotation, 1, function(x) {sqrt(sum(x[1:2]^2));});
# What variables (rows) do you want to display as arrows?
#useful <- contrib > (mean(contrib) + 2*sd(contrib));
useful <- colnames(mapping)[interest];
if (length(useful) > 1) {
biplot(p$x[, c("PC1", "PC2")], p$rotation[useful, c("PC1", "PC2")], xlabs = extra[, "Description"], asp = p$sdev[2]/p$sdev[1]);
}