R/mod_genotypic3.R

Defines functions mod_genotypic3_server mod_genotypic3_ui

#' genotypic3 UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_genotypic3_ui <- function(id){
  ns <- NS(id)
  tagList(
    shiny::sidebarLayout(
      shiny::sidebarPanel(tags$h3("DPCA"),
                          tags$br(),
                          tags$p('To identify genetic clusters, we can apply 
                                 another multivariate approach known as DAPC. 
                                 This approach is convenient when we are more 
                                 interested in describing the diversity 
                                 among groups of individuals than within groups. 
                                 This approach is focused on finding discriminant
                                 functions that better describe the differences
                                 among groups, while minimizing the differences 
                                 within groups. However, to find the discriminant 
                                 functions, DAPC needs the groups to be known a 
                                 priori and in many cases we just do not know 
                                 how many groups are present in our sample. 
                                 To address this issue, the adegenet package 
                                 implements the function find.clusters (to find 
                                 the number of clusters by running first a 
                                 PCA and then using the k-means algorithm) 
                                 and the function dapc to establish how are 
                                 the relationships among the clusters.'),
                          tags$br(),
                          shiny::fileInput(ns("filevcf"), "Choose a VCF file",
                                           multiple = F,
                                           accept = ".vcf",
                                           buttonLabel = "Uploading..."),
                          shiny::fileInput(ns("filetxt"), "Choose a TXT file",
                                           multiple = F,
                                           accept = ".txt",
                                           buttonLabel = "Uploading...")
      ),
      mainPanel(tags$h2("Results ML"),
                tags$br(),
                tags$br(),
                tags$h4("DPCA plot"),
                shiny::plotOutput(ns("plotDPCA"), height = "500px",
                                  dblclick = "double_click",
                                  brush = brushOpts(
                                    id = "brush_plot",
                                    resetOnNew = TRUE
                                  )),
                tags$h4("Clusters"),
                shiny::dataTableOutput(ns("table1")
                                  ),
                tags$h4("DPCA custom plot"),
                shiny::plotOutput(ns("plotDPCA2"), height = "500px",
                                  dblclick = "double_click",
                                  brush = brushOpts(
                                    id = "brush_plot",
                                    resetOnNew = TRUE
                                  )),
                tags$h4("Membership probabilities"),
                shiny::plotOutput(ns("plotDPCA3"), height = "500px",
                                  dblclick = "double_click",
                                  brush = brushOpts(
                                    id = "brush_plot",
                                    resetOnNew = TRUE
                                  ))
                
      )
    )
  )
}
    
