@@ -14,9 +14,10 @@ requireNamespace("anndataR", quietly = TRUE)
1414# '
1515# ' @param i_adata AnnData object, integrated data
1616# ' @param u_adata AnnData object, unintegrated dataset
17+ # ' @param split_id numeric, split id of the integrated data
1718# ' @return AnnData object with .var and .obs added
1819# '
19- get_obs_var_for_integrated <- function (i_adata , u_adata ) {
20+ get_obs_var_for_integrated <- function (i_adata , u_adata , split_id ) {
2021
2122 i_adata $ obs <- u_adata $ obs [i_adata $ obs_names , ]
2223 i_adata $ var <- u_adata $ var [i_adata $ var_names , ]
@@ -25,16 +26,23 @@ get_obs_var_for_integrated <- function(i_adata, u_adata) {
2526 # everything is from batch 1, but some samples need to be labelled to come from batch 2
2627 if (i_adata $ uns [" method_id" ] == " perfect_integration" ) {
2728 cat(
28- " Control method 'perfect_integration' detected. Changing batch labels for split 2 .\n "
29+ " Control method 'perfect_integration' detected. Changing batch labels.\n "
2930 )
3031 cat(" Computing new batch labels\n " )
3132 # mutate is needed as donors that are used for controls, we won't have the mapping
3233 i_adata_new_batch_labels <- get_batch_label_perfect_integration(
3334 u_adata = u_adata ,
3435 i_adata = i_adata ,
35- split_id = 1
36+ split_id = split_id
3637 )
3738
39+ # safeguard
40+ if (! all(i_adata_new_batch_labels $ donor == i_adata $ obs $ donor )) {
41+ stop(
42+ " Donor labels do not match between new batch labels and integrated data. This should not happen!"
43+ )
44+ }
45+
3846 cat(" Attaching new batch labels\n " )
3947 i_adata $ obs $ batch <- i_adata_new_batch_labels $ new_batch_label
4048 }
@@ -53,12 +61,14 @@ get_obs_var_for_integrated <- function(i_adata, u_adata) {
5361# ' @return a dataframe with donor and new batch label
5462# '
5563get_batch_label_perfect_integration <- function (u_adata , i_adata , split_id ) {
64+ # this return which batch sample we used for a donor for a given split
5665 actual_donor_batch_map <- unique(
5766 u_adata $ obs [(u_adata $ obs $ split == split_id ), c(" donor" , " batch" )]
5867 )
59- # mutate is needed as donors that are used for controls, we won't have the mapping
68+ # mutate is needed as donors that are used for controls, won't have batch_new as
69+ # the split id is 0
6070 i_adata_new_batch_labels <- i_adata $ obs [, c(" donor" , " batch" )] %> %
61- left_join(actual_donor_batch_map , by = " donor" , suffix = c(" _old" , " _new" )) %> %
71+ left_join(actual_donor_batch_map , by = " donor" , suffix = c(" _old" , " _new" )) %> %
6272 mutate(new_batch_label = ifelse(is.na(batch_new ), batch_old , batch_new )) %> %
6373 select(donor , new_batch_label )
6474
0 commit comments