Skip to content

Commit c89af7b

Browse files
authored
Merge pull request #89 from william-hutchison/fix-SCTransform
Fix SCTransform
2 parents df0394b + ccd92d6 commit c89af7b

File tree

4 files changed

+35
-83
lines changed

4 files changed

+35
-83
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: HPCell
22
Title: Massively-parallel R native pipeline for single-cell analysis
3-
Version: 0.3.11
3+
Version: 0.3.13
44
Authors@R: c(person("Stefano", "Mangiola", email = "[email protected]",
55
role = c("aut", "cre")),
66
person("Jiayi", "Si", email = "[email protected]",

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export(remove_dead_scuttle)
6363
export(remove_doublets_scDblFinder)
6464
export(remove_empty_DropletUtils)
6565
export(remove_empty_threshold)
66-
export(run_targets_pipeline)
6766
export(save_experiment_data)
6867
export(score_cell_cycle_seurat)
6968
export(se_add_dispersion)

R/functions.R

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -970,29 +970,31 @@ non_batch_variation_removal <- function(input_read_RNA_assay,
970970

971971
# Rename assay
972972
assay_name_old = input_read_RNA_assay |> Assays() |> _[[1]]
973-
input_read_RNA_assay_transform = input_read_RNA_assay |>
973+
input_read_RNA_assay = input_read_RNA_assay |>
974974
RenameAssays(
975975
assay.name = assay_name_old,
976976
new.assay.name = assay)
977977
}
978978

979979
# avoid small number of cells
980980
if (!is.null(empty_droplets_tbl)) {
981-
filtered_counts <- input_read_RNA_assay_transform |>
981+
input_read_RNA_assay <- input_read_RNA_assay |>
982982
left_join(empty_droplets_tbl, by = ".cell") |>
983983
dplyr::filter(!empty_droplet)
984984
}
985-
986-
counts =
987-
filtered_counts |>
985+
986+
if (!is.null(alive_identification_tbl)) {
987+
input_read_RNA_assay =
988+
input_read_RNA_assay |>
988989
left_join(
989990
alive_identification_tbl |>
990991
select(.cell, any_of(factors_to_regress)),
991992
by=".cell"
992993
)
994+
}
993995

994996
if(!is.null(cell_cycle_score_tbl))
995-
counts = counts |>
997+
input_read_RNA_assay = input_read_RNA_assay |>
996998

997999
left_join(
9981000
cell_cycle_score_tbl |>
@@ -1005,32 +1007,36 @@ non_batch_variation_removal <- function(input_read_RNA_assay,
10051007
# variable_features = readRDS(input_path_merged_variable_genes)
10061008
#
10071009
# # Set variable features
1008-
# VariableFeatures(counts) = variable_features
1010+
# VariableFeatures(input_read_RNA_assay) = variable_features
10091011

10101012
# Normalise RNA
1011-
normalized_rna <-
1013+
input_read_RNA_assay <-
10121014
input_read_RNA_assay |>
1013-
Seurat::SCTransform(
1014-
counts,
1015+
Seurat::SCTransform(
10151016
assay=assay,
10161017
return.only.var.genes=FALSE,
10171018
residual.features = NULL,
10181019
vars.to.regress = factors_to_regress,
10191020
vst.flavor = "v2",
10201021
scale_factor=2186,
10211022
conserve.memory=T,
1022-
min_cells=0,
1023+
min_cells=0
10231024
) |>
10241025
GetAssayData(assay="SCT")
1025-
1026-
1026+
10271027
if (class_input == "SingleCellExperiment") {
1028-
1029-
write_HDF5_array_safe(normalized_rna, "SCT", external_path)
1028+
1029+
if(input_read_RNA_assay[,1,drop=FALSE] |> is.nan() |> any())
1030+
warning("HPCell says: some features might be all 0s, NaN are added by Seurat in the SCT assay, and kept in the assay because SingleCellExperiment requires same feature set for all assays.")
1031+
1032+
write_HDF5_array_safe(input_read_RNA_assay, "SCT", external_path)
10301033

10311034
} else if (class_input == "Seurat") {
1032-
1033-
normalized_rna
1035+
1036+
# Remove NaN features from SCT assay
1037+
input_read_RNA_assay <- input_read_RNA_assay[!apply(input_read_RNA_assay, 1, function(row) all(is.nan(row))), ]
1038+
1039+
input_read_RNA_assay
10341040

10351041
}
10361042

@@ -1109,9 +1115,11 @@ preprocessing_output <- function(input_read_RNA_assay,
11091115

11101116
# Add normalisation
11111117
if(!is.null(non_batch_variation_removal_S)){
1112-
if(input_read_RNA_assay |> is("Seurat"))
1113-
input_read_RNA_assay[["SCT"]] = non_batch_variation_removal_S
1114-
else if(input_read_RNA_assay |> is("SingleCellExperiment")){
1118+
if(input_read_RNA_assay |> is("Seurat")) {
1119+
non_batch_variation_removal_S_assay <- CreateAssay5Object(data = non_batch_variation_removal_S)
1120+
input_read_RNA_assay[["SCT"]] <- non_batch_variation_removal_S_assay
1121+
1122+
} else if(input_read_RNA_assay |> is("SingleCellExperiment")){
11151123
message("HPCell says: in order to attach SCT assay to the SingleCellExperiment, SCT was added to external experiments slot")
11161124
#input_read_RNA_assay = input_read_RNA_assay[rownames(non_batch_variation_removal_S),
11171125
# altExp(input_read_RNA_assay) = SingleCellExperiment(assay = list(SCT = non_batch_variation_removal_S))
@@ -1145,10 +1153,12 @@ preprocessing_output <- function(input_read_RNA_assay,
11451153
)
11461154

11471155
# Attach annotation
1148-
if (inherits(annotation_label_transfer_tbl, "tbl_df")){
1149-
input_read_RNA_assay <- input_read_RNA_assay |>
1150-
left_join(annotation_label_transfer_tbl, by = ".cell")
1151-
}
1156+
try({
1157+
if (inherits(annotation_label_transfer_tbl, "tbl_df")){
1158+
input_read_RNA_assay <- input_read_RNA_assay |>
1159+
left_join(annotation_label_transfer_tbl, by = ".cell")
1160+
}
1161+
}, silent = TRUE)
11521162

11531163

11541164
input_read_RNA_assay

man/run_targets_pipeline.Rd

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)