@ -1,25 +1,26 @@
##########################################################
# #
# This app has been written by Marcel Costa-García, #
# and has reused some internal functions from sangerseqR #
# package, including getPeaks and peakvalues #
# #
##########################################################
############################################################
# #
# This app has been written by Marcel Costa-García, #
# and has reused some internal functions from sangerseqR #
# package, including getPeaks and peakvalues #
# #
############################################################
# Load of dependencies
library ( shiny )
library ( shiny )
library ( tidyverse )
library ( tidyverse )
library ( sangerseqR )
library ( sangerseqR )
library ( msa )
library ( msa )
# Define UI for application that draws a histogram
ui <- fluidPage (
# Define UI ---------------------------------------------------------------
ui <- fluidPage (
# Application title
# Application title
titlePanel ( " ChromatoR" ) ,
titlePanel ( " ChromatoR" ) ,
# Sidebar with a slider input for number of bins
navbarPage ( " ChromatoR" ,
navbarPage ( " Apps" ,
tabPanel ( " Detección de Bases" ,
tabPanel ( " Detección de Bases" ,
sidebarPanel (
sidebarPanel (
fileInput ( " file1" , " Sube fichero ab1" , multiple = FALSE ) ,
fileInput ( " file1" , " Sube fichero ab1" , multiple = FALSE ) ,
@ -31,14 +32,13 @@ ui <- fluidPage(
textInput ( " old" , label = " Referencia" ) ,
textInput ( " old" , label = " Referencia" ) ,
actionButton ( " calab" , " Analizar" )
actionButton ( " calab" , " Analizar" )
) ,
) ,
# Show a plot of the generated distribution
mainPanel (
mainPanel (
tags $ head ( tags $ style ( HTML ( " pre,.wrap { white-space: pre-wrap; word-break: break-all; }" ) ) ) ,
tags $ head ( tags $ style ( HTML ( " pre,.wrap { white-space: pre-wrap; word-break: break-all; }" ) ) ) ,
verbatimTextOutput ( " Primseq" ) %>% tagAppendAttributes ( class = " wrap" ) ,
verbatimTextOutput ( " Primseq" ) %>% tagAppendAttributes ( class = " wrap" ) ,
verbatimTextOutput ( " align" )
verbatimTextOutput ( " align" )
)
)
) ,
) ,
tabPanel ( " Visor de Cromatograma" ,
tabPanel ( " Visor de Cromatograma" ,
sidebarPanel (
sidebarPanel (
numericInput ( " visStart" , label = " Inicio" , value = 0 ) ,
numericInput ( " visStart" , label = " Inicio" , value = 0 ) ,
@ -55,29 +55,28 @@ ui <- fluidPage(
)
)
)
)
# Define server logic required to draw a histogram
# Define server -----------------------------------------------------------
server <- function ( input , output ) {
server <- function ( input , output ) {
# observe({
#
# obj<<-readsangerseq(input$file1)
# }
# })
# Define and initialize reactive variables
obj <- reactiveValues ( )
obj <- reactiveValues ( )
obj $ aborig <- NULL
obj $ aborig <- NULL
obj_ab <- NULL
obj_ab <- NULL
obj $ seq <- NULL
obj $ seq <- NULL
# File Input
observe ( {
observe ( {
if ( ! is.null ( input $ file1 ) ) {
if ( ! is.null ( input $ file1 ) ) { # This ensures that the reading is only tried when File selected
obj $ aborig <- readsangerseq ( input $ file1 $ datapath )
obj $ aborig <- readsangerseq ( input $ file1 $ datapath )
}
}
} )
} )
# This generates the threshold selector calculating the median of the peaks
output $ thr <- renderUI ( {
output $ thr <- renderUI ( {
if ( ! is.null ( obj $ aborig ) ) {
print ( 1 )
if ( ! is.null ( obj $ aborig ) ) { # Only when we have a sangerseq object
obj_ab <- obj $ aborig
obj_ab <- obj $ aborig
## Functions
getpeaks <- function ( trace ) {
getpeaks <- function ( trace ) {
r <- rle ( trace )
r <- rle ( trace )
indexes <- which ( rep ( diff ( sign ( diff ( c ( - Inf , r $ values , - Inf ) ) ) ) == -2 ,
indexes <- which ( rep ( diff ( sign ( diff ( c ( - Inf , r $ values , - Inf ) ) ) ) == -2 ,
@ -94,15 +93,19 @@ server <- function(input, output) {
Apeaks ,
Apeaks ,
Tpeaks ,
Tpeaks ,
Cpeaks )
Cpeaks )
thr_calc <- quantile ( peakCusMatrix [ , 2 ] , 0.5 )
thr_calc <- quantile ( peakCusMatrix [ , 2 ] , 0.5 ) # Percentile 50
numericInput ( " thr" , label = " Peak Threshold" , value = thr_calc )
numericInput ( " thr" , label = " Peak Threshold" , value = thr_calc )
}
}
} )
} )
# Peak detection and Base asignation
observeEvent ( input $ calab , {
observeEvent ( input $ calab , {
if ( ! is.null ( obj $ aborig ) ) {
if ( ! is.null ( obj $ aborig ) ) {
obj_ab <- obj $ aborig
## Functions
obj_ab <- obj $ aborig # We will work in a local variable
## Function definition
getpeaks <- function ( trace ) {
getpeaks <- function ( trace ) {
r <- rle ( trace )
r <- rle ( trace )
indexes <- which ( rep ( diff ( sign ( diff ( c ( - Inf , r $ values , - Inf ) ) ) ) == -2 ,
indexes <- which ( rep ( diff ( sign ( diff ( c ( - Inf , r $ values , - Inf ) ) ) ) == -2 ,
@ -131,34 +134,40 @@ server <- function(input, output) {
}
}
return ( pos )
return ( pos )
}
}
# Progress notification
progress <- shiny :: Progress $ new ( min = 0 , max = 3 )
progress <- shiny :: Progress $ new ( min = 0 , max = 3 )
progress $ set ( message = " Start" , value = 1 )
progress $ set ( message = " Start" , value = 1 )
print ( " Start" )
print ( " Start" )
# Peak detection for each channel
Apeaks <- getpeaks ( obj_ab @ traceMatrix [ , 1 ] )
Apeaks <- getpeaks ( obj_ab @ traceMatrix [ , 1 ] )
Cpeaks <- getpeaks ( obj_ab @ traceMatrix [ , 2 ] )
Cpeaks <- getpeaks ( obj_ab @ traceMatrix [ , 2 ] )
Gpeaks <- getpeaks ( obj_ab @ traceMatrix [ , 3 ] )
Gpeaks <- getpeaks ( obj_ab @ traceMatrix [ , 3 ] )
Tpeaks <- getpeaks ( obj_ab @ traceMatrix [ , 4 ] )
Tpeaks <- getpeaks ( obj_ab @ traceMatrix [ , 4 ] )
# Peaks joining and filtering by defined threshold
peakthr <- input $ thr
peakthr <- input $ thr
peakCusMatrix <- rbind ( Gpeaks [Gpeaks [ , 2 ] > peakthr , ] ,
peakCusMatrix <- rbind ( Gpeaks [Gpeaks [ , 2 ] > peakthr , ] ,
Apeaks [Apeaks [ , 2 ] > peakthr , ] ,
Apeaks [Apeaks [ , 2 ] > peakthr , ] ,
Tpeaks [Tpeaks [ , 2 ] > peakthr , ] ,
Tpeaks [Tpeaks [ , 2 ] > peakthr , ] ,
Cpeaks [Cpeaks [ , 2 ] > peakthr , ] )
Cpeaks [Cpeaks [ , 2 ] > peakthr , ] )
peakCusMatrix <- peakCusMatrix %>% as.data.frame ( ) %>% dplyr :: rename ( Int = V2 ) %>% group_by ( indexes ) %>% summarise ( Int = max ( Int ) [1 ] )
# In case there is more than one peak by position, we keep the higher
peakCusMatrix <- peakCusMatrix %>% as.data.frame ( ) %>%
dplyr :: rename ( Int = V2 ) %>% group_by ( indexes ) %>% summarise ( Int = max ( Int ) [1 ] )
peakCusMatrix <- as.matrix ( peakCusMatrix )
peakCusMatrix <- as.matrix ( peakCusMatrix )
# Using the custom function, we aggregate peaks defining two distance thresholds
pos1 <- redDiffs ( peakCusMatrix , input $ dist1 )
pos1 <- redDiffs ( peakCusMatrix , input $ dist1 )
pos <- redDiffs ( pos1 , input $ dist2 )
print ( nrow ( pos ) )
pos1 <- pos1 [ , 1 ]
pos <- pos [ , 1 ]
pos <- redDiffs ( pos1 , input $ dist2 ) [ , 1 ]
progress $ set ( message = " Peaks Detected" , value = 2 )
progress $ set ( message = " Peaks Detected" , value = 2 )
print ( " Peaks Detected" )
print ( " Peaks Detected" )
# This is taken from sangerseqR
# Defining starts and stops of peak windows
primarypeaks <- pos
primarypeaks <- pos
diffs <- diff ( c ( 0 , primarypeaks ) )
diffs <- diff ( c ( 0 , primarypeaks ) )
starts <- primarypeaks - 0.5 * diffs
starts <- primarypeaks - 0.5 * diffs
@ -172,11 +181,12 @@ server <- function(input, output) {
tempPosMatrix <- matrix ( nrow = length ( starts ) , ncol = 4 )
tempPosMatrix <- matrix ( nrow = length ( starts ) , ncol = 4 )
tempAmpMatrix <- matrix ( nrow = length ( starts ) , ncol = 4 )
tempAmpMatrix <- matrix ( nrow = length ( starts ) , ncol = 4 )
# Ratio min to include a second peak as secondary sequence
ratio <- input $ ratio
ratio <- input $ ratio
length ( starts )
# Base detection for each peak
for ( i in 1 : length ( starts ) ) {
for ( i in 1 : length ( starts ) ) {
# Detection of peak position and signal for each peak window
Apeak <- peakvalues ( Apeaks , starts [i ] , stops [i ] )
Apeak <- peakvalues ( Apeaks , starts [i ] , stops [i ] )
Cpeak <- peakvalues ( Cpeaks , starts [i ] , stops [i ] )
Cpeak <- peakvalues ( Cpeaks , starts [i ] , stops [i ] )
Gpeak <- peakvalues ( Gpeaks , starts [i ] , stops [i ] )
Gpeak <- peakvalues ( Gpeaks , starts [i ] , stops [i ] )
@ -185,42 +195,59 @@ server <- function(input, output) {
is.na ( Cpeak [2 ] ) &
is.na ( Cpeak [2 ] ) &
is.na ( Gpeak [2 ] ) &
is.na ( Gpeak [2 ] ) &
is.na ( Tpeak [2 ] ) ) next #rare case where no peak found
is.na ( Tpeak [2 ] ) ) next #rare case where no peak found
# Signal of each channel join
signals <- c ( Apeak [1 ] , Cpeak [1 ] , Gpeak [1 ] , Tpeak [1 ] )
signals <- c ( Apeak [1 ] , Cpeak [1 ] , Gpeak [1 ] , Tpeak [1 ] )
tempAmpMatrix [i , ] <- signals
tempAmpMatrix [i , ] <- signals
# Peak position of each channel join
positions <- c ( Apeak [2 ] , Cpeak [2 ] , Gpeak [2 ] , Tpeak [2 ] )
positions <- c ( Apeak [2 ] , Cpeak [2 ] , Gpeak [2 ] , Tpeak [2 ] )
tempPosMatrix [i , ] <- positions
tempPosMatrix [i , ] <- positions
# Ratio to the maximum by channel
signalratios <- signals / max ( signals , na.rm = TRUE )
signalratios <- signals / max ( signals , na.rm = TRUE )
# Channel order definition
Bases <- c ( " A" , " C" , " G" , " T" )
Bases <- c ( " A" , " C" , " G" , " T" )
# Bases <- c("G", "A", "T", "G")
# Discarding bases (same order as channel) that doesn't reach the minimum ratio to max
Bases [signalratios < ratio ] <- NA
Bases [signalratios < ratio ] <- NA
#sort by decreasing signal strength
# Sort by decreasing signal strength
Bases <- Bases [order ( signals , decreasing = TRUE ) ]
Bases <- Bases [order ( signals , decreasing = TRUE ) ]
# Definition of primary base and secondary base
positions <- positions [order ( signals , decreasing = TRUE ) ]
positions <- positions [order ( signals , decreasing = TRUE ) ]
if ( length ( Bases [ ! is.na ( Bases ) ] ) == 4
if ( length ( Bases [ ! is.na ( Bases ) ] ) == 4 #if there are 4 bases or none, "N" assignation
| length ( Bases [ ! is.na ( Bases ) ] ) == 0 ) {
| length ( Bases [ ! is.na ( Bases ) ] ) == 0 ) {
primary <- c ( primary , " N" )
primary <- c ( primary , " N" )
secondary <- c ( secondary , " N" )
secondary <- c ( secondary , " N" )
}
}
else if ( length ( Bases [ ! is.na ( Bases ) ] ) > 1 ) {
else if ( length ( Bases [ ! is.na ( Bases ) ] ) > 1 ) { #if there is more than 1 base
primary <- c ( primary , Bases [1 ] )
primary <- c ( primary , Bases [1 ] )
Bases2 <- Bases [2 : 4 ]
Bases2 <- Bases [2 : 4 ]
secondary <- c ( secondary ,
secondary <- c ( secondary ,
mergeIUPACLetters ( paste ( sort ( Bases2 [ ! is.na ( Bases2 ) ] ) ,
mergeIUPACLetters ( paste ( sort ( Bases2 [ ! is.na ( Bases2 ) ] ) ,
collapse = " " ) ) )
collapse = " " ) ) )
}
}
else {
else { #if there is only one base, primary and secondary are the same
primary <- c ( primary , Bases [1 ] )
primary <- c ( primary , Bases [1 ] )
secondary <- c ( secondary , Bases [1 ] )
secondary <- c ( secondary , Bases [1 ] )
}
}
}
}
# Redefining sangerseq object with the new sequence and matrices
obj_ab @ peakPosMatrix <- tempPosMatrix [rowSums ( ! is.na ( tempPosMatrix ) ) > 0 , ]
obj_ab @ peakPosMatrix <- tempPosMatrix [rowSums ( ! is.na ( tempPosMatrix ) ) > 0 , ]
obj_ab @ peakAmpMatrix <- tempAmpMatrix [rowSums ( ! is.na ( tempPosMatrix ) ) > 0 , ]
obj_ab @ peakAmpMatrix <- tempAmpMatrix [rowSums ( ! is.na ( tempPosMatrix ) ) > 0 , ]
obj_ab @ primarySeqID <- " sangerseq package primary basecalls"
obj_ab @ primarySeqID <- " sangerseq package primary basecalls"
obj_ab @ primarySeq <- DNAString ( paste ( primary , collapse = " " ) )
obj_ab @ primarySeq <- DNAString ( paste ( primary , collapse = " " ) )
obj_ab @ secondarySeqID <- " sangerseq package secondary basecalls"
obj_ab @ secondarySeqID <- " sangerseq package secondary basecalls"
obj_ab @ secondarySeq <- DNAString ( paste ( secondary , collapse = " " ) )
obj_ab @ secondarySeq <- DNAString ( paste ( secondary , collapse = " " ) )
# We convert obj_ab into a global variable that other functions will be able to use
obj_ab <<- obj_ab
obj_ab <<- obj_ab
# This is a triggering for other observing events
if ( is.null ( obj $ seq ) ) {
if ( is.null ( obj $ seq ) ) {
obj $ seq <- 1
obj $ seq <- 1
} else {
} else {
@ -234,25 +261,26 @@ server <- function(input, output) {
}
}
} )
} )
# Predicted sequence printing
output $ Primseq <- renderText ( {
output $ Primseq <- renderText ( {
observeEvent ( obj $ seq , { } )
observeEvent ( obj $ seq , { } ) #This is triggered when the sequence is predicted
if ( ! is.null ( obj $ seq ) ) {
if ( ! is.null ( obj $ seq ) ) {
print ( 1 )
as.character ( obj_ab @ primarySeq )
as.character ( obj_ab @ primarySeq )
}
}
} )
} )
# Alignament with a reference sequence
output $ align <- renderText ( {
output $ align <- renderText ( {
observeEvent ( obj $ seq , { } )
observeEvent ( obj $ seq , { } ) #This is triggered when the sequence is predicted
if ( ! is.null ( obj $ seq ) & input $ old != " " ) {
print ( 2 )
if ( ! is.null ( obj $ seq ) & input $ old != " " ) { #Only if there is a predicted sequence AND a reference sequence defined by the user
alignament <- msaClustalW ( c ( " Reference" = toupper ( input $ old ) , " Predicted" = obj_ab @ primarySeq %>% as.character ) , type = " dna" )
alignament <- msaClustalW ( c ( " Reference" = toupper ( input $ old ) , " Predicted" = obj_ab @ primarySeq %>% as.character ) , type = " dna" )
paste ( capture.output ( print ( alignament , show = " complete" ) ) , collapse = " \n" )
paste ( capture.output ( print ( alignament , show = " complete" ) ) , collapse = " \n" )
}
}
} )
} )
# This is a minimal and maximal filter definition for the Visor Table
output $ tabmin <- renderUI ( {
output $ tabmin <- renderUI ( {
if ( ! is.null ( obj $ seq ) & input $ old != " " ) {
if ( ! is.null ( obj $ seq ) & input $ old != " " ) {
numericInput ( " tabmin_num" , label = " Mínimo en tabla" , value = 0 )
numericInput ( " tabmin_num" , label = " Mínimo en tabla" , value = 0 )
@ -265,46 +293,56 @@ server <- function(input, output) {
}
}
} )
} )
# A table showing the predicted sequence discrepancies with a reference sequenc.
output $ visTab <- renderTable ( {
output $ visTab <- renderTable ( {
observeEvent ( obj $ seq , { } )
observeEvent ( obj $ seq , { } )
if ( ! is.null ( obj $ seq ) & input $ old != " " & ! is.null ( input $ tabmin_num ) ) {
print ( 3 )
if ( ! is.null ( obj $ seq ) & input $ old != " " & ! is.null ( input $ tabmin_num ) ) { #Only if there is a predicted sequence,a reference sequence defined by the user AND the min and max filters have been constructed
alignament <- msaClustalW ( c ( " Reference" = toupper ( input $ old ) , " Predicted" = obj_ab @ primarySeq %>% as.character ) , type = " dna" )
alignament <- msaClustalW ( c ( " Reference" = toupper ( input $ old ) , " Predicted" = obj_ab @ primarySeq %>% as.character ) , type = " dna" )
# We get each aligned sequence separately
pred <- ( alignament @ unmasked [2 ] %>% as.character ( ) %>% strsplit ( " " ) ) [ [1 ] ]
pred <- ( alignament @ unmasked [2 ] %>% as.character ( ) %>% strsplit ( " " ) ) [ [1 ] ]
ref <- ( alignament @ unmasked [1 ] %>% as.character ( ) %>% strsplit ( " " ) ) [ [1 ] ]
ref <- ( alignament @ unmasked [1 ] %>% as.character ( ) %>% strsplit ( " " ) ) [ [1 ] ]
cons <- pred == ref
cons <- pred == ref # T or F vector
aln <- data.frame ( pred , ref , cons , PosAln = 1 : length ( pred ) )
aln <- data.frame ( pred , ref , cons , PosAln = 1 : length ( pred ) ) #We generate the Alignament Index
aln <- mutate ( aln , cons = case_when (
aln <- mutate ( aln , cons = case_when (
cons == T ~ " " ,
cons == T ~ " " ,
TRUE ~ " ?"
TRUE ~ " ?"
) )
) )
aln %>% filter ( pred != " -" ) %>% add_column ( " PosChrom" = 1 : nrow ( .) ) %>%
merge ( aln , all = T ) %>% arrange ( PosAln ) %>% add_column ( " PosChromAnterior" = c ( NA , .$PosChrom [1 : ( nrow ( .) -1 ) ] ) ) %>%
aln %>% filter ( pred != " -" ) %>% add_column ( " PosChrom" = 1 : nrow ( .) ) %>% #We generate the Chromatogram Index
merge ( aln , all = T ) %>% arrange ( PosAln ) %>%
add_column ( " PosChromAnterior" = c ( NA , .$PosChrom [1 : ( nrow ( .) -1 ) ] ) ) %>% #After merging, we assign in new column the previous index
filter ( cons == " ?" ) %>%
filter ( cons == " ?" ) %>%
filter ( PosAln >= input $ tabmin_num & PosAln <= input $ tabmax_num )
filter ( PosAln >= input $ tabmin_num & PosAln <= input $ tabmax_num )
}
}
} )
} )
# Function to generate a plot object of the chromatogram with the peaks highlighted and the predicted sequence
plotvis <- eventReactive ( input $ butvis , {
plotvis <- eventReactive ( input $ butvis , {
if ( ! is.null ( obj $ seq ) ) {
if ( ! is.null ( obj $ seq ) ) {
print ( " Inicio imagen" )
print ( " Inicio imagen" )
# Definition of the width (number of bases), min (start base index) and max (end base index) of the chromatogram
width <- input $ visWidth
width <- input $ visWidth
min <- input $ visStart
min <- input $ visStart
max <- min + width
max <- min + width
# Defining the bases to be plotted
let <- obj_ab @ primarySeq %>% as.character %>% strsplit ( " " )
let <- obj_ab @ primarySeq %>% as.character %>% strsplit ( " " )
peakpos <- apply ( obj_ab @ peakAmpMatrix , 1 , function ( x ) which ( x == max ( x , na.rm = T ) ) [1 ] )
picks_x <- sapply ( 1 : length ( peakpos ) , function ( i ) obj_ab @ peakPosMatrix [i , peakpos [i ] ] )
# Defining the peak position
peakpos <- apply ( obj_ab @ peakAmpMatrix , 1 , function ( x ) which ( x == max ( x , na.rm = T ) ) [1 ] ) #Getting the column position with the maximum signal by row
picks_x <- sapply ( 1 : length ( peakpos ) , function ( i ) obj_ab @ peakPosMatrix [i , peakpos [i ] ] ) #Obtaining the position of the peaks defined by the previous line
# Construction of a data.frame with peak position, base letter and base index
picks <- data.frame ( rows = picks_x , Base = let [ [1 ] ] , num = 1 : length ( let [ [1 ] ] ) ) %>%
picks <- data.frame ( rows = picks_x , Base = let [ [1 ] ] , num = 1 : length ( let [ [1 ] ] ) ) %>%
filter ( num >= min & num <= max ) #%>% mutate(rows=rows-min)
filter ( num >= min & num <= max )
ranPeaks <- range ( picks $ rows )
ranPeaks <- range ( picks $ rows )
obj $ plot <- obj_ab @ traceMatrix %>% as.data.frame ( ) %>% dplyr :: rename ( A = V1 , C = V2 , G = V3 , T = V4 ) %>% add_column ( rows = 1 : nrow ( .) ) %>%
obj $ plot <- obj_ab @ traceMatrix %>% as.data.frame ( ) %>%
dplyr :: rename ( A = V1 , C = V2 , G = V3 , T = V4 ) %>% add_column ( rows = 1 : nrow ( .) ) %>%
filter ( rows >= ranPeaks [1 ] & rows <= ranPeaks [2 ] ) %>% gather ( Base , Value , - rows ) %>%
filter ( rows >= ranPeaks [1 ] & rows <= ranPeaks [2 ] ) %>% gather ( Base , Value , - rows ) %>%
mutate ( Base = factor ( Base , levels = c ( " G" , " A" , " T" , " C" ) ) ) %>%
mutate ( Base = factor ( Base , levels = c ( " G" , " A" , " T" , " C" ) ) ) %>%
ggplot ( aes ( rows , Value ) ) +
ggplot ( aes ( rows , Value ) ) +
@ -319,6 +357,7 @@ server <- function(input, output) {
}
}
} )
} )
# Render of the chromatogram plot
output $ visor <- renderPlot ( {
output $ visor <- renderPlot ( {
observeEvent ( input $ butvis , { plotvis ( ) } )
observeEvent ( input $ butvis , { plotvis ( ) } )
obj $ plot
obj $ plot