-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
163 lines (131 loc) · 5.13 KB
/
app.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
library(shiny)
library(plotly)
library(DT)
library(ggplot2)
library(tibble)
library(ggsurvfit)
library(survminer)
library(gridExtra)
library(tidyverse)
library(broom)
library(survival)
library(shinydashboard)
select=read.csv("data/subtype_select.csv")
k=1
m=select$cancer[k]
n=select$subtype[k]
data1=read.csv(paste( "data/",m,"_", n, ".csv",sep=""))
data1<-data1[order(data1$BatchId),]
data1<-data1[,-1]
data=data1[data1$os_time!=0&!is.na(data1$os_time),]
data=data[data$os_time>0,]
names=colnames(data)[1883:ncol(data)]
data2=data[,2:1882]
data3<-log2(data2+1)
genes<-colnames(data3)
datause<-cbind(data3,data$os_status,data$os_time)
colnames(datause)[1882]<-"os_status"
colnames(datause)[1883]<-"os_time"
datausepre=datause[,1:1881]
geneid=colnames(datausepre)
exclude=c()
means1=colMeans(datausepre)
ps_rho_cutoff=0.9
thr=2
x_sub=datausepre[,means1>thr]
xcorr=cor(x_sub)
for(i in 1:ncol(x_sub)){
for(j in i:ncol(x_sub)){
if(xcorr[i,j]>ps_rho_cutoff & xcorr[i,j]!=1){
exclude_1=ifelse(means1[geneid==rownames(xcorr)[i]]>=means1[geneid==colnames(xcorr)[j]],
colnames(xcorr)[j], rownames(xcorr)[i])
exclude=c(exclude, exclude_1)
}
}
}
if(length(unique(exclude))==0){
datapre=x_sub
}else{
datapre=x_sub[,!(colnames(x_sub) %in% unique(exclude))]
}
geneidfinal=colnames(datapre)
#data.train=cbind(data.train.raw$submitter_id,x.train,data.train.raw[,1883:ncol(data.train.raw)])
datausefinal=cbind(datause$os_time,datause$os_status,datapre)
colnames(datausefinal)[1]='os_time'
colnames(datausefinal)[2]='os_status'
#dataplot=read.csv(paste('/users/yilin/Downloads/cureitsurvival-2/cure',m,n,'.csv'))
dataplot=read.csv(paste('data/cure',m,n,'.csv'))
dataplot$color[dataplot$scoretest<0.01]=1
dataplot$color[dataplot$scoretest>=0.01]=0
dataplot$color=as.factor(dataplot$color)
dataplot$curepvalue<-apply(dataplot[,c('curepvalue1','curepvalue2','curepvalue3')],1,min)
dataplot<-dataplot%>%filter(curepvalue!=0)
dataplot$curepvalue=-log10(dataplot$curepvalue)
dataplot$survivalpvalue<-apply(dataplot[,c('survivalpvalue1','survivalpvalue2','survivalpvalue3')],1,min)
dataplot<-dataplot%>%filter(survivalpvalue!=0)
dataplot$survivalpvalue=-log10(dataplot$survivalpvalue)
dataplot$coxpvalue=-log10(dataplot$scoretest)
dataplot<-dataplot[!dataplot$survivalpvalue>=10,]
dataplot<-dataplot[!dataplot$curepvalue>=10,]
dataplot<-dataplot[!dataplot$coxpvalue>=10,]
#dataplot<-dataplot[!abs(dataplot$survival)>=10,]
#dataplot<-dataplot[!abs(dataplot$cure)>=10,]
#dataplot<-dataplot[!abs(dataplot$cox)>=10,]
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidPage(
fluidRow(
column(6,plotlyOutput("plot")),
column(6,plotlyOutput("text"),plotOutput("text1"))
)
#plotlyOutput("plot"),
#plotlyOutput("text"),
#plotlyOutput("text1")
)
)
)
server<-function(input,output){
#data <- reactive({
#dataplot
# })
output$plot <- renderPlotly({
#d <- data()
plot_ly(dataplot, x= ~curepvalue, y=~survivalpvalue, split=~color, mode = "markers", type = "scatter", source="mysource")
#ggplot(data=dataplot,aes(x=curepvalue,y=survivalpvalue,col=color))+geom_point()+scale_color_manual(values=c('Black','Red'))
})
output$text <- renderPlotly({
event.data <- event_data("plotly_click", source = "mysource")
print(event.data)
if(is.null(event.data)) { return(NULL)}
index=which(dataplot$survivalpvalue==event.data$y)
geneid=dataplot$gene[index]
datausefinal$label1=cut(datausefinal[,geneid],breaks=c(min(datausefinal[,geneid]),quantile(datausefinal[,geneid],0.25),quantile(datausefinal[,geneid],0.5),quantile(datausefinal[,geneid],0.75),max(datausefinal[,geneid])),
include.lowest = T,right = F,label=c(0,1,2,3))
fit<-survfit(Surv(datausefinal$os_time,datausefinal$os_status)~datausefinal$label1,data=datausefinal)
#plot_ly(datausefinal,x=~os_time,y=~os_status)
#ggplot(data=datausefinal,aes(x=os_time,y=os_status))+geom_point()
p<-ggsurvplot(fit,data=datausefinal, risk.table = TRUE, risk.table.y.text.col = TRUE)+labs(title=geneid)
ggplotly(p[[1]])
})
output$text1 <- renderPlot({
event.data <- event_data("plotly_click", source = "mysource")
print(event.data)
if(is.null(event.data)) { return(NULL)}
index=which(dataplot$survivalpvalue==event.data$y)
geneid=dataplot$gene[index]
#plot_ly(data=datausefinal,x=~str(geneid),type='histogram')
hist(datausefinal[,geneid],main=geneid)
})
output$download_plotly_widget <- downloadHandler(
filename = function() {
paste("/users/yilin/Downloads/data-", Sys.Date(), ".html", sep = "")
},
content = function(file) {
# export plotly html widget as a temp file to download.
saveWidget(as_widget(session_store$plt), file, selfcontained = TRUE)
}
)
}
shinyApp(ui, server)