R/mod_genotypic2.R

Defines functions mod_genotypic2_server mod_genotypic2_ui

#' genotypic2 UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_genotypic2_ui <- function(id){
  ns <- NS(id)
  tagList(
    shiny::sidebarLayout(
      shiny::sidebarPanel(tags$h3("PCA"),
                          tags$br(),
                          tags$p('PCA (Principal component analysis) is a multivariate 
                                  technique that summarizes 
                                 the information provided by the genetic markers 
                                 (for example, SNPs) into a few set of components. 
                                 We can apply PCA to genlight objects with the 
                                 function glPca of the package adgenet'),
                          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("PCA plot"),
                shiny::plotOutput(ns("plotPCA"), height = "500px",
                                  dblclick = "double_click",
                                  brush = brushOpts(
                                    id = "brush_plot",
                                    resetOnNew = TRUE
                                  ))
                
      )
    )
  )
}
    
#' genotypic2 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_genotypic2_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
      
    output$plotPCA <- 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
      
      LimaBeanPCA <- glPca(LimaBeanData3, nf = 3) 
      # to carry out a PCA on a genlight object. With the argument nf as NULL, 
      #you are asked interactively for the number of principal components to be 
      #retained. For this data, three axes were retained.
      
      # to create a customized PCA plot with the package ggplot2
      
      Limapcascores <- as.data.frame(LimaBeanPCA$scores)
      Limapcascores$pop <- pop(LimaBeanData3)
      
      
      set.seed(5)
      colors <- brewer.pal(n = nPop(LimaBeanData3), name = "Set1")
      p <- ggplot(Limapcascores, aes(x=PC1, y=PC2, colour=pop))
      p <- p + geom_hline(yintercept = 0) 
      p <- p + geom_vline(xintercept = 0) 
      p <- p + geom_point(size=3)
      p <- p + theme_bw()
      p <- p + scale_color_manual(values=colors ) +
        xlab(sprintf("PC1 %f percent", 100*LimaBeanPCA$eig[1]/sum(LimaBeanPCA$eig))) + 
        ylab(sprintf("PC2 %f percent", 100*LimaBeanPCA$eig[2]/sum(LimaBeanPCA$eig)))
      p
      
    })
  })
}
    
## To be copied in the UI
# mod_genotypic2_ui("genotypic2_ui_1")
    
## To be copied in the server
# mod_genotypic2_server("genotypic2_ui_1")
Viinky-Kevs/microsoftAI documentation built on April 10, 2022, 12:01 p.m.