Browse code

bug fixes to unordered SCEs

alexq authored on 29/10/2024 09:46:38
Showing 1 changed files

... ...
@@ -66,6 +66,9 @@ lisaClust <-
66 66
            lisaFunc = "K",
67 67
            minLambda = 0.05) {
68 68
     if (methods::is(cells, "SummarizedExperiment")) {
69
+      
70
+      cells <- cells[,order(cells[[imageID]])]
71
+      
69 72
       cd <- spicyR:::.format_data(
70 73
         cells, imageID, cellType, spatialCoords, FALSE
71 74
       )
... ...
@@ -85,33 +88,36 @@ lisaClust <-
85 88
       
86 89
       SummarizedExperiment::colData(cells)[regionName] <- regions
87 90
     } else if (is(cells, "data.frame")) {
91
+      
92
+      cells <- cells[order(cells[[imageID]]),]
93
+      
88 94
       cd <- cells
89 95
       cd <- cd[, c(cellType, imageID, spatialCoords)]
90 96
       colnames(cd) <- c("cellType", "imageID", "x", "y")
91 97
       cd$cellID <- as.character(seq_len(nrow(cd)))
92
-            cd$imageCellID <- as.character(seq_len(nrow(cd)))
93
-
94
-            lisaCurves <- lisa(cd,
95
-                Rs = Rs,
96
-                BPPARAM = BPPARAM,
97
-                window = window,
98
-                window.length = window.length,
99
-                whichParallel = whichParallel,
100
-                sigma = sigma,
101
-                lisaFunc = lisaFunc,
102
-                minLambda = minLambda
103
-            )
104
-
105
-            kM <- kmeans(lisaCurves, k)
106
-            regions <- paste("region", kM$cluster, sep = "_")
107
-
108
-            cells[regionName] <- regions
109
-        } else {
110
-            stop(
111
-                "Unsupported datatype for cells: please use",
112
-                "SingleCellExperiment or SpatialExperiment"
113
-            )
114
-        }
115
-
116
-        cells
98
+      cd$imageCellID <- as.character(seq_len(nrow(cd)))
99
+      
100
+      lisaCurves <- lisa(cd,
101
+                         Rs = Rs,
102
+                         BPPARAM = BPPARAM,
103
+                         window = window,
104
+                         window.length = window.length,
105
+                         whichParallel = whichParallel,
106
+                         sigma = sigma,
107
+                         lisaFunc = lisaFunc,
108
+                         minLambda = minLambda
109
+      )
110
+      
111
+      kM <- kmeans(lisaCurves, k)
112
+      regions <- paste("region", kM$cluster, sep = "_")
113
+      
114
+      cells[regionName] <- regions
115
+    } else {
116
+      stop(
117
+        "Unsupported datatype for cells: please use",
118
+        "SingleCellExperiment or SpatialExperiment"
119
+      )
117 120
     }
121
+    
122
+    cells
123
+  }