R/get_xavg.R

#' Order the entries in each matrix according to the rowMeans of the selected matrix (clusterBy = ).
#' @description Calculates rowMeans of selected matrix in the list. Order the entries of all matrices according to the ordered rowMeans. Called by "plot_hm"-function.
#' @param mat A list with matrices and additional information about the selected region. mat generated by "get_matrix"-function. Default value is NULL.
#' @param clusterBy Integer which specifies the matrix to which all matrices will be ordered. Default value is 1.
#' @return list with ordered matrix entries and additional information about the region entered in "get_matrix"-function. Will be used of "plot_hm"-function

get_xavg = function (mat = NULL, clusterBy = 1){
  ########## check mat input (got from "get_matrix" function) ##########
  if(is.null(mat)){
    stop("no mat inserted")
  }
  
  nmats = length(mat)-5
  mat_mean = mat[[clusterBy]]
  
  ########## calculate rowMean and create id ##########
  mat_mean[,'rowMean'] = rowMeans(mat_mean[,7:ncol(mat_mean)], na.rm = TRUE)
  mat_mean[,'id'] = paste(mat[[1]][,V1], mat[[1]][,V2], sep = '_')

  ########## create id for every entry in each matrix, merge and order it by clusterBy input ##########

  for (i in 1:(nmats)){
    mat[[i]][,'id'] =  paste(mat[[i]][,V1], mat[[i]][,V2], sep = '_')
    mat[[i]] = merge(mat[[i]], mat_mean[,c('rowMean','id')], by='id')
    mat[[i]] = mat[[i]][order(rowMean)]
    mat[[i]] [, id := NULL]
  }

  mat_ssum =list()
  for (i in 1:nmats){
    mat_ssum[[i]] = colMeans(mat[[i]][,7:(ncol(mat[[i]])-1)], na.rm = TRUE)
  }
  
  matXAvg = mat
  return(list(matXAvg, mat_ssum))
}
ClaudiaRHD/chipAnalyser documentation built on June 18, 2020, 8:31 p.m.