forked from apratap/PCBC_DataExplorer_ShinyApp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
global_functions.R
180 lines (151 loc) · 5.84 KB
/
global_functions.R
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
#for faster rendering caching the computationally expensive functions
memoised_corAndPvalue <- memoise::memoise(function(...) corAndPvalue(...))
output_download_data <- function(mat, file) {
df <- cbind(data.frame(ID=rownames(mat)),
as.data.frame(mat))
write.csv(df, file, row.names=F, col.names=T)
}
get_eset_withcorrelated_genes <- function(geneIds, eset, corThreshold, corDirection='both'){
expMatrix <- exprs(eset)
flog.debug('Calculating correlated genes ....', name="server")
#expression matrix with selected genes
m1 <- expMatrix[rownames(expMatrix) %in% geneIds,]
#expression matrix with which the selected genes will be correlated
m2 <- expMatrix
#calculate correlation
res <- memoised_corAndPvalue(t(m1),t(m2),nThreads=4)
cor <- round(res$cor,digits=3)
# Force threshold to be positive, so not confused by negative values
corThreshold <- abs(corThreshold)
# Subset based on direction
if (corDirection == 'positive') {
cor <- cor >= corThreshold
}
else if (corDirection == 'negative') {
cor <- cor <= -corThreshold
}
else if (corDirection == 'both') {
cor <- abs(cor) >= corThreshold
}
else {
cor <- abs(cor) >= corThreshold
}
#columns of the cor matrix which have correlation with some gene > corThreshold
cols_to_select <- apply(cor,2,any)
correlated_genes <- union(colnames(cor)[cols_to_select], rownames(m1))
flog.debug('Done calculating correlated genes', name="server")
eset[rownames(expMatrix) %in% correlated_genes,]
}
get_expMatrix_withcorrelated_genes <- function(geneIds, expMatrix, corThreshold, corDirection='both'){
cat('Calculating correlated genes ....')
#expression matrix with selected genes
m1 <- expMatrix[rownames(expMatrix) %in% geneIds,]
#expression matrix with which the selected genes will be correlated
m2 <- expMatrix
#calculate correlation
res <- memoised_corAndPvalue(t(m1),t(m2),nThreads=4)
cor <- round(res$cor,digits=3)
# Force threshold to be positive, so not confused by negative values
corThreshold <- abs(corThreshold)
# Subset based on direction
if (corDirection == 'positive') {
cor <- cor >= corThreshold
}
else if (corDirection == 'negative') {
cor <- cor <= -corThreshold
}
else if (corDirection == 'both') {
cor <- abs(cor) >= corThreshold
}
else {
cor <- abs(cor) >= corThreshold
}
#columns of the cor matrix which have correlation with some gene > corThreshold
cols_to_select <- apply(cor,2,any)
correlated_genes <- union(colnames(cor)[cols_to_select], rownames(m1))
cat('Done','\n')
expMatrix[rownames(expMatrix) %in% correlated_genes,]
}
#filter metadata
get_filtered_metadata <- function(input, metadata){
filtered_metadata <- metadata
if( length(input$linetype) != 0 ){
filtered_metadata <- subset(filtered_metadata, Cell_Line_Type %in% input$linetype)
}
if( length(input$gene_combination) != 0 ){
filtered_metadata <- subset(filtered_metadata, Reprogramming_Gene_Combination %in% input$gene_combination)
}
if(length(input$vector_type) != 0){
filtered_metadata <- subset(filtered_metadata, Reprogramming_Vector_Type %in% input$vector_type)
}
if(length(input$tissue_origin) != 0){
filtered_metadata <- subset(filtered_metadata, Tissue_of_Origin %in% input$tissue_origin)
}
if(length(input$diff_state) != 0){
filtered_metadata <- subset(filtered_metadata, Differentiation_State %in% input$diff_state)
}
if(length(input$cell_origin) != 0){
filtered_metadata <- subset(filtered_metadata, Cell_Type_of_Origin %in% input$cell_origin)
}
if(length(input$originating_lab_id) != 0){
filtered_metadata <- subset(filtered_metadata, Originating_Lab_ID %in% input$originating_lab_id)
}
filtered_metadata
}
filter_by_metadata <- function(input, eset){
filtered_metadata <- pData(eset)
if( length(input$linetype) != 0 ){
filtered_metadata <- subset(filtered_metadata, Cell_Line_Type %in% input$linetype)
}
if( length(input$gene_combination) != 0 ){
filtered_metadata <- subset(filtered_metadata, Reprogramming_Gene_Combination %in% input$gene_combination)
}
if(length(input$vector_type) != 0){
filtered_metadata <- subset(filtered_metadata, Reprogramming_Vector_Type %in% input$vector_type)
}
if(length(input$tissue_origin) != 0){
filtered_metadata <- subset(filtered_metadata, Tissue_of_Origin %in% input$tissue_origin)
}
if(length(input$diff_state) != 0){
filtered_metadata <- subset(filtered_metadata, Diffname_short %in% input$diff_state)
}
if(length(input$cell_origin) != 0){
filtered_metadata <- subset(filtered_metadata, Cell_Type_of_Origin %in% input$cell_origin)
}
if(length(input$originating_lab) != 0){
filtered_metadata <- subset(filtered_metadata, Originating_Lab %in% input$originating_lab)
}
if(length(input$gender) != 0){
filtered_metadata <- subset(filtered_metadata, Gender %in% input$gender)
}
eset[, rownames(filtered_metadata)]
}
#create the annotation data frame for the heatmap
get_filteredAnnotation <- function(input,metadata){
if(length(input$heatmap_annotation_labels) == 0){
stop('please select atleast one heatmap annotation variable \n\n')
}
else{
annotation <- metadata[,c(input$heatmap_annotation_labels),drop=F]
# rownames(annotation) <- metadata$Sample
annotation
}
}
get_heatmapAnnotation <- function(heatmap_annotation_labels, metadata){
if(length(heatmap_annotation_labels) == 0){
stop('please select atleast one heatmap annotation variable \n\n')
}
else{
annotation <- metadata[, heatmap_annotation_labels, drop=F]
annotation
}
}
clean_list <- function(x, change_case=toupper) {
# Split by space, comma or new lines
x <- unlist(strsplit(x, split=c('[\\s+,\\n+\\r+)]'),perl=T))
# convert everything to specified case
x <- change_case(x)
# remove the blank entries
x <- x[!(x == "")]
x
}