【问题标题】:How do I remove suffix from a list of Ensembl IDs in R [duplicate]如何从 R 中的 Ensembl ID 列表中删除后缀 [重复]
【发布时间】:2019-09-03 20:26:29
【问题描述】:

我有一个很大的列表,其中包含来自许多细胞系的表达基因。 Ensembl 基因通常带有版本后缀,但我需要删除它们。我找到了几个描述此herehere 的参考资料,但它们对我不起作用,可能是因为我的数据结构(我认为它是列表中的嵌套数组?)。有人可以帮助我了解代码的细节以及我对自己的数据结构的理解吗?

这是一些示例数据

>listOfGenes_version <- list("cellLine1" = c("ENSG001.1", "ENSG002.1", "ENSG003.1"), "cellLine2" = c("ENSG003.1", "ENSG004.1"))

>listOfGenes_version
$cellLine1
[1] "ENSG001.1" "ENSG002.1" "ENSG003.1"

$cellLine2
[1] "ENSG003.1" "ENSG004.1"

而我想看到的是

>listOfGenes_trimmed
$cellLine1
[1] "ENSG001" "ENSG002" "ENSG003"

$cellLine2
[1] "ENSG003" "ENSG004"

以下是我尝试过的一些方法,但没有奏效

>listOfGenes_trimmed <- str_replace(listOfGenes_version, pattern = ".[0-9]+$", replacement = "")      
Warning message:
In stri_replace_first_regex(string, pattern, fix_replacement(replacement),  :
  argument is not an atomic vector; coercing  

>listOfGenes_trimmed <- lapply(listOfGenes_version, gsub('\\..*', '', listOfGenes_version))
Error in match.fun(FUN) : 
  'gsub("\\..*", "", listOfGenes_version)' is not a function, character or symbol

非常感谢!

【问题讨论】:

    标签: r list gsub stringr


    【解决方案1】:

    一个选项是将模式指定为.(元字符 - 所以转义),后跟一个或多个数字(\\d+)在字符串的末尾($)并替换为空白(@987654324 @)

    lapply(listOfGenes_version,  sub, pattern = "\\.\\d+$", replacement = "")
    #$cellLine1
    #[1] "ENSG001" "ENSG002" "ENSG003"
    
    #$cellLine2
    #[1] "ENSG003" "ENSG004"
    

    . 是一个匹配任何字符的元字符,因此我们需要对其进行转义以获取字面值,因为模式默认为 regex

    【讨论】:

      猜你喜欢
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多