R/ff_list_list_functions.R

Defines functions compare_ff_list_list cor_ff_list_list compare_ff_list_list_vs_ff_list_list cor_ff_list_list_vs_ff_list_list likelihood_ff_list_list_vs_ff_list_list convertSparse2occ_ff_list_list

Documented in compare_ff_list_list compare_ff_list_list_vs_ff_list_list convertSparse2occ_ff_list_list cor_ff_list_list cor_ff_list_list_vs_ff_list_list likelihood_ff_list_list_vs_ff_list_list

##' function variations to run on lists of vectors, handling them as one vector
##' 
##' Copies of several functions that are originally for numeric vectors, but in this _list variations work on list of vectors
##' 
##' @name ff_list_list_functions
##' @author Mark Heron
##' 
##' @import ffbase
NULL


##' compare_ff_list_list
##'
##' Applies a comparison function pairwise between all data_list elements and saves the results in a matrix
##' 
##' @export
##' @param data_list list of the ff_list objects to compare
##' @param comparison_function comparison function to use, e.g. cor_ff_list
##' @return comparison matrix
##' 
compare_ff_list_list <- function(data_list, comparison_function) {
  
  result_matrix <- matrix(0, nrow=length(data_list), ncol=length(data_list))
  for(i in seq_along(data_list)) {
    
    for(j in seq_along(data_list)) { #(i+1):length(data_list)
      if(i <= j) {next}
      
      common_elements <- intersect(names(data_list[[i]]), names(data_list[[j]]))
      result_matrix[i,j] <- comparison_function(data_list[[i]][common_elements], data_list[[j]][common_elements])
      
      if(length(common_elements) < max(length(data_list[[i]]),length(data_list[[j]])) ) {
        warning("Elements of two lists don't match!")
      }
    }
  }
  result_matrix <- result_matrix + t(result_matrix) + diag(1,length(data_list))
  
  colnames(result_matrix) <- names(data_list)
  rownames(result_matrix) <- names(data_list)
  return(result_matrix)
}



##' cor_ff_list_list
##'
##' Computes the pairwise correlations between the list elements and saves them in a matrix.
##' 
##' @export
##' @param data_list list of the ff_list objects to compute the correlations between
##' @return correlation matrix
##' 
cor_ff_list_list <- function(data_list) {
  
  return( compare_ff_list_list(data_list, cor_ff_list) )
}



##' compare_ff_list_list_vs_ff_list_list
##'
##' Applies a comparison function pairwise between the list elements of two different lists and saves the results in a matrix.
##' 
##' @export
##' @param first_list list of the ff_list objects to compute the correlations from
##' @param second_list list of the ff_list objects to compute the correlations to
##' @param comparison_function comparison function to use, e.g. cor_ff_list
##' @return comparison matrix
##' 
compare_ff_list_list_vs_ff_list_list <- function(first_list, second_list, comparison_function) {
  
  result_matrix <- matrix(0, nrow=length(first_list), ncol=length(second_list))
  for(i in 1:(length(first_list))) {
    for(j in 1:length(second_list)) {
      common_elements <- intersect(names(first_list[[i]]), names(second_list[[j]]))
      result_matrix[i,j] <- comparison_function(first_list[[i]][common_elements], second_list[[j]][common_elements])
      
      if(length(common_elements) < max(length(first_list[[i]]),length(second_list[[j]])) ) {
        warning("Elements of two lists don't match!")
      }
    }
  }
  
  colnames(result_matrix) <- names(second_list)
  rownames(result_matrix) <- names(first_list)
  return(result_matrix)
}



##' cor_ff_list_list_vs_ff_list_list
##'
##' Computes the pairwise correlations between the list elements of two different lists and saves them in a matrix.
##' 
##' @export
##' @param first_list list of the ff_list objects to compute the correlations from
##' @param second_list list of the ff_list objects to compute the correlations to
##' @return correlation matrix
##' 
cor_ff_list_list_vs_ff_list_list <- function(first_list, second_list) {
  
  return(compare_ff_list_list_vs_ff_list_list(first_list, second_list, cor_ff_list))
  
}


##' likelihood_ff_list_list_vs_ff_list_list
##'
##' Computes the pairwise likelihood between the list elements of two different lists and saves them in a matrix.
##' 
##' @export
##' @param first_list list of the ff_list objects that represent the predictions
##' @param second_list list of the ff_list objects that represent the measurements
##' @return likelihood matrix
##' 
likelihood_ff_list_list_vs_ff_list_list <- function(first_list, second_list) {
  
  return(compare_ff_list_list_vs_ff_list_list(first_list, second_list, likelihood_ff_list))
}



##' convertSparse2occ_ff_list_list
##'
##' Individually converts a list of different sparse representation of dyad positions to nucleosome occupancy as list of ff vectors.
##' 
##' @export
##' @param data_list list of different sparse representation matrix lists
##' @param lengths list of chromosome lengths, \code{names} must match the \code{names} of data_list sub-elements (i.e. \code{names(data_list[[1]])})
##' @return list of lists of ff vectors with genomic occuopancy
##' 
convertSparse2occ_ff_list_list <- function(data_list, lengths) {
  
  occ_list <- list()
  for(data_name in names(data_list)) {
    
    occ_list[[data_name]] <- convertSparse2occ_ff_list(data_list[[data_name]][names(lengths)], lengths)
  }
  return(occ_list)
}
markheron/nucular documentation built on Feb. 18, 2020, 12:32 a.m.