-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
612 lines (535 loc) · 21.7 KB
/
app.R
File metadata and controls
612 lines (535 loc) · 21.7 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
library(shiny)
library(DT)
library(stringr)
#install.packages("shinyBS")
library(shinyBS)
#install.packages("pracma")
#library(pracma)
#library(qdapTools)
#install.packages("hash")
library(hash)
#hard code the codons so the app is not dependent on any files
yeastCodOld = list(
c("UUU","F","0.59","26.1","(170666)"),
c("UCU","S","0.26","23.5","(153557)"),
c("UAU","Y","0.56","18.8","(122728)"),
c("UGU","C","0.63"," 8.1","( 52903)"),
c("UUC","F","0.41","18.4","(120510)"),
c("UCC","S","0.16","14.2","( 92923)"),
c("UAC","Y","0.44","14.8","( 96596)"),
c("UGC","C","0.37"," 4.8","( 31095)"),
c("UUA","L","0.28","26.2","(170884)"),
c("UCA","S","0.21","18.7","(122028)"),
c("UAA","*","0.47"," 1.1","( 6913)"),
c("UGA","*","0.30"," 0.7","( 4447)"),
c("UUG","L","0.29","27.2","(177573)"),
c("UCG","S","0.10"," 8.6","( 55951)"),
c("UAG","*","0.23"," 0.5","( 3312)"),
c("UGG","W","1.00","10.4","( 67789)"),
c("CUU","L","0.13","12.3","( 80076)"),
c("CCU","P","0.31","13.5","( 88263)"),
c("CAU","H","0.64","13.6","( 89007)"),
c("CGU","R","0.14"," 6.4","( 41791)"),
c("CUC","L","0.06"," 5.4","( 35545)"),
c("CCC","P","0.15"," 6.8","( 44309)"),
c("CAC","H","0.36"," 7.8","( 50785)"),
c("CGC","R","0.06"," 2.6","( 16993)"),
c("CUA","L","0.14","13.4","( 87619)"),
c("CCA","P","0.42","18.3","(119641)"),
c("CAA","Q","0.69","27.3","(178251)"),
c("CGA","R","0.07"," 3.0","( 19562)"),
c("CUG","L","0.11","10.5","( 68494)"),
c("CCG","P","0.12"," 5.3","( 34597)"),
c("CAG","Q","0.31","12.1","( 79121)"),
c("CGG","R","0.04"," 1.7","( 11351)"),
c("AUU","I","0.46","30.1","(196893)"),
c("ACU","T","0.35","20.3","(132522)"),
c("AAU","N","0.59","35.7","(233124)"),
c("AGU","S","0.16","14.2","( 92466)"),
c("AUC","I","0.26","17.2","(112176)"),
c("ACC","T","0.22","12.7","( 83207)"),
c("AAC","N","0.41","24.8","(162199)"),
c("AGC","S","0.11"," 9.8","( 63726)"),
c("AUA","I","0.27","17.8","(116254)"),
c("ACA","T","0.30","17.8","(116084)"),
c("AAA","K","0.58","41.9","(273618)"),
c("AGA","R","0.48","21.3","(139081)"),
c("AUG","M","1.00","20.9","(136805)"),
c("ACG","T","0.14"," 8.0","( 52045)"),
c("AAG","K","0.42","30.8","(201361)"),
c("AGG","R","0.21"," 9.2","( 60289)"),
c("GUU","V","0.39","22.1","(144243)"),
c("GCU","A","0.38","21.2","(138358)"),
c("GAU","D","0.65","37.6","(245641)"),
c("GGU","G","0.47","23.9","(156109)"),
c("GUC","V","0.21","11.8","( 76947)"),
c("GCC","A","0.22","12.6","( 82357)"),
c("GAC","D","0.35","20.2","(132048)"),
c("GGC","G","0.19"," 9.8","( 63903)"),
c("GUA","V","0.21","11.8","( 76927)"),
c("GCA","A","0.29","16.2","(105910)"),
c("GAA","E","0.70","45.6","(297944)"),
c("GGA","G","0.22","10.9","( 71216)"),
c("GUG","V","0.19","10.8","( 70337)"),
c("GCG","A","0.11"," 6.2","( 40358)"),
c("GAG","E","0.30","19.2","(125717)"),
c("GGG","G","0.12"," 6.0","( 39359)"))
yeastCod = list( #I replaced the RNA with DNA
c("TTT","F","0.59","26.1","(170666)"),
c("TCT","S","0.26","23.5","(153557)"),
c("TAT","Y","0.56","18.8","(122728)"),
c("TGT","C","0.63"," 8.1","( 52903)"),
c("TTC","F","0.41","18.4","(120510)"),
c("TCC","S","0.16","14.2","( 92923)"),
c("TAC","Y","0.44","14.8","( 96596)"),
c("TGC","C","0.37"," 4.8","( 31095)"),
c("TTA","L","0.28","26.2","(170884)"),
c("TCA","S","0.21","18.7","(122028)"),
c("TAA","*","0.47"," 1.1","( 6913)"),
c("TGA","*","0.30"," 0.7","( 4447)"),
c("TTG","L","0.29","27.2","(177573)"),
c("TCG","S","0.10"," 8.6","( 55951)"),
c("TAG","*","0.23"," 0.5","( 3312)"),
c("TGG","W","1.00","10.4","( 67789)"),
c("CTT","L","0.13","12.3","( 80076)"),
c("CCT","P","0.31","13.5","( 88263)"),
c("CAT","H","0.64","13.6","( 89007)"),
c("CGT","R","0.14"," 6.4","( 41791)"),
c("CTC","L","0.06"," 5.4","( 35545)"),
c("CCC","P","0.15"," 6.8","( 44309)"),
c("CAC","H","0.36"," 7.8","( 50785)"),
c("CGC","R","0.06"," 2.6","( 16993)"),
c("CTA","L","0.14","13.4","( 87619)"),
c("CCA","P","0.42","18.3","(119641)"),
c("CAA","Q","0.69","27.3","(178251)"),
c("CGA","R","0.07"," 3.0","( 19562)"),
c("CTG","L","0.11","10.5","( 68494)"),
c("CCG","P","0.12"," 5.3","( 34597)"),
c("CAG","Q","0.31","12.1","( 79121)"),
c("CGG","R","0.04"," 1.7","( 11351)"),
c("ATT","I","0.46","30.1","(196893)"),
c("ACT","T","0.35","20.3","(132522)"),
c("AAT","N","0.59","35.7","(233124)"),
c("AGT","S","0.16","14.2","( 92466)"),
c("ATC","I","0.26","17.2","(112176)"),
c("ACC","T","0.22","12.7","( 83207)"),
c("AAC","N","0.41","24.8","(162199)"),
c("AGC","S","0.11"," 9.8","( 63726)"),
c("ATA","I","0.27","17.8","(116254)"),
c("ACA","T","0.30","17.8","(116084)"),
c("AAA","K","0.58","41.9","(273618)"),
c("AGA","R","0.48","21.3","(139081)"),
c("ATG","M","1.00","20.9","(136805)"),
c("ACG","T","0.14"," 8.0","( 52045)"),
c("AAG","K","0.42","30.8","(201361)"),
c("AGG","R","0.21"," 9.2","( 60289)"),
c("GTT","V","0.39","22.1","(144243)"),
c("GCT","A","0.38","21.2","(138358)"),
c("GAT","D","0.65","37.6","(245641)"),
c("GGT","G","0.47","23.9","(156109)"),
c("GTC","V","0.21","11.8","( 76947)"),
c("GCC","A","0.22","12.6","( 82357)"),
c("GAC","D","0.35","20.2","(132048)"),
c("GGC","G","0.19"," 9.8","( 63903)"),
c("GTA","V","0.21","11.8","( 76927)"),
c("GCA","A","0.29","16.2","(105910)"),
c("GAA","E","0.70","45.6","(297944)"),
c("GGA","G","0.22","10.9","( 71216)"),
c("GTG","V","0.19","10.8","( 70337)"),
c("GCG","A","0.11"," 6.2","( 40358)"),
c("GAG","E","0.30","19.2","(125717)"),
c("GGG","G","0.12"," 6.0","( 39359)"))
#generate two maps - one that maps amino acid to codons, another that maps codons to amino acids
#filter so we only have three codons per amino acid
#we skip handling other things, such as start/stop codons etc.
allAminoAcids = c("A","R","N","D","C","E","Q","G","H","I","L","K","M","F","P","S","T","W","Y","V")
numTmpOrig = vector(mode = "list", length = 20)
codonsPerAAOrig = vector(mode = "list", length = 20)
for(i in 1:length(yeastCod)) {
item = yeastCod[[i]]
l = allAminoAcids == item[2]
if (sum(l) == 1) {
ind = which(l)
codonsPerAAOrig[[ind]] = c(codonsPerAAOrig[[ind]], item[1])
numTmpOrig[[ind]] = c(numTmpOrig[[ind]], as.numeric(item[4]))
} else {
print(paste0("Skipping: ", item[2]))
}
}
#Now, filter out the least used codons
#We only keep max 3 variants for each amino acid, and remove the ones with low frequency per thousand
numTmp = numTmpOrig
codonsPerAA = codonsPerAAOrig
for (i in 1:length(codonsPerAAOrig)) {
fr = numTmpOrig[[i]]
if (length(fr) > 3) {
srt = sort(fr, index.return=TRUE, decreasing = TRUE)
indToKeep = srt$ix[1:3]
numTmp[[i]] = numTmpOrig[[i]][indToKeep]
codonsPerAA[[i]] = codonsPerAAOrig[[i]][indToKeep]
} else {
#sort the others as well
srt = sort(fr, index.return=TRUE, decreasing = TRUE)
numTmp[[i]] = numTmpOrig[[i]][srt$ix]
codonsPerAA[[i]] = codonsPerAAOrig[[i]][srt$ix]
}
}
#the codons are sorted on frequency. So, we should rather use more of the ones in the beginning than the ones in the end
#test
#numTmpOrig[[16]] #23.5 14.2 18.7 8.6 14.2 9.8
#numTmp[[16]] #23.5 18.7 14.2 , looks good
#codonsPerAAOrig[[16]]#"UCU" "UCC" "UCA" "UCG" "AGU" "AGC"
#codonsPerAA[[16]]#"UCU" "UCA" "UCC", looks good. It should be either UCC or AGU at the end
#now create the hash maps
AAToCodon = hash( keys=allAminoAcids, values=codonsPerAA)
codKeys = NULL
codVals = NULL
for (i in 1:length(codonsPerAA)) {
cod = codonsPerAA[[i]]
for (j in 1:length(cod)) {
codKeys = c(codKeys, cod[[j]])
codVals = c(codVals, allAminoAcids[[i]])
}
}
CodonToAA = hash( keys=codKeys, values=codVals)
#there's a bug in textAreaInput, fixed by the code below (which is a copy of the same input)
#See https://stackoverflow.com/questions/40808889/a-dynamically-resizing-shiny-textareainput-box
#based on Shiny textAreaInput
textAreaInput2 <- function (inputId, label, value = "", width = NULL, height = NULL,
cols = NULL, rows = NULL, placeholder = NULL, resize = NULL)
{
value <- restoreInput(id = inputId, default = value)
if (!is.null(resize)) {
resize <- match.arg(resize, c("both", "none", "vertical",
"horizontal"))
}
style <- paste("max-width: 100%;", if (!is.null(width))
paste0("width: ", validateCssUnit(width), ";"), if (!is.null(height))
paste0("height: ", validateCssUnit(height), ";"), if (!is.null(resize))
paste0("resize: ", resize, ";"))
if (length(style) == 0)
style <- NULL
div(class = "form-group",
tags$label(label, `for` = inputId), tags$textarea(id = inputId,
class = "form-control", placeholder = placeholder, style = style,
rows = rows, cols = cols, value))
}
#super inefficent, but that doesn't really matter
cleanupSeq = function(s) {
errorsInd = NULL
errorsChar = NULL
s = toupper(s)
if (str_length(s) == 0) {
return(s)
}
ss = strsplit(s,"")[[1]]
ret = ""
for (i in 1:length(ss)) {
if (ss[i] %in% allAminoAcids) {
ret = paste0(ret,ss[i])
} else {
if (!( (ss[i] == "\t") | (ss[i] == " ") | (ss[i] == "\n") )) { #accept whitespace, just remove it
errorsInd = c(errorsInd, i)
errorsChar = c(errorsChar, ss[i])
}
}
}
return (list(ret, errorsInd, errorsChar))
}
hammingDist = function(str1, str2) {
hDist = 0;
for(i in 1:str_length(str1)) {
if (substr(str1,i,i) != substr(str2,i,i)) {
hDist = hDist + 1
}
}
return (hDist)
}
#test
hammingDist("ATTAAT", "BFTTAT") #expected 3, ok
#for testing:
hammingDists = function(sequences) {
res = NULL
for(i in 1:(length(sequences) - 1)) {
for(j in (i+1):length(sequences)) {
res = c(res,hammingDist(sequences[[i]], sequences[[j]]))
}
}
return(res)
}
hammingDists(list("ABCD", "BBCD", "CCCD", "CABC")) #expect 1,2,4,2,4,3, ok
RNAToProt = function(RNAs) {
res = vector(mode = "list", length = length(RNAs))
for (i in 1:length(RNAs)) {
prot = NULL
RNA = RNAs[[i]]
for (j in 1:(str_length(RNAs[[i]])/3)) {
codon = substr(RNA, (j-1)*3 + 1, j*3)
prot = paste0(prot,CodonToAA[[codon]])
}
res[[i]] = prot
}
return(res)
}
#test
RNAToProt(list("AUCAUGGCU")) #expect IMA, ok
ui <- fluidPage(
titlePanel("Codon Diversification Design Tool"),
tabsetPanel(
id = 'mainTab',
tabPanel("Application",
fluidRow(
column(12,
textAreaInput2("inputSeq", "Input sequence", width="100%", resize="none", cols=140, rows=8)
)
),
fluidRow(
column(6,
mainPanel(width="100%",
fluidRow(
column(4, textAreaInput2("numCopies", "Number of copies", width="100%", resize="none", cols=20, rows=1)),
column(4, actionButton("genButton", "Gen. variants", width = "100%")),
conditionalPanel(
'output.dataGenerated === "t"',
column(4,actionButton("expButton", "Export to file", width = "100%"))
)
)
)
),
column(6,
uiOutput("warningsTitle"),
verbatimTextOutput("warnings"),
tags$head(tags$style("#warnings{color: red;overflow-y:scroll;max-height: 100px;}"))
)
),
fluidRow(
column(12,
conditionalPanel(
'output.dataGenerated === "t"',
DT::dataTableOutput("results", width="100%")
)
)
)
),
tabPanel("Instructions",
h2("Summary"),
p(HTML("This tool was published in 'ourPaperReference'. It can be used to generate several DNA Sequences producing the same protein sequence,<br>where the DNA sequences are optimized to be as diverse as possible. The supported organism is <i>Saccharomyces cerevisiae</i>. <br>")),
h2("Instructions"),
p(HTML(" 1. Paste the protein sequence you want to use in the toolkit<br>")),
p(HTML(" 2. Enter the desired number of diverse DNA copies to generate<br>")),
p(HTML(" 3. Press 'Gen. primers' to generate the DNA sequences.<br>")),
p(HTML(" 4. A warning will be displayed if the inputs are not right.<br>")),
p(HTML(" 5. Download the sequences as a file using 'Export to file'<br>")),
h2("References"),
p(HTML("Our reference<br>")),
)
),
bsModal("expModalDlg", "Export to file", "expButton", size = "large",textInput('filename', 'Filename', "Sequences"), downloadButton('downloadSeq', 'Download'))
)
dataGen = "f"
#We don't allow x and y to be the same here, that will mess this up. n is the total number of copies
#we use the upper right corner in a matrix, and make a vector of only the valid combinations in the upper right corner
#This function calculates the index in that vector.
#
#example below, with n=8, get the index of (6,8). Row is the smallest, i.e. 6, col the largest, i.e. 8
# Index is A + B + C (remember, 1-indexing, not 0)
# A = (row-1)*(n-row+1)
# B = (row-1)*(row-2)/2
# C = col-row
# 0BBBBAAA
# 00BBBAAA
# 000BBAAA
# 0000BAAA
# 00000AAA
# 000000CC
#this is acutally not used in the end, but kept as it is good for debugging, clarity, etc.
getInd = function(n,x,y) {
row = min(x,y)
col = max(x,y)
A = (row-1)*(n-row+1)
B = (row-1)*(row-2)/2
C = col-row
return(A + B + C)
}
#test
#getInd(8,2,3) #should be 8, ok
#getInd(8,6,8) #should be 27, ok
#getInd(10,4,3) #should be 18, ok
#looks ok
#temp for test, comment out later:
inSeq = "ACMT"
numCopies = 8
genSequences = function(inSeq, numCopies) {
results = rep("", numCopies)
accHammDist = rep(0, numCopies * (numCopies - 1)/2)#This is the accumulated Hamming distance so far between all pairs of sequences. For indexing, see above
#precalculate coordinate mapping to save time
ind2Row = rep(NA, length(accHammDist))
ind2Col = rep(NA, length(accHammDist))
ind = 1
for (row in 1:(numCopies-1)) {
for (col in (row+1):numCopies) {
ind2Row[ind] = row
ind2Col[ind] = col
ind = ind + 1
}
}
#test:
#ind2Row: 1 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5 5 5 6 6 7 looks good
#ind2Col: 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8 8 looks good
for (i in 1:str_length(inSeq)) {
#Calculate how many of each codon we should distribute (there's probably a faster way to do this):
AA = substr(inSeq, i, i)
cods = AAToCodon[[AA]]
codonsToDistr = rep(0, length(cods))
for (j in 1:numCopies) {
ind = ((j-1) %% length(cods)) + 1
codonsToDistr[ind] = codonsToDistr[ind] + 1
}
#test codonsToDistr - 8: 3 3 2 6: 2 2 2 - looks good
#now, start by addressing the items with the smallest hamming distance - sort the hamming dist vector and address them in order
#I'm not sure if this is guaranteed to give an optimal solution, but it will be close
srt = sort(accHammDist, index.return = TRUE)
codonIndAss = rep(NA, numCopies)
for (j in 1:length(accHammDist)) {
hammInd = srt$ix[j]
row = ind2Row[hammInd] #so, we are looking at a row and a col in the hamming index matrix, where the row and col represent the distance between two copies of the sequence
col = ind2Col[hammInd]
#assign a codon to copy "row" at this position
if (is.na(codonIndAss[[row]])) {
#check if y is assigned - that will limit the choices
possible = which(codonsToDistr != 0)
if (!is.na(codonIndAss[[col]])) {
#remove the y codon from the possible choices if there are others to choose from
possibleTmp = possible[!(possible == codonIndAss[col])]
if (length(possibleTmp) != 0) {
possible = possibleTmp
}
}
#use the codon with most copies left first. For those with the same, randomize
maxval = max(codonsToDistr[possible])
possible = possible[codonsToDistr[possible] == maxval]
if (length(possible) > 1) {
#randomize and pick one of them
possible = possible[sample(length(possible), 1)]
}
#now, assign the codon
codonIndAss[row] = possible
codonsToDistr[possible] = codonsToDistr[possible] - 1
results[[row]] = paste0(results[[row]], cods[possible]) #this is potentially slow, reallocating the string all the time.
}
#assign a codon to copy 'col' at this position
if (is.na(codonIndAss[[col]])) {
#check if y is assigned - that will limit the choices
possible = which(codonsToDistr != 0)
#remove the x codon from the possible choices if there are others to choose from (we know it is assigned since we just did it)
possibleTmp = possible[!(possible == codonIndAss[row])]
if (length(possibleTmp) != 0) {
possible = possibleTmp
}
#use the codon with most copies left first. For those with the same, randomize
maxval = max(codonsToDistr[possible])
possible = possible[codonsToDistr[possible] == maxval]
if (length(possible) > 1) {
#randomize and pick one of them
possible = possible[sample(length(possible), 1)]
}
#now, assign the codon
codonIndAss[col] = possible
codonsToDistr[possible] = codonsToDistr[possible] - 1
results[[col]] = paste0(results[[col]],cods[possible]) #this is potentially slow, reallocating the string all the time.
}
#now, add to the hamming distance if they are different
if (codonIndAss[[row]] != codonIndAss[[col]]) {
accHammDist[hammInd] = accHammDist[hammInd] + hammingDist(cods[[codonIndAss[[row]]]], cods[[codonIndAss[[col]]]])
}
}
}
return (list(results, accHammDist))
}
#test
origProt = "ACMTACMTMTACMTMTACMTMTACMTMTACMTMTACMTMTACMT"
res = genSequences(inSeq = origProt, numCopies = 8)
res
hammingDists(res[[1]])
all(res[[2]] == hammingDists(res[[1]])) #ok
#check that they all convert to the original string
res2 = RNAToProt(res[[1]])
all(res2 == origProt) #ok
for (i in 1:length(res[[1]])) {
print(res[[1]][[i]])
}
lastGenDf = NULL
onGenButton = function(input, output, session) {
strWarn = ""
dataGen = "f"
failed = FALSE
#get input sequence and check it
inp = paste0(input$inputSeq)
seqTmp = cleanupSeq(input$inputSeq)
sq = seqTmp[[1]]
if (length(seqTmp[[2]]) > 0) {
pos = seqTmp[[2]][1:min(3,length(seqTmp[[2]]))]
ellipseTxt = ""
if (length(seqTmp[[2]]) > 3) {
ellipseTxt = ", ..."
}
positions = paste(as.character(pos), sep="' '", collapse=", ")
chrs = seqTmp[[3]][1:min(3,length(seqTmp[[3]]))]
chars = paste(chrs, sep="' '", collapse=", ")
strWarn = paste0(strWarn, "Error in input sequence: Three first: positions: ", positions, ellipseTxt, " Characters: ", chars, ellipseTxt,"\n")
failed = TRUE
}
#get the number of copies
numCopies = as.numeric(input$numCopies)
if (is.na(numCopies) || numCopies < 1 || numCopies > 100) {
strWarn = paste0(strWarn, "The number of copies must be between 1 and 100\n")
failed = TRUE
}
dfRes <- data.frame(Sequence=character(),
stringsAsFactors=FALSE)
colnames(dfRes) = c("Sequence")
if (!failed) {
dataGen = "t"
#Here we do the main job
res = genSequences(sq, numCopies)
dfRes <- data.frame(Sequence=res[[1]],
stringsAsFactors=FALSE)
#for (i in 1:length(res[[1]])) {
# rbind(dfRes, res[[1]][[i]], stringsAsFactors=F)
#}
print(dfRes)
print(res[[1]])
}
lastGenDf <<- dfRes
output$dataGenerated = renderText(dataGen)
output$results = DT::renderDataTable(DT::datatable({dfRes}, escape = FALSE, width="100%", rownames = FALSE, class="compact", selection = 'none', options = list(dom = 't', pageLength = 20, scrollX = TRUE, scrollY = TRUE)))
output$warningsTitle = renderText({HTML("<b>Warnings</b>")})
output$warnings = renderText(strWarn)
}
server <- function(input, output, session) {
output$dataGenerated = renderText(dataGen)
outputOptions(output, "dataGenerated", suspendWhenHidden = FALSE)
observeEvent(input$genButton, {
onGenButton(input, output, session)
})
output$downloadSeq <- downloadHandler(
filename = function(){ paste0(input$filename, ".txt")
},
#filename = paste0("test", ".FASTA"),
content = function(file) {
stringsToWrite = c("Generated sequences:", "", "")
for (i in 1:length(lastGenDf$Sequence)) {
stringsToWrite = c(stringsToWrite, paste0("Sequence ", i,":"), "", "")
s = lastGenDf$Sequence[i]
ll = str_length(s)
for (j in seq(1, ll, by=80)) {
stringsToWrite = c(stringsToWrite,paste0(substr(s, j, j+79)))
}
stringsToWrite = c(stringsToWrite, "", "")
}
writeLines(stringsToWrite, con=file)
toggleModal(session, modalId = "expModalDlg") #close the dialog
})
}
#don't add any code after the shinyApp line below, it has to be the last line!
shinyApp(ui = ui, server = server)