-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunInterStudyHetPerm.R
259 lines (161 loc) · 6.38 KB
/
runInterStudyHetPerm.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
library(PharmacoGx)
library(meta)
library(SummarizedExperiment)
library(coop)
library(foreach)
# library(doParallel)
# library(doRNG)
library(iterators)
library(lme4)
library(data.table)
library(RhpcBLASctl)
print(RhpcBLASctl::blas_get_num_procs())
RhpcBLASctl::blas_set_num_threads(1)
#registerDoParallel(40)
args <- commandArgs(trailingOnly = TRUE)
method <- "perm"
drug <- args[1]
# tissue <- "Breast"
tissue <- args[2]
gene <- args[3]
badchars <- "[ ]|[/]|[:]|[-]"
make.names.2 <- function(x) return(gsub(pat=badchars, rep=".", x))
R <- as.numeric(args[4]) ## TODO: need method to pick this
home <- Sys.getenv("HOME")
scratch <- Sys.getenv("SCRATCH")
project <- Sys.getenv("PROJECT")
dataDir <- Sys.getenv("DATA")
myDataDir <- dataDir
mySigDir <- file.path(project, paste0("pearson_",method,"_res"))
myOutDir <- file.path(project, paste0("hetTest"))
runlistDir <- file.path(project, paste0("runlist_files"))
containername <- Sys.getenv("containername", unset=NA_character_)
if(!is.na(containername)){
myOutDir <- file.path(containername, myOutDir)
mySigDir <- file.path(containername, mySigDir)
myDataDir <- file.path(containername, myDataDir)
project <- file.path(containername, project)
runlistDir <- file.path(containername, runlistDir)
}
loadPSet <- function(psetName, tissue){
switch(psetName,
CCLE = {
pset <- readRDS(file.path(myDataDir,"CCLE.rds"))
}, CCLE.CTRPv2 = {
pset <- readRDS(file.path(myDataDir,"CCLE.CTRPv2.rds"))
}, GDSC_v1 = {
pset <- readRDS(file.path(myDataDir,"GDSC1.rds"))
}, GDSC_v2 = {
pset <- readRDS(file.path(myDataDir,"GDSC2.rds"))
}, gCSI = {
pset <- readRDS(file.path(myDataDir,"gCSI.rds"))
}, GRAY = {
pset <- readRDS(file.path(myDataDir,"GRAY.rds"))
}, UHNBreast = {
pset <- readRDS(file.path(myDataDir,"UHNBreast.rds"))
}, Tavor = {
pset <- readRDS(file.path(myDataDir, "Tavor.rds"))
}, BeatAML = {
pset <- readRDS(file.path(myDataDir, "BeatAML.rds"))
}, "FIMM-AML-MCM" = {
pset <- readRDS(file.path(myDataDir, "FIMM_MCM.rds"))
}, {stop("Please Provide a valid pset")})
mData <- mDataNames(pset)
gene_type_col <- ifelse("GeneBioType" %in% colnames(featureInfo(pset, mData)), "GeneBioType", "gene_type")
ft <- rownames(featureInfo(pset, mData))[which(featureInfo(pset, mData)[[gene_type_col]] == "protein_coding")]
if(is.na(tissue)||tissue=="all"){
chosen.cells <- cellNames(pset)
} else {
chosen.cells <- cellNames(pset)[which(cellInfo(pset)$tissueid == tissue)]
}
return(list(pset = pset, mData = mData, chosen.cells = chosen.cells))
}
toRunExtended <- data.frame(fread(file.path(runlistDir,"toRunMetaByGene.txt"), header=FALSE))
# need to do this "trick" because names are made path safe, and arguments are derived from paths for snakemake's sake
toRunThis <- toRunExtended[make.names.2(toRunExtended[,3]) == drug & make.names.2(toRunExtended[,2]) == tissue & make.names.2(toRunExtended[,1]) == gene, ]
drug <- unique(toRunThis[,3])
tissue <- unique(toRunThis[,2])
gene <- unique(toRunThis[,1])
pSets <- toRunThis[,4]
if(!file.exists(myOutDir)) dir.create(myOutDir)
pset.list <- lapply(pSets, loadPSet, tissue=tissue)
names(pset.list) <- pSets
runPermutations <- function(beta_obs, errors, model.data, R){
perm_res <- numeric(R)
perm_res <- foreach(i =icount(R), .combine=c, .inorder=FALSE) %do% {
errors_p <- sample(errors)
model.data$y_p <- beta_obs*model.data[,"x"] + errors_p
lmer_p <- suppressMessages(lmer(y_p~(x + 0| dataset) + x + 0, model.data))
sum(unlist(ranef(lmer_p))^2)
}
return(perm_res)
}
psetMolProf <- lapply(pset.list, function(pset.pars){
pset <- pset.pars$pset
chosen.cells <- pset.pars$chosen.cells
mData <- pset.pars$mData
mol.prof <- assay(summarizeMolecularProfiles(pset, mData, cell.lines = chosen.cells))
return(mol.prof)
})
names(psetMolProf) <- pSets
y.list <- lapply(pset.list, function(pset.pars){
pset <- pset.pars$pset
chosen.cells <- pset.pars$chosen.cells
drug.res <- summarizeSensitivityProfiles(pset, "aac_recomputed", cell.lines = chosen.cells)[drug,]
return(drug.res)
})
x.list <- lapply(pset.list, function(pset.pars){
pset <- pset.pars$pset
chosen.cells <- pset.pars$chosen.cells
mData <- pset.pars$mData
mol.prof <- psetMolProf[[name(pset)]]
if(grepl(pat="ENSG", x=gene)){
myx <- grep(paste0("^", gene, "(\\.[0-9]+)?$"), rownames(mol.prof))
} else {
myx <- grep(paste0("^", gene, "$"), rownames(mol.prof))
}
if(!length(myx)) return(rep(NA_real_, times=length(pset.pars$chosen.cells)))
if(length(myx) > 1) stop(paste0("multiple genes matched:", gene, " id in pset ", name(pset), ", please investigate"))
mol.prof <- mol.prof[myx,]
return(mol.prof)
})
tissue.list <- lapply(pset.list, function(pset.pars){
return(cellInfo(pset.pars$pset)[pset.pars$chosen.cells,"tissueid"])
})
model.data <- data.frame(x = unlist(x.list),
y = unlist(y.list),
dataset = unlist(lapply(pset.list, function(pset.pars){
return(rep(name(pset.pars$pset), times=length(pset.pars$chosen.cells)))
})),
tissueid = unlist(tissue.list)
)
model.data <- model.data[complete.cases(model.data),]
regressOutTissueAndScale <- function(model.data){
for(ds in model.data[,"dataset"]){
myx <- model.data[,"dataset"]==ds
if(length(unique(model.data[myx,"tissueid"]))>=2){
model.data[myx,"x"] <- residuals(lm(x~tissueid, data=model.data[myx,]))
model.data[myx,"y"] <- residuals(lm(y~tissueid, data=model.data[myx,]))
}
model.data[myx,"x"] <- scale(model.data[myx, "x"])
model.data[myx,"y"] <- scale(model.data[myx, "y"])
}
return(model.data)
}
model.data <- regressOutTissueAndScale(model.data)
if(sum(table(model.data[,"dataset"]) >= 20) > 2){
lmer1 <- lmer(y~(x + 0| dataset) + x + 0, model.data)
tobs <- sum(unlist(ranef(lmer1))^2)
beta_obs <- fixef(lmer1)[1]
errors <- model.data[,"y"] - fixef(lmer1)[1]*model.data[,"x"]
permRes <- runPermutations(beta_obs, errors, model.data, R)
heterogeneityRes <- list(gene = gene, pvalue = mean(permRes > tobs))
saveRDS(heterogeneityRes, file=file.path(myOutDir, make.names.2(paste(drug, tissue, gene, "hetPerm", R, "out.rds", sep="_"))))
} else {
message(paste0("Skipping gene: ", gene))
}
# Sirolimus_Myeloid_ENSG00000164850
# 5.Fluorouracil_Myeloid_ENSG00000112697
# Navitoclax_Lung_ENSG00000002586
# Lapatinib_Breast_ENSG00000197520
# ENSG00000100300 Breast Alpelisib CCLE.CTRPv2