【问题标题】:Issues using which in R for loop [duplicate]在 R for 循环中使用 which 的问题 [重复]
【发布时间】:2016-08-20 15:55:07
【问题描述】:

我有两个数据框。第一个数据框是遗传变异列表、它们的标识符以及它们在染色体上的位置。第二个是基因列表,其中每行中的列指定基因在染色体上的开始和停止位置。

我想查看哪些遗传变异属于由 start_20 和 stop_20 列表示的基因“范围”。一个遗传变异可能属于超过 1 个基因的范围。例如,这里的 snp "rs1" 将映射到基因 A 和基因 B。

这是我迄今为止尝试过的:

基因范围的df

chromosome<-c("1", "1", "2")
start_20<-c("1", "1", "5")  
stop_20<-c("4", "4", "6")  
gene<-c("A", "B", "C") 

genelist=data.frame(chromosome, start_20,  stop_20, gene,stringsAsFactors=F  )

snps的df及其位置

chromosome<-c("1", "2")
snp<-c("rs1", "rs2")
position<-c("3", "5") 

snplist=data.frame(chromosome,snp,position,stringsAsFactors=F)

目的是通过碱基对位置将 snp 与基因匹配(即 snp 1 的位置为“3”,这意味着它映射到基因 A 和基因 B)。

genelist.bychrome <- vector("list", 2)

按染色体排列的基因列表。

for(i in 1:2) genelist.bychrome[[i]] <- genelist[genelist[,"chromosome"]==i,]  

长度为 nrow(snplist) 的空容器 找到匹配的基因就放在这里

gene.matched <- rep("",nrow(snplist))

gene.matched<-as.list(gene.matched)

#looping across each observation in snplist

    for(i in 1:nrow(snplist)){

# snplist[i,"chromosome"] is the chromosome of interest
# Because of consecutive ordering genelist.bychrome[[3]] gives the genelist       for chromosome 3

 Therefore, genelist.bychrome[[ snplist[i,"chromosome"] ]] gives the genelist for the chromosome of interest

 VERY IMPORTANT: get.gene gives the index in genelist.bychrome[[     snplist[i,"chromosome"] ]], NOT genelist

        if(snplist[i,"chromosome"] <= 1){ 

                get.gene<- which((genelist.bychrome[[ snplist[i,"chromosome"] ]][,"stop_20"] >= snplist[i,"position"])  &    

            # get matching list element of genelist.bychrome 
            # in this element collect indices for rows where stop position is greater than the postion of the snp and
            # start position is less than the position of the snp
            # this should collect multiple rows for some snps   
            # dump the gene for this index in the matching element of gene.matched 
            # i.e get.gene<- which(genelist.bychrome[[1]]  [,"stop_20"] >= snplist[1,3])  & (genelist.bychrome[[1]]  [,"start_20"] <= snplist[1,3])
            # gene.matched <- genelist.bychrome[[1]][get.gene,"gene"]


                    ( genelist.bychrome[[ snplist[i,"chromosome"] ]][,"start_20"] <= snplist[i,"position"])) # correct                  
                        if(length(get.gene)!=0) gene.matched[i]<- genelist.bychrome[[ snplist[i,"chromosome"] ]][get.gene,"gene"]
                             } 

                                    } # end for()

#bind the matched genes to the snplist
    snplist.new <- cbind(snplist,gene.matched)

任何提示将不胜感激!谢谢。

【问题讨论】:

  • 您说,“snp 1 的位置为 '3',这意味着它映射到基因 7 和 8”。你怎么知道位置 3 映射到 7 和 8?
  • 您好,抱歉,我刚刚意识到这是我的问题中的一个错误(我写了一个新版本以使其更直观)。基因 7 和 8 现在应​​该读取基因 A 和基因 B。

标签: r bioinformatics


【解决方案1】:

我相信您的问题出在 For 循环内的 which 语句中。因为如果您添加 as.numeric(),这条线将按预期工作。试试这个,genelist.bychrome[[as.numeric(snplist[i,"chromosome"]) ]]

但总的来说,我建议将数字向量定义为数字数据类型,除非您有其他理由,例如您的染色体向量可以定义为 c(1,1,2) 而不是 c("1 ","1","2")。并且在处理定义为字符串的数字数据时也可以使用as.numeric(),例如在引用列表索引、比较操作等时。

让我知道这是否有效。

【讨论】:

  • 感谢您的帮助!我会确保我记得在未来这样做。但是,现在循环中的向量不匹配,因为我收到“要替换的项目数不是替换长度的倍数”消息。鉴于一个 snp 可能匹配多个基因,我希望gene.matched 是长度为 'snplist' 的字符向量列表,如下所示:gene.matched
【解决方案2】:

更新:通过删除第一个 if 语句、按照建议转换向量“as.numeric”并在开始时制作一个没有预定长度的空列表(我猜这可能并不总是一个好主意)解决了这个问题)。

谢谢!

#make data frames

     genelist = data.frame(chromosome=c(1,1,2),start_20=c(1, 1, 5), stop_20=c(4, 4, 6), gene=c("A", "B", "C"), stringsAsFactors=F)

     snplist=data.frame(chromosome=c(1,2),snp=c("rs1", "rs2"),position=c(3,5),stringsAsFactors=F)

 #objective is to get genes per snp

    genelist.bychrome <- vector("list", 2)
    for(i in 1:2) genelist.bychrome[[i]] <-   genelist[genelist[,"chromosome"]==i,]  

    gene.matched <- list()

 #looping across each observation in snplist

    for(i in 1:nrow(snplist)){

                get.gene<-  which((genelist.bychrome[[as.numeric(snplist[i,"chromosome"]) ]] [,"stop_20"] >=  snplist[i,"position"])  &                         
                    (genelist.bychrome[[as.numeric(snplist[i,"chromosome"]) ]] [,"start_20"] <= snplist[i,"position"]))

                        if(length(get.gene)!=0) gene.matched[[i]]<- genelist.bychrome[[ snplist[i,"chromosome"] ]][get.gene,"gene"]

                            }                   

    names(gene.matched)=snplist$snp

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多