-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule1.R
More file actions
432 lines (381 loc) · 15.3 KB
/
Copy pathmodule1.R
File metadata and controls
432 lines (381 loc) · 15.3 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#raw data tab
#tabPanel("Raw Data",
Module_Raw_data_UI<- function(id)
{
ns<-NS(id)
tagList(
#Select organism
fluidRow(column(2,
br(),
br(),
selectInput(ns("organism"), label = h5("Select organism"),
choices = list("","Human" = 1, "Mouse" = 2),
selected = 1))),
#select refrence genome
fluidRow(column(2,
selectInput(ns("refgenome"), label = h5("Select reference genome"),
choices = list("", "BioMart" = 1, "Own upload" = 2),
selected = NULL)),
column(1,
br(),
br(),
actionButton(ns("help1"), "Help"))),
uiOutput(ns("inp_ch")),
fluidRow(column(4,uiOutput(ns("file_ref_genome")))),
fluidRow(column(4,uiOutput(ns("file3")))),
#Read expression table
fluidRow(column(4,uiOutput(ns("file1")))),
#Read annotation table
# #conditionalPanel("input.file1",
fluidRow(column(4,uiOutput(ns("file2")))),#,
#
bsModal(ns("modalhelp"), "Selection of a reference genome", ns("help1"),
helpText('When using Kallisto Data, a gene annotation file is needed, which
links transcript identifiers to gene names.\n
Either select the BioMart Database to construct a gene annotation file
or upload your own file.\n
In case of uploading your own file, it must be a .csv or .tsv file
containing a column called "TXNAME" which holds the ensembl transcript id versions
and a column called "SYMBOL" containing the external gene names / gene symbols.'))
)
}
Module_Raw_data_Input<-function(input,output,session)
{
#Step 1: Raw data tab
#Aim: Display count/expression table and annotation table
#Procedure:
#Option 1: Generate count table from kallisto file and upload annotation table
#Take kallisto file path as input
#Upload annotation table
#Option 2: Upload Count/expression table and annotation table
#Upload count table/expression table and annotation table from file
#observeEvent(input$reset_button, {js$reset()})
#Function that reads data from a file
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
print("module 1 line 56 ok")
input_file <- function(in_file)
{
if(is.null(in_file))
{
return (NULL)
}
else
return(read.csv(in_file$datapath, header = TRUE,sep = "\t",check.names = FALSE,quote = "\""))
}
output$inp_ch<-renderUI({
if(input$organism!="")
radioButtons(session$ns("filechoice"), "Choose starting point",
choices = list("kallisto"=1, "count table"=2),selected = 2 )
})
#Option 1: start with kallisto
#Input kallisto file path to compute count table/expression table to be displayed
output$file3<-renderUI({
# validate(need(try(input$organism!=""),"Please choose organism under study"))
#validate(need(try(as.numeric(input$filechoice)!=0),"Please choose input source"))
# print(as.numeric(input$filechoice))
req(input$filechoice)
if(as.numeric(input$filechoice)==1 && input$organism!="")
{
#shinyDirButton("dir", "Chose directory","Upload")
textInput(session$ns("kallisto_path"), "Kallisto path", value = "")#E:/Zenitha/kallisto")
}
})
output$file_ref_genome <- renderUI({
if(as.numeric(input$refgenome == 2)){
fileInput(session$ns("refgenome_path"), label = h5('Choose Reference genome file to upload'),
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
))
}
# textInput(session$ns("refgenome_path"), "Reference genome path", value = "")
})
#compute expression table/count table
#t2g--->txi
#This reactive prepeares input for the reactive txi which generates count table from kallisto files
t2g<-reactive({
if (!exists("mart")) {
org<-NULL
if(as.numeric(input$organism==1)) org<-"hsapiens_gene_ensembl"
else if(as.numeric(input$organism==2)) org<-"mmusculus_gene_ensembl"
mart <-
biomaRt::useMart(
#host = "jul2017.archive.ensembl.org",
#host = "uswest.ensembl.org",
#host="grch37.ensembl.org",#,
host="ensembl.org",#
#host="useast.ensembl.org",
biomart = "ENSEMBL_MART_ENSEMBL",
dataset = org)
print(mart)
t2g <- biomaRt::getBM(
attributes = c("ensembl_transcript_id_version",
"ensembl_gene_id",
"external_gene_name"),
mart = mart
)
print(head(t2g))
t2g
}
})
# create the count matrix out of the kallisto abundance table
txi <-
reactive( {
#validate(need(try(input$kallisto_path!=""),"Please enter path of kallisto file"))
if (as.numeric(input$filechoice)==1 && input$kallisto_path!=""){
print("module 1 line 126 ok")
# Create a Progress object
progress <- shiny::Progress$new()
# Make sure it closes when we exit this reactive, even if there's an error
on.exit(progress$close())
progress$set(message = "Processing kallisto files", value = 0)
print(dir(input[["kallisto_path"]]))
print(sapply(dir(input[["kallisto_path"]]),
function(id)
{
#file.path(input[["kallisto_path"]], id,"*.tsv")
print("143")
print(file.path(input[["kallisto_path"]],id, "*.h5"))
#list.files(
file.path(input[["kallisto_path"]],id, "*.h5")
}
))
# Increment the progress bar, and update the detail text.
progress$inc(1/2, detail = paste("Doing part", 1,"/",2))
# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
if(as.numeric(input$refgenome==1))
{
d<-tximport(sapply(dir(input[["kallisto_path"]]),
function(id)
{
print("154")
print(list.files(file.path(input[["kallisto_path"]],id), "*.h5",full.names = T))
list.files(
file.path(input[["kallisto_path"]],id)
, "*.h5"
, full.names=TRUE)
}
),#kallisto/abundance.h5
"kallisto",
tx2gene = t2g()[, c("ensembl_transcript_id_version",
"external_gene_name")]
)
print(head(t2g()))
print("line 189")
print(head(d))
}
else
{
#input_data<-input_file(input$refgenome_path)
t2x <- read.delim(file=input$refgenome_path$datapath, sep=",")
# t2x<-merge.data.frame(x = t2g(), y = t2x,
# by.x = "ensembl_transcript_id_version", by.y = "TXNAME")
t2x<-t2x[,c("TXNAME", "SYMBOL")]
d<-tximport(sapply(dir(input[["kallisto_path"]]),
function(id)
{
print("154")
print(list.files(file.path(input[["kallisto_path"]],id), "*.h5",full.names = T))
list.files(
file.path(input[["kallisto_path"]],id)
, "*.h5"
, full.names=TRUE)
}
),#kallisto/abundance.h5
"kallisto",
tx2gene = t2x
#geneIdCol = "external_gene_name",
#txOut = T,
#ignoreTxVersion = TRUE
)
}
print("line 184")
# Increment the progress bar, and update the detail text.
progress$inc(2/2, detail = paste("Doing part", 2,"/",2))
# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
print("module 1 line 164")
d
}
})
#Option2: Upload count/expression table and annotation table from user
#Input file containing count table/expression table as input from user
output$file1<-renderUI({
#validate(need(try(input$organism)!="","Please choose organism under study"))
req(input$filechoice)
if(as.numeric(input$filechoice)==2)
{
fileInput(session$ns("file1"), label = h5('Choose Expression file to upload'),
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
)
}
})
#Input file containing annotation file as input from the user
output$file2<-renderUI({
# validate(need(try(input$organism!=""),"Please choose organism under study"),
# need(try(!is.null(input$filechoice)),"Please choose startng point of input"))
# if( input$organism!="" && !is.null(input$filechoice))
# {
req(input$filechoice)
print("line 194 ok")
#print(as.numeric(input$filechoice))
if(as.numeric(input$filechoice)==2)#!is.null(input$file1) &&
{
req(input$file1)
fileInput(session$ns("file2"), label = h5('Choose Annotation file to upload'),
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
)
}
else if( as.numeric(input$filechoice)==1)#input$kallisto_path!="" &&
{
req(input$kallisto_path)
fileInput(session$ns("file2"), label = h5('Choose Annotation file to upload'),
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
)
}
})
#compose count /expression table obtained as input to be displayed
#edata reactive contains the count/expression table
edata<-reactive({
#setting up expression data
#Get expression table from file
if(as.numeric(input$filechoice)==2)
{
input_exp<-input_file(input$file1)
if(!is.null(input_exp))
{
data<-input_exp[,-1]
col_names<-colnames(data)
print("line 248 module 1")
print(head(data))
#convert entries to integers
data=as.matrix(data)
storage.mode(data)="integer"
data= data.frame(data)
rownames(data)=as.character(input_exp[,1])
colnames(data)<-col_names
data<-data
}
}
else if(as.numeric(input$filechoice)==1)
{
dat<-as.data.frame(txi()$counts)
sample_names<-colnames(dat)
#convert entries to integers
data=as.matrix(dat)
storage.mode(data)="integer"
data= data.frame(data)
rownames(data)=rownames(dat)
colnames(data)<-sample_names
print("module 1 line 271 ok")
print(head(data))
data <- data#[ rowSums(data) > 1, ]
}
})
edata2 <- reactive({
if(as.numeric(input$filechoice)==2)
{
input_exp<-input_file(input$file1)
if(!is.null(input_exp))
{
data<-input_exp[,-1]
col_names<-colnames(data)
print("line 248 module 1")
print(head(data))
#convert entries to integers
data=as.matrix(data)
storage.mode(data)="integer"
data= data.frame(data)
rownames(data)=as.character(input_exp[,1])
colnames(data)<-col_names
data<-data
}
}
else if(as.numeric(input$filechoice)==1)
{
data <- txi()
}
})
#Compose annotation data from file
#pdata contains annotation file
pData<-reactive({
#setting up annotation data
#Get annotation table from file
pheno<-input_file(input$file2)
print("module 1 line 285 ok")
print(pheno)
#Get expression table
data<-edata()
#print(head(data))
#Expression table and annotation table should not be null
if(!is.null(data) && !is.null(pheno))
{
#Assuming that the first column of annotation table is sample id ,extract all sample IDs
sample_id = pheno[,1]
#print(sample_id)
#Get all sample IDs from expression table(sample ID refer to column names of expression table)
exp_sample_id = colnames(data)
#Check if all sample ID in expression table are present in the annotation table
if (all(exp_sample_id %in% sample_id))
{
#print("Yay")
#set all variables of annotation table as factors
col<-1:ncol(pheno)
for (i in col)
{
pheno[,i]<-as.factor(pheno[,i])
}
#Remove those sample IDs that are present in the expression table but absent in the annotation table from the annotation table
idx<-NULL
if(!all(sample_id %in% exp_sample_id))
{
idx<-which(!(sample_id %in% exp_sample_id))
pheno <- pheno[-idx, ]
}
pheno<-pheno
}
else
{
pheno<-NULL
}
pheno
}
})
out<-list(file2=reactive({input$file2}),
edata=reactive({edata()}),
pData=reactive({pData()}),
organism=reactive({input$organism}),
edata2=reactive({edata2()}))#c(1,2,3,4)}))
#input$file2}))#,edata=reactive({edata()}),pData=reactive({pData()}))
return(out)
}