4

So I'm working with a list that contains other lists inside, with this structure:

library(graph)
library(RBGL)
library(Rgraphviz)

show(tree)

$`SO:0001968`
$`SO:0001968`$`SO:0001622`
$`SO:0001968`$`SO:0001622`$`SO:0001624`
$`SO:0001968`$`SO:0001622`$`SO:0001624`$`SO:0002090`
[1] 1

$`SO:0001968`$`SO:0001622`$`SO:0001623`
$`SO:0001968`$`SO:0001622`$`SO:0001623`$`SO:0002091`
[1] 1

$`SO:0001968`$`SO:0001969`
$`SO:0001968`$`SO:0001969`$`SO:0002090`
[1] 1

$`SO:0001968`$`SO:0001969`$`SO:0002091`
[1] 1


dput(tree)
list(`SO:0001968` = list(`SO:0001622` = list(`SO:0001624` = list(
    `SO:0002090` = 1), `SO:0001623` = list(`SO:0002091` = 1)), 
    `SO:0001969` = list(`SO:0002090` = 1, `SO:0002091` = 1)))

The data I use to build the list comes from an object called g:

show(g)

A graphNEL graph with directed edges
Number of Nodes = 7 
Number of Edges = 8 


dput(g)
new("graphNEL",
nodes = c("SO:0001968", "SO:0001969", "SO:0001622", 
"SO:0001623", "SO:0001624", "SO:0002090", "SO:0002091"), edgeL = list(
    `SO:0001968` = list(edges = 3:2), `SO:0001969` = list(edges = 6:7), 
    `SO:0001622` = list(edges = 5:4), `SO:0001623` = list(edges = 7L), 
    `SO:0001624` = list(edges = 6L), `SO:0002090` = list(edges = integer(0)), 
    `SO:0002091` = list(edges = integer(0))), edgeData = new("attrData",

    data = list(`SO:0001968|SO:0001622` = list(weight = 1), `SO:0001968|SO:0001969` = list(
        weight = 1), `SO:0001969|SO:0002090` = list(weight = 1), 
        `SO:0001969|SO:0002091` = list(weight = 1), `SO:0001622|SO:0001624` = list(
            weight = 1), `SO:0001622|SO:0001623` = list(weight = 1), 
        `SO:0001623|SO:0002091` = list(weight = 1), `SO:0001624|SO:0002090` = list(
            weight = 1)), defaults = list(weight = 1)), nodeData = new("attrData",

    data = list(`SO:0001968` = list(label = "coding_transcript_variant"), 
        `SO:0001969` = list(label = "coding_transcript_intron_variant"), 
        `SO:0001622` = list(label = "UTR_variant"), `SO:0001623` = list(
            label = "5_prime_UTR_variant"), `SO:0001624` = list(
            label = "3_prime_UTR_variant"), `SO:0002090` = list(
            label = "3_prime_UTR_intron_variant"), `SO:0002091` = list(
            label = "5_prime_UTR_intron_variant")), defaults = list(
        label = NA_character_)), renderInfo = new("renderInfo",

    nodes = list(), edges = list(), graph = list(), pars = list()), 
    graphData = list(edgemode = "directed"))

Each SO:000XXX corresponds to a name, and I can find the names using the function nodeData, that returns a named list:

nodeData(g, nodes(g), "label")

$`SO:0001968`
[1] "coding_transcript_variant"

$`SO:0001969`
[1] "coding_transcript_intron_variant"

$`SO:0001622`
[1] "UTR_variant"

$`SO:0001623`
[1] "5_prime_UTR_variant"

$`SO:0001624`
[1] "3_prime_UTR_variant"

$`SO:0002090`
[1] "3_prime_UTR_intron_variant"

$`SO:0002091`
[1] "5_prime_UTR_intron_variant"

What I need is to replace (or rename) the data in the tree list with the corresponding string of the nodeData function.

For example, replace the 'SO:0001968' in the tree list for coding_transcript_variant from the nodeData function.

JFernandez
  • 265
  • 4
  • 16
  • What package are you using, `data.tree` ? Also, make your `tree` and `g` objects [reproducible](https://stackoverflow.com/q/5963269/680068), `dput(tree)`. – zx8754 Dec 12 '17 at 11:11
  • 1
    Please provide reproducible data. – zx8754 Dec 12 '17 at 14:40
  • @zx8754 Just added it. – JFernandez Dec 18 '17 at 07:22
  • Cannot reproduce `g` object, error: Error in .local(.Object, ...) : unused argument (data = list(`SO:0001968|SO:0001622` = list(weight = 1)... – zx8754 Dec 18 '17 at 14:05
  • @zx8754 Yeah, I just checked it and you are right, seems like an internal error from dput(), cause I tried with another graphnel and doesn't work too. – JFernandez Dec 18 '17 at 14:31

2 Answers2

3

This recursive function should do the trick :

# you will do this but I couldn't install your packages
# nodeD <- nodeData(g, nodes(g), "label")

nodeD <- list(`SO:0001968` = "coding_transcript_variant",
              `SO:0001969` = "coding_transcript_intron_variant",
              `SO:0001622` = "UTR_variant",
              `SO:0001623` = "5_prime_UTR_variant",
              `SO:0001624` = "3_prime_UTR_variant",
              `SO:0002090` = "3_prime_UTR_intron_variant",
              `SO:0002091` = "5_prime_UTR_intron_variant")

rename_items <- function(item){
  if (is.list(item)){
    item <- lapply(item,rename_items)
    names(item) <- unname(nodeD[names(item)])
  }
  item
}

tree2 <- rename_items(tree)

Result

# $coding_transcript_variant
# $coding_transcript_variant$UTR_variant
# $coding_transcript_variant$UTR_variant$`3_prime_UTR_variant`
# $coding_transcript_variant$UTR_variant$`3_prime_UTR_variant`$`3_prime_UTR_intron_variant`
# [1] 1
# 
# 
# $coding_transcript_variant$UTR_variant$`5_prime_UTR_variant`
# $coding_transcript_variant$UTR_variant$`5_prime_UTR_variant`$`5_prime_UTR_intron_variant`
# [1] 1
# 
# 
# 
# $coding_transcript_variant$coding_transcript_intron_variant
# $coding_transcript_variant$coding_transcript_intron_variant$`3_prime_UTR_intron_variant`
# [1] 1
# 
# $coding_transcript_variant$coding_transcript_intron_variant$`5_prime_UTR_intron_variant`
# [1] 1
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
0

If you save the output from nodeData() to a vector, you can use the names() function to assign the names to a list().

An example of assigning names to list elements:

x <- 1:5
y <- 11:20
z <- 21:25

theList <- list(x,y,z)

listNames <- c("element1","element2","element3")
names(theList) <- listNames
# access first element by name, using $ form of extract operator
theList$element1

...and the output:

> theList$element1
[1] 1 2 3 4 5
>

You may need to unlist() the output of nodeData() as follows:

theNames <- unlist(nodeData(g, nodes(g), "label"))
names(g) <- theNames 
Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • I need the names in that particular list called `tree`. And also, if I do that, the vector doesn't even have the same lenght so it won't work. – JFernandez Dec 12 '17 at 13:50
  • 1
    @JFernandez - without access to your data to replicate your problem, the best I can do is provide an example with data I generated myself. – Len Greski Dec 13 '17 at 02:25