| 
						
						
						
					 | 
				
				 | 
				
					@ -1,5 +1,9 @@ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(shiny) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(rhandsontable) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(tidyverse) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(reshape2) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(Matrix) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(CitFuns) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					library(BDCIT) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					print(getwd()) | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -61,6 +65,23 @@ ui <- fluidPage( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                h3("Nitrogen"), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                tableOutput("nitrogen") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        ), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        tabPanel("scRNAseq", | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                 sidebarPanel( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     textInput("sqlquery", label = "sqlquery", value = ""), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     uiOutput("PATID"), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     checkboxInput("sct_sel", "Mostrar filtrados"), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     checkboxInput("cd45_chk", "Purificación CD45"), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     textInput("genes", label="genes", value = "") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                 ), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                 mainPanel( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     tabsetPanel( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                         tabPanel("Table", tableOutput("sc_table")), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                         tabPanel("Plots",  | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                                  plotOutput("sc_plot", height = "1000px"), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                                  plotOutput("sc_expr"), height = "600px") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                     ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                 ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					) | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -767,6 +788,95 @@ server <- function(input, output) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    ## scRNAseq | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    output$PATID = renderUI({ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        observeEvent(input$goButton, {}) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        sc_cod<-sqlFetch(dta, "CNAG") %>% pull(CODIGO) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        selectizeInput("sc_cod", "CÓDIGO", sc_cod, multiple = T) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    }) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    output$sc_table<-renderTable({ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (input$sct_sel){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            if (input$sqlquery != ""){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                print(input$sqlquery) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                sqlQuery(dta, input$sqlquery) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            }else{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                if (!is.null(input$sc_cod)){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    sqlFetch(dta, "CNAG") %>% filter(CODIGO %in% input$sc_cod) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                }else{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    sqlFetch(dta, "CNAG") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        }else{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            sqlFetch(dta, "CNAG") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    }) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    output$sc_plot <-renderPlot({ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        meta<<-readRDS(paste0(scRNAseqRoute,"metadata_full_object.rds")) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					         | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (input$sqlquery != ""){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            sc_codigos<-sqlQuery(dta, input$sqlquery) %>% pull(CNAG_NAME) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        }else{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            if (!is.null(input$sc_cod)){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                sc_codigos<-sqlFetch(dta, "CNAG") %>% filter(CODIGO %in% input$sc_cod) %>% pull(CNAG_NAME) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            }else{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                sc_codigos<-sqlFetch(dta, "CNAG") %>% pull(CNAG_NAME) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        sc_codigos<-gsub(" _","_", sc_codigos ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        sc_codigos<-gsub("_ ","_", sc_codigos ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        sc_codigos<-gsub(" ","_", sc_codigos ) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					         | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        meta<-meta %>% mutate(sample2=gsub("_CD45", "", sample)) %>% filter(sample2 %in% sc_codigos) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (isFALSE(input$cd45_chk)){ meta<-meta %>% filter(!grepl("_CD45", sample))} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					         | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        g1<-ggplot(meta, aes(coord_x, coord_y, color=predicted.id))+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            geom_point(size=0.2)+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            guides(colour = guide_legend(override.aes = list(size=2)))+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            theme_bw()+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            theme(aspect.ratio = 1) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					         | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        meta_perc<-meta %>% group_by(sample, predicted.id) %>% summarise(N=n()) %>% mutate(N=perc(N)) %>%  | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            spread(predicted.id, N) %>% gather("predicted.id","N",-sample) %>% mutate(N=case_when(is.na(N)~0,TRUE~N)) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        g2<-ggheatmap(meta_perc, "sample","predicted.id", "N", color = "grey50") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        ggpubr::ggarrange(g1,g2, ncol=1, heights = c(0.3, 0.7)) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    }) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    output$sc_expr <-renderPlot({ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (input$genes != ""){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            expr<-readRDS(paste0(scRNAseqRoute,"expression_full_object.rds")) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            genes<-strsplit(input$genes, ",")[[1]] | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            if (length(genes) > 1){ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                df.expr<-as.data.frame(as.matrix(expr[genes,])) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                df.expr["Gene"]<-rownames(df.expr) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                mdf.expr<-melt(df.expr) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            }else{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                df.expr<-as.data.frame(t(as.matrix(expr[genes[1],]))) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                df.expr["Gene"]<-genes[1] | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                mdf.expr<-melt(df.expr) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					             | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            alldata<-merge(meta, mdf.expr, by.x="barcode", by.y = "variable") | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					             | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					         | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        # order <- clustsort(alldata %>% spread(Gene, value) %>% select(predicted.id, all_of(genes)) %>%   | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #                        group_by(predicted.id) %>% summarise(across(all_of(genes), mean)) %>%  as.data.frame) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #  | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        # g1<-ggplot(alldata, aes(predicted.id, value, fill=predicted.id))+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     geom_violin(scale = "width")+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     geom_jitter(width=0.2, size=0.1, alpha=0.3)+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     scale_x_discrete(limits=order$x)+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     guides(fill=F)+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     facet_wrap(.~Gene)+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     theme_bw()+ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        #     theme(axis.text.x = element_text(angle=90, hjust=1, vjust = 0.5)) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        g2<-ggheatmap(alldata, x="predicted.id",y="Gene",value="value", color="grey")+coord_equal() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        # ggpubr::ggarrange(g1,g2, ncol=1) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        g2 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    }) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					# Run the application  | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
					 | 
				
				 | 
				
					
 |