Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions R/dong_cell_anno.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@



#' Title
#' single cell annotation by scPred
#'
#' @param sce
#' @param sce_ref
#' @param name
#' @param mini_cluster
#' @param threshold
#' @param merge_seurat_cluster
#' @param name_merge_seurat
#' @param source
#' @param path_ref
#' Cell type annotation using a combination of unbiased feature selection from a reduced-dimension space, and machine-learning probability-based prediction method.
#' Refer to [scPred](https://genomebiology.biomedcentral.com/articles/10.1186/s13059-019-1862-5)
#' @param sce A seurat object containing cells to be classified
#' @param sce_ref A Seurat object with trained model(s) using scPred or a scPred object
#' @param name A character used to name a new column of metadata containing cell type annotation by `scPred`
#' @param mini_cluster Minimal cell counts of the cluster
#' @param threshold Threshold used for probabilities to classify cells into classes. All cells below this threshold value will be labels as "unassigned". In the case of binary classification (two cell tyoes), a threshold of 0.5 will force all cells to be classified to any of the two cell types. For multi-class classification, if there's no probability higher than the threshold associated to a cell type, this will be labelled as "unassigned"
#' @param merge_seurat_cluster Whether to merge prediction results by`scPred` with clustering results
#' @param name_merge_seurat A character used to name a new column of metadata containing the results of `merge_seurat_cluster`
#' @param source Character string related to computer system name choose from win and linux
#' @param path_ref The path or the name of the file where the `sce_ref` is read from
#'
#' @return
#' @return Seurat object with updated metadata containing cell type annotation by `scPred`
#' @export
#'
#' @examples
dong_cell_anno<-function(sce, sce_ref = NULL, name = "Model1",merge_seurat_cluster = T, name_merge_seurat = "scpred_seurat", mini_cluster = 50, threshold = 0.65, source = "win", path_ref = NULL){
dong_cell_anno<-function(sce, sce_ref = NULL, name = "Model1", merge_seurat_cluster = T, name_merge_seurat = "scpred_seurat", mini_cluster = 50, threshold = 0.65, source = "win", path_ref = NULL){


if(is.null(sce_ref)){
Expand Down
3 changes: 1 addition & 2 deletions R/irGSEA-main-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ irGSEA.score <- function(object = NULL, assay = NULL, slot = "scale.data",
maxRank = ucell.MaxRank,
w_neg = 1,
ncores = ncores,
force.gc = T,
seed = seeds)
force.gc = T )
colnames(ucell.scores) <- stringr::str_remove(colnames(ucell.scores), pattern = "_UCell")
object[["UCell"]] <- SeuratObject::CreateAssayObject(counts = t(ucell.scores))
object <- SeuratObject::SetAssayData(object, slot = "scale.data",
Expand Down
60 changes: 31 additions & 29 deletions R/training_sc_anno.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@



#' Title
#' Train a prediction model by scPred
#'
#' @param sce
#' @param group
#' @param remove_cell
#' @param subset_cell
#' @param propotion
#' @param dims
#' @param model
#' @param verbose
#' @param fig.type
#' @param pt.size
#' @param cols
#' @param seed
#' @param show_col
#' @param show_plot
#' @param path
#' @param cores
#' @param palette
#' @param assay
#' @param width
#' @param height
#' @param recorrect
#' Given a seurat object with cell-type labels, train a prediction model for cell type annotation by `scPred`.
#' Refer to [scPred](https://genomebiology.biomedcentral.com/articles/10.1186/s13059-019-1862-5)
#' @param sce A seurat object
#' @param group Column in meta.data containing the cell-type labels of each single cell
#' @param remove_cell A vector of variables in `group` you want to remove from the model
#' @param subset_cell Whether to select the subsets of cells as training model according to certain proportion
#' @param proportion Proportion used to select the subsets of cells. The parameter works when subset_cell ="True".
#' @param dims Number of PCs for clustering
#' @param model Classification model supported via caret package. Default is "mda"
#' @param verbose Whether to print a progress bar once expression testing begins
#' @param fig.type Format of plot saving, such as pdf and png
#' @param pt.size Size of point
#' @param cols Vector of colors, each color corresponds to an identity class. This may also be a single character, such as "normal" and "random", to a palette as specified by `IOBR::palettes`. See `palettes` for details
#' @param seed Seed of the random number generator, default is 123. The parameter works when cols ="random"
#' @param show_col Whether to display the palettes
#' @param show_plot Whether to display the plot
#' @param path Path of the output saving directory
#' @param cores Number of nodes to be forked
#' @param palette Numeric value corresponding with color palette. Default is 4, other options: 1, 2, 3
#' @param assay Assay to use in model training, e.g. RNA, SCT, integrated
#' @param width width of plot when saving
#' @param height height of plot when saving
#' @param recorrect Whether to correct for batch effects
#'
#' @return
#' @return scPred object
#' @export
#'
#' @examples
Expand Down Expand Up @@ -85,7 +87,7 @@ training_sc_anno<-function(sce,



# 确定训练目标------------------------------------------------------
# Define training objectives------------------------------------------------------
if(!is.null(group)){
target<- group
message(paste0(">>> Idents of Seurat object is: ", target))
Expand Down Expand Up @@ -144,9 +146,9 @@ training_sc_anno<-function(sce,
sces_merged <- merge(sce[[1]], y = sce[2:length(sce)], project = "project", merge.data = TRUE)
VariableFeatures(sces_merged) <- sct_features
pc.num<-50
#' 利用变异的feature跑PCA
#' Run PCA with variable features
sces_merged <- RunPCA(object = sces_merged, assay = "SCT", features = sct_features, npcs = pc.num)
#' harmony会继续利用PCA进行校正
#' Correct batch effect with harmony
library(harmony)
sces_merged <- RunHarmony(object = sces_merged,
assay.use = "SCT",
Expand Down Expand Up @@ -201,11 +203,11 @@ training_sc_anno<-function(sce,
save(sce, file = paste0(path$abspath,"0-",group,"-reference-seurat-data-for-scPred.RData") )
####################################

#' 训练和初步测试预测效果
#' Training and preliminary testing of prediction effects
sce <- scPred:: getFeatureSpace(sce, group)
# Secondly, we train the classifiers for each cell using the trainModel function.
# By default, scPred will use a support vector machine with a radial kernel.
#' 并行运算
#' Parallel computing
##################################

if(cores>1){
Expand Down Expand Up @@ -235,7 +237,7 @@ training_sc_anno<-function(sce,

###################################
###################################
#' 将预测模型提取出来-这样就可以丢弃sce对象
#' Extracting out the predictive model
scpred <-scPred:: get_scpred(sce)
save(scpred, file = paste0(path$abspath,"0-",group,"-cell-annotation-model.RData"))

Expand Down
Loading