-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.R
1633 lines (1542 loc) · 73.4 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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##################################################################################################
### eDNAssay: A learned model for evaluating qPCR assay specificity
##################################################################################################
library(shiny)
#setRepositories(addURLs = c(BioC = "https://bioconductor.org/packages/3.8/bioc"))
options(repos = BiocManager::repositories())
library(Biostrings)
library(dplyr)
library(stringr)
library(caret)
library(randomForest)
library(DT)
library(shinycssloaders)
### Load trained model
load("eDNAssay_trained_model.RData")
### Used below to format slider
sliderInput2 <-
function(inputId,
id,
label,
min,
max,
value,
step = NULL,
from_min,
to_max) {
x <- sliderInput(inputId, label, min, max, value, step)
x$children[[2]]$attribs <- c(x$children[[2]]$attribs,
"data-from-min" = from_min,
"data-to-max" = to_max)
x
}
##################################################################################################
### Define user interface
ui <- tagList(
tags$head(tags$style(
HTML(
"body {padding-top: 70px; padding-bottom: 20px; padding-left: 20px; padding-right: 20px;}",
"#big-heading {display: flex; flex-direction: row; justify-content: start; align: left; align-items: center; padding-left: 15px;
padding-right: 15px; padding-top: 0px; padding-bottom: 0px; color: #024f94; font-size: 34pt; font-weight: bold; width: 100%;}",
"h1 {font-size: 18pt; font-weight: bold;}",
"h2 {font-size: 22pt; font-weight: bold; margin-top: 10px; margin-bottom: 0px;}",
"p {font-size: 11.5pt;}",
"a {font-size: 11.5pt;}",
"code {color: black; background-color: #F5F5F5;}",
".progress-bar{background-color: #024f94; border-color: #024f94;}",
".js-irs-0 .irs-single, .js-irs-0 .irs-bar-edge, .js-irs-0 .irs-bar, js-irs-0 .irs-line
{background: #024f94; border-top: #024f94; border-bottom: #024f94;}",
".js-irs-0 .irs-single {font-size: 9.5pt; font-weight: bold; top: 0px; bottom: -20px;}",
".irs-grid-text {font-size: 11.5pt; font-weight: bold;}",
".irs-from, .irs-to, .irs-min, .irs-max {visibility: hidden !important;}",
"label {font-size: 11.5pt; margin-bottom: 6px;}",
".form-group, .selectize-control {margin-bottom: -10px;}",
".shiny-split-layout>div {overflow: hidden;}"
)
)),
navbarPage(
"eDNAssay",
position = "fixed-top",
header = div(
id = "big-heading",
"eDNAssay: A learned model of qPCR cross-amplification",
windowTitle = "eDNAssay"
),
### Predict amplification page
tabPanel(
"Predict Amplification",
hr(),
p(
paste(
'Welcome to eDNAssay! This tool uses supervised machine learning to predict qPCR assay specificity, particularly
as applied to environmental samples. Simply input aligned sequences and brief metadata. For each sequence, a random forest
classifier \U2012 trained on TaqMan probe-based qPCR results \U2012 outputs the probability of being assigned to the "amplify"
class. The Optimize Threshold page can help determine in vitro testing requirements. See the Learn More page and Kronenberger et al.
(2022) for more information.'
)
),
br(),
### Sidebar for inputting and outputting data
sidebarLayout(
sidebarPanel(
h1("Input aligned sequences"),
p(
("\U2022 Input a FASTA file containing aligned sequences."),
p(
"\U2022 Oligonucleotide sequences must appear first,
ordered as forward primer, reverse primer, then probe."
),
p(
"\U2022 Only IUPAC-approved characters are allowed. Dashes are treated as Ns (any base) for a
conservative estimate of assay specificity."
),
p(
"\U2022 See this"
,
a(
href = "FVIR_alignment.fas",
"example FASTA",
download = NA,
target = "_blank"
)
)
),
fileInput("alignment", "Aligned sequence file"),
hr(style = "border-top: 1px solid #A9A9A9;"),
h1("Input metadata"),
textInput("Assay", "Assay name"),
br(),
numericInput(
"F_Tm",
HTML(paste0("Forward primer T", tags$sub("m"))),
60,
min = 45,
max = 75,
step = 0.1
),
br(),
numericInput(
"R_Tm",
HTML(paste0("Reverse primer T", tags$sub("m"))),
60,
min = 45,
max = 75,
step = 0.1
),
br(),
numericInput(
"P_Tm",
HTML(paste0("Probe T", tags$sub("m"))),
70,
min = 55,
max = 85,
step = 0.1
),
hr(style = "border-top: 1px solid #A9A9A9;"),
h1("Predict amplification"),
actionButton("goButton", "PREDICT",
class =
"btn-primary",
style = "margin-top: 20px; background: #024f94"),
br(),
br(),
hr(style = "border-top: 1px solid #A9A9A9;"),
h1("Download results"),
p(strong("Amplification predictions")),
downloadButton("downloadData", "Download"),
br(),
br(),
p(strong("Base-pair mismatches")),
p(
em(
"If of interest; see Kronenberger et al. (2022) for parameter definitions."
)
),
downloadButton("downloadData2", "Download"),
br(),
),
# Main panel for displaying outputs ----
mainPanel(tableOutput("table") %>% withSpinner(color =
"#024f94"))
),
br()
),
### Optimize Threshold page
tabPanel(
"Optimize Threshold",
hr(),
h1(
"Adjust false negative error cost to determine testing requirements"
),
p(
paste(
'Binary classification models like eDNAssay utilize a threshold value to assign class labels to
probabilities. The default threshold is typically 0.5. Here, this would mean that templates with
assignment probabilities < 0.5 are predicted to not amplify and those > 0.5 are predicted to amplify.
However, this threshold may be moved (or "optimized") to make certain types of errors less likely. This
is known as a type of cost-sensitive learning, which appreciates that the consequences of false negative
and false positive errors are often unequal. For an eDNA assay, false negatives (predicting a
template will not amplify when it does) are typically more "costly" than false positives (predicting a
template will amplify when it does not). The strength of false negatives relative to false
positives may change depending on the assay and application. For example, false negative costs may be
low for a target species that is unprotected and easy to detect using traditional
methods (e.g., 1X the false positive cost), but high for a target that is protected and highly cryptic
(e.g., 100X the false positive cost).'
),
em(
"Move the slider to see how false negative error tolerance influences
the optimal assignment threshold (left panel) and the number of taxa requiring in vitro specificity testing (right
panel; visible following analysis)."
)
),
br(),
sidebarPanel(
width = 12,
height = 12,
sliderInput2(
inputId = "costratio",
label = "How many times more impactful is a false negative than a false positive for your assay?",
br(),
min = 0,
max = 100,
value = 1,
step = 1,
from_min = 1,
to_max = 100
)
),
mainPanel(splitLayout(
cellWidths = c("75%", "75%"),
plotOutput(outputId = "opt"),
plotOutput(outputId = "hist")
))
),
### Information page
tabPanel(
"Learn More",
hr(),
h1("What this tool is for"),
p(
'Quantitative PCR (qPCR) assays applied to environmental samples need to discriminate among suites of
sequences that may very similar. To ensure assay specificity, environmental DNA (eDNA) practitioners
typically evaluate sequences from all closely related taxa (e.g., confamilials) within a pre-defined
geographic area. Any taxa that are not deemed "different enough" in computer-based in silico testing
must be put through time- and resource-intensive, laboratory-based in vitro testing. However, the
determination that an assay is "different enough" in silico is often dubious. Practitioners would
benefit greatly from more accurate and reliable in silico testing methods.'
),
p(
"To address this need, we developed eDNAssay \U2012 an online tool that uses supervised machine learning to
predict the probability of qPCR cross-amplification. Instead of relying on thermodynamic models and
simple mismatch heuristics (as do the vast majority of existing in silico tools) our model uses a random forest
algorithm to directly incorporate empirical data and achieves exceptional performance as a result. Assignment
probabilities can be evaluated at different false negative error tolerances to determine which nontarget taxa
require in vitro testing."
),
hr(),
h1("How to use this tool"),
p(
"Users provide three inputs on the Predict Amplification page: 1) a sequence alignment file, 2) a sequence
metadata file, and 3) estimated oligonucleotide melting temperatures. Input files have specific formatting
requirements."
),
p(
"Users receive three outputs: 1) a prediction file with assignment probabilities for assay
cross-amplification, 2) a data file displaying mismatch count information for off-app analysis \U2012 both
on the Predict Amplification page, and 3) a suggested optimal threshold based on a user-defined cost
ratio \U2012 on the Optimize Threshold page. The figure below indicates the relationship between user
inputs and tool outputs."
),
img(
src = "eDNAssay_workflow.jpeg",
height = 450,
width = 720,
),
p(
paste(
"eDNAssay was trained using qPCR results as detailed in Kronenberger et al. (2022). Using this tool to
predict specificity under different reaction conditions will likely produce less accurate results."
),
hr(),
h1("How to cite this tool"),
p(
paste(
"Kronenberger JA, Wilcox TM, Mason DH, Franklin TW, McKelvey KS, Young MK, and Schwartz MK (in press).
eDNAssay: a machine learning tool that accurately predicts qPCR cross-amplification."
),
em("Molecular Ecology Resources.")
),
hr(),
h1("Where to access scripts and previous model versions"),
p(
paste("See our"),
a(href = "https://github.com/NationalGenomicsCenter/eDNAssay", "GitHub repository", target = "_blank"),
"for the training data and code. This repository also contains a SYBR Green-based primer-only model."
),
hr(),
h1("Published model performance"),
p(
"The performance of machine learning classifiers is commonly assessed either through cross-validation and/or using a
seperate test dataset. Testing the model on new data helps ensure that high accuracy is not a result of overfitting. See these
metrics below, including confusion matrices and frequency distributions of class assignment probabilities, with incorrect
predictions in red."
),
p(
'Cross-validation accuracy was estimated using the results of 268 specificity tests, produced by pairing 10 qPCR assays
with synthetic gene fragments from 82 nontarget species. "Amplify"" and "nonamplify" classes were unbalanced, so new
instances of the minority class ("nonamplify") were created for 386 effective tests.'
),
br(),
img(
src = "Training_CM_AP.jpeg",
height = 350,
width = 700,
style = "align: center; padding-left: 0px; padding-right: 0px"
),
br(),
br(),
p(
"Model performance was also tested via an additional 144 specificity tests, produced by pairing 6 new qPCR assays
(not used in model training) with genomic DNA from 25 nontarget species. These templates were not sequenced.
Rather, for each species, we used the mean assigment probability for all sequences on GenBank. Because the actual
sequences tested may differ from those on GenBank, accuracy estimates may be biased."
),
br(),
img(
src = "Testing_CM_AP.jpeg",
height = 350,
width = 700,
style = "align: center; padding-left: 0px; padding-right: 0px"
),
hr(),
h1("Unpublished model performance"),
p("UPDATED JULY 2022"),
p(
"Additional tests of model accuracy have been generated since the publication of Kronenberger et al. (2022) during
routine specificity testing. These include 171 specificity tests, produced by pairing seven assays with 74 synthetic
gene fragments. We are only including templates with known sequences as they will be incorporated into the larger
training dataset for subsequent versions of eDNAssay. Some of these assays have a degenerate base and/or melting
temperatures outside the range of the current training data, indicating the model may be robust to novel assay parameters."
),
br(),
img(
src = "Training_2.0_CM_AP.jpeg",
height = 350,
width = 700,
style = "align: center; padding-left: 0px; padding-right: 0px"
),
hr(),
h1("Questions or comments?"),
p(
paste("Feel free to contact us at the"),
a(
href = "https://www.fs.usda.gov/rmrs/ngc",
"National Genomics Center for Wildlife and Fish Conservation",
target = "_blank"
),
"with any feedback."
),
p(
"eDNAssay was created by John Kronenberger at ",
a(
href = "[email protected]",
target = "_blank"
),
"and Taylor Wilcox at ",
a(href = "[email protected]",
target = "_blank"),
br()
)
)
)
)
)
##################################################################################################
### Define server logic
server <- function(input, output) {
### Reactive inputs
Assay <- eventReactive(input$goButton, {
input$Assay
})
F_Tm <- eventReactive(input$goButton, {
input$F_Tm
})
R_Tm <- eventReactive(input$goButton, {
input$R_Tm
})
P_Tm <- eventReactive(input$goButton, {
input$P_Tm
})
threshold <- reactive(input$threshold)
### Save matrix as a dataframe
input_seqs <-
eventReactive(input$goButton, {
as.matrix(readDNAStringSet(input$alignment$datapath))
})
out_matrix <- eventReactive(input$goButton, {
req(input$alignment)
input_matrix <- input_seqs()
input_matrix <-
as.data.frame(input_matrix, stringsAsFactors = FALSE)
### Create metadata columns
input_matrix2 <- input_matrix
input_matrix2$Name <- row.names(input_matrix)
input_matrix2$Taxon <- input_matrix2$Name
input_matrix2$Taxon <- gsub("\\.", "", input_matrix2$Taxon)
input_matrix2$Taxon <- gsub("\\+", " ", input_matrix2$Taxon)
input_matrix2$Taxon <-
gsub("NC ", "NC", input_matrix2$Taxon)
input_matrix2$Taxon <-
gsub("UNVERIFIED: ", "", input_matrix2$Taxon)
input_matrix2$Taxon <-
trimws(gsub("\\w*[0-9]+\\w*\\s*", "", input_matrix2$Taxon))
input_matrix2$Taxon <-
word(
input_matrix2$Taxon,
start = 1,
end = 2,
sep = fixed(" ")
)
input_matrix2$Taxon[1:3] <- rep("Target", 3)
input_matrix2$Type <- "Template"
input_matrix2$Type[1:3] <- "Oligo"
input_matrix <-
select(input_matrix2, Taxon, Name, Type, everything())
### Update oligo names
input_matrix[1, 2] = "F"
input_matrix[2, 2] = "R"
input_matrix[3, 2] = "P"
### Label sequence names that could not be formatted as "unspecified"
input_matrix_names <- input_matrix[, 1:3]
input_matrix_names[is.na(input_matrix_names)] <-
"Unspecified"
input_matrix_seqs <- input_matrix[, 4:ncol(input_matrix)]
input_matrix <- cbind(input_matrix_names, input_matrix_seqs)
### Convert "NA" in template sequences to "N" to accommodate indels
input_matrix <- replace(input_matrix, input_matrix == "-", NA)
input_matrix_oligos <- input_matrix[1:3,]
input_matrix_templates <-
input_matrix[4:nrow(input_matrix),]
input_matrix_templates[is.na(input_matrix_templates)] <- "N"
input_matrix <-
rbind(input_matrix_oligos, input_matrix_templates)
### Define length variables and relabel rows
length_type <- length(input_matrix$Type)
length_oligo <- length(which(input_matrix$Type == "Oligo"))
length_template <-
length(which(input_matrix$Type == "Template"))
row.names(input_matrix) <- c(1:length_type)
### Create separate dataframes for each oligo and remove nucleotides outside oligo binding sites
oligo_matrices <-
lapply(1:length_oligo, function(x)
input_matrix[input_matrix$Type == "Template",-which(is.na(input_matrix[x, ]))])
names(oligo_matrices) = input_matrix$Name[1:length_oligo]
### Match oligo and template sequences (accounting for IUPAC ambiguity codes) and count the number
### of mismatches (=TRUE) in different positions; total, the last five bp on 3' and 5' ends,
### and the last bp on the 3' end (terminus)
ot_match <-
list() # Returns list of dataframes with sequences of oligo and templates at binding sites
mm_list <-
list() # Returns a list of lists with mismatches (accounting for IUPAC ambiguities)
mm_unlist <-
list() # Converts mm_list to list of dataframes and finds mismatches in different positions
for (i in 1:length_oligo) {
ot_match[[i]] <-
rbind(input_matrix[i, -which(is.na(input_matrix[i, ]))],
oligo_matrices[[which(names(oligo_matrices) ==
input_matrix[i, 2])]])
mm_list[[i]] <-
lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[i]][1, -(1:3)] == ot_match[[i]][x, -(1:3)] |
ot_match[[i]][1, -(1:3)] ==
"R" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"R" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"Y" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"Y" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"M" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"M" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"K" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"K" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"S" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"S" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"W" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"W" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"B" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"B" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"B" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"D" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"D" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"D" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"H" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"H" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"H" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"V" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"V" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"V" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"N" & ot_match[[i]][x, -(1:3)] == "A" |
ot_match[[i]][1, -(1:3)] ==
"N" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] ==
"N" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] ==
"N" & ot_match[[i]][x, -(1:3)] == "T" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "R" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "R" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "Y" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "Y" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "M" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "M" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "K" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "K" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "S" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "S" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "W" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "W" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "B" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "B" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "B" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "D" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "D" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "D" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "H" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "H" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "H" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "V" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "V" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "V" |
ot_match[[i]][1, -(1:3)] ==
"A" & ot_match[[i]][x, -(1:3)] == "N" |
ot_match[[i]][1, -(1:3)] ==
"C" & ot_match[[i]][x, -(1:3)] == "N" |
ot_match[[i]][1, -(1:3)] ==
"G" & ot_match[[i]][x, -(1:3)] == "N" |
ot_match[[i]][1, -(1:3)] ==
"T" & ot_match[[i]][x, -(1:3)] == "N",
TRUE,
FALSE
))
mm_unlist[[i]] <-
data.frame(matrix(
unlist(mm_list[[i]]),
nrow = length_template,
byrow = TRUE
))
mm_unlist[c(FALSE, TRUE, FALSE)] = lapply(mm_unlist[c(FALSE, TRUE, FALSE)], function(x)
rev(x))
mm_unlist[[i]]$mm_term <-
as.numeric(mm_unlist[[i]][, ncol(mm_unlist[[i]])] == "FALSE")
mm_unlist[[i]]$mm_total <-
rowSums(mm_unlist[[i]][, -ncol(mm_unlist[[i]])] == "FALSE", na.rm = TRUE)
mm_unlist[[i]]$mm_end3p <-
rowSums(mm_unlist[[i]][, ((ncol(mm_unlist[[i]]) - 2 - 4):(ncol(mm_unlist[[i]]) -
2))] == "FALSE")
mm_unlist[[i]]$mm_end5p <-
rowSums(mm_unlist[[i]][, 1:5] == "FALSE")
}
names(mm_unlist) = input_matrix$Name[1:length_oligo]
### Create a dataframe for total mismatch number and position
mm_final <-
as.data.frame(
cbind(
rep(sub(" .*", "", names(mm_unlist)), each = length_template),
rep(input_matrix[1:length_oligo, 2], each =
length_template),
rep(input_matrix[(length_oligo + 1):length_type, 1], length_oligo),
rep(input_matrix[(length_oligo + 1):length_type, 2], length_oligo),
rep(input_matrix[(length_oligo + 1):length_type, 3], length_oligo),
as.numeric(sapply(mm_unlist, `[[`, "mm_total")),
as.numeric(sapply(mm_unlist, `[[`, "mm_end5p")),
as.numeric(sapply(mm_unlist, `[[`, "mm_end3p")),
as.numeric(sapply(mm_unlist, `[[`, "mm_term"))
)
)
colnames(mm_final) <-
c(
"Assay",
"Oligo",
"Taxon",
"Name",
"Type",
"Total_mm",
"End5p_mm",
"End3p_mm",
"Term_mm"
)
mm_final$Total_mm <-
as.numeric(as.character(mm_final$Total_mm))
mm_final$End5p_mm <-
as.numeric(as.character(mm_final$End5p_mm))
mm_final$End3p_mm <-
as.numeric(as.character(mm_final$End3p_mm))
mm_final$Term_mm <-
as.numeric(as.character(mm_final$Term_mm))
### Repeat the above loop for all mismatch types (AA, AG/GA, AC/CA, TT, TG/GT, TC/CT, CC, and GG);
### loop i used for forward primers (seq(1,length_oligo,2)), j for reverse primers (i+1), and k
### for probes (j+1)
aa_list <-
list() # Returns a list of lists with AA mismatches
aa_unlist <-
list() # Converts aa_list to dataframes and counts AA mismatches in different positions
for (i in seq(1, length_oligo, 3)) {
for (j in (i + 1)) {
for (k in (j + 1)) {
aa_list[[i]] <- lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[i]][1, -(1:3)] == "A" &
ot_match[[i]][x, -(1:3)] == "T",
TRUE,
FALSE
))
}
aa_list[[j]] <-
lapply(2:(length_template + 1), function(x)
ifelse(ot_match[[j]][1, -(1:3)] == "T" &
ot_match[[j]][x, -(1:3)] == "A", TRUE, FALSE))
}
aa_list[[k]] <-
lapply(2:(length_template + 1), function(x)
ifelse(ot_match[[k]][1, -(1:3)] == "A" &
ot_match[[k]][x, -(1:3)] == "T", TRUE, FALSE))
aa_unlist[[i]] <-
data.frame(matrix(
unlist(aa_list[[i]]),
nrow = length_template,
byrow = TRUE
))
aa_unlist[[j]] <-
rev(data.frame(
matrix(
unlist(aa_list[[j]]),
nrow = length_template,
byrow = TRUE
)
))
aa_unlist[[k]] <-
data.frame(matrix(
unlist(aa_list[[k]]),
nrow = length_template,
byrow = TRUE
))
aa_unlist[[i]]$aa_term <-
as.numeric(aa_unlist[[i]][, ncol(aa_unlist[[i]])] == "TRUE")
aa_unlist[[j]]$aa_term <-
as.numeric(aa_unlist[[j]][, ncol(aa_unlist[[j]])] == "TRUE")
aa_unlist[[k]]$aa_term <-
as.numeric(aa_unlist[[k]][, ncol(aa_unlist[[k]])] == "TRUE")
aa_unlist[[i]]$aa_total <-
rowSums(aa_unlist[[i]][, -ncol(aa_unlist[[i]])] == "TRUE", na.rm = TRUE)
aa_unlist[[j]]$aa_total <-
rowSums(aa_unlist[[j]][, -ncol(aa_unlist[[j]])] == "TRUE", na.rm = TRUE)
aa_unlist[[k]]$aa_total <-
rowSums(aa_unlist[[k]][, -ncol(aa_unlist[[k]])] == "TRUE", na.rm = TRUE)
aa_unlist[[i]]$aa_end3p <-
rowSums(aa_unlist[[i]][, ((ncol(aa_unlist[[i]]) - 2 - 4):(ncol(aa_unlist[[i]]) -
2))] == "TRUE")
aa_unlist[[j]]$aa_end3p <-
rowSums(aa_unlist[[j]][, ((ncol(aa_unlist[[j]]) - 2 - 4):(ncol(aa_unlist[[j]]) -
2))] == "TRUE")
aa_unlist[[k]]$aa_end3p <-
rowSums(aa_unlist[[k]][, ((ncol(aa_unlist[[k]]) - 2 - 4):(ncol(aa_unlist[[k]]) -
2))] == "TRUE")
aa_unlist[[i]]$aa_end5p <-
rowSums(aa_unlist[[i]][, 1:5] == "TRUE")
aa_unlist[[j]]$aa_end5p <-
rowSums(aa_unlist[[j]][, 1:5] == "TRUE")
aa_unlist[[k]]$aa_end5p <-
rowSums(aa_unlist[[k]][, 1:5] == "TRUE")
}
names(aa_unlist) = input_matrix$Name[1:length_oligo]
#-------------------------------------
ag_list <-
list() # Returns a list of lists with AG and GA mismatches
ag_unlist <-
list() # Converts ag_list to dataframes and counts AG and GA mismatches in different positions
for (i in seq(1, length_oligo, 3)) {
for (j in (i + 1)) {
for (k in (j + 1)) {
ag_list[[i]] <- lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[i]][1, -(1:3)] == "A" & ot_match[[i]][x, -(1:3)] == "C" |
ot_match[[i]][1, -(1:3)] == "G" &
ot_match[[i]][x, -(1:3)] == "T",
TRUE,
FALSE
))
}
ag_list[[j]] <-
lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[j]][1, -(1:3)] == "T" & ot_match[[j]][x, -(1:3)] == "G" |
ot_match[[j]][1, -(1:3)] == "C" &
ot_match[[j]][x, -(1:3)] == "A",
TRUE,
FALSE
))
}
ag_list[[k]] <-
lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[k]][1, -(1:3)] == "A" & ot_match[[k]][x, -(1:3)] == "C" |
ot_match[[k]][1, -(1:3)] == "G" &
ot_match[[k]][x, -(1:3)] == "T",
TRUE,
FALSE
))
ag_unlist[[i]] <-
data.frame(matrix(
unlist(ag_list[[i]]),
nrow = length_template,
byrow = TRUE
))
ag_unlist[[j]] <-
rev(data.frame(
matrix(
unlist(ag_list[[j]]),
nrow = length_template,
byrow = TRUE
)
))
ag_unlist[[k]] <-
data.frame(matrix(
unlist(ag_list[[k]]),
nrow = length_template,
byrow = TRUE
))
ag_unlist[[i]]$ag_term <-
as.numeric(ag_unlist[[i]][, ncol(ag_unlist[[i]])] == "TRUE")
ag_unlist[[j]]$ag_term <-
as.numeric(ag_unlist[[j]][, ncol(ag_unlist[[j]])] == "TRUE")
ag_unlist[[k]]$ag_term <-
as.numeric(ag_unlist[[k]][, ncol(ag_unlist[[k]])] == "TRUE")
ag_unlist[[i]]$ag_total <-
rowSums(ag_unlist[[i]][, -ncol(ag_unlist[[i]])] == "TRUE", na.rm = TRUE)
ag_unlist[[j]]$ag_total <-
rowSums(ag_unlist[[j]][, -ncol(ag_unlist[[j]])] == "TRUE", na.rm = TRUE)
ag_unlist[[k]]$ag_total <-
rowSums(ag_unlist[[k]][, -ncol(ag_unlist[[k]])] == "TRUE", na.rm = TRUE)
ag_unlist[[i]]$ag_end3p <-
rowSums(ag_unlist[[i]][, ((ncol(ag_unlist[[i]]) - 2 - 4):(ncol(ag_unlist[[i]]) -
2))] == "TRUE")
ag_unlist[[j]]$ag_end3p <-
rowSums(ag_unlist[[j]][, ((ncol(ag_unlist[[j]]) - 2 - 4):(ncol(ag_unlist[[j]]) -
2))] == "TRUE")
ag_unlist[[k]]$ag_end3p <-
rowSums(ag_unlist[[k]][, ((ncol(ag_unlist[[k]]) - 2 - 4):(ncol(ag_unlist[[k]]) -
2))] == "TRUE")
ag_unlist[[i]]$ag_end5p <-
rowSums(ag_unlist[[i]][, 1:5] == "TRUE")
ag_unlist[[j]]$ag_end5p <-
rowSums(ag_unlist[[j]][, 1:5] == "TRUE")
ag_unlist[[k]]$ag_end5p <-
rowSums(ag_unlist[[k]][, 1:5] == "TRUE")
}
names(ag_unlist) = input_matrix$Name[1:length_oligo]
#-------------------------------------
ac_list <-
list() # Returns a list of lists with AC and CA mismatches
ac_unlist <-
list() # Converts ac_list to dataframes and counts AC and CA mismatches in different positions
for (i in seq(1, length_oligo, 3)) {
for (j in (i + 1)) {
for (k in (j + 1)) {
ac_list[[i]] <- lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[i]][1, -(1:3)] == "A" & ot_match[[i]][x, -(1:3)] == "G" |
ot_match[[i]][1, -(1:3)] == "C" &
ot_match[[i]][x, -(1:3)] == "T",
TRUE,
FALSE
))
}
ac_list[[j]] <-
lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[j]][1, -(1:3)] == "T" & ot_match[[j]][x, -(1:3)] == "C" |
ot_match[[j]][1, -(1:3)] == "G" &
ot_match[[j]][x, -(1:3)] == "A",
TRUE,
FALSE
))
}
ac_list[[k]] <-
lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[k]][1, -(1:3)] == "A" & ot_match[[k]][x, -(1:3)] == "G" |
ot_match[[k]][1, -(1:3)] == "C" &
ot_match[[k]][x, -(1:3)] == "T",
TRUE,
FALSE
))
ac_unlist[[i]] <-
data.frame(matrix(
unlist(ac_list[[i]]),
nrow = length_template,
byrow = TRUE
))
ac_unlist[[j]] <-
rev(data.frame(
matrix(
unlist(ac_list[[j]]),
nrow = length_template,
byrow = TRUE
)
))
ac_unlist[[k]] <-
data.frame(matrix(
unlist(ac_list[[k]]),
nrow = length_template,
byrow = TRUE
))
ac_unlist[[i]]$ac_term <-
as.numeric(ac_unlist[[i]][, ncol(ac_unlist[[i]])] == "TRUE")
ac_unlist[[j]]$ac_term <-
as.numeric(ac_unlist[[j]][, ncol(ac_unlist[[j]])] == "TRUE")
ac_unlist[[k]]$ac_term <-
as.numeric(ac_unlist[[k]][, ncol(ac_unlist[[k]])] == "TRUE")
ac_unlist[[i]]$ac_total <-
rowSums(ac_unlist[[i]][, -ncol(ac_unlist[[i]])] == "TRUE", na.rm = TRUE)
ac_unlist[[j]]$ac_total <-
rowSums(ac_unlist[[j]][, -ncol(ac_unlist[[j]])] == "TRUE", na.rm = TRUE)
ac_unlist[[k]]$ac_total <-
rowSums(ac_unlist[[k]][, -ncol(ac_unlist[[k]])] == "TRUE", na.rm = TRUE)
ac_unlist[[i]]$ac_end3p <-
rowSums(ac_unlist[[i]][, ((ncol(ac_unlist[[i]]) - 2 - 4):(ncol(ac_unlist[[i]]) -
2))] == "TRUE")
ac_unlist[[j]]$ac_end3p <-
rowSums(ac_unlist[[j]][, ((ncol(ac_unlist[[j]]) - 2 - 4):(ncol(ac_unlist[[j]]) -
2))] == "TRUE")
ac_unlist[[k]]$ac_end3p <-
rowSums(ac_unlist[[k]][, ((ncol(ac_unlist[[k]]) - 2 - 4):(ncol(ac_unlist[[k]]) -
2))] == "TRUE")
ac_unlist[[i]]$ac_end5p <-
rowSums(ac_unlist[[i]][, 1:5] == "TRUE")
ac_unlist[[j]]$ac_end5p <-
rowSums(ac_unlist[[j]][, 1:5] == "TRUE")
ac_unlist[[k]]$ac_end5p <-
rowSums(ac_unlist[[k]][, 1:5] == "TRUE")
}
names(ac_unlist) = input_matrix$Name[1:length_oligo]
#-------------------------------------
tt_list <-
list() # Returns a list of lists with TT mismatches
tt_unlist <-
list() # Converts tt_list to dataframes and counts TT mismatches in different positions
for (i in seq(1, length_oligo, 3)) {
for (j in (i + 1)) {
for (k in (j + 1)) {
tt_list[[i]] <- lapply(2:(length_template + 1), function(x)
ifelse(
ot_match[[i]][1, -(1:3)] == "T" &
ot_match[[i]][x, -(1:3)] == "A",
TRUE,
FALSE
))
}
tt_list[[j]] <-
lapply(2:(length_template + 1), function(x)
ifelse(ot_match[[j]][1, -(1:3)] == "A" &
ot_match[[j]][x, -(1:3)] == "T", TRUE, FALSE))
}
tt_list[[k]] <-
lapply(2:(length_template + 1), function(x)
ifelse(ot_match[[k]][1, -(1:3)] == "T" &
ot_match[[k]][x, -(1:3)] == "A", TRUE, FALSE))
tt_unlist[[i]] <-
data.frame(matrix(
unlist(tt_list[[i]]),
nrow = length_template,
byrow = TRUE
))
tt_unlist[[j]] <-
rev(data.frame(
matrix(
unlist(tt_list[[j]]),
nrow = length_template,
byrow = TRUE
)
))
tt_unlist[[k]] <-
data.frame(matrix(
unlist(tt_list[[k]]),
nrow = length_template,
byrow = TRUE
))
tt_unlist[[i]]$tt_term <-
as.numeric(tt_unlist[[i]][, ncol(tt_unlist[[i]])] == "TRUE")
tt_unlist[[j]]$tt_term <-
as.numeric(tt_unlist[[j]][, ncol(tt_unlist[[j]])] == "TRUE")
tt_unlist[[k]]$tt_term <-
as.numeric(tt_unlist[[k]][, ncol(tt_unlist[[k]])] == "TRUE")
tt_unlist[[i]]$tt_total <-
rowSums(tt_unlist[[i]][, -ncol(tt_unlist[[i]])] == "TRUE", na.rm = TRUE)
tt_unlist[[j]]$tt_total <-
rowSums(tt_unlist[[j]][, -ncol(tt_unlist[[j]])] == "TRUE", na.rm = TRUE)
tt_unlist[[k]]$tt_total <-
rowSums(tt_unlist[[k]][, -ncol(tt_unlist[[k]])] == "TRUE", na.rm = TRUE)
tt_unlist[[i]]$tt_end3p <-
rowSums(tt_unlist[[i]][, ((ncol(tt_unlist[[i]]) - 2 - 4):(ncol(tt_unlist[[i]]) -
2))] == "TRUE")
tt_unlist[[j]]$tt_end3p <-
rowSums(tt_unlist[[j]][, ((ncol(tt_unlist[[j]]) - 2 - 4):(ncol(tt_unlist[[j]]) -
2))] == "TRUE")
tt_unlist[[k]]$tt_end3p <-
rowSums(tt_unlist[[k]][, ((ncol(tt_unlist[[k]]) - 2 - 4):(ncol(tt_unlist[[k]]) -
2))] == "TRUE")
tt_unlist[[i]]$tt_end5p <-
rowSums(tt_unlist[[i]][, 1:5] == "TRUE")
tt_unlist[[j]]$tt_end5p <-
rowSums(tt_unlist[[j]][, 1:5] == "TRUE")
tt_unlist[[k]]$tt_end5p <-
rowSums(tt_unlist[[k]][, 1:5] == "TRUE")
}
names(tt_unlist) = input_matrix$Name[1:length_oligo]
#-------------------------------------
tg_list <-
list() # Returns a list of lists with TG and GT mismatches
tg_unlist <-