@sjcrh/proteinpaint-server 2.122.0 → 2.124.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils/getBurden.R DELETED
@@ -1,371 +0,0 @@
1
- # TODOs: see TODOs in the code
2
-
3
- # function to compute burden estimate
4
- # fitsData: file path to fit data
5
- # survData: file path to surv data
6
- # sampleData: file path to sample data
7
- # parallelizeChcs: boolean for whether to parallelize across chcs
8
- get_burden <- function(fitsData, survData, sampleData, parallelizeChcs) {
9
- load(fitsData)
10
- load(survData)
11
- # Qi made many newdata_chc_sampled so we have 1000 times more donors -- but in different files.
12
- load(sampleData)
13
- # survs[[1]]
14
-
15
- ############################ These are the input values in APP that users can change. Edgar, these should be the same as the APP before, variable names and units. #############
16
- ### Input the primary DX.
17
- # pr=5
18
- # agecut=40 ##### Edgar, This is not an user input paramter, but we input this. This depends on the DX. For example, here for CNS we use 40. For HL DX, it is 55. I will give this value for each DX.
19
-
20
- # # # Input person's values, 18 input X's , plus the input primary DX
21
- # sexval=1 #sex, take value 1 for male and 0 for female
22
- # whiteval=1 # Race white or not, 1 for white, 0 for non-white
23
- # agedxval=6 # age at primary cancer DX
24
-
25
- # #### Chemotherapy
26
- # steroidval=0 #Steroids 1 for yes 0 for no
27
- # bleoval=0; ##Bleomycin
28
- # vcrval=12; #Vincristine
29
- # etopval=2500; #Etoposide
30
- # itmtval=0; #Intrathecal Methotrexate
31
- # cedval=1.6 # Cyclophosphamide, 0.7692 mean 7692.
32
- # cispval=300 #Cisplatin
33
- # doxval=0 #Anthracycline, 3 mean 300 ml/m2
34
- # carboval=0 ## Carboplatin
35
- # hdmtxval=0 ## High-Dose Methotrexate
36
-
37
- # # Radiation
38
- # brainval=5.4 #Brain, 5.4 means 54Gy, 5400 cGy. #####Same for all RT doses.#####
39
- # chestval=2.4 # chest/neck RT, 2.4 for 24 Gy
40
- # heartval=0 # Heart RT
41
- # pelvisval=0 #pelvis RT
42
- # abdval=2.4 # Abdominal RT
43
-
44
- ####################################################################################
45
-
46
- ##### if no TX, use these.
47
- # steroidval=0; bleoval=0; vcrval=0; etopval=0; itmtval=0; cedval=0; cispval=0; brainval=0;
48
- # doxval=0; chestval=0; abdval=0;
49
-
50
- # survs[[1]]
51
-
52
- ############### no TX
53
- # steroidval=0; bleoval=0; vcrval=0; etopval=0; itmtval=0; cedval=0; cispval=0; brainval=0; doxval=0; chestval=0; abdval=0; heartval=0; pelvisval=0; carboval=0; hdmtxval=0
54
-
55
- newdata_chc_sampled=do.call("rbind", replicate(6,newdata_chc_sampled, simplify = FALSE))
56
- newdata_chc_sampled$t.startage=seq(5,70,1)
57
- newdata_chc_sampled$t.endage=seq(6,71,1)
58
- ### originally data fit to 60 only. using cphfits can get est up to 60 only. ==> later I further cut at 50 or so to fit lines, becuase original data had 95th percentile around age 50 or so.
59
- newdata_chc_sampled=newdata_chc_sampled[newdata_chc_sampled$t.endage<=60,]
60
-
61
- # paste(names(input), input, sep = ":", collapse = ",")
62
- pr=input$diaggrp
63
- # agecut was previously hardcoded to 40 above
64
- agecut=c('1'=50, '2'=45, '3'=55, '4'=50, '5'=40, '6'=60, '7'=50, '8'=45, '9'=45, '10'=45, '11'=50 )[pr]
65
- sexval=input$sex
66
- newdata_chc_sampled$sex=input$sex # sexval
67
- newdata_chc_sampled$white=input$white # whiteval
68
- newdata_chc_sampled$agedx2=input$agedx # agedxval
69
- newdata_chc_sampled$steroid=input$steroid # steroidval
70
- newdata_chc_sampled$bleodose=input$bleo # bleoval
71
- newdata_chc_sampled$vcrdose=input$vcr # vcrval
72
- newdata_chc_sampled$etopdose=input$etop # etopval
73
- newdata_chc_sampled$itmtxdose=input$itmt # itmtval
74
- newdata_chc_sampled$ced_sum2=input$ced # cedval
75
- newdata_chc_sampled$cisplatdose=input$cisp # cispval
76
- newdata_chc_sampled$brainrad2=input$brain # brainval
77
- newdata_chc_sampled$doxed_sum2=input$dox # doxval
78
- newdata_chc_sampled$chestrad2=input$chest # chestval
79
- newdata_chc_sampled$abdrad2=input$abd # abdval
80
- newdata_chc_sampled$heartradboth2=input$heart # heartval
81
- newdata_chc_sampled$pelvisrad2=input$pelvis # pelvisval
82
- newdata_chc_sampled$carboplatdose=input$carbo # carboval
83
- newdata_chc_sampled$hdmtxdose=input$hdmtx # hdmtxval
84
-
85
- # newdata_chc_sampled$sex=sexval
86
- # newdata_chc_sampled$white=whiteval
87
- # newdata_chc_sampled$agedx2=agedxval
88
- # newdata_chc_sampled$steroid=steroidval
89
- # newdata_chc_sampled$bleodose=bleoval
90
- # newdata_chc_sampled$vcrdose=vcrval
91
- # newdata_chc_sampled$etopdose=etopval
92
- # newdata_chc_sampled$itmtxdose=itmtval
93
- # newdata_chc_sampled$ced_sum2=cedval
94
- # newdata_chc_sampled$cisplatdose=cispval
95
- # newdata_chc_sampled$brainrad2=brainval
96
- # newdata_chc_sampled$doxed_sum2=doxval
97
- # newdata_chc_sampled$chestrad2=chestval
98
- # newdata_chc_sampled$abdrad2=abdval
99
- # newdata_chc_sampled$heartradboth2=heartval
100
- # newdata_chc_sampled$pelvisrad2=pelvisval
101
- # newdata_chc_sampled$carboplatdose=carboval
102
- # newdata_chc_sampled$hdmtxdose=hdmtxval
103
-
104
- # 1="Acute lymphoblastic leukemia"
105
- # 2="AML"
106
- # 3="Hodgkin lymphoma"
107
- # 4="Non-Hodgkin lymphoma"
108
- # 5="Central nervous system"
109
- # 6="Bone tumor"
110
- # 7="STS"
111
- # 8="Wilms tumor"
112
- # 9="Neuroblastoma"
113
- # 10="Retinoblastoma"
114
- # 11="Germ cell tumor";
115
-
116
- #results <- mclapply(X = chc_nums, FUN = function(chc_num) predict(cphfits2[[chc_num]], newdata = data.frame(newdata_chc_sampled,primary=pr),type='expected'), mc.cores = cores)
117
- predictChc <- function(chc_num) {
118
- predict(cphfits2[[chc_num]], newdata = data.frame(newdata_chc_sampled,primary=pr),type='expected')
119
- }
120
- chcCols <- NULL
121
- if (parallelizeChcs) {
122
- # parallelize over chc_nums
123
- chcCols <- foreach(chc_num = chc_nums, .combine = cbind) %dopar% {
124
- predictChc(chc_num)
125
- }
126
- } else {
127
- # do not parallelize over chc_nums
128
- # parallelizing over bootstraps instead
129
- chcCols <- foreach(chc_num = chc_nums, .combine = cbind) %do% {
130
- predictChc(chc_num)
131
- }
132
- }
133
-
134
- newdata_chc_sampled = data.frame(newdata_chc_sampled, chcCols)
135
- names(newdata_chc_sampled)[25:50]=paste0("est_chc",chc_nums)
136
- newdata_chc_sampled = newdata_chc_sampled %>%
137
- mutate(sumN_tmp = rowSums(dplyr::select(.,starts_with("est_chc"))))%>%
138
- group_by(mrn) %>%
139
- mutate(sumN_obs = cumsum(sumN_tmp)) %>%
140
- as.data.frame()
141
-
142
- ##Qi: the sumN here depends on all the 26 grouped conditions. So the input X's all matter. That is, if sex is not in a CHC of interest, it would make a difference here on sumN (becuase sex was on some CHCs), and hence make a difference on burden of that CHC even that it is not in the cphfits of that CHC.
143
- newdata_chc_sampled = newdata_chc_sampled %>%
144
- group_by(mrn) %>%
145
- mutate(chc20 = sumN_obs[t.endage == 20]) %>%
146
- ungroup() %>%
147
- as.data.frame()
148
- newdata_chc_sampled$death =1
149
- newdata_chc_sampled$obsCHCat20 = newdata_chc_sampled$current.chc
150
-
151
-
152
- # survival probability
153
- # https://stats.stackexchange.com/questions/288393/calculating-survival-probability-per-person-at-time-t-from-cox-ph
154
-
155
-
156
- newdata_chc_sampled$survprob = exp(-predict(survs[[1]],newdata=data.frame(newdata_chc_sampled,primary=pr),type='expected'))
157
-
158
- #----------------------------------------------------------------------------------------------------------------#
159
- ##### Qi added the below "cumprod" for survival by time t. But need to figure out: What is the "survprob" in BCCT formulat? Should it be survival of the segment, or survival by time t? == need to figure out with YY. Discussed, YY confirmed my way: survival prob in the formula is cumulative, not for that segment.
160
- #----------------------------------------------------------------------------------------------------------------#
161
-
162
- #----------------------------------------------------------------------------------------------------------------#
163
- ## If assume "survprob" is over time (not for each segment):
164
- #### why does the survprob does not decrease over time? I think this is not the real survival probability over time. Do I have to do multiplication over time thinking survprob is the survival over that segment? Try the multiplication over time.===== I think this make sense. In the "predict" above, survial=exp(-expected) was for each row (thinking each row is a separate person). While in newdata_chc_sampled, the rows are for the same person, and the survival depends on the previoys line, so need to multiply the survival from the previous line.
165
- newdata_chc_sampled$survprob4=cumprod(newdata_chc_sampled$survprob)
166
- newdata_chc_sampled$survprob=newdata_chc_sampled$survprob4
167
-
168
- # plot(c(0,90),c(0,1),type="n")
169
- survspline=smooth.spline(newdata_chc_sampled$t.endage[newdata_chc_sampled$t.endage<=agecut],newdata_chc_sampled$survprob[newdata_chc_sampled$t.endage<=agecut],spar=0.5)
170
- predsurv=predict(survspline,seq(0,95,1))
171
-
172
- # lines(predsurv$x,predsurv$y,col=3,lty=2)
173
-
174
- ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
175
-
176
- ###### get rid of the est_chcXX and "sumN"columns which were used to calculate the survival probability only.
177
- # invisible(dim(newdata_chc_sampled))
178
- newdata_chc_sampled=newdata_chc_sampled[,-grep("est_chc", colnames(newdata_chc_sampled))]
179
- newdata_chc_sampled=newdata_chc_sampled[,-grep("sumN", colnames(newdata_chc_sampled))]
180
- # invisible(dim(newdata_chc_sampled))
181
-
182
- ### Add rows t.startage from 60 to 94, and t.endage from 65 to 95; so we can get burden 60-90.
183
- add=newdata_chc_sampled[newdata_chc_sampled$t.startage<=39,]
184
- # table(add$t.startage)
185
- # table(add$t.endage)
186
- add$t.startage=add$t.startage+55
187
- add$t.endage=add$t.endage+55
188
- # table(add$t.startage)
189
- # table(add$t.endage)
190
- newdata_chc_sampled=rbind(newdata_chc_sampled,add)
191
- newdata_chc_sampled=newdata_chc_sampled[order(newdata_chc_sampled$mrn,newdata_chc_sampled$t.startage),]
192
- ### replace the survival prob with the calculated/extrapolated survival probability
193
- smooth_surv=data.frame(age=predsurv$x,surv=predsurv$y)
194
- smooth_surv$surv[smooth_surv$age<=20]=1
195
- #### survival probability cannot be <0. Hanle the years with survival prob<0
196
- #https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=1310013501 This page had the conditional survival based on age
197
- ## take the last year with a positive survival prob, and its survival prob
198
- positive=smooth_surv[smooth_surv$surv>0,]
199
- alast=tail(positive,1)[1,1]
200
- slast=tail(positive,1)[1,2]
201
- #smooth_surv$alast=alast
202
- #smooth_surv$alast=slast
203
- smooth_surv$interval=smooth_surv$age-alast
204
- ### use the last positive survival prob*0.5^(years from the last age with positive survival probability), assuming the conditions survival prob after that age 50% each year.
205
- cave <- function(x) slast*0.5^(max(x["interval"],0))
206
- smooth_surv$surv1=apply(smooth_surv,1,cave)
207
- smooth_surv$surv[smooth_surv$surv<0]=smooth_surv$surv1[smooth_surv$surv<0]
208
-
209
- newdata_chc_sampled=merge(newdata_chc_sampled,smooth_surv,by.x="t.endage",by.y="age")
210
- newdata_chc_sampled$survprob=newdata_chc_sampled$surv
211
-
212
- # when there is an interaction in the model, it gave warning. So I would make a new data with all 0's to make it work.
213
- newdata0=matrix(0,nrow=1,ncol=18)
214
- newdata0=as.data.frame(newdata0)
215
- colnames(newdata0)=c("sex","white","agedx2","steroids","bleodose","vcrdose","etopdose","itmtxdose","ced_sum2",
216
- "cisplatdose","brainrad2","doxed_sum2","chestrad2","abdrad2","heartradboth2","pelvisrad2","carboplatdose","hdmtxdose")
217
-
218
-
219
- newdata_chc_sampled1=newdata_chc_sampled ## do this so each run on chc_num loops below starts with the original newdata_chc_sampled1
220
-
221
-
222
- ##########################################################################
223
- person_burden=NULL
224
-
225
- get_estimate <- function(chc_num) { #### Edgar, you may make this in separate runs to save time.
226
- # print(chc_num)
227
- newdata_chc_sampled=newdata_chc_sampled1
228
-
229
- # linear predictor
230
- newdata_chc_sampled$exp_lp = predict(cphfits2[[chc_num]], newdata = data.frame(newdata_chc_sampled,primary=pr),type='risk',reference="zero")
231
-
232
- # Baseline nelson-aalan est
233
- # https://stats.stackexchange.com/questions/46532/cox-baseline-hazard
234
- j=chc_num
235
- base = basehaz(cphfits2[[chc_num]],centered = F) # this is a cumulative hazard, so need to convert it into non-cumulative version
236
- #centered, if TRUE return data from a predicted survival curve at the mean values of the covariates fit$mean, if FALSE return a prediction for all covariates equal to zero.
237
- #request the hazard for that covariate combination from the survfit() function that is called by basehaz(). https://stats.stackexchange.com/questions/565210/about-getting-baseline-survival-probability-for-a-piecewise-cox-model-with-inter
238
-
239
-
240
- ### Max time in the data is 70.42. We need to estimate up to 90.
241
- #Yutaka: I think we should smooth the cumulative hazard and then take the derivative to get the hazard.
242
- #One thread I found on Web is: "As an approximation you can smooth the fitted baseline cumulative hazard (e.g. by package pspline) and ask for its derivative." Can you try using smooth.spline and smooth the cumulative hazard and then get the derivative? https://cran.r-project.org/web/packages/pspline/pspline.pdf
243
-
244
-
245
- #### Qi added: base is for different DX. Now we run within each pr, so neeed cumulaive hazrd for that pr only
246
- base=base[base$strata==paste("primary=",pr,sep=""),] #cumulative hazard
247
- base=base[base$time<=agecut,] ### shouldn't we use the same age cutoff as the survival function splines? Yes, do so.
248
-
249
- ##### study the smooth parameter. I think spar=1 is the best one to use (most smoothest)
250
- cumHspline=smooth.spline(base$time,base$hazard,spar=1)
251
- predcumhz=predict(cumHspline,seq(0,95,1)) ### predicted cumulative hazard
252
-
253
-
254
- ##### In order to use the above way to get dN0, do Daisuke's original way using cumhz difference. But the difference is that: we fit cumhz with smooth.spline and can extend it to 90 years old.
255
- base=data.frame(time=predcumhz$x,hazard=predcumhz$y) ##Daisuke used the cumHz, here we smoothed it and then use it.
256
- #### fitted values had <0 values in age 0-8 or so. change to 0 cumulative hazard.
257
- base$hazard[base$hazard<0]=0
258
- base2 = base %>%
259
- mutate(hazard2 = hazard - c(0,hazard[-length(hazard)])) %>%
260
- ungroup() %>% as.data.frame()
261
-
262
- base2 = base2 %>%
263
- mutate(time_cat = cut(time,breaks=seq(0,95,1),right = FALSE, include.lowest = TRUE)) %>%
264
- ungroup()
265
-
266
- base3 = base2 %>%
267
- group_by(time_cat) %>%
268
- dplyr::summarize(dN0 = sum(hazard2)) %>%
269
- filter(!is.na(time_cat))
270
-
271
- ###############
272
- # BCCT
273
- ###############
274
- newdata_chc_sampled$time_cat = cut(newdata_chc_sampled$t.startage,breaks=seq(0,95,1),right = FALSE, include.lowest = TRUE)
275
-
276
- #newdata_chc_sampled$time_cat = cut(newdata_chc_sampled$t.startage,breaks=seq(0,90,5),right = FALSE, include.lowest = TRUE) this won't work, because the input donors file had "t.startage" up to 55 only
277
-
278
- newdata_chc_sampled = newdata_chc_sampled %>%
279
- left_join(base3,by="time_cat")
280
- newdata_chc_sampled$dN0 = ifelse(is.na(newdata_chc_sampled$dN0),0,newdata_chc_sampled$dN0)
281
-
282
- BCCT = newdata_chc_sampled %>%
283
- group_by(mrn) %>%
284
- mutate(BCCT_tmp = exp_lp*survprob*dN0) %>%
285
- mutate(BCCT = cumsum(BCCT_tmp)) %>%
286
- filter(t.startage>=20) %>%
287
- ungroup() %>%
288
- as.data.frame()
289
-
290
- for_web_BCCT = as.data.frame(tidyr::pivot_wider(BCCT,id_cols = mrn, names_from=time_cat,values_from=BCCT))
291
- for_web_BCCT =for_web_BCCT[,-1]
292
-
293
- #### for non-recurrent ones, maximum burden is 1 if the grouped conditions had only 1 condition. (11, 19,29) had only 1 conditons non-recurrent. (15,17,25) had 2 conditons. Take 25 as an example, it had obesity/underweight where underweight was so rare. So max 1 is still good.
294
- #### non-recurrent CHCs are 11, 15, 17, 19, 25, 29. ==I think making it maximum 1 is not good always, becuase these are grouped conditions. For example, chc=10 contains 3 non-recurrent events, so one person could have each of these once, making it maximum 3 in this person for chc=10.
295
- ncoltmp=75 ## from 20 to 94
296
- if(chc_num %in% c(11, 15, 17, 19, 25, 29)){
297
- for_web_BCCT2=apply(for_web_BCCT,c(1,2),function(x) min(x,1))
298
- for_web_BCCT=as.data.frame(for_web_BCCT2)
299
- colnames(for_web_BCCT)=colnames(person_burden[1:ncoltmp])
300
- }
301
- #For example, chc=10 contains 3 non-recurrent events, so one person could have each of these once, making it maximum 3 in this person for chc=10.
302
- if(chc_num %in% c(10)){
303
- for_web_BCCT2=apply(for_web_BCCT,c(1,2),function(x) min(x,3))
304
- for_web_BCCT=as.data.frame(for_web_BCCT2)
305
- colnames(for_web_BCCT)=colnames(person_burden[1:ncoltmp])
306
- }
307
- ##### if female condition 6, then it is 0 for males.
308
- if(chc_num %in% c(6) & sexval==1){
309
- for_web_BCCT2=matrix(0,nrow=1,ncol=ncoltmp)
310
- for_web_BCCT=as.data.frame(for_web_BCCT2)
311
- colnames(for_web_BCCT)=colnames(person_burden[1:75])
312
- }
313
- ##### if male condition 7, then it is 0 for females.d
314
- if(chc_num %in% c(7) & sexval==0){
315
- for_web_BCCT2=matrix(0,nrow=1,ncol=ncoltmp)
316
- for_web_BCCT=as.data.frame(for_web_BCCT2)
317
- colnames(for_web_BCCT)=colnames(person_burden[1:ncoltmp])
318
- }
319
-
320
- for_web_BCCT$chc=chc_num
321
-
322
- return(for_web_BCCT)
323
- }
324
-
325
- results <- NULL
326
- if (parallelizeChcs) {
327
- # parallelize over chc_nums
328
- results <- foreach(chc_num = chc_nums) %dopar% {
329
- get_estimate(chc_num)
330
- }
331
- } else {
332
- # do not parallelize over chc_nums
333
- # parallelizing over bootstraps instead
334
- results <- foreach(chc_num = chc_nums) %do% {
335
- get_estimate(chc_num)
336
- }
337
- }
338
-
339
- # this serial loop works
340
- # for(chc_num in chc_nums) {
341
- # person_burden=rbind(person_burden, get_estimate(chc_num))
342
- # }
343
-
344
- # get estimates
345
- # parallelize across chc_nums
346
- #results <- mclapply(X = chc_nums, FUN = get_estimate, mc.cores = cores)
347
- #results <- lapply(X = chc_nums, FUN = get_estimate)
348
-
349
- # combine rows into person_burden data frame
350
- # TODO: need to verify this
351
- for (n in 1:length(results)) {
352
- row <- results[[n]]
353
- if (!identical(names(row), names(results[[1]]))) {
354
- # some rows may have empty column names because they
355
- # used the columns names from the person_burden table, which
356
- # is NULL when get_estimate() is run in parallel (see the
357
- # if() statements in get_estimate())
358
- # in this situation, use the column names from the first row
359
- names(row) <- names(results[[1]])
360
- }
361
- person_burden <- rbind(person_burden, row)
362
- }
363
-
364
- # person_burden[,30:31]
365
- # sum(person_burden[,31]) ## total burden at 50 years old. 8.971574 for this example.
366
-
367
- #### The predicated burden for 26 grouped CHCs from age 20 to 95.
368
- # write.csv(person_burden,file=paste("primary",pr,".csv"),row.names=F)
369
- #toJSON(person_burden, digits = NA, na = "string")
370
- return(person_burden)
371
- }
@@ -1,40 +0,0 @@
1
- library(jsonlite)
2
- library(Matrix)
3
-
4
- # This script assumes the .Rds file is a gene
5
- # by sample matrix. Use this script to extract
6
- # the values per sample for a particular gene.
7
-
8
- args <- commandArgs(trailingOnly = TRUE)
9
- if (length(args) == 0) {
10
- stop("No arguments provided.")
11
- }
12
-
13
- # str
14
- file_path <- args[1]
15
- # str
16
- gene_name <- args[2]
17
-
18
- data <- tryCatch({
19
- readRDS(file_path)
20
- }, error = function(e) {
21
- # Show error if file is not found or cannot be read
22
- # (i.e. not an RDS file)
23
- stop(paste("Error reading RDS file:", e$message))
24
- })
25
-
26
- # Show error if gene not found
27
- if (!(gene_name %in% rownames(data))) {
28
- stop(paste("Gene", gene_name, "not found in the matrix."))
29
- }
30
-
31
- # Extract the gene data and filter out zero values
32
- gene_data <- data[gene_name, ]
33
- non_zero_indices <- gene_data != 0
34
- filtered_samples <- colnames(data)[non_zero_indices]
35
- filtered_values <- as.numeric(gene_data[non_zero_indices])
36
-
37
- # Create a JSON object with the samples as the keys and the
38
- # values as the values, e.g. {"sample1": 1, "sample2": 2, ...n }
39
- result <- setNames(as.list(filtered_values), filtered_samples)
40
- toJSON(result, pretty = TRUE, auto_unbox = TRUE)
package/utils/hclust.R DELETED
@@ -1,110 +0,0 @@
1
- # Usage: echo <in_json> | Rscript hclust.R > <out_json>
2
-
3
- # in_json: [string] input data in JSON format. Streamed through stdin.
4
- # out_json: [string] clustering results in JSON format. Streamed to stdout.
5
-
6
- # Image is in Rplots.pdf
7
-
8
- ############################
9
- # !!! NOTE !!! #
10
- ############################
11
- # must not auto-install missing package in any R script!
12
- # declare required packages in dockerfile
13
- # at 2023/12, a problem emerged for pp container running in gdc qa-pink
14
- # since the docker images lacks the packages, but the auto-install was prevented due to container safety (no internet query)
15
- # this script will not run, leading to hard to decipher crashing
16
-
17
- # To plot the heatmap uncomment line `library(ggplot2) and lines after "Visualization" comment
18
-
19
- suppressWarnings({
20
- suppressPackageStartupMessages(library(jsonlite))
21
- })
22
- #library(flashClust)
23
- #library(ggplot2) # Uncomment this line to plot heatmap in R
24
-
25
- # Distance matrix
26
- con <- file("stdin", "r")
27
- json <- readLines(con)
28
- close(con)
29
- input <- fromJSON(json)
30
-
31
- #if (length(input$valueIsTransformed) == 0 || input$valueIsTransformed == FALSE) {
32
- # normalized_matrix <- t(scale(t(input$matrix))) # Applying z-score normalization
33
- #} else { # No normalization
34
- # normalized_matrix <- input$matrix
35
- #}
36
-
37
- #rownames(normalized_matrix) <- input$row_names
38
- #colnames(normalized_matrix) <- input$col_names
39
- #normalized_matrix <- na.omit(normalized_matrix) # Removes rows with NA values
40
-
41
- #print ("normalized_matrix")
42
- #print (dim(normalized_matrix))
43
-
44
- # For Rows (i.e genes)
45
- RowDist <- dist(input$matrix, method = tolower(input$distance_method)) # Transposing the matrix
46
- # Hierarchical clustering
47
- RowDend <- hclust(RowDist, method = input$cluster_method)
48
- #RowDend <- flashClust(RowDist, method = input$cluster_method)
49
- RowDendMergeDf <- as.data.frame(RowDend$merge)
50
- colnames(RowDendMergeDf) <- c("n1","n2")
51
- #print ("merge")
52
- #print (RowDendMergeDf)
53
-
54
- #print ("height")
55
- RowDendOrderHeight <- as.data.frame(RowDend$height)
56
- colnames(RowDendOrderHeight) <- "height"
57
- #print (RowDendOrderHeight)
58
-
59
- # For columns (i.e samples)
60
- ColumnDist <- dist(t(input$matrix), method = tolower(input$distance_method)) # Transposing the matrix
61
-
62
- # Hierarchical clustering
63
-
64
- ColumnDend <- hclust(ColumnDist, method = input$cluster_method)
65
- #ColumnDend <- flashClust(ColumnDist,method = input$cluster_method)
66
-
67
- ColumnDendMergeDf <- as.data.frame(ColumnDend$merge)
68
- colnames(ColumnDendMergeDf) <- c("n1","n2")
69
- #print ("merge")
70
- #print (ColumnDendMergeDf)
71
-
72
- #print ("height")
73
- ColumnDendOrderHeight <- as.data.frame(ColumnDend$height)
74
- colnames(ColumnDendOrderHeight) <- "height"
75
- #print (ColumnDendOrderHeight)
76
-
77
- SortedRowNames <- input$row_names[RowDend$order]
78
- SortedColumnNames <- input$col_names[ColumnDend$order]
79
-
80
- output_df <- list()
81
- output_df$method <- input$cluster_method
82
- output_df$RowMerge <- RowDendMergeDf
83
- output_df$RowHeight <- RowDendOrderHeight
84
- output_df$ColumnMerge <- ColumnDendMergeDf
85
- output_df$ColumnHeight <- ColumnDendOrderHeight
86
-
87
- sorted_row_names_df2 <- as.data.frame(SortedRowNames)
88
- colnames(sorted_row_names_df2) <- c("name")
89
- output_df$RowOrder <- sorted_row_names_df2
90
-
91
- sorted_col_names_df2 <- as.data.frame(SortedColumnNames)
92
- colnames(sorted_col_names_df2) <- c("name")
93
- output_df$ColOrder <- sorted_col_names_df2
94
-
95
-
96
- toJSON(output_df, digits = NA, na = "string") # Setting digits = NA makes toJSON() use the max precision. na='string' causes any "not a number" to be reported as string. This from ?toJSON() documentation
97
-
98
-
99
- # Visualization of heatmap, uncomment code below to get ggplot2 image of heatmap
100
-
101
- #SortedMatrix <- input$matrix[RowDend$order, ColumnDend$order]
102
- #SortedRowNames <- rownames(input$matrix)[RowDend$order]
103
- #SortedColumnNames <- colnames(input$matrix)[ColumnDend$order]
104
- #
105
- #matrix_melt <- melt(SortedMatrix)
106
- #ggplot(data = matrix_melt, aes(x = X1, y = X2, fill = value)) +
107
- # geom_tile() + scale_fill_gradient(low="blue", high="red")
108
-
109
- #ggsave("heatmap.png")
110
-
package/utils/km.R DELETED
@@ -1,13 +0,0 @@
1
- library(survival)
2
-
3
- con <- file("stdin","r")
4
- dat <- read.table(con,sep="\t",header=T,quote="")
5
- test <- survdiff(Surv(futime, fustat) ~ rx,data=dat)
6
- testOut <- capture.output(test)
7
- testOut <- gsub(" ","",testOut)
8
- pvalue <- strsplit(testOut[length(testOut)], split = ",")[[1]][2]
9
- if (!grepl("^p=",pvalue)){
10
- stop("Unexpected p-value format.")
11
- }
12
- cat(sub("p=","",pvalue),"\n",sep="")
13
- close(con)
package/utils/lowess.R DELETED
@@ -1,9 +0,0 @@
1
- library(jsonlite)
2
-
3
- # read in data
4
- con <- file("stdin", "r")
5
- json <- readLines(con)
6
- close(con)
7
- data <- fromJSON(json)
8
- result = lowess(data$X, data$Y)
9
- toJSON(result)