library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(Seurat)
## Attaching SeuratObject
library(SeuratData)
## ── Installed datasets ───────────────────────────────────── SeuratData v0.2.2 ──
## ✓ ifnb    3.1.0                         ✓ pbmcsca 3.0.0
## ✓ panc8   3.0.2
## ────────────────────────────────────── Key ─────────────────────────────────────
## ✓ Dataset loaded successfully
## > Dataset built with a newer version of Seurat than installed
## ❓ Unknown version of Seurat installed
library(openxlsx)
library(HGNChelper)
pbmc <- readRDS("/mnt/nectar_volume/home/eraz0001/pbmc_human/pbmc_tutorial_final.rds")
DimPlot(pbmc)

source("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/gene_sets_prepare.R")
source("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/sctype_score_.R")

there are different tissue type here. you can select if you wish

db_ = "https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/ScTypeDB_full.xlsx"; tissue = "Muscle"
gs_list = gene_sets_prepare(db_, tissue)
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Warning in checkGeneSymbols(markers_all): x contains non-approved gene symbols
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
## Maps last updated on: Thu Oct 24 12:31:05 2019
es.max = sctype_score(scRNAseqData = pbmc[["RNA"]]@scale.data, scaled = TRUE, gs = gs_list$gs_positive, gs2 = gs_list$gs_negative)
cL_resutls = do.call("rbind", lapply(unique(pbmc@meta.data$seurat_clusters), function(cl){
    es.max.cl = sort(rowSums(es.max[ ,rownames(pbmc@meta.data[pbmc@meta.data$seurat_clusters==cl, ])]), decreasing = !0)
    head(data.frame(cluster = cl, type = names(es.max.cl), scores = es.max.cl, ncells = sum(pbmc@meta.data$seurat_clusters==cl)), 10)
}))
sctype_scores = cL_resutls %>% group_by(cluster) %>% top_n(n = 1, wt = scores) 
sctype_scores$type[as.numeric(as.character(sctype_scores$scores)) < sctype_scores$ncells/4] = "Unknown"
print(sctype_scores[,1:3])
## # A tibble: 9 × 3
## # Groups:   cluster [9]
##   cluster type                  scores
##   <fct>   <chr>                  <dbl>
## 1 2       Unknown                 83.5
## 2 3       Muscle satellite cell  121. 
## 3 1       Unknown                 41.6
## 4 6       Smooth muscle cells     50.7
## 5 4       Unknown                 31.0
## 6 0       Unknown                 93.4
## 7 5       Mesenchymal stem cell   47.9
## 8 7       Myocytes                19.9
## 9 8       Myoepithelial cells     38.0
pbmc@meta.data$customclassif = ""
for(j in unique(sctype_scores$cluster)){
  cl_type = sctype_scores[sctype_scores$cluster==j,]; 
  pbmc@meta.data$customclassif[pbmc@meta.data$seurat_clusters == j] = as.character(cl_type$type[1])
}
DimPlot(pbmc, reduction = "umap", label = TRUE, repel = TRUE, group.by = 'customclassif')  

lapply(c("ggraph","igraph","tidyverse", "data.tree"), library, character.only = T)
## Loading required package: ggplot2
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble  3.1.6     ✓ purrr   0.3.4
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x tibble::as_data_frame() masks igraph::as_data_frame(), dplyr::as_data_frame()
## x purrr::compose()        masks igraph::compose()
## x tidyr::crossing()       masks igraph::crossing()
## x dplyr::filter()         masks stats::filter()
## x igraph::groups()        masks dplyr::groups()
## x dplyr::lag()            masks stats::lag()
## x purrr::simplify()       masks igraph::simplify()
## [[1]]
##  [1] "ggraph"             "ggplot2"            "HGNChelper"        
##  [4] "openxlsx"           "pbmcsca.SeuratData" "panc8.SeuratData"  
##  [7] "ifnb.SeuratData"    "SeuratData"         "SeuratObject"      
## [10] "Seurat"             "dplyr"              "stats"             
## [13] "graphics"           "grDevices"          "utils"             
## [16] "datasets"           "methods"            "base"              
## 
## [[2]]
##  [1] "igraph"             "ggraph"             "ggplot2"           
##  [4] "HGNChelper"         "openxlsx"           "pbmcsca.SeuratData"
##  [7] "panc8.SeuratData"   "ifnb.SeuratData"    "SeuratData"        
## [10] "SeuratObject"       "Seurat"             "dplyr"             
## [13] "stats"              "graphics"           "grDevices"         
## [16] "utils"              "datasets"           "methods"           
## [19] "base"              
## 
## [[3]]
##  [1] "forcats"            "stringr"            "purrr"             
##  [4] "readr"              "tidyr"              "tibble"            
##  [7] "tidyverse"          "igraph"             "ggraph"            
## [10] "ggplot2"            "HGNChelper"         "openxlsx"          
## [13] "pbmcsca.SeuratData" "panc8.SeuratData"   "ifnb.SeuratData"   
## [16] "SeuratData"         "SeuratObject"       "Seurat"            
## [19] "dplyr"              "stats"              "graphics"          
## [22] "grDevices"          "utils"              "datasets"          
## [25] "methods"            "base"              
## 
## [[4]]
##  [1] "data.tree"          "forcats"            "stringr"           
##  [4] "purrr"              "readr"              "tidyr"             
##  [7] "tibble"             "tidyverse"          "igraph"            
## [10] "ggraph"             "ggplot2"            "HGNChelper"        
## [13] "openxlsx"           "pbmcsca.SeuratData" "panc8.SeuratData"  
## [16] "ifnb.SeuratData"    "SeuratData"         "SeuratObject"      
## [19] "Seurat"             "dplyr"              "stats"             
## [22] "graphics"           "grDevices"          "utils"             
## [25] "datasets"           "methods"            "base"