R/read.computeMatrix.file.R

Defines functions read.computeMatrix.file

Documented in read.computeMatrix.file

#' @title \code{computeMatrix} *.gz file reader
#'
#' @description The function reads a matrix.file.gz generated by deepTools/computeMatrix function or by \link{computeMatrix.deeptools}. The value can be passed to \link{plot.density.profile} function.
#'
#' @param matrix.file A string indicating indicating the full path to the matrix.file.gz generated by deepTools/computeMatrix function or by \link{computeMatrix.deeptools}.
#'
#' @export read.computeMatrix.file
#'
#' @return The functions returns a named list containing:
#' \itemize{
#'   \item{\code{metadata}}{data.frame with the information gotten from the matrix_file.gz}
#'   \item{\code{matrix.data}}{data.frame with the scores gotten from}
#'   \item{\code{original.file.path}}{with full path to the original matrix_file.gz.}
#'  }
#'  This list can be passed as it is to the function \link{plot.density.profile}.
#'
# @import dplyr
# @importFrom data.table fread
# @importFrom stringr str_split
# @importFrom tidyr separate


read.computeMatrix.file = function(matrix.file) {

  #-----------------------------#
  # Check if Rseb is up-to-date #
  Rseb::actualize(update = F, verbose = F)   #
  #-----------------------------#

  # Matrix file check
  if (class(matrix.file) != "character" | length(matrix.file)  != 1 | !grepl(".gz$", matrix.file[1])) {
    return(warning("The matrix.file must be a single string indicating the full path to the matrix.file.gz generated by deepTools/computeMatrix function or by Rseb::computeMatrix.deeptools()."))
  }

  # Load required libraries
  require(dplyr)

  # Import metadata and create a table
  metadata = data.table::fread(matrix.file, nrows = 1, stringsAsFactors = F, sep = "@", h = F)$V2
  metadata = gsub(x = metadata, pattern = "[{]|[}]", replacement = "")
  metadata = gsub(x = metadata, pattern = "\\s", replacement = "_")
  metadata = gsub(x = metadata, pattern = "[\"],\"", replacement = ",")
  metadata = gsub(x = metadata, pattern = "\":\"", replacement = ":")
  metadata = gsub(x = metadata, pattern = "\":", replacement = ":")
  metadata = gsub(x = metadata, pattern = ",\"", replacement = "@")
  metadata = gsub(x = metadata, pattern = "\"", replacement = "")
  metadata = gsub(x = metadata, pattern = "[,]missing", replacement = "@missing")
  metadata = gsub(x = metadata, pattern = "[,]sort", replacement = "@sort")
  metadata = gsub(x = metadata, pattern = "[,]unscaled", replacement = "@unscaled")

  metadata = as.data.frame(stringr::str_split(string = metadata, pattern = "@"),
                           col.names = "metadata", stringsAsFactors = F)

  metadata =
    metadata %>%
    tidyr::separate(col = "metadata", sep = ":", c("parameters", "values"))

  metadata =
    metadata %>%
    mutate(values = gsub(x = metadata$values, pattern = '[[]|[]]', replacement = ""))


  # Generate the output
  return(list(metadata = metadata,
              matrix.data = as.data.frame(data.table::fread(matrix.file, skip = 1)),
              original.file.path = matrix.file))
}
sebastian-gregoricchio/Rseb documentation built on June 14, 2024, 12:22 p.m.