在GRanges对象上循环时获取错误(eval(expr,envir,enclos)中的错误:尝试应用非函数)

时间:2022-12-24 19:37:14

I have two dat sets: one as a GRanges object made by GRangesList with multiple subsets (grl) and another one (Test). I would like to loop over all subsets of grl and use function findOverlaps in "GenomicRanges"library to find their overlap with Test and save data (only one value after each iteration) in a list.

我有两个数据集:一个是由GRangesList制作的GRanges对象,具有多个子集(grl)和另一个(Test)。我想循环遍历grl的所有子集并使用“GenomicRanges”库中的函数findOverlaps来查找它们与Test的重叠并在列表中保存数据(每次迭代后只有一个值)。

the output should look like:

输出应该如下:

gr1   2
gr2   5
gr3   1


gr1 <-
  GRanges(seqnames = "chr2", ranges = IRanges(3, 6),
          strand = "+", score = 5L, GC = 0.45)
gr2 <-
  GRanges(seqnames = c("chr1", "chr1"),
          ranges = IRanges(c(7,13), width = 3),
          strand = c("+", "-"), score = 3:4, GC = c(0.3, 0.5))
gr3 <-
  GRanges(seqnames = c("chr1", "chr2"),
          ranges = IRanges(c(1, 4), c(3, 9)),
          strand = c("-", "-"), score = c(6L, 2L), GC = c(0.4, 0.1))
grl <- GRangesList("gr1" = gr1, "gr2" = gr2, "gr3" = gr3)
grl


Test <-
    GRanges(seqnames = c("chr1", "chr1"),
          ranges = IRanges(c(1, 5), c(2, 6)),
          strand = c("-", "-"), score = c(6L, 2L), GC = c(0.5, 0.2))


myFunction <- function(input,feature){

    tmp = list()

        for (f in 1:length(objects(feature))){

            mtch = findOverlaps(currmySegm, eval(parse(text=paste0("feature$", objects(feature[f]), sep = "")))))
            **some calculations**
            value <- mean(...)  
            }
        temp[[objects(feature[f])]] <- value
}

myFunction(Test,grl)

Error in eval(expr, envir, enclos) : attempt to apply non-function

I think there is either something wrong with eval(parse(text=paste0("feature$", objects(feature[f]), sep = "")))) which I can not pass the subsets of the grl to the function findOverlaps or saving the results as a list

我认为eval有问题(parse(text = paste0(“feature $”,objects(feature [f]),sep =“”)))))我无法将grl的子集传递给函数findOverlaps或将结果保存为列表

1 个解决方案

#1


-1  

Different findOverlaps examples:

不同的findOverlaps示例:

# GRangesList object
gr1 <-
  GRanges(seqnames = "chr2", ranges = IRanges(3, 6),
          strand = "+", score = 5L, GC = 0.45)
gr2 <-
  GRanges(seqnames = c("chr1", "chr1"),
          ranges = IRanges(c(7,13), width = 3),
          strand = c("+", "-"), score = 3:4, GC = c(0.3, 0.5))
gr3 <-
  GRanges(seqnames = c("chr1", "chr2"),
          ranges = IRanges(c(1, 4), c(3, 9)),
          strand = c("-", "-"), score = c(6L, 2L), GC = c(0.4, 0.1))
grl <- GRangesList("gr1" = gr1, "gr2" = gr2, "gr3" = gr3)


# GRanges object
Test <-
    GRanges(seqnames = c("chr1", "chr1"),
          ranges = IRanges(c(1, 5), c(2, 6)),
          strand = c("-", "-"), score = c(6L, 2L), GC = c(0.5, 0.2))

# Get overlaps at the GRanges entry level of a GRangesList object
# This extracts the overlapping GRanges entries from your grl
# GRangesList object that overlap with a feature from Test.
hits1 <- lapply(grl, function(x) findOverlaps(x, Test));
grl.overlapping <- list();
for (i in 1:length(hits1)) { 
    grl.overlapping[[i]] <- grl[[i]][queryHits(hits1[[i]])];
}
names(grl.overlapping) <- names(grl);
print(grl.overlapping);

# Count the number of overlapping GRanges objects for every
# list entry from grl
counts <- lapply(hits1, length);
print(counts);

# Get overlaps at the GRangesList level
# Note this will only tell you whether *any* GRanges entries from your
# grl GRangesList object overlap with a feature from Test; it won't 
hits2 <- findOverlaps(grl, Test);
print(grl[queryHits(hits2)]);

#1


-1  

Different findOverlaps examples:

不同的findOverlaps示例:

# GRangesList object
gr1 <-
  GRanges(seqnames = "chr2", ranges = IRanges(3, 6),
          strand = "+", score = 5L, GC = 0.45)
gr2 <-
  GRanges(seqnames = c("chr1", "chr1"),
          ranges = IRanges(c(7,13), width = 3),
          strand = c("+", "-"), score = 3:4, GC = c(0.3, 0.5))
gr3 <-
  GRanges(seqnames = c("chr1", "chr2"),
          ranges = IRanges(c(1, 4), c(3, 9)),
          strand = c("-", "-"), score = c(6L, 2L), GC = c(0.4, 0.1))
grl <- GRangesList("gr1" = gr1, "gr2" = gr2, "gr3" = gr3)


# GRanges object
Test <-
    GRanges(seqnames = c("chr1", "chr1"),
          ranges = IRanges(c(1, 5), c(2, 6)),
          strand = c("-", "-"), score = c(6L, 2L), GC = c(0.5, 0.2))

# Get overlaps at the GRanges entry level of a GRangesList object
# This extracts the overlapping GRanges entries from your grl
# GRangesList object that overlap with a feature from Test.
hits1 <- lapply(grl, function(x) findOverlaps(x, Test));
grl.overlapping <- list();
for (i in 1:length(hits1)) { 
    grl.overlapping[[i]] <- grl[[i]][queryHits(hits1[[i]])];
}
names(grl.overlapping) <- names(grl);
print(grl.overlapping);

# Count the number of overlapping GRanges objects for every
# list entry from grl
counts <- lapply(hits1, length);
print(counts);

# Get overlaps at the GRangesList level
# Note this will only tell you whether *any* GRanges entries from your
# grl GRangesList object overlap with a feature from Test; it won't 
hits2 <- findOverlaps(grl, Test);
print(grl[queryHits(hits2)]);