我有两个数据帧。第一个数据框是遗传变异,它们的标识符及其在染色体上的位置的列表。第二个是基因列表,其中每行中的列指定基因在染色体上的起始和终止位置。
我想看看哪些遗传变异属于由start_20和stop_20 cols表示的基因'范围'。遗传变异可能落入超过1个基因的范围内。例如,这里snp“rs1”将映射到基因A和基因B。
这是我到目前为止所尝试的:
基因范围
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 )
sns的df及其位置
chromosome<-c("1", "2")
snp<-c("rs1", "rs2")
position<-c("3", "5")
snplist=data.frame(chromosome,snp,position,stringsAsFactors=F)
目的是通过碱基对位置将snps与基因匹配(即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)
任何提示都将不胜感激!谢谢。
答案 0 :(得分:1)
我相信你的问题出在For循环中的which语句中。因为如果你添加as.numeric(),这条线将按预期工作。试试这个,genelist.bychrome[[as.numeric(snplist[i,"chromosome"]) ]]
但总的来说,我建议将数字向量定义为数值数据类型,除非你有理由不这样做,比如你的染色体向量可以定义为c(1,1,2)而不是c(“1 ”, “1”, “2”)。每当您使用定义为字符串的数字数据时,也可以使用as.numeric()
,例如在引用列表索引,比较操作等时。
让我知道这是否有效。
答案 1 :(得分:0)
更新:通过删除第一个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