Skip to content

Commit 353c325

Browse files
authored
Update clean_duplicates.R
1 parent ed6bb46 commit 353c325

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

R/clean_duplicates.R

+27-21
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,30 @@ clean_duplicates <- function(setup, segs, xy_thresh = 1, z_thresh = 10, compare_
5353
# Search and identify cells that have the same x,y coordinate
5454
for (r in 1:n_rows){ # r for row
5555
for (c in 1:n_cols) { # c for column
56-
if (!is.na(segs$segmentations[[s]]$soma$x[r])) {
57-
ind_x <- which.min(abs(segs$segmentations[[s+c]]$soma$x- segs$segmentations[[s]]$soma$x[r]))
58-
ind_y <- which.min(abs(segs$segmentations[[s+c]]$soma$y- segs$segmentations[[s]]$soma$y[r]))
59-
if((length(ind_x) != 0) & (length(ind_y) != 0)){
60-
if (ind_x==ind_y){
61-
comp_matrix[r,c+1] <- ind_x
62-
# exact_overlap_cnt <- exact_overlap_cnt + 1
56+
if(length(is.na(segs$segmentations[[s]]$soma$x[r])) != 0){
57+
if (!is.na(segs$segmentations[[s]]$soma$x[r])) {
58+
ind_x <- which.min(abs(segs$segmentations[[s+c]]$soma$x- segs$segmentations[[s]]$soma$x[r]))
59+
ind_y <- which.min(abs(segs$segmentations[[s+c]]$soma$y- segs$segmentations[[s]]$soma$y[r]))
60+
if((length(ind_x) != 0) & (length(ind_y) != 0)){
61+
if (ind_x==ind_y){
62+
comp_matrix[r,c+1] <- ind_x
63+
# exact_overlap_cnt <- exact_overlap_cnt + 1
6364

64-
} else {
65-
# in cells isolated by x, threshold by difference in y
66-
diff_y <- abs(segs$segmentations[[s+c]]$soma$y[ind_x] - segs$segmentations[[s]]$soma$y[r])
65+
} else {
66+
# in cells isolated by x, threshold by difference in y
67+
diff_y <- abs(segs$segmentations[[s+c]]$soma$y[ind_x] - segs$segmentations[[s]]$soma$y[r])
6768

68-
# in cells isolated by y, threshold by difference in x
69-
diff_x <- abs(segs$segmentations[[s+c]]$soma$x[ind_y] - segs$segmentations[[s]]$soma$x[r])
69+
# in cells isolated by y, threshold by difference in x
70+
diff_x <- abs(segs$segmentations[[s+c]]$soma$x[ind_y] - segs$segmentations[[s]]$soma$x[r])
7071

71-
if (diff_y < diff_x & diff_y < xy_thresh) {
72-
comp_matrix[r,c+1] <- ind_x
73-
# thresh_overlap_cnt <- thresh_overlap_cnt + 1
72+
if (diff_y < diff_x & diff_y < xy_thresh) {
73+
comp_matrix[r,c+1] <- ind_x
74+
# thresh_overlap_cnt <- thresh_overlap_cnt + 1
7475

75-
} else if (diff_x < diff_y & diff_x < xy_thresh) {
76-
comp_matrix[r,c+1] <- ind_y
77-
# thresh_overlap_cnt <- thresh_overlap_cnt + 1
76+
} else if (diff_x < diff_y & diff_x < xy_thresh) {
77+
comp_matrix[r,c+1] <- ind_y
78+
# thresh_overlap_cnt <- thresh_overlap_cnt + 1
79+
}
7880
}
7981
}
8082
}
@@ -90,7 +92,7 @@ clean_duplicates <- function(setup, segs, xy_thresh = 1, z_thresh = 10, compare_
9092

9193

9294
### start if else statement for last 5 columns here
93-
while (sep_cnt< sep_z && c != n_cols+1) {
95+
while (sep_cnt< sep_z && c != n_cols+1 & length(comp_matrix > 0)) {
9496
prev <- is.na(comp_matrix[r,c])
9597
cur <- is.na(comp_matrix[r,c+1])
9698
if (cur) {
@@ -114,8 +116,12 @@ clean_duplicates <- function(setup, segs, xy_thresh = 1, z_thresh = 10, compare_
114116
intensities <- vector(length=end_soma)
115117

116118
for (c in 1:end_soma) {
117-
if (!is.na(comp_matrix[r,c])) {
118-
intensities[c] <- segs$segmentations[[s+c-1]]$soma$intensity[comp_matrix[r,c]]
119+
if(length(comp_matrix) > 0){
120+
if (!is.na(comp_matrix[r,c])) {
121+
intensities[c] <- segs$segmentations[[s+c-1]]$soma$intensity[comp_matrix[r,c]]
122+
} else {
123+
intensities[c] <- NA
124+
}
119125
} else {
120126
intensities[c] <- NA
121127
}

0 commit comments

Comments
 (0)