Skip to content

Commit d614e7f

Browse files
committed
Error -> warning in case of nonmatching dimnames, use full_join instead of bind_cols
1 parent e03463d commit d614e7f

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

R/utilities.R

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,14 @@ check_se_dimnames <- function(se) {
624624
# Stop if column names of assays do not overlap, or if some assays have
625625
# column names and others don't
626626
if (check_if_assays_are_NOT_overlapped(se, dim = "cols")) {
627-
stop(
628-
"tidySummarizedExperiment says: at least one of the assays in your SummarizedExperiment have column names, but they don't completely overlap between assays."
627+
warning(
628+
"tidySummarizedExperiment says: at least one of the assays in your SummarizedExperiment have column names, but they don't completely overlap between assays. It is strongly recommended to make the assays consistent, to avoid erroneous matching of samples."
629629
)
630630
}
631631
# Same for row names
632632
if (check_if_assays_are_NOT_overlapped(se, dim = "rows")) {
633-
stop(
634-
"tidySummarizedExperiment says: at least one of the assays in your SummarizedExperiment have row names, but they don't completely overlap between assays."
633+
warning(
634+
"tidySummarizedExperiment says: at least one of the assays in your SummarizedExperiment have row names, but they don't completely overlap between assays. It is strongly recommended to make the assays consistent, to avoid erroneous matching of features."
635635
)
636636
}
637637

@@ -640,20 +640,26 @@ check_se_dimnames <- function(se) {
640640
# (At this point we know that all assays have the same dimnames (could be
641641
# NULL), but they could be in different order)
642642
if (is.null(colnames(se)) &&
643-
length(assays(se)) > 0 &&
644-
!is.null(colnames(assays(se, withDimnames = FALSE)[[1]]))) {
645-
warning(
646-
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have column names, but the SummarizedExperiment does not. Setting colnames(se) to column names of first assay."
647-
)
648-
colnames(se) <- colnames(assays(se, withDimnames = FALSE)[[1]])
643+
length(assays(se)) > 0) {
644+
cn <- vapply(assays(se, withDimnames = FALSE), function(x) !is.null(colnames(x)), FALSE)
645+
if (any(cn)) {
646+
idx <- which(cn)[1]
647+
warning(
648+
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have column names, but the SummarizedExperiment does not. Setting colnames(se) to column names of first assay with column names (assay ", idx, ")."
649+
)
650+
colnames(se) <- colnames(assays(se, withDimnames = FALSE)[[idx]])
651+
}
649652
}
650653
if (is.null(rownames(se)) &&
651-
length(assays(se)) > 0 &&
652-
!is.null(rownames(assays(se, withDimnames = FALSE)[[1]]))) {
653-
warning(
654-
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have row names, but the SummarizedExperiment does not. Setting rownames(se) to row names of first assay."
655-
)
656-
rownames(se) <- rownames(assays(se, withDimnames = FALSE)[[1]])
654+
length(assays(se)) > 0) {
655+
rn <- vapply(assays(se, withDimnames = FALSE), function(x) !is.null(rownames(x)), FALSE)
656+
if (any(rn)) {
657+
idx <- which(rn)[1]
658+
warning(
659+
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have row names, but the SummarizedExperiment does not. Setting rownames(se) to row names of first assay with row names (assay ", idx, ")."
660+
)
661+
rownames(se) <- rownames(assays(se, withDimnames = FALSE)[[idx]])
662+
}
657663
}
658664

659665
# If the assays as well as the SE have dimnames, but they don't overlap
@@ -662,16 +668,16 @@ check_se_dimnames <- function(se) {
662668
length(assays(se)) > 0 &&
663669
!is.null(colnames(assays(se, withDimnames = FALSE)[[1]])) &&
664670
!all(colnames(assays(se, withDimnames = FALSE)[[1]]) %in% colnames(se))) {
665-
stop(
666-
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have column names, but they don't agree with the column names of the SummarizedExperiment object itself."
671+
warning(
672+
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have column names, but they don't agree with the column names of the SummarizedExperiment object itself. It is strongly recommended to make the assays consistent, to avoid erroneous matching of samples."
667673
)
668674
}
669675
if (!is.null(rownames(se)) &&
670676
length(assays(se)) > 0 &&
671677
!is.null(rownames(assays(se, withDimnames = FALSE)[[1]])) &&
672678
!all(rownames(assays(se, withDimnames = FALSE)[[1]]) %in% rownames(se))) {
673-
stop(
674-
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have row names, but they don't agree with the row names of the SummarizedExperiment object itself."
679+
warning(
680+
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have row names, but they don't agree with the row names of the SummarizedExperiment object itself. It is strongly recommended to make the assays consistent, to avoid erroneous matching of features."
675681
)
676682
}
677683

@@ -705,12 +711,18 @@ get_count_datasets <- function(se) {
705711
}
706712

707713
# Rearrange if assays has colnames and rownames
708-
if (!is.null(rownames(se)) & !is.null(rownames(.x))) .x = .x[rownames(se), , drop = FALSE]
709-
if (!is.null(colnames(se)) & !is.null(colnames(.x))) .x = .x[, colnames(se), drop = FALSE]
714+
if (!is.null(rownames(se)) && !is.null(rownames(.x)) &&
715+
all(rownames(se) %in% rownames(.x))) {
716+
.x = .x[rownames(se), , drop = FALSE]
717+
}
718+
if (!is.null(colnames(se)) && !is.null(colnames(.x)) &&
719+
all(colnames(se) %in% colnames(.x))) {
720+
.x = .x[, colnames(se), drop = FALSE]
721+
}
710722

711723
# If I don't have assay colnames and rownames add them
712-
if (!is.null(rownames(se)) & is.null(rownames(.x))) rownames(.x) = rownames(se)
713-
if (!is.null(colnames(se)) & is.null(colnames(.x))) colnames(.x) = colnames(se)
724+
if (!is.null(rownames(se)) && is.null(rownames(.x))) rownames(.x) = rownames(se)
725+
if (!is.null(colnames(se)) && is.null(colnames(.x))) colnames(.x) = colnames(se)
714726

715727
.x %>%
716728
# matrix() %>%
@@ -733,8 +745,9 @@ get_count_datasets <- function(se) {
733745
when(
734746
length(.)>0 ~
735747

748+
reduce(., full_join, by = c(f_(se)$name, s_(se)$name)),
736749
# reduce(., left_join, by = c(f_(se)$name, s_(se)$name)),
737-
bind_cols(., .name_repair = c("minimal")) %>% .[!duplicated(colnames(.))],
750+
# bind_cols(., .name_repair = c("minimal")) %>% .[!duplicated(colnames(.))],
738751
~ expand.grid(
739752
rownames(se), colnames(se)
740753
) %>%
@@ -1299,10 +1312,12 @@ order_assays_internally_to_be_consistent <- function(se) {
12991312
assays(se, withDimnames = FALSE) %>% as.list(),
13001313
names(assays(se)),
13011314
~ {
1302-
if (!is.null(rownames(se)) & !is.null(rownames(.x))) {
1315+
if (!is.null(rownames(se)) && !is.null(rownames(.x)) &&
1316+
all(rownames(se) %in% rownames(.x))) {
13031317
.x = .x[rownames(se), , drop = FALSE]
13041318
}
1305-
if (!is.null(colnames(se)) & !is.null(colnames(.x))) {
1319+
if (!is.null(colnames(se)) && !is.null(colnames(.x)) &&
1320+
all(colnames(se) %in% colnames(.x))) {
13061321
.x = .x[, colnames(se), drop = FALSE]
13071322
}
13081323
.x

0 commit comments

Comments
 (0)