使用命名列表重命名列表列表

时间:2017-12-12 10:59:53

标签: r list bioinformatics

所以我正在使用包含其他列表的列表,具有以下结构:

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)))

我用来构建列表的数据来自一个名为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"))

每个SO:000XXX对应一个名称,我可以使用函数nodeData找到名称,它返回一个命名列表:

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"

我需要的是使用tree函数的相应字符串替换(或重命名)nodeData列表中的数据。

例如,从'SO:0001968'函数替换tree coding_transcript_variant列表中的nodeData

2 个答案:

答案 0 :(得分:3)

这个递归函数应该可以解决这个问题:

# 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)

<强>结果

# $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

答案 1 :(得分:0)

如果将nodeData()的输出保存到矢量,则可以使用names()功能将名称分配给list()

为列表元素指定名称的示例:

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

...和输出:

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

您可能需要unlist() nodeData()的输出,如下所示:

theNames <- unlist(nodeData(g, nodes(g), "label"))
names(g) <- theNames