代码之家  ›  专栏  ›  技术社区  ›  Josh

rentrez的摘要列表在使用append()合并后停止工作

  •  0
  • Josh  · 技术社区  · 8 年前

    tl;dr:由 rentrez ,为什么这些列表不再与其他 伦特雷斯 函数合并后使用 append() ?

    我正在使用 伦特雷斯 . 我可以毫无问题地搜索出版物和下载摘要。然而,一个摘要列表中一定有一些我不明白的特别之处,因为当我使用 附加() 尝试合并列表。我还没能通过阅读文档来弄清楚这有什么区别。下面是允许我搜索Pubmed和下载记录的代码:

    # set search term and retmax
    term_set <- '"Transcription, Genetic"[Mesh] AND "Regulatory Sequences, Nucleic Acid"[Mesh] AND 2017:2018[PDAT]'
    retmax_set <- 500
    # search pubmed using web history
    search.l <- entrez_search(db = "pubmed", term = term_set, use_history = T)
    # get summaries of search hits using web history 
    for (seq_start in seq(0, search.l$count, retmax_set)) {
        if (seq_start == 0) {summary.l <- list()} 
        summary.l[[length(summary.l)+1]] <- entrez_summary(
            db = "pubmed", 
            web_history = search.l$web_history, 
            retmax = retmax_set, 
            retstart = seq_start
        )
    }
    

    但是,使用 summary.l <- list() 然后 summary.l[[length(summary.l)+1]] <- entrez_summary(... 结果显示一个摘要列表(此搜索中有3个子列表)。这将导致多个 for 在数据提取的后续步骤中循环(见下文),是一种不健康的数据结构。

    # extract desired information from esummary, convert to dataframe
    for (i in 1:length(summary.l)) {
        if (i == 1) {faut.laut.l <- list()}
        faut.laut <- summary.l[[i]] %>% 
            extract_from_esummary(
                c("uid", "sortfirstauthor", "lastauthor"), 
                simplify = F
            )
        faut.laut.l <- c(faut.laut.l, faut.laut)
    }
    faut.laut.df <- rbindlist(faut.laut.l)
    

    使用 附加() 在下面的代码中,给出了所有1334个摘要的一个列表,避免了子列表。

    # get summaries of search hits using web history 
    for (seq_start in seq(0, search.l$count, retmax_set)) {
        if (seq_start == 0) {
            summary.append.l <- entrez_summary(
                db = "pubmed", 
                web_history = search.l$web_history, 
                retmax = retmax_set, 
                retstart = seq_start
            )
        } 
        summary.append.l <- append(
            summary.append.l,
            entrez_summary(
                db = "pubmed", 
                web_history = search.l$web_history, 
                retmax = retmax_set, 
                retstart = seq_start
            )
        )
    }
    

    但是,在随后的步骤中 extract_from_esummary() 抛出一个错误,即使文档中说 esummaries 应该是摘要对象的列表。

    # extract desired information from esummary, convert to dataframe
    faut.laut.append.l <- extract_from_esummary(
        esummaries = summary.append.l,
        elements = c("uid", "sortfirstauthor", "lastauthor"), 
        simplify = F
    )
    Error in UseMethod("extract_from_esummary", esummaries) : 
    no applicable method for 'extract_from_esummary' applied to an object of class "list"
    
    faut.laut.append.df <- rbindlist(faut.laut.append.l)
    Error in rbindlist(faut.laut.append.l) : 
    object 'faut.laut.append.l' not found
    

    在一次调用 entrez_summary() 不需要列表的连接。因此,下面的代码可以工作。

    # set search term and retmax
    term_set_small <- 'kadonaga[AUTH]'
    retmax_set <- 500
    # search pubmed using web history
    search_small <- entrez_search(db = "pubmed", term = term_set_small, use_history = T)
    # get summaries from search with <500 hits
    summary_small <- entrez_summary(
        db = "pubmed", 
        web_history = search_small$web_history, 
        retmax = retmax_set
    )
    # extract desired information from esummary, convert to dataframe
    faut.laut_small <- extract_from_esummary(
        esummaries = summary_small,
        elements = c("uid", "sortfirstauthor", "lastauthor"), 
        simplify = F
    )
    faut.laut_small.df <- rbindlist(faut.laut_small)
    

    为什么 附加() 打破夏天,这能避免吗?谢谢。

    1 回复  |  直到 8 年前
        1
  •  1
  •   david w    8 年前

    文件 extract_from_esummary 在这方面有点混乱。它真正需要的是 esummary 对象或 esummary_list . 因为 摘要 对象本身继承自一个列表 摘要 对任何被抛出的列表进行处理。我会修改文档,也许会考虑为对象设计一个更好的设计。

    要解决这个特殊的问题,有一些方法。第一,你可以重新分类摘要列表

    class(summary.append.l) <- c("list", "esummary_list")
    extract_from_esummary(summary.append.l, "sortfirstauthor")
    

    应该会成功的。另一个选择是在执行任何追加操作之前提取相关数据。这和你的例子很相似 lapply 更少 for

    all_the_summs <- lapply(seq(0,50,5),  function(s) {
        entrez_summary(db="pubmed", 
                       web_history=search.l$web_history, 
                       retmax=5,  retstart=s)
    })
    desired_fields <- lapply(all_the_summs, extract_from_esummary, c("uid", "sortfirstauthor", "lastauthor"), simplify=FALSE)  
    res <- do.call(cbind.data.frame, desired_fields)
    

    希望这提供了一条前进的道路。

    推荐文章