This is a test version of Biostars. For the public version, visit https://www.biostars.org.
nested loop to overlap two genomic ranges lists?

Hi all,

I want to create a matrix that lists number of overlaps between two lists. So the rows would be one list and the columns would be the other.

this is what I have to far:

for (i in length(list.1)) { 
    for (j in length(list.2)) {
     table <- table(countOverlaps(list.1[[i]], list.2[[j]], type='any', maxgap=0)) }}

but I am only getting one overlap

I have tried:

x <- lapply(list.1, function(x,y) {table(countOverlaps(x,y,type="any", maxgap=0))}, list.2)

but the numbers do not match when I do an individual test

any clues?

thanks in advance

r granges

Shouldn't it be for(i in c(1:length(list.1)) {?

yes! I forgot to add the 1 in

1 answer

if anyone is coming from google, i forgot to build a matrix and assign the rows and columns

mymatrix <- matrix(nrow=length(list.1), ncol=length(list.2))

for (i in 1:length(list1)) { 
    for (j in 1:length(list.2)) {
    tmp <- table(countOverlaps(query=list.1[[i]], subject=list.2[[j]], type='any', maxgap=0)) 
    if("0" %in% names(tmp)) {
        mymatrix[i,j] <- sum(tmp[2:length(tmp)])
        } else mamatrix[i,j] <- sum(tmp)
        rm(tmp)
    }}

Log in to answer this question.