add_attributes: Add attributes to igraph object

View source: R/map_information_on_reaction_network.R

add_attributesR Documentation

Add attributes to igraph object

Description

The function add_attributes adds attributes to an igraph object. Attributes can be either added for edges attribute_type = "edges" or vertices attribute_type = "vertices". The function will return a igraph object.

For attribute_type = "edges", the function adds edge weights to a igraph object g. The weights are stored in the attributes object. The function will return a igraph object with updated edge weights. The attributes object can be either a matrix or a data.frame. The matrix is an adjacency matrix containing as entries the weights. The weights will be stored in the E(g)$value slot of the returned igraph object. The data.frame contains the columns vertex, a character vector of length 2, specifying the out- and ingoing vertices for the edge and the edge weights in the remaining columns. The weights will be stored in the respective slots with same names as the colnames of attributes of the returned igraph object.

For attribute_type = "vertices", the function adds vertex attributes to a igraph object g. The values are stored in the attributes object. The function will return a igraph object with updated vertex attributes. The attributes object is a data.frame. The data.frame contains the columns col_vertex, a character vector of length 1, specifying the vertices and the vertex attributes weights in the remaining columns. The attributes will be stored in the respective slots with same names as the colnames of attributes for each vertex of the returned igraph object.

Usage

add_attributes(
  g,
  attribute_type = c("edges", "vertex"),
  attributes,
  cols_vertex = colnames(attributes)[1:2]
)

Arguments

g

igraph object

attribute_type

character of length 1, either "edges" or "vertices"

attributes

data.frame or matrix containing edge or vertex attribute information

cols_vertex

character of length 1 or length 2 specifying the columns in attributes

Details

For attribute_type = "edges", cols_vertex has to be adjusted only when attributes is a data.frame. The character of length 2 will specify the columns containing the out- and ingoing vertices of the graph. For attribute_type = "vertices", cols_vertex has to be adjusted such that it specifies the vertices. The character of length 1 will specify the column containing the vertices of the graph.

Value

igraph object

Author(s)

Thomas Naake, thomasnaake@googlemail.com

Examples

FA <- c("FA(12:0)", "FA(14:0)", "FA(16:0)")

## create data.frame with reactions and reaction order
reactions <- rbind(
    c(1, "RHEA:15421", "M_ATP + M_CoA + M_FA = M_PPi + M_AMP + M_AcylCoA", FALSE),
    c(2, "RHEA:15325", "M_Glycerol-3-P + M_AcylCoA = M_CoA + M_LPA", FALSE),
    c(3, "RHEA:19709", "M_AcylCoA + M_LPA = M_CoA + M_PA", FALSE),
    c(4, "RHEA:27429", "M_H2O + M_PA = M_Pi + M_1,2-DG", FALSE)
)
reactions <- data.frame(order = reactions[, 1], reaction_RHEA = reactions[, 2],
    reaction_formula = reactions[, 3], directed = reactions[, 4])
reactions$order <- as.numeric(reactions$order)
reactions$directed <- as.logical(reactions$directed)

## create the list with reactions
reaction_l <- create_reactions(substrates = list(FA = FA), reactions = reactions)

## create the adjacency matrix
reaction_adj <- create_reaction_adjacency_matrix(reaction_l)

g <- igraph::graph_from_adjacency_matrix(reaction_adj, weighted = TRUE, 
    diag = FALSE)

## attribute_type: vertex
attributes_df <- data.frame(
   name = c("CoA(12:0)", "CoA(14:0)", "CoA(16:0)", "DG(12:0/12:0/0:0)",
       "DG(12:0/14:0/0:0)", "DG(12:0/16:0/0:0)", "DG(14:0/12:0/0:0)",
       "DG(14:0/14:0/0:0)", "DG(14:0/16:0/0:0)", "DG(16:0/12:0/0:0)",
       "DG(16:0/14:0/0:0)", "DG(16:0/16:0/0:0)", "FA(12:0)", "FA(14:0)",
       "FA(16:0)", "PA(12:0/0:0)", "PA(12:0/12:0)", "PA(12:0/14:0)",
       "PA(12:0/16:0)", "PA(14:0/0:0)", "PA(14:0/12:0)", "PA(14:0/14:0)", 
       "PA(14:0/16:0)", "PA(16:0/0:0)", "PA(16:0/12:0)", "PA(16:0/14:0)",
       "PA(16:0/16:0)"),
   logFC_cond1 = c(-5.08,  0.75,  5.43, -0.62,  2.35, 1.39, 2.91,  0.26, 
       -4.14,  0.19,  6.18, 0.78, -1.81,  4.66, -0.10,  2.84, -0.81,
       -0.81, -0.32,  0.17,  2.25, -1.94,  0.80, 4.21,  0.20, -3.29, 
       -0.11),
   logFC_cond2 = c(-2.73,  6.14,  1.98,  0.09,  1.57,  1.77,  3.08,  4.04,
       -3.01, 1.22, -4.25, 0.39, 0.53, 3.30, 7.10, 2.81, -0.99, -0.09,
       -8.25, 4.94, -3.54, -7.74, -1.98, 0.73,  2.36,  2.53, -0.62))
       
## apply the function
add_attributes(g, attribute_type = "vertex", attributes = attributes_df, 
    cols_vertex = "name")

## attribute_type: edges, attributes: data.frame
attributes <- data.frame(
    rbind(
        c("CoA(12:0)", "PA(12:0/0:0)", 0.5),
        c("CoA(12:0)", "PA(14:0/12:0)", 0.8)
))
names(attributes) <- c("from", "to", "weight")
attributes$weight <- as.numeric(attributes$weight)

## apply the function
add_attributes(g, attribute_type = "edges", attributes = attributes, cols_vertex = c("from", "to"))

## attribute_type:edges, attributes: matrix
attributes <- matrix(c(0, 0.5, 0.8, 0, 0, 0, 0, 0, 0), ncol = 3, byrow = TRUE, 
    dimnames = list(
        c("CoA(12:0)", "PA(12:0/0:0)", "PA(14:0/12:0)"),
        c("CoA(12:0)", "PA(12:0/0:0)", "PA(14:0/12:0)")))

## apply the function
add_attributes(g, attribute_type = "edges", attributes = attributes)

michaelwitting/wormLipidPredictR documentation built on Dec. 20, 2024, 5:35 a.m.