-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection_scaling.R
48 lines (41 loc) · 1.79 KB
/
selection_scaling.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#' Variable feature selection and scaling of seurat object
#'
#' This package contains functions that identifies the variable features in a seurat object after quality filtering and normalization. The second function is scaling which should be done after variable feature identification.
#'
#' @param input is the file after the normalization of the seurat object
#' @examples
#' variable_features("input.rds")
#' @export
variable_features <- function(input){
if(!(tools::file_ext(input)) == "rds") {
return("Input file should be an rds file")
} else if(tools::file_ext(input) == "rds") {
normalized <- readRDS(input)
if(class(normalized) != "Seurat") {
return("File is not a seurat object")
} else {
normalized <- Seurat::FindVariableFeatures(normalized, selection.method = "vst", nfeatures = 2000)
top10 <- head(Seurat::VariableFeatures(normalized), 10)
plot1 <- Seurat::VariableFeaturePlot(normalized)
plot2 <- Seurat::LabelPoints(plot = plot1, points = top10, repel = TRUE)
finalplots <- plot1 + plot2
ggplot2::ggsave(finalplots, filename = "variable_features_plot.png", width = 15, height = 8, dpi = 300)
saveRDS(top10, file = "top10_variablefeatures.rds")
saveRDS(normalized, file = "variablefeatures.rds")
}
}
}
scaling_seurat <- function(input){
if(!(tools::file_ext(input)) == "rds") {
return("Input file should be an rds file")
} else if(tools::file_ext(input) == "rds") {
for_scaling <- readRDS(input)
if(class(for_scaling) != "Seurat") {
return("File is not a seurat object")
} else {
all.genes <- rownames(for_scaling)
for_scaling <- Seurat::ScaleData(for_scaling, features = all.genes)
saveRDS(for_scaling, file = "after_scaling.rds")
}
}
}