Created
March 8, 2021 08:40
-
-
Save tuhulab/c40536c234a5770aa3dd9377071bbf37 to your computer and use it in GitHub Desktop.
R function to merge libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
counttable_merge_library_fun <- function(counttable_data = ..., | |
lib_to_merge_vector = ...){ | |
lib_id <- counttable_data %>% colnames() %>% str_extract("lib\\d{1,}") | |
merged_counttable <- sapply(lib_to_merge_vector, function(one_lib_id_to_merge){ | |
merged_counts <- counttable_data %>% select((lib_id == one_lib_id_to_merge) %>% which()) %>% rowSums() | |
merged_counts_df <- tibble(one_lib_id_to_merge = merged_counts) | |
return(merged_counts_df) | |
}) # The function to merge libs for counttable ----------------- | |
# list tidy | |
MergedLib <- do.call(rbind.data.frame, merged_counttable) %>% t() %>% as.data.frame() %>% tibble() | |
rownames(MergedLib) <- c() | |
# new colnames | |
colnames(MergedLib) <- paste0(colnames(counttable_data)[which(lib_id %in% lib_to_merge_vector)] %>% str_extract("NG[:graph:]{1,}_lib\\d{1,}") %>% unique(), "_merged") | |
# remove the old libs | |
index_of_oldLibs <- which(lib_id %in% lib_to_merge_vector) | |
counttable_data_rmOldLib <- counttable_data %>% select(-all_of(index_of_oldLibs)) | |
# merge with new libs | |
counttable_data_rmOldLib_addMergedLib <- counttable_data_rmOldLib %>% bind_cols(MergedLib) | |
return(counttable_data_rmOldLib_addMergedLib) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment