6
6
# ' @param y a \code{\link{SummarizedExperiment}} object when \code{x} is a
7
7
# ' \code{\link{SummarizedExperiment}} object. Disabled when \code{x} is a list.
8
8
# '
9
- # ' @param assay_name A single character value for selecting the
9
+ # ' @param assay_name A character value for selecting the
10
10
# ' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{assay}}
11
11
# ' to be merged. (By default: \code{assay_name = "counts"})
12
12
# '
119
119
# ' collapse_samples = TRUE)
120
120
# ' tse_temp
121
121
# '
122
+ # ' # Merge all available assays
123
+ # ' tse <- relAbundanceCounts(tse)
124
+ # ' ts1 <- relAbundanceCounts(tse1)
125
+ # ' tse_temp <- mergeSEs(tse, tse1, assay_name = assayNames(tse))
126
+ # '
122
127
NULL
123
128
124
129
# ################################## Generic ####################################
@@ -140,9 +145,9 @@ setMethod("mergeSEs", signature = c(x = "SimpleList"),
140
145
# ################# Input check ##################
141
146
# Check the objects
142
147
class <- .check_objects_and_give_class(x )
143
- # Can the assay_name the found form all the objects
144
- assay_name_bool <- .assays_cannot_be_found(assay_name = assay_name , x )
145
- if ( any( assay_name_bool ) ){
148
+ # CHeck which assays can be found, and if any --> FALSE
149
+ assay_name <- .assays_cannot_be_found(assay_name = assay_name , x )
150
+ if ( .is_a_bool( assay_name ) && assay_name == FALSE ){
146
151
stop(" 'assay_name' must specify an assay from assays. 'assay_name' " ,
147
152
" cannot be found at least in one SE object." ,
148
153
call. = FALSE )
@@ -704,9 +709,7 @@ setMethod("right_join", signature = c(x = "ANY"),
704
709
# Remove all information but rowData, colData, metadata and assay
705
710
row_data <- rowData(tse )
706
711
col_data <- colData(tse )
707
- assay <- assay(tse , assay_name )
708
- assays <- SimpleList(name = assay )
709
- names(assays ) <- assay_name
712
+ assays <- assays(tse )[ assay_name ]
710
713
metadata <- metadata(tse )
711
714
# Create a list of arguments
712
715
args <- list (assays = assays ,
@@ -784,34 +787,53 @@ setMethod("right_join", signature = c(x = "ANY"),
784
787
785
788
}
786
789
# ########################## .assays_cannot_be_found ############################
787
- # This function checks that the assay can be found from TreeSE objects of a list.
790
+ # This function checks that the assay(s) can be found from TreeSE objects of a list.
788
791
789
792
# Input: the name of the assay and a list of TreeSE objects
790
- # Output: A list of boolean values
793
+ # Output: A list of assay_names that can be found or FALSE if any
791
794
.assays_cannot_be_found <- function (assay_name , x ){
792
- # Check if the assay_name can be found. If yes, then FALSE. If not, then TRUE
793
- list <- lapply(x , .assay_cannot_be_found , assay_name = assay_name )
794
- # Unlist the list
795
- result <- unlist(list )
796
- return (result )
795
+ # Loop through objects
796
+ assays <- lapply(x , FUN = function (tse ){
797
+ # Check if the assay_names can be found. If yes, then TRUE. If not, then FALSE
798
+ temp <- lapply(assay_name , .assay_cannot_be_found , tse = tse )
799
+ # Unlist and return
800
+ return ( unlist(temp ) )
801
+ })
802
+ # Create a data.frame from the result
803
+ assays <- as.data.frame(assays , row.names = assay_name )
804
+ colnames(assays ) <- paste0(" tse" , seq_len(length(assays )))
805
+ # Which assays can be found from all the objects?
806
+ assays <- rownames(assays )[ rowSums(assays ) == ncol(assays ) ]
807
+ # If none of assays were found, return FALSE
808
+ if ( length(assays ) == 0 ){
809
+ assays <- FALSE
810
+ }
811
+ # Give warning if assays were dropped
812
+ if ( length(assays ) < length(assay_name ) ){
813
+ warning(" The following assay(s) was not found from all the objects " ,
814
+ " so it is dropped from the output: " ,
815
+ paste0(" '" , setdiff(assay_name , assays ), sep = " '" , collapse = " , " ),
816
+ call. = FALSE )
817
+ }
818
+ return (assays )
797
819
}
798
820
799
821
# ########################### .assay_cannot_be_found #############################
800
- # This function checks that the assay can be found from TreeSE. If it cannot be found
801
- # --> TRUE, if it can be found --> FALSE
822
+ # This function checks that the assay can be found from TreeSE. If it can be found
823
+ # --> TRUE, if it cannot be found --> FALSE
802
824
803
825
# Input: the name of the assay and TreSE object
804
826
# Output: TRUE or FALSE
805
827
.assay_cannot_be_found <- function (assay_name , tse ){
806
- # Check if the assay_name can be found. If yes, then FALSE . If not, then TRUE
828
+ # Check if the assay_name can be found. If yes, then TRUE . If not, then FALSE
807
829
tryCatch(
808
830
{
809
831
.check_assay_present(assay_name , tse )
810
- return (FALSE )
832
+ return (TRUE )
811
833
812
834
},
813
835
error = function (cond ) {
814
- return (TRUE )
836
+ return (FALSE )
815
837
}
816
838
)
817
839
}
@@ -850,9 +872,12 @@ setMethod("right_join", signature = c(x = "ANY"),
850
872
rowdata <- .merge_rowdata(tse1 , tse2 , join )
851
873
# Merge colData
852
874
coldata <- .merge_coldata(tse1 , tse2 , join )
853
- # Merge assay
854
- assay <- .merge_assay(tse1 , tse2 , assay_name , join , missing_values , rowdata , coldata )
855
- assays <- SimpleList(name = assay )
875
+ # Merge assays
876
+ assays <- lapply(assay_name , .merge_assay ,
877
+ tse1 = tse1 , tse2 = tse2 ,
878
+ join = join , missing_values = missing_values ,
879
+ rd = rowdata , cd = coldata )
880
+ assays <- SimpleList(assays )
856
881
names(assays ) <- assay_name
857
882
# Combine metadata
858
883
metadata <- c( metadata(tse1 ), metadata(tse2 ) )
@@ -997,12 +1022,16 @@ setMethod("right_join", signature = c(x = "ANY"),
997
1022
matching_variables2 <- matching_variables2 [ ! is.na(matching_variables2 ) ]
998
1023
999
1024
# Make the matching variables unique
1000
- matching_variables_mod1 <- paste0(matching_variables1 , " _X" )
1001
- matching_variables_ids1 <- matching_variables_ids1 [ ! is.na(matching_variables_ids1 ) ]
1002
- colnames(df1 )[ matching_variables_ids1 ] <- matching_variables_mod1
1003
- matching_variables_mod2 <- paste0(matching_variables2 , " _Y" )
1004
- matching_variables_ids2 <- matching_variables_ids2 [ ! is.na(matching_variables_ids2 ) ]
1005
- colnames(df2 )[ matching_variables_ids2 ] <- matching_variables_mod2
1025
+ if ( length(matching_variables1 ) > 0 ){
1026
+ matching_variables_mod1 <- paste0(matching_variables1 , " _X" )
1027
+ matching_variables_ids1 <-
1028
+ matching_variables_ids1 [ ! is.na(matching_variables_ids1 ) ]
1029
+ colnames(df1 )[ matching_variables_ids1 ] <- matching_variables_mod1
1030
+ matching_variables_mod2 <- paste0(matching_variables2 , " _Y" )
1031
+ matching_variables_ids2 <-
1032
+ matching_variables_ids2 [ ! is.na(matching_variables_ids2 ) ]
1033
+ colnames(df2 )[ matching_variables_ids2 ] <- matching_variables_mod2
1034
+ }
1006
1035
1007
1036
# Add rownames to one of the columns
1008
1037
df1 $ rownames_merge_ID <- rownames(df1 )
@@ -1012,9 +1041,39 @@ setMethod("right_join", signature = c(x = "ANY"),
1012
1041
# Add rownames and remove additional column
1013
1042
rownames(df ) <- df $ rownames_merge_ID
1014
1043
df $ rownames_merge_ID <- NULL
1015
-
1044
+
1016
1045
# Combine matching variables if found
1017
1046
if ( length(matching_variables1 ) > 0 ){
1047
+ # Get the class of each variable
1048
+ class1 <- unlist(lapply(matching_variables_mod1 , FUN = function (x ){class(df [,x ])}))
1049
+ class2 <- unlist(lapply(matching_variables_mod2 , FUN = function (x ){class(df [,x ])}))
1050
+ # If there are mismatches in classes, variables are not matching
1051
+ mismatch <- class1 != class2
1052
+ if ( any( mismatch ) ){
1053
+ # Loop through mismatches
1054
+ for ( i in which(mismatch ) ){
1055
+ # Givve warning that variables are renamed
1056
+ warning(" Datasets include equally named variables called '" ,
1057
+ matching_variables1 [i ], " ' but their class differ. \n " ,
1058
+ " In the output, variables are not combined and they are " ,
1059
+ " renamed based on their class." ,
1060
+ call. = FALSE )
1061
+ # Name variables based on their class
1062
+ colnames(df )[ colnames(df ) == matching_variables_mod1 [i ] ] <-
1063
+ paste0(matching_variables1 [i ], " _" , class1 [i ])
1064
+ colnames(df )[ colnames(df ) == matching_variables_mod2 [i ] ] <-
1065
+ paste0(matching_variables2 [i ], " _" , class2 [i ])
1066
+ # Remove variable from matching list
1067
+ matching_variables1 <- matching_variables1 [- i ]
1068
+ matching_variables2 <- matching_variables2 [- i ]
1069
+ matching_variables_mod1 <- matching_variables_mod1 [- i ]
1070
+ matching_variables_mod2 <- matching_variables_mod2 [- i ]
1071
+ }
1072
+ }
1073
+ }
1074
+ # If there are still matching variables
1075
+ if ( length(matching_variables1 ) > 0 ){
1076
+ # Loop over matching variables
1018
1077
for (i in 1 : length(matching_variables1 ) ){
1019
1078
# Get columns
1020
1079
x <- matching_variables_mod1 [i ]
0 commit comments