#' genotypic3 Server Functions
#' @import plotly
#' @import adegenet
#' @import randomForest
#' @import ggplot2
#' @import hierfstat
#' @import ape
#' @import poppr
#' @import pegas
#' @import vcfR
#' @import RColorBrewer
#' @import readr
#' @import tidyverse
#' @import mice
#' @import imputeTS
#' @import knitr
#' @import rgl
#' @import caTools
#' @import randomForestExplainer
#' @import dplyr
#' @import ranger
#' @import vegan
#' @import pvclust
#' @import ape
#' @import nnet
#' @import NeuralNetTools
#' @import tidyr
#' @import openxlsx
#' @import cluster
#' @import ggdendro
#' @import factoextra
#' @import foreach
#' @import gridExtra
#' @import caret
#' @import NbClust
#' @import dendextend
#' @import pals
#' @import expss
#' @import e1071
#' @import ROCR
#' @import pROC
#' @import aricode
#' @import dendextend
#' @import fpc
#' @import mclust
#' @import corrplot
#' @import scales
#' @import ggpubr
#' @import grDevices
#' @noRd 
mod_genotypic3_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    output$plotDPCA <- renderPlot({
      tryCatch(
        {
          LimaBeanGBS = read.vcfR(input$filevcf$datapath)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      tryCatch(
        {
          data = read.table(input$filetxt$datapath, header = TRUE)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      Genepool = as.character(data$Genepool)
      LimaBeanData3 <- vcfR2genlight(LimaBeanGBS)
      ploidy(LimaBeanData3) <- 2
      pop(LimaBeanData3) <- Genepool
      
      LimaBeanData2 = vcfR2genind(LimaBeanGBS, 
                                  pop= Genepool, 
                                  NA.char= "NA")
      
      grp <- find.clusters(LimaBeanData2, 
                           max.n.clust = 30, 
                           choose.n.clust = FALSE,
                           n.clust=5,
                           n.pca=100, choose=FALSE) 
      # You are asked interactively for the number of PCs to be 
      #retained (we chose 500, more than the maximum for this dataset) 
      #and the number of clusters to be retained (we chose K=5 according 
      #to the BIC values shown in the graph and previous biological knowledge). 
      #Choosing the "right" number of clusters is a complex issue, the BIC 
      #graph is just a guide. The researcher should explore different numbers 
      #of clusters and choose the number that makes more sense from a biological point of view.
      
      dapc <- dapc(LimaBeanData2, 
                   grp$grp, 
                   n.pca=100, n.da=4) 
      
      scatter(dapc) # to obtain a basic scatterplot of the dapc
      
    })
    
    
    output$plotDPCA2 <- renderPlot({
      tryCatch(
        {
          LimaBeanGBS = read.vcfR(input$filevcf$datapath)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      tryCatch(
        {
          data = read.table(input$filetxt$datapath, header = TRUE)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      Genepool = as.character(data$Genepool)
      LimaBeanData3 <- vcfR2genlight(LimaBeanGBS)
      ploidy(LimaBeanData3) <- 2
      pop(LimaBeanData3) <- Genepool
      
      LimaBeanData2 = vcfR2genind(LimaBeanGBS, 
                                  pop= Genepool, 
                                  NA.char= "NA")
      
      grp <- find.clusters(LimaBeanData2, 
                           max.n.clust = 30, 
                           choose.n.clust = FALSE,
                           n.clust=5,
                           n.pca=100, choose=FALSE) 
      # You are asked interactively for the number of PCs to be 
      #retained (we chose 500, more than the maximum for this dataset) 
      #and the number of clusters to be retained (we chose K=5 according 
      #to the BIC values shown in the graph and previous biological knowledge). 
      #Choosing the "right" number of clusters is a complex issue, the BIC 
      #graph is just a guide. The researcher should explore different numbers 
      #of clusters and choose the number that makes more sense from a biological point of view.
      
      dapc <- dapc(LimaBeanData2, 
                   grp$grp, 
                   n.pca=100, n.da=4) 
      
      myCol2 <- c("pink","red","blue","light blue", "green") # to assign colors to each of the five clusters
      
      scatter(dapc, scree.da=FALSE, bg="white", pch=20,  cell=0, cstar=0, col=myCol2, solid=1.0,
              cex=3,clab=0, leg=TRUE, posi.leg= "bottomleft", scree.pca=TRUE, posi.pca = "topright", ratio.pca=0.3)
      
    })
    
    output$table1 <- renderDataTable({
      tryCatch(
        {
          LimaBeanGBS = read.vcfR(input$filevcf$datapath)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      tryCatch(
        {
          data = read.table(input$filetxt$datapath, header = TRUE)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      Genepool = as.character(data$Genepool)
      LimaBeanData3 <- vcfR2genlight(LimaBeanGBS)
      ploidy(LimaBeanData3) <- 2
      pop(LimaBeanData3) <- Genepool
      
      LimaBeanData2 = vcfR2genind(LimaBeanGBS, 
                                  pop= Genepool, 
                                  NA.char= "NA")
      
      table(pop(LimaBeanData2), grp$grp)
      
    })
    
    output$plotDPCA3 <- renderPlot({
      tryCatch(
        {
          LimaBeanGBS = read.vcfR(input$filevcf$datapath)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      tryCatch(
        {
          data = read.table(input$filetxt$datapath, header = TRUE)
        },
        error = function(e){
          stop(safeError(e))
        }
      )
      
      Genepool = as.character(data$Genepool)
      LimaBeanData3 <- vcfR2genlight(LimaBeanGBS)
      ploidy(LimaBeanData3) <- 2
      pop(LimaBeanData3) <- Genepool
      
      LimaBeanData2 = vcfR2genind(LimaBeanGBS, 
                                  pop= Genepool, 
                                  NA.char= "NA")
      
      grp <- find.clusters(LimaBeanData2, 
                           max.n.clust = 30, 
                           choose.n.clust = FALSE,
                           n.clust=5,
                           n.pca=100, choose=FALSE) 
      # You are asked interactively for the number of PCs to be 
      #retained (we chose 500, more than the maximum for this dataset) 
      #and the number of clusters to be retained (we chose K=5 according 
      #to the BIC values shown in the graph and previous biological knowledge). 
      #Choosing the "right" number of clusters is a complex issue, the BIC 
      #graph is just a guide. The researcher should explore different numbers 
      #of clusters and choose the number that makes more sense from a biological point of view.
      
      dapc <- dapc(LimaBeanData2, 
                   grp$grp, 
                   n.pca=100, n.da=4) 
      
      compoplot.dapc(dapc) #to visualize the membership probabilities as a bar plot
      
    })
    
  })
}
    
## To be copied in the UI
# mod_genotypic3_ui("genotypic3_ui_1")
    
## To be copied in the server
# mod_genotypic3_server("genotypic3_ui_1")
Viinky-Kevs/microsoftAI documentation built on April 10, 2022, 12:01 p.m.