-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBBBC022_pipeline_random.R
154 lines (117 loc) · 5.12 KB
/
BBBC022_pipeline_random.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
####################
### BBBC022 Data ###
####################
# Pipeline from hit selection to enrichment ratio for random feature selection with BBBC022 dataset.
# use the following functions:
library(magrittr)
library(dplyr)
library(tidyverse)
library(stringr)
# uplodaing functions
source("hit_selection_correlation_function.R")
source("hit_selection_jaccard_function.R")
source("enrichment_ratio_function.R")
filenames <- list.files(path = "../../input/BBBC022_2013/Profile/feat_selected/random/")
filenames <- c("200","250","300","350","400","500","600","700","750")
nCPU <- detectCores()
all <- 1:length(filenames)
for(n in all){
f <- list.files(path = paste("../../input/BBBC022_2013/Profile/feat_selected/random/", filenames[n], "/", sep = ""))
numfeat <- filenames[n]
hit.ratio.p <- c()
hit.ratio.j <- c()
set.seed(42)
N <- 10
seeds <- sample(1:10000, N, replace=F)
j <- 0
for(i in f){
j <- j + 1
Pf <- readRDS(paste("../../input/BBBC022_2013/Profile/feat_selected/random/",
filenames[n], "/",
i,
sep = ""))
Pf <- Pf$data %>%
rename(Metadata_Plate = Plate,
Metadata_Well = Well,
Metadata_broad_sample = Image_Metadata_BROAD_ID,
Metadata_cpd_name = Image_Metadata_SOURCE_COMPOUND_NAME)
variables <-
colnames(Pf) %>% str_subset("^Cells_|^Cytoplasm_|^Nuclei_")
metadata <-
colnames(Pf) %>% str_subset("^Metadata_")
#################
# Hit Selection #
#################
start.time <- Sys.time()
# reproductibility
dir.save <- "BBBC022_2013/Profile"
hit <- hit_selection_correlation(Pf,
n.replicate = 4,
filename = filenames[n],
cor.method = "pearson",
feat.selected = F,
seed = seeds[j],
N = 5000,
nCPU = nCPU,
dir.save = dir.save)
hit.ratio.p <- cbind(hit.ratio.p, hit)
num.feat <- round(0.05*length(variables))
hit <- hit_selection_jaccard(Pf,
n.replicate = 4,
filename = filenames[n],
n.feat = num.feat,
feat.selected = F,
seed = seeds[j],
N = 5000,
nCPU = nCPU,
dir.save = dir.save)
hit.ratio.j <- cbind(hit.ratio.j, hit)
}
print("mean hit ratio: ")
print(mean(hit.ratio.p))
print("standard deviation hit ratio: ")
print(sd(hit.ratio.p)/n)
print("mean hit ratio: ")
print(mean(hit.ratio.j))
print("standard deviation hit ratio: ")
print(sd(hit.ratio.j)/N)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
####################
# Enrichment ratio #
####################
data.filenames.p <- list.files(path = "../../input/BBBC022_2013/Profile/hit_selected/random/Pearson/")
data.filenames.j <- list.files(path = "../../input/BBBC022_2013/Profile/hit_selected/random/Jaccard/")
######
data.filenames.p <- data.filenames.p[grep(as.character(numfeat),data.filenames.p)]
data.filenames.j <- data.filenames.j[grep(as.character(numfeat),data.filenames.j)]
######
# dataframe of result
enrichment.ratio <- data.frame(mean = numeric(0), quant = numeric(0), percent = numeric(0), filename = character(0), method = character(0))
seeds2 <- sample(1:10000, length(data.filenames.p), replace=F)
for(i in 1:length(data.filenames.p)){
print(i)
seed <- seeds2[i]
pf.p <- readRDS(file.path("..", "..", "input", "BBBC022_2013", "Profile", "hit_selected", "random", "Pearson", data.filenames.p[i]))
enrichment.ratio <- bind_rows(enrichment.ratio,
enrichment_ratio(pf.p, top.x = 0.02, seed = seed,
nCPU = nCPU, N = 1000, filename = data.filenames.p[i], method = "Pearson"))
pf.j <- readRDS(file.path("..", "..", "input", "BBBC022_2013", "Profile", "hit_selected", "random", "Jaccard", data.filenames.j[i]))
enrichment.ratio <- bind_rows(enrichment.ratio,
enrichment_ratio(pf.j, top.x = 0.02, seed = seed,
nCPU = nCPU, N = 1000, filename = data.filenames.j[i], method = "Jaccard"))
}
###### result
enr.ratio <- enrichment.ratio %>% mutate(ratio = percent/mean)
enr.ratio %<>%
group_by(method) %>%
summarise(ratio.mean = mean(ratio),
ratio.sd = sd(ratio)/sqrt(n()))
enr.ratio
filename.enr.ratio <- paste("../../input/BBBC022_2013/Profile/enrichment_ratio/random/enr_ratio_",
filenames[n],
"feat.Rda",
sep = "")
save(enr.ratio, file=filename.enr.ratio)
}