-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.R
More file actions
160 lines (122 loc) · 3.97 KB
/
helpers.R
File metadata and controls
160 lines (122 loc) · 3.97 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
df_to_signature <- function(df) {
colnames(df) <- c("pathway", "gene")
pathways <- unique(df[[1]])
sig <- list()
for (i in seq_along(1:length(pathways))) {
sig[[i]] <- df %>%
filter(pathway == pathways[i]) %>%
pull(gene)
}
names(sig) <- pathways
return(sig)
}
deseq_format_test <- function(rds) {
if ((class(rds[[1]]) == "DESeqDataSet") & (class(rds[[2]]) == "DESeqResults")) {
return(TRUE)
}
}
deseq_transform <- function(res, p_co, lfc_co) {
res %<>%
as.data.frame() %>%
rownames_to_column(var = "symbol") %>%
as_tibble() %>%
filter(!is.na(padj)) %>%
mutate(significant = ifelse(padj <= p_co & log2FoldChange >= lfc_co,
"Up",
ifelse(padj <= p_co & log2FoldChange <= -lfc_co,
"Down",
"Not Sig")))
return(res)
}
parse_rna_genes <- function(gene_list) {
gene_list <- gsub("[[:space:]]", "", gene_list)
genes <- str_split(gene_list, "(,|;)")[[1]]
return(genes[genes != ""])
}
get_count_message <- function(mtx) {
msg <- paste("HTSeq count data uploaded:", ncol(mtx), "files,", nrow(mtx), "genes")
return(msg)
}
filter_mt <- function(mtx, metadata) {
metadata[match(colnames(mtx), metadata[["Sample"]]),]
}
is_valid_dge <- function(mt, dge_var, g1, g2) {
if (sum(mt[[dge_var]] == g1) <= 1 || sum(mt[[dge_var]] == g2) <= 1) {
return(FALSE)
} else {
return(TRUE)
}
}
get_dge_message <- function(isvalid, res = NULL) {
if(isvalid) {
msg <- paste0("Differential gene expression (DGE) analysis is finished\n\n",
res@elementMetadata[2,2] %>%
str_remove("^.*:") %>%
str_trim(), "\n\n",
"You can visualize the results in the 'DGE Visualization' tab.")
} else {
msg <- "The specified differential gene expression (DGE) analysis is not valid.\n\nThe sample size in each group should be > 1."
}
return(msg)
}
cts_to_dds <- function(mtx, metadata, var = 1) {
withProgress(message = "Loading Data..", value = 0.3, {
dds <- DESeq2::DESeqDataSetFromMatrix(countData = mtx,
colData = metadata,
design= as.formula(paste0("~", var)))
incProgress(0.5, message = "Building DESeq2 Data Objects..")
dds <- DESeq2::DESeq(dds)
return(dds)
})
}
assign_km_clu <- function(vsd, km_res) {
vsd@colData$Kmeans <- LETTERS[km_res$cluster]
return(vsd)
}
assign_km_clu_col <- function(coldata, km_res) {
coldata$Kmeans <- LETTERS[km_res$cluster]
return(coldata)
}
trubble <- function(cts) {
tmp <- as.data.frame(
rbind(
cts[1:5, ],
... = rep("...", length(cts[1, ])),
cts[(nrow(cts) - 4):(nrow(cts)), ]
)
)
if (ncol(tmp) > 10) {
tmp2 <- tmp[, 1:10]
} else {
tmp2 <- tmp
}
nr <- nrow(cts)
nc <- ncol(cts)
if (ncol(tmp) > 10) {
output <- paste(
"Your pre-processed data contains", nr, "genes and", nc,
"samples. Showing the first 10 samples:\n"
)
} else {
output <- paste(
"Your pre-processed data contains", nr, "genes and",
nc, "samples.\n"
)
}
test <- as.matrix(tmp2)
test <- rbind(colnames(tmp2), test)
y <- sprintf(paste0("%",max(nchar(test)),"s"), test)
y <- matrix(y, nrow = 12)
gen <- c("", rownames(tmp2))
gen <- gsub("\\s", " ", format(gen, width = max(nchar(gen))))
if (ncol(tmp) > 10) {
output2 <- paste("\n", ncol(tmp) - 10, "Samples not shown\n")
} else {
output2 <- NULL
}
cat(output, "\n")
for(i in 1:nrow(y)) {
cat(gen[i], y[i, ], "\n")
}
cat(output2)
}