R/Concat_files.R

Defines functions Concat_files

Documented in Concat_files

#' Combines data from files produced by \code{\link{LDnClusteringEL}}
#'
#' \code{\link{LDnClusteringEL}} produces one \code{.rds} file for each chromosome. This function concatenates the data from each \code{.rds} file into a single file
#' 
#' @param LDnCl_out Path to folder containing output from \code{\link{LDnClusteringEL}}
#' @keywords Linkage disequilibrium network clustering, complexity reduction
#' @author Petri Kemppainen \email{petrikemppainen2@@gmail.com}, zitong.li \email{lizitong1985@@gmail.com}
#' @return Returns concatenated file with the same data as in the individual \code{.rds} files. Can be used with e.g. \code{\link{emmax_group}} as with output produced by \code{\link{LDnClusteringEL}}
#' @references Kemppainen, P., Knight, C. G., Sarma, D. K., Hlaing, T., Prakash, A., Maung Maung, Y. N., Walton, C. (2015). Linkage disequilibrium network analysis (LDna) gives a global view of chromosomal inversions, local adaptation and geographic structure. Molecular Ecology Resources, 15(5), 1031-1045. https://doi.org/10.1111/1755-0998.12369\cr
#' \cr
#' Li, Z., Kemppainen, P., Rastas, P., Merila, J. Linkage disequilibrium clustering-based approach for association mapping with tightly linked genome-wide data. Accepted to Molecular Ecology Resources.
#' @examples
#' \dontrun{
#' LDnCL_combined <- Concat_files("path/to/rds/files")
#'}                   
#' @export
#' 
Concat_files <- function(LDnCl_out){
  file_names <- list.files(LDnCl_out)
  
  LDnCl_combined <- list()
  LDnCl_combined$cluster_summary  <- do.call(rbind, lapply(file_names, function(x){
    readRDS(paste(LDnCl_out, x, sep = '/'))$cluster_summary
  }))
  
  LDnCl_combined$cluster_PCs <- do.call(cbind, lapply(file_names, function(x){
    readRDS(paste(LDnCl_out, x, sep = '/'))$cluster_PCs
  }))
  
  LDnCl_combined$clusters <- unlist(lapply(file_names, function(x){
    readRDS(paste(LDnCl_out, x, sep = '/'))$clusters
  }), recursive=FALSE)
  
  LDnCl_combined$MCL <- do.call(cbind, lapply(file_names, function(x){
    readRDS(paste(LDnCl_out, x, sep = '/'))$MCL
  }))
  
  LDnCl_combined$Cons <- do.call(cbind, lapply(file_names, function(x){
    readRDS(paste(LDnCl_out, x, sep = '/'))$Cons
  }))
  return(LDnCl_combined)
}
petrikemppainen/LDna documentation built on April 14, 2024, 6:37 p.m.