server.R 9.35 KB
Newer Older
1 2 3 4 5 6 7 8 9
source('helper.R')

shinyServer(
    
    function(input, output) {
        
        # reactive function
        
        # 
10
        # DGINN reactive
11 12 13
        # 
        
        dginn_df_reactive <- reactive({
14
          
15 16
            dginn_file <- "../data/covid_comp_DGINNscreen.csv"
            
17 18 19 20
            if (is.null(dginn_file)){
                return(NULL)
            }
            else{
21 22
            colnames <- c("File","Name","Gene","Description","GeneSize","NbSpecies","omegaM0Bpp","omegaM0codeml","BUSTED","BUSTED_p-value","MEME_NbSites","MEME_PSS","BppM1M2","BppM1M2_p-value","BppM1M2_NbSites","BppM1M2_PSS","BppM7M8","BppM7M8_p-value","BppM7M8_NbSites","BppM7M8_PSS","codemlM1M2","codemlM1M2_p-value","codemlM1M2_NbSites","codemlM1M2_PSS","codemlM7M8","codemlM7M8_p-value","codemlM7M8_NbSites","codemlM7M8_PSS")
              
23
            df <- read.csv2(dginn_file,sep=",")
24 25 26 27 28 29 30 31 32 33 34 35 36 37
            
            #split primates and bat data
            df_primates <- df[,c(5,6,2,3,7:30)]
            df_primates[,2] <- "primates"
            names(df_primates) <- colnames
            df_bats <- df[,c(31,32,4,3,33:56)]
            df_bats[,2] <- "bats"
            names(df_bats) <- names(df_primates)
            df <- rbind(df_primates,df_bats)
            
            sel <- df$BppM1M2_NbSites+df$BppM7M8_NbSites+df$codemlM1M2_NbSites+df$codemlM7M8_NbSites
            sel <- sel == 0 | is.na(sel)
            
            return(df[!sel,])
38
            }
39
            
40
        })
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
41
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
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
        dginn_site_df_reactive <- reactive({
          
          selected <- input$dginn_data_rows_selected
          
          if (is.null(selected)){
            return(NULL)
          }
          else{
          
          raw_selected <- dginn_df_reactive()[selected,]
        
          models <- c("BppM1M2","BppM7M8","codemlM1M2","codemlM7M8")
        
          df <- data.frame()
        
          for(i in 1:length(models)){
          
            if(dginn_df_reactive()[selected,paste(models[i],"NbSites",sep="_")] >0){
            
              dginn_position <- as.integer(strsplit(as.character(dginn_df_reactive()[selected,paste(models[i],"PSS",sep="_")]),",")[[1]])
              dginn_pvalue <- as.integer(strsplit(as.character(dginn_df_reactive()[selected,paste(models[i],"p-value",sep="_")]),",")[[1]])
            
              dginn_position_df <- data.frame("models" = models[i],"site" = dginn_position,"p-value" = dginn_pvalue )
              df <- rbind(df,dginn_position_df)
            
            }
          
          }
          return(df)
          }
        })
        
74 75 76 77
        # 
        # Tree data reactive
        # 
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
78 79
        tree_data_reactive <- reactive({
          
80
          selected <- input$dginn_data_rows_selected
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
81 82
          
          file=dginn_df_reactive()[selected,"File"]
83
          species=dginn_df_reactive()[selected,"Name"]
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
84 85 86
          path="../data/"
          gene=dginn_df_reactive()[selected,"Gene"]
          
87
          file_url=paste(path,species,file,sep="/")
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
88 89 90 91
          read_file(paste(file_url,".phylip_phyml_tree.txt",sep=""))
          
        })   
        
92 93 94 95
        # 
        # MSA data reactive
        #    
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
96 97 98 99 100
        msa_data_reactive <- reactive({
          selected <- input$dginn_data_rows_selected
          
          
          file=dginn_df_reactive()[selected,"File"]
101
          species=dginn_df_reactive()[selected,"Name"]
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
102 103
          path="../data/"
          gene=dginn_df_reactive()[selected,"Gene"]
104 105 106
          file_url=paste(path,species,file,sep="/")
          
          read_file(paste(file_url,".best.fas",sep=""))
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
107 108
          
        })
109 110 111
        
        # rendering functions
        
112 113
        # renderDataTable -- DGINN data table 
        output$dginn_data <- DT::renderDataTable({
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
114

115 116
            df <- dginn_df_reactive()
        },
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
117 118
        
        selection = 'single',    
119
        rownames= FALSE,
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
120
        options = list(pageLength = 5, columnDefs = list( list(targets = 0, visible = FALSE)))
121
        )
122
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
123
        #renderPlotOutput -- ggTree + msa + summary 
124 125 126 127 128
        output$ggtree_plot <- renderPlot({
            
            selected <- input$dginn_data_rows_selected

            if (! is.null(selected)){
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
129
                species=dginn_df_reactive()[selected,"Name"]
130 131 132
                file=dginn_df_reactive()[selected,"File"]
                path="../data/"
                gene=dginn_df_reactive()[selected,"Gene"]
133 134
                file_url=paste(path,species,file,sep="/")
                
135 136
                tree=read.tree(paste(file_url,".phylip_phyml_tree.txt",sep=""))
                model=input$model_parameter_rb
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
137 138 139 140
                
                #
                # get dginn_data_model_summary
                #
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
141
                models <- c("MEME","BppM1M2","BppM7M8","codemlM1M2","codemlM7M8")
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
142
                
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
143 144 145 146 147
                dginn_data_model_summary <- list()
                dginn_data_model_summary[1] <- c()

                for(i in 1:length(models)){

148 149 150
                  if(dginn_df_reactive()[selected,paste(models[i],"NbSites",sep="_")] >0){
                    
                    dginn_position <- as.character(dginn_df_reactive()[selected,paste(models[i],"PSS",sep="_")])
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
151 152 153 154 155 156 157 158 159
                    dginn_data_model_summary[1] = c(dginn_data_model_summary[1],as.integer(strsplit(dginn_position,",")[[1]]))
                    dginn_data_model_summary[[i+1]]= as.integer(strsplit(dginn_position,",")[[1]])
                  }
                  else{
                    dginn_data_model_summary[[i+1]] <- NA
                  }
                }
                names(dginn_data_model_summary) <- c("summary",models)
                dginn_data_model_summary$summary <- table(dginn_data_model_summary$summary)
160

NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
161 162
                phylopic_id=""
                
163
                if(species=="primates"){
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
164 165 166
                  phylopic_id="2d078b25-e6a0-4beb-a5d3-5d6f16be8ebf"
                }
                
167
                if(species=="bats"){
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
168 169
                  phylopic_id="18bfd2fc-f184-4c3a-b511-796aafcc70f6"
                }
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
170

171

172
                p <- ggtree(tree,size=1)  + geom_tiplab(size=5)
173
                
174
                p_msa_window <- dginn_msaplot(p, paste(file_url,".best.fas",sep=""), offset=1, width=5, window=c(120, 200))
175
                
176 177
                p_msa <- dginn_msaplot(p, paste(file_url,".best.fas",sep=""),dginn_position=as.numeric(names(dginn_data_model_summary$summary)), offset=1, width=5)
                p_msa_summary <- dginn_msaplot_summary(p, paste(file_url,".best.fas",sep=""),dginn_summary=dginn_data_model_summary, offset=1, width=5)
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
178
                
179
                grid.arrange(cowplot::get_legend(p_msa_summary),p_msa_summary+ theme(legend.position = "none") ,p_msa+ theme(legend.position = "none"),nrow=3,ncol=1,heights=c(10,5,2*dginn_df_reactive()[selected,"NbSpecies"]))
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
                
                removeUI(
                  selector="#tree"
                )
                insertUI(
                  selector = "#ggtree_plot",
                  where = "afterEnd",
                  ui = downloadLink("tree",".tree")
                  
                )
                removeUI(
                  selector="#msa"
                )                
                insertUI(
                  selector = "#ggtree_plot",
                  where = "afterEnd",
                  ui = downloadLink("msa",".fasta")
                  
                ) 
199
            }
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
200 201 202
            

            
203 204 205
        }
        )
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
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
        #renderDataTable -- DGINN sites
        
        output$dginn_site_data <- DT::renderDataTable({
          
          df <- dginn_site_df_reactive()
        },
        
        selection = 'single',    
        rownames= FALSE,
        options = list(pageLength = 5)
        )
        
        #renderPlotOutput -- ggTree + msa around sites
        output$ggtree_ggmsa_site_plot <- renderPlot({
          
          selected <- input$dginn_data_rows_selected
          selected_site <- input$dginn_site_data_rows_selected
          
          if (! is.null(selected_site)){
            species=dginn_df_reactive()[selected,"Name"]
            file=dginn_df_reactive()[selected,"File"]
            path="../data/"
            gene=dginn_df_reactive()[selected,"Gene"]
            file_url=paste(path,species,file,sep="/")
            tree=read.tree(paste(file_url,".phylip_phyml_tree.txt",sep=""))
            
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
232 233 234 235 236 237
            site <- dginn_site_df_reactive()[selected_site,"site"]*3
            codon_size <- 2
            codon_window <- 2
            msa_path <- paste(file_url,".best.fas",sep="")
            ggmsa(msa_path, site-codon_size-(codon_window*3), site+(codon_window*3), color = "Chemistry_NT", seq_name = T,consensus_views = TRUE, disagreement = FALSE)+ geom_seqlogo()+ geom_msaBar() 

NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
238 239 240 241 242 243 244 245 246
            #p <- ggtree(tree) + geom_tiplab()
            
            #data = tidy_msa(paste(file_url,".best.fas",sep=""), site-10, site+10)
            #p + geom_facet(geom = geom_msa, data = data,  panel = 'msa', color = "Chemistry_NT") +xlim_tree(1)

            }
        })
        
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
247 248 249 250 251 252 253 254
        output$tree <- downloadHandler(
          filename = function() {
            paste0(input$tree, ".tree")
          },
          content = function(file) {
            write(tree_data_reactive(), file)
          }
        )
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
255
        
NAVRATIL VINCENT's avatar
NAVRATIL VINCENT committed
256 257 258 259 260 261 262 263
        output$msa <- downloadHandler(
          filename = function() {
            paste0(input$msa, ".msa")
          },
          content = function(file) {
            write(msa_data_reactive(), file)
          }
        )
264
        
265 266
    }
)
267