@@ -970,29 +970,31 @@ non_batch_variation_removal <- function(input_read_RNA_assay,
970
970
971
971
# Rename assay
972
972
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 | >
974
974
RenameAssays(
975
975
assay.name = assay_name_old ,
976
976
new.assay.name = assay )
977
977
}
978
978
979
979
# avoid small number of cells
980
980
if (! is.null(empty_droplets_tbl )) {
981
- filtered_counts <- input_read_RNA_assay_transform | >
981
+ input_read_RNA_assay <- input_read_RNA_assay | >
982
982
left_join(empty_droplets_tbl , by = " .cell" ) | >
983
983
dplyr :: filter(! empty_droplet )
984
984
}
985
-
986
- counts =
987
- filtered_counts | >
985
+
986
+ if (! is.null(alive_identification_tbl )) {
987
+ input_read_RNA_assay =
988
+ input_read_RNA_assay | >
988
989
left_join(
989
990
alive_identification_tbl | >
990
991
select(.cell , any_of(factors_to_regress )),
991
992
by = " .cell"
992
993
)
994
+ }
993
995
994
996
if (! is.null(cell_cycle_score_tbl ))
995
- counts = counts | >
997
+ input_read_RNA_assay = input_read_RNA_assay | >
996
998
997
999
left_join(
998
1000
cell_cycle_score_tbl | >
@@ -1005,32 +1007,36 @@ non_batch_variation_removal <- function(input_read_RNA_assay,
1005
1007
# variable_features = readRDS(input_path_merged_variable_genes)
1006
1008
#
1007
1009
# # Set variable features
1008
- # VariableFeatures(counts ) = variable_features
1010
+ # VariableFeatures(input_read_RNA_assay ) = variable_features
1009
1011
1010
1012
# Normalise RNA
1011
- normalized_rna <-
1013
+ input_read_RNA_assay <-
1012
1014
input_read_RNA_assay | >
1013
- Seurat :: SCTransform(
1014
- counts ,
1015
+ Seurat :: SCTransform(
1015
1016
assay = assay ,
1016
1017
return .only.var.genes = FALSE ,
1017
1018
residual.features = NULL ,
1018
1019
vars.to.regress = factors_to_regress ,
1019
1020
vst.flavor = " v2" ,
1020
1021
scale_factor = 2186 ,
1021
1022
conserve.memory = T ,
1022
- min_cells = 0 ,
1023
+ min_cells = 0
1023
1024
) | >
1024
1025
GetAssayData(assay = " SCT" )
1025
-
1026
-
1026
+
1027
1027
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 )
1030
1033
1031
1034
} 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
1034
1040
1035
1041
}
1036
1042
@@ -1109,9 +1115,11 @@ preprocessing_output <- function(input_read_RNA_assay,
1109
1115
1110
1116
# Add normalisation
1111
1117
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" )){
1115
1123
message(" HPCell says: in order to attach SCT assay to the SingleCellExperiment, SCT was added to external experiments slot" )
1116
1124
# input_read_RNA_assay = input_read_RNA_assay[rownames(non_batch_variation_removal_S),
1117
1125
# 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,
1145
1153
)
1146
1154
1147
1155
# 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 )
1152
1162
1153
1163
1154
1164
input_read_RNA_assay
0 commit comments