-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompDiffAbndnc.R
More file actions
184 lines (143 loc) · 6.51 KB
/
compDiffAbndnc.R
File metadata and controls
184 lines (143 loc) · 6.51 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env Rscript
require(docopt)
require(methods)
"
Usage:
compDiffAbndnc.R (-h | --help | --version)
compDiffAbndnc.R DIR
Description: This script calculates differential abundance using a feature table
Options:
--version Show the current version.
Arguments:
DIR Provide directory where cyttools.args.Rdata file is located
" -> doc
args <- docopt(doc)
ARGS_DIR <- args$DIR
cat("\nLoading arguments from", ARGS_DIR, "\n")
load(paste(ARGS_DIR, "cyttools.args.Rdata", sep = ""))
RESULTS_DIR <- args$OUT
source("cyttoolsFunctions.R")
panelDesign <- read.delim(args$PANEL)
targets <- read.delim(args$METADATA)
colsToCheck <- c("TimePoint", "Condition", "SampleID", "FileName", "Group")
if(checkDesignCols(targets, colsToCheck)){
missingCols <- colsToCheck[which(colsToCheck %in% colnames(targets) == F)]
cat("\n\nERROR: PANEL file does not include required columns.
\n\nMissing Columns:", missingCols,
"\n\nPlease run cyttools.R --makeMetaDataBlank to generate compatible meta data file.\n\nStopping cyttools.R\n\n")
q()
}
targets$TimePoint <- factor(targets$TimePoint, levels = unique(targets$TimePoint))
targets$Condition <- factor(targets$Condition, levels = unique(targets$Condition))
targets$SampleID <- factor(targets$SampleID, levels = unique(targets$SampleID))
if(length(unique(targets$TimePoint)) > 1){
exprDesign <- paste(targets$Condition, targets$TimePoint, sep = "_")
}else{
exprDesign <- targets$Condition
}
if(all(table(targets$SampleID) == 1)){
if(length(unique(targets$Group)) <= 1){
design <- model.matrix(~ 0 + exprDesign)
}else{
design <- model.matrix(~targets$Group + exprDesign)
}
}else{
design <- model.matrix(~targets$SampleID + exprDesign)
}
colnames(design) <- gsub("targets\\$SampleID|targets\\$Group", "BatchEffect", colnames(design))
orderList <- gsub("\\s", ".", targets$FileName)
props_table <- read.delim(args$FEATURETABLE, row.names = 1)
props_table <- props_table[,orderVectorByListOfTerms(colnames(props_table), orderList)]
propData <- logitTransform(props_table)
fit <- lmFit(propData, design = design)
if(checkLmFit(fit, propData)){
design <- model.matrix(~ 0 + exprDesign + targets$SampleID)
colnames(design) <- gsub("exprDesign", "Cnd", colnames(design))
colnames(design) <- gsub("targets\\$SampleID|targets\\$Group", "BatchEffect", colnames(design))
fit <- lmFit(propData, design = design)
if(checkLmFit(fit, propData)){
design <- model.matrix(~ 0 + exprDesign)
colnames(design) <- gsub("exprDesign", "Cnd", colnames(design))
colnames(design) <- gsub("targets\\$SampleID|targets\\$Group", "BatchEffect", colnames(design))
fit <- lmFit(propData, design = design)
cat("\n\nWARNING: Limma cannot estimate random effects due to sample size, using simplified model for analysis\n\n")
}else{
cat("\n\nWARNING: Limma cannot estimate group effects due to sample size, using simplified model for analysis\n\n")}
}
# Automate generation of contrast matrix
cont.matrix <- matrix(nrow = length(colnames(design)), ncol = (length(colnames(design)) - 1)*(length(colnames(design)))/2) # levels X Contrasts
prevNumContrasts <- 1
for ( i in 1:nrow(cont.matrix)){
numContrasts <- nrow(cont.matrix) - i
endContrasts <- prevNumContrasts + numContrasts - 1
if (numContrasts > 0){
cont.matrix[i,c(prevNumContrasts:endContrasts)] <- 1
}
k <- i
l <- i + numContrasts - 1
if ( k <= l){
for ( j in k:l){
cont.matrix[j+1,c(prevNumContrasts:endContrasts)[j - i + 1]] <- -1
}
}
prevNumContrasts <- prevNumContrasts + numContrasts
}
cont.matrix[is.na(cont.matrix)] <- 0
row.names(cont.matrix) <- colnames(design)
colnames(cont.matrix) <- paste("Contrast", c(1:ncol(cont.matrix)), sep = "")
# remove comparisons that don't make sense from an experimental design perspective
contMatrixRowIDs <- colsplit(row.names(cont.matrix), "\\_", c("Condition", "TimePoint"))
validContrastIndex <- vector(length = ncol(cont.matrix))
humanReadableColNames <- vector(length = ncol(cont.matrix))
for ( i in 1:ncol(cont.matrix)){
contrastColumn <- cont.matrix[,i]
firstCondition <- contMatrixRowIDs$Condition[cont.matrix[,i] == 1]
secondCondition <- contMatrixRowIDs$Condition[cont.matrix[,i] == -1]
firstTimePoint <- contMatrixRowIDs$TimePoint[cont.matrix[,i] == 1]
secondTimePoint <- contMatrixRowIDs$TimePoint[cont.matrix[,i] == -1]
if(all(unlist(lapply(c(firstTimePoint, secondTimePoint), is.na))) &
firstCondition != secondCondition){
validContrastIndex[i] <- T
humanReadableColNames[i] <- paste(firstCondition,
secondCondition,
sep = "_vs_")
}else if(any(unlist(lapply(c(firstCondition, secondCondition, firstTimePoint, secondTimePoint), is.na)))){
validContrastIndex[i] <- F
}else if(firstCondition == secondCondition & firstTimePoint != secondTimePoint){
validContrastIndex[i] <- T
humanReadableColNames[i] <- paste(firstCondition,
paste(firstTimePoint,
secondTimePoint,
sep = "_vs_"),
sep = ".")
}else if(firstCondition != secondCondition & firstTimePoint == secondTimePoint){
validContrastIndex[i] <- T
humanReadableColNames[i] <- paste(firstTimePoint,
paste(firstCondition,
secondCondition,
sep = "_vs_"),
sep = ".")
}else{validContrastIndex[i] <- F}
}
cont.matrix.2 <- cont.matrix[,validContrastIndex] %>% as.matrix()
colnames(cont.matrix) <- humanReadableColNames[validContrastIndex]
if(any(grepl("BatchEffect", humanReadableColNames))){
cont.matrix <- cont.matrix[,grep("BatchEffect", colnames(cont.matrix), invert = T)]
}
if(is.null(dim(cont.matrix))){
fit2 <- eBayes(fit)
}else{
fit2 <- contrasts.fit(fit, cont.matrix)
fit2 <- eBayes(fit2)
}
diffAbndncStatsTable <- data.frame()
for ( i in 1:length(colnames(fit2))){
nextPart <- topTable(fit2, coef = i, number = Inf)
nextPart$Source <- rep(colnames(fit2)[i], nrow(nextPart))
nextPart$Mapping <- row.names(nextPart)
diffAbndncStatsTable <- rbind(diffAbndncStatsTable, nextPart)
}
nodeAbndncStatsFile <- paste(RESULTS_DIR, "nodeDifferentialAbundanceTable.txt", sep = "")
write.table(diffAbndncStatsTable, nodeAbndncStatsFile, sep = "\t", quote = F, row.names = F)
workspaceFile <- paste(RESULTS_DIR, "compDiffAbndncWorkspace.Rdata", sep = "")
save.image(file = workspaceFile)