-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompDiffCount.R
More file actions
151 lines (107 loc) · 5 KB
/
compDiffCount.R
File metadata and controls
151 lines (107 loc) · 5 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
#!/usr/bin/env Rscript
require(docopt)
require(methods)
"
Usage:
compDiffCount.R (-h | --help | --version)
compDiffCount.R DIR
Description: This script calculates differential counts 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")
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))
orderList <- gsub("\\s", ".", targets$FileName)
# read in clusterd FCS files
cluster_dir <- args$CLUSTERDIR # grabs directory from initial cyttools call
file <- list.files(cluster_dir ,pattern='.fcs$', full=TRUE) # captures all FCS files in the directory
cluster.flowSet.trans <- read.flowSet(file)
countTable <- cluster.flowSet.trans %>%
fsApply(function(x){return(as.data.frame(exprs(x)))}, simplify = F) %>%
bind_rows(.id = "FileNames") %>%
group_by(FileNames, Mapping) %>%
summarise(n()) %>%
left_join(read_tsv(args$ANNOTATIONS)) %>%
ungroup() %>%
group_by(FileNames, Immunophenotypes) %>%
summarise(Count = sum(`n()`)) %>%
spread(FileNames,
Count,
fill = 0) %>%
column_to_rownames("Immunophenotypes")
cell_count_total <- cluster.flowSet.trans %>%
fsApply(function(x){return(as.data.frame(exprs(x)))}, simplify = F) %>%
bind_rows(.id = "FileNames") %>%
group_by(FileNames) %>%
summarise(n())
immunophenotypes_order <- order(str_count(row.names(countTable)), decreasing = T)
unique_filtered_count_table <- unique(countTable[immunophenotypes_order,])
uniq_immunophenotypes <- row.names(countTable)[immunophenotypes_order][!duplicated(countTable[immunophenotypes_order,])]
row.names(unique_filtered_count_table) <- uniq_immunophenotypes
propData <- DGEList(unique_filtered_count_table,
lib.size = cell_count_total$`n()`[cell_count_total$FileNames %in% colnames(unique_filtered_count_table)])
exprDesign <- targets$Condition
diffAbndncStatsTable <- tibble()
for (baseline in levels(exprDesign)){
tmpExprDesign <- relevel(exprDesign, baseline)
design <- model.matrix(~tmpExprDesign)
colnames(design) <- gsub("tmpExprDesign", "Cnd.", colnames(design))
colnames(design) <- gsub("targets\\$SampleID|targets\\$Group", "BatchEffect", colnames(design))
fit <- estimateDisp(propData, design)
fit <- glmQLFit(fit, design, robust=TRUE)
for(experimental in (levels(tmpExprDesign)[-1])){
res <- glmQLFTest(fit, coef = paste0("Cnd.", experimental))
topTable <- topTags(res, n = Inf)$table %>%
rownames_to_column("ClusterID") %>%
mutate(Observation = rep("Count", nrow(.)),
Condition = rep(experimental, nrow(.)),
Baseline = rep(baseline, nrow(.)))
diffAbndncStatsTable <- diffAbndncStatsTable %>% bind_rows(topTable)
}
}
diffAbndncStatsTable <- diffAbndncStatsTable %>%
select(ClusterID, Observation, Condition, Baseline, logFC, PValue, FDR)
nodeAbndncStatsFile <- paste(RESULTS_DIR, "nodeDifferentialCountTable.txt", sep = "")
write.table(diffAbndncStatsTable, nodeAbndncStatsFile, sep = "\t", quote = F, row.names = F)
dir.create(paste0(RESULTS_DIR, "ANALYZED_FCS/"),
showWarnings = F)
dir <- dirname(args$FEATURETABLE)
file <- list.files(dir ,pattern='.fcs$', full=TRUE, recursive = T) # captures all FCS files in the directory
for( files in file){
rawFCS <- read.FCS(files, transformation = F)
statData <- exprs(rawFCS) %>%
as.data.frame() %>%
select(ConsensusCluster:cyttools_dim_y) %>%
left_join(diffAbndncStatsTable %>%
mutate(FDR_ID = paste(Observation, Condition, Baseline, sep = "_")) %>%
select(ClusterID, FDR_ID, FDR) %>%
mutate(FDR = -log10(FDR)) %>%
spread(FDR_ID,
FDR) %>%
mutate(ConsensusCluster = as.numeric(gsub("Cluster", "", ClusterID))) %>%
select(-ClusterID))
clusterFCS <- flowCore::cbind2(rawFCS, as.matrix(statData %>% select(-(ConsensusCluster:cyttools_dim_y))))
out.fcs.file <- paste0(RESULTS_DIR, "ANALYZED_FCS/analyzed_", basename(files))
write.FCS(clusterFCS, out.fcs.file)
}
# workspaceFile <- paste(RESULTS_DIR, "compDiffCountWorkspace.Rdata", sep = "")
#
# save.image(file = workspaceFile)