R/wTO.in.line.R

Defines functions wTO.in.line

Documented in wTO.in.line

#' @title wTO.in.line
#'
#' @param d correlation matrix to be converted into the line format.
#' @description Transforms a correlation matrix into the line format.
#' @return the wTO matrix into a data.frame: Node1, Node2 and wTO.
#' @importFrom data.table as.data.table
#' @importFrom stats na.omit
#' @importFrom reshape2 melt
#' @author Deisy Morselli Gysi <deisy at bioinf.uni-leipzig.de>

#' @export
#'

#' @title wTO.in.line
#' 
#' @param d correlation matrix to be converted into the line format.
#' @description Transforms a correlation matrix into the line format.
#' @return the wTO matrix into a data.frame: Node1, Node2 and wTO.
#' @importFrom data.table as.data.table
#' @importFrom stats na.omit
#' @importFrom reshape2 melt
#' @export
wTO.in.line <- function(d) {
  # Extract the upper triangular indices
  upper_triangle_indices <- which(upper.tri(d, diag = FALSE), arr.ind = TRUE)
  
  # Create a data.table with the relevant values
  d_melted <- data.table::data.table(
    Node.1 = rownames(d)[upper_triangle_indices[, 1]],
    Node.2 = colnames(d)[upper_triangle_indices[, 2]],
    wTO = d[upper_triangle_indices]
  )
  
  return(d_melted)
}



# 
# wTO.in.line <-function(d){
#   # names2= matrix(names(d), nrow = nrow(d), ncol = ncol(d), byrow = T)
#   # names3= matrix(row.names(d), nrow = nrow(d), ncol = ncol(d))
#   #
#   # Genes.1 <- names2[upper.tri(names2)]
#   # Genes.2 <- names3[upper.tri(names3)]
#   # M.Genes.1 <- apply(cbind(Genes.1, Genes.2), 1, min)
#   # M.Genes.2<- apply(cbind(Genes.1, Genes.2), 1, max)
#   # # M.nomes = paste(M.Genes.1, M.Genes.2, sep = "~")
#   #
#   #
#   # M.sup <- d[upper.tri(d)]
#   # corre=data.table::as.data.table(cbind(M.Genes.1 ,M.Genes.2, M.sup))
#   # names(corre)<-c("Node.1", "Node.2", "wTO")
#   # # row.names(corre)= paste(M.Genes.1, M.Genes.2, sep = "<->")
#   # # corre$wTO = as.numeric(as.matrix(corre$wTO))
#   #
# 
#   # system.time(correlations<-cor(mydata,use="pairwise.complete.obs"))#get correlation matrix
#   upperTriangle<-upper.tri(d, diag=F) #turn into a upper triangle
#   d.upperTriangle<-d #take a copy of the original cor-mat
#   d.upperTriangle[!upperTriangle]<-NA#set everything not in upper triangle to NA
#   d_melted<-data.table::as.data.table(stats::na.omit(reshape2::melt(as.matrix(d.upperTriangle), value.name ="correlationCoef"))) #use melt to reshape the matrix into triplets, na.omit to get rid of the NA rows
#   names(d_melted)<-c("Node.1", "Node.2", "wTO")
# 
# 
#   return(d_melted)
# }

Try the wTO package in your browser

Any scripts or data that you put into this service are public.

wTO documentation built on Sept. 11, 2024, 6:21 p.m.