我在R中有以下两个数据帧:
> head(gene)
V1 V2 V3
1 chr2 178525989 10
2 chr2 178525990 10
3 chr2 178525991 10
4 chr2 178525992 10
5 chr2 178525993 10
6 chr2 178525994 10
> head(exons)
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 chr2 lrg exonic_part 178807212 178807423 . - . 001
2 chr2 lrg exonic_part 178804552 178804655 . - . 002
3 chr2 lrg exonic_part 178802138 178802341 . - . 003
4 chr2 lrg exonic_part 178800395 178800682 . - . 004
5 chr2 lrg exonic_part 178799825 178799910 . - . 005
6 chr2 lrg exonic_part 178799487 178799731 . - . 006
exons$V4
和exons$V5
中的每一对表示范围的开始和结束。在exons
中有364行,因此有364行。
我需要做的是检查gene$V2
的每个元素,并检查它是否包含在exons
中的任何一个范围内。如果包含它,我需要在另一个向量中添加一个外显子条目,如果它被排除了,我需要在该向量中添加一个“内含子”条目。
因此,例如,如果gene$V2
的前三个元素至少包含在一个范围内,而接下来的三个元素没有包含在内,我希望得到这样的向量:
> include_exclude[1:6]
[1] "exon" "exon" "exon" "intron" "intron" "intron"
目前,我正在使用来自inside.range()
的spatstat.utils
函数来完成此操作,该函数接受一个或多个要检查(x
)和一个范围(r
)的值,并输出TRUE
(如果在范围内)和FALSE
(超出范围)。我将它与循环中的循环一起使用:
include_exclude <- c()
for (i in 1:dim(gene)[1]){
list <- c()
for (x in 1:dim(exons)) {
list <- c(list,inside.range(as.numeric(gene$V2[i]),as.numeric(exons[x,4:5])))
}
if (sum(list) > 0) {include_exclude <- c(include_exclude, "exon")}
else {include_exclude <- c(include_exclude, "intron")}
print(i) #to see how far along the loop is
}
然而,这显然是一种极其低效的做法。我怀疑在R中使用一个apply
函数的效率要高得多,但我从来没有真正理解它们,也不知道如何在这种情况下使用它们。有人能帮忙吗?
谢谢!
发布于 2020-06-23 17:55:41
如果您的每个gene$V2
都在exons$V4
到$V5
中的至少一个范围内,这将返回。
sapply(gene$V2, function(v) any(exons$V4 <= v & v <= exons$V5, na.rm = TRUE))
# [1] FALSE FALSE FALSE FALSE FALSE FALSE
很容易将其转换为"exon"
或"intron"
(使用ifelse
、dplyr::if_else
、data.table::fifelse
或仅使用向量查找),如
rets <- sapply(gene$V2, function(v) any(exons$V4 <= v & v <= exons$V5, na.rm = TRUE))
ifelse(rets, "exon", "intron")
# [1] "intron" "intron" "intron" "intron" "intron" "intron"
c("intron", "exon")[ 1 + rets ]
# [1] "intron" "intron" "intron" "intron" "intron" "intron"
发布于 2020-06-23 17:54:10
您可以使用V4
和V5
列创建一个序列,并检查gene$V2
中是否存在该值,并相应地分配该值。
all_sequence <- unique(unlist(Map(`:`, exons$V4, exons$V5)))
gene$include_exclude <- ifelse(gene$V2 %in% all_sequence, 'exon', 'intron')
使用ifelse
很容易理解,但是不需要ifelse
也可以做到这一点。
gene$include_exclude <- c('intron', 'exon')[(gene$V2 %in% all_sequence) + 1]
https://stackoverflow.com/questions/62546334
复制