├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── R ├── scMCA.R ├── scMCA_gui.R └── sysdata.rda ├── README.md ├── data ├── mca_lung.rda └── ref.expr.rda └── man ├── scMCA.Rd └── scMCA_vis.Rd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: scMCA 2 | Type: Package 3 | Title: Mouse Cell atlas 4 | Version: 0.2.0 5 | Author: Guoji Guo 6 | Maintainer: Yincong Zhou 7 | Description: Mouse cell atlas package for single cell mapping 8 | License: GPL-3 9 | Encoding: UTF-8 10 | LazyData: true 11 | Imports: plotly,reshape2,dplyr, shiny, DT 12 | RoxygenNote: 6.0.1 13 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(gettissue) 4 | export(plotMCA) 5 | export(scMCA) 6 | export(scMCA_vis) 7 | import(dplyr) 8 | import(plotly) 9 | import(shiny) 10 | importFrom(DT,dataTableOutput) 11 | importFrom(DT,renderDataTable) 12 | importFrom(pheatmap,pheatmap) 13 | importFrom(plotly,plot_ly) 14 | importFrom(reshape2,melt) 15 | importFrom(shinythemes,shinytheme) 16 | -------------------------------------------------------------------------------- /R/scMCA.R: -------------------------------------------------------------------------------- 1 | ##------------------------------------------------------------------------------------------------------ 2 | 3 | #' Mouse Cell Atlas mapping 4 | #' description 5 | #' @param scdata data.frame or matrix, col correspond to cells and rows correspond to genes 6 | #' @param numbers_plot default is 3, number, it will return top "numbers_plot" records in the plot 7 | #' @importFrom reshape2 melt 8 | #' @export 9 | 10 | scMCA <- function(scdata,numbers_plot=3){ 11 | tst.expr <- data.frame(matrix(nrow = dim(ref.expr)[1],ncol=dim(scdata)[2])) 12 | rownames(tst.expr)<-rownames(ref.expr) 13 | colnames(tst.expr)<-colnames(scdata) 14 | for (i in rownames(tst.expr)) { 15 | if(i%in%rownames(scdata))tst.expr[i,]<- scdata[i,] 16 | } 17 | tst.expr[is.na(tst.expr)]<-0 18 | tst.expr<-as.matrix(t(t(tst.expr)/colSums(tst.expr))*100000) 19 | tst.expr<-log(tst.expr+1) 20 | cors <- cor(log(ref.expr+1),tst.expr) 21 | 22 | cors_index <- apply(cors,2,gettissue,numbers_plot) 23 | cors_index <- sort(unique(as.integer(cors_index))) 24 | scblast.result <- apply(cors,2,function(x) rownames(cors)[which.max(x)]) 25 | cors_in = cors[cors_index,] 26 | colnames(cors_in)<-colnames(scdata) 27 | cors_out = reshape2::melt(cors_in)[c(2,1,3)] 28 | colnames(cors_out)<- c("Cell","Cell type","Score") 29 | cors_out <- as.data.frame(cors_out %>% group_by(Cell) %>% top_n(n=numbers_plot,wt=Score)) 30 | 31 | 32 | result <- list() 33 | result[["cors_matrix"]] <- cors 34 | result[['top_cors']]<-numbers_plot 35 | result[['scMCA']]<-scblast.result 36 | result[['scMCA_probility']]<-cors_out 37 | return(result) 38 | } 39 | 40 | #' @export 41 | gettissue <- function(x,Num=3){ 42 | top_markers <-order(x,decreasing = T)[1:Num] 43 | return(top_markers) 44 | } 45 | 46 | #' @import dplyr 47 | #' @importFrom reshape2 melt 48 | #' @importFrom plotly plot_ly 49 | #' @importFrom pheatmap pheatmap 50 | #' @export 51 | plotMCA <- function(mca_result,interactive_plot=F, numbers_plot=3, col_font_size = 1, row_font_size=8, show_col=T,show_bar=T, show_tree = T){ 52 | data(ref.expr) 53 | cors <- mca_result$cors_matrix 54 | cors_index <- apply(cors,2,gettissue,numbers_plot) 55 | cors_index <- sort(unique(as.integer(cors_index))) 56 | data = cors[cors_index,] 57 | cors_out = reshape2::melt(data)[c(2,1,3)] 58 | colnames(cors_out)<- c("Cell","Cell type","Score") 59 | cors_out <- as.data.frame(cors_out %>% group_by(Cell) %>% top_n(n=numbers_plot,wt=Score)) 60 | mca_result$scMCA_probility <- cors_out 61 | mca_result$top_cors <- numbers_plot 62 | height=dim(data)[1]*10+230 63 | tree_height = 0 64 | if(isTRUE(show_tree)){tree_height=50} 65 | 66 | p<-pheatmap( 67 | data, 68 | clustering_distance_rows = "euclidean", 69 | clustering_distance_cols = "euclidean", 70 | clustering_method = "ward.D", 71 | fontsize_col = col_font_size, 72 | fontsize_row = row_font_size, 73 | color = colorRampPalette(c("grey", "white", "red"))(100), 74 | cellheight = 10, 75 | show_colnames = show_col, 76 | border_color = NA, 77 | height = height, 78 | legend = show_bar, 79 | treeheight_col = tree_height, 80 | treeheight_row = tree_height 81 | ) 82 | if(isTRUE(interactive_plot)){ 83 | 84 | inter_data<-data[rev(p$tree_row$order),][,p$tree_col$order] 85 | height= length(p$tree_row$order)*10+230 86 | plot_ly(x=colnames(inter_data),y=rownames(inter_data),z = inter_data, colors = colorRamp(c("grey", "white","red")),height=height, type = "heatmap", showscale=show_bar) %>% layout(autosize=T, margin=list(l=0,r=230,b=180,t=20,pad=4),font=list(size=row_font_size),xaxis=list(showticklabels=show_col),yaxis=list(side="right")) 87 | } 88 | else{ 89 | p 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /R/scMCA_gui.R: -------------------------------------------------------------------------------- 1 | #' Visualize scMCA result in a web browser. 2 | #' 3 | #' Runs interactive \code{shiny} session of \code{scMCA} 4 | #' 5 | #' @param mca_result an object generate by scMCA funtion 6 | #' 7 | #' @name scMCA_vis 8 | #' @aliases scMCA_vis 9 | #' @import shiny 10 | #' @importFrom DT dataTableOutput renderDataTable 11 | #' @import dplyr 12 | #' @import plotly 13 | #' @importFrom pheatmap pheatmap 14 | #' @importFrom shinythemes shinytheme 15 | #' @export 16 | scMCA_vis <- function(mca_result){ 17 | ui = tagList( 18 | navbarPage( 19 | theme = shinythemes::shinytheme("yeti"), 20 | "scMCA", 21 | tabPanel("Visualizer", 22 | sidebarPanel( 23 | h4("Info panal"), 24 | sliderInput("slider", "Number:", 1, 20, mca_result$top_cors), 25 | h4("plot options"), 26 | column(6, 27 | sliderInput("col_size", "Cell name size:", 1, 10,4) 28 | ), 29 | column(6, 30 | sliderInput("row_size", "Cell type size:", 1,10,8) 31 | ), 32 | checkboxInput("checkcells", label = "Show Cells' name", value = TRUE), 33 | checkboxInput("checkbar", label = "Show Color Bar", value = TRUE), 34 | checkboxInput("checktree", label = "Show cluster tree", value = TRUE) 35 | 36 | ), 37 | mainPanel( 38 | tabsetPanel( 39 | tabPanel("Heatmap", 40 | h4("Heatmap out"), 41 | plotOutput('heatmapplot') 42 | ), 43 | tabPanel("plotly", 44 | h4("plotly heatmap out"), 45 | plotlyOutput("heatmapplotly") 46 | ), 47 | tabPanel("Result table", 48 | h4("result table"), 49 | DT::dataTableOutput("mytable") 50 | ) 51 | ) 52 | ) 53 | ) 54 | ) 55 | ) 56 | server = function(input, output) { 57 | 58 | cors_data <- reactive({ 59 | cors_index <- apply(mca_result$cors_matrix,2,gettissue,input$slider) 60 | cors_index <- sort(unique(as.integer(cors_index))) 61 | cors_out = reshape2::melt(mca_result$cors_matrix[cors_index,])[c(2,1,3)] 62 | colnames(cors_out)<- c("Cell","Cell type","Score") 63 | as.data.frame(cors_out %>% group_by(Cell) %>% top_n(n=input$slider,wt=Score)) 64 | }) 65 | 66 | 67 | heightSize <- function() { 68 | length(unique(cors_data()$`Cell type`)) * 10+100 69 | } 70 | output$heatmapplot <- renderPlot({ 71 | plotMCA(mca_result,numbers_plot = input$slider, col_font_size=input$col_size,row_font_size=input$row_size,show_col = input$checkcells, show_bar = input$checkbar, show_tree = input$checktree) 72 | },height = heightSize) 73 | 74 | output$heatmapplotly <- renderPlotly({ 75 | plotMCA(mca_result,numbers_plot = input$slider, col_font_size=input$col_size, row_font_size = input$row_size,interactive_plot = T,show_col = input$checkcells,show_bar = input$checkbar) 76 | }) 77 | 78 | output$mytable = DT::renderDataTable( 79 | cors_data(), 80 | extensions = 'Buttons', 81 | server = FALSE, 82 | filter = 'top', 83 | options = list( 84 | dom = 'Bfrtip', 85 | buttons = 86 | list( 87 | list( 88 | extend = 'csv', 89 | buttons = c('csv'), 90 | exportOptions = list( 91 | modifiers = list(page = "current") 92 | ) 93 | )) 94 | ) 95 | ) 96 | } 97 | shinyApp(ui=ui, server = server,options = list("launch.browser"=TRUE)) 98 | } 99 | -------------------------------------------------------------------------------- /R/sysdata.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ggjlab/scMCA/0ac6b5233547543692d61164e313e4d42514ff20/R/sysdata.rda -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # scMCA 2 | 3 | #### A tool defines cell types in mouse based on single-cell digital expression 4 | 5 | At first scMCA is a breif R package for large scale data(large DGE) from scMCA online function [Mouse Cell Atlas](http://bis.zju.edu.cn/MCA) ,to alleviate burdens of our main Server. 6 | 7 | Now we add a UI for visulizing the scMCA reuslt. 8 | ### Installation 9 | 10 | ```R 11 | #This require devtools 12 | install.packages('devtools') 13 | library(devtools) 14 | # scMCA requires ggplot2/reshape2/plotly/shiny/shinythemes/shiny 15 | install_github("ggjlab/scMCA") 16 | ``` 17 | 18 | ### Quick Start 19 | 20 | ```R 21 | library(scMCA) 22 | # mca_lung is an example expression matrix from MCA project. 23 | > data(mca_lung) 24 | > dim(mca_lung) 25 | [1] 2884 80 26 | # 2884 genes expression value of 80 cells 27 | 28 | # scMCA has two parameters , single cell expression matrix(scdata) and 29 | # the number of most similar cell types 30 | > mca_result <- scMCA(scdata = mca_lung, numbers_plot = 3) 31 | 32 | ``` 33 | The return of scMCA() is a list which contains 4 parts. 34 | * cors_matrix: Pearson correlation coefficient matrix of each cell and cell type. 35 | * top_cors: equals to numbers_plot 36 | * scMCA: the most relevant cell type for each query cell 37 | * scMCA_probility: the top n relevant cell types for each query cell 38 | 39 | ```R 40 | # open shiny for visualize result for scMCA 41 | scMCA_vis(mca_result) 42 | ``` 43 | 44 | scMCA_vis() provides a bref function for visualizing and downloading of scMCA results 45 | ![scMCA_vis](http://bis.zju.edu.cn/MCA/assets/img/scMCA_vis_demo.png) 46 | -------------------------------------------------------------------------------- /data/mca_lung.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ggjlab/scMCA/0ac6b5233547543692d61164e313e4d42514ff20/data/mca_lung.rda -------------------------------------------------------------------------------- /data/ref.expr.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ggjlab/scMCA/0ac6b5233547543692d61164e313e4d42514ff20/data/ref.expr.rda -------------------------------------------------------------------------------- /man/scMCA.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/scMCA.R 3 | \name{scMCA} 4 | \alias{scMCA} 5 | \title{Mouse Cell Atlas mapping 6 | description} 7 | \usage{ 8 | scMCA(scdata, numbers_plot = 3, shiny_ui = T) 9 | } 10 | \arguments{ 11 | \item{scdata}{data.frame or matrix, col correspond to cells and rows correspond to genes} 12 | 13 | \item{numbers_plot}{default is 3, number, it will return top "numbers_plot" records in the plot} 14 | 15 | \item{plot_heat}{default TRUE, boolean, this will return the result alongwith a heatmap, else will return in a list} 16 | 17 | \item{interactive_plot}{defalut is TRUE, boolean, IF TRUE, a plotly package will be used for interactive plotting} 18 | } 19 | \description{ 20 | Mouse Cell Atlas mapping 21 | description 22 | } 23 | -------------------------------------------------------------------------------- /man/scMCA_vis.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/scMCA_gui.R 3 | \name{scMCA_vis} 4 | \alias{scMCA_vis} 5 | \title{Visualize scMCA result in a web browser.} 6 | \usage{ 7 | scMCA_vis(mca_result) 8 | } 9 | \arguments{ 10 | \item{mca_result}{an object generate by scMCA funtion} 11 | } 12 | \description{ 13 | Runs interactive \code{shiny} session of \code{scMCA} 14 | } 15 | --------------------------------------------------------------------------------