Question: about appending GRANGES
0
gravatar for Bogdan
4.7 years ago by
Bogdan570
Palo Alto, CA, USA
Bogdan570 wrote:

Hi ; I would appreciate a piece of advice on the following ;

I would like to append a GRange object "interactions" that is initially EMPTY ;

interactions <- append(interactions, interactions_chr[k])

how do I initialize a GRange object "initeractions" that is EMPTY at the very beginning ?

 

thanks ,

 

bogdan

 

granges • 1.7k views
ADD COMMENTlink modified 4.7 years ago • written 4.7 years ago by Bogdan570
Answer: about appending GRANGES
1
gravatar for Hervé Pagès
4.7 years ago by
Hervé Pagès ♦♦ 14k
United States
Hervé Pagès ♦♦ 14k wrote:

Hi Bodgan,

You create an empty GRanges object by calling GRanges().

Note that growing an object in a loop with something like

interactions <- GRanges()
for (i in seq_len(N)) {
    gr <- ... make some GRanges object ...    
    interactions <- append(interactions, gr)
}

is generally very inefficient and should be avoided.

If you know in advance how many iterations you're going to do, it is a lot more efficient to do:

interactions <- vector(mode="list", length=N)
for (i in seq_len(N)) {
    gr <- ... make some GRanges object ...   
    interactions[[i]] <- gr
}
interactions <- do.call(c, interactions)

Alternatively, you can replace the for loop by a call to lapply() which is more elegant but does basically the same.

H.

ADD COMMENTlink modified 4.7 years ago • written 4.7 years ago by Hervé Pagès ♦♦ 14k
Answer: about appending GRANGES
0
gravatar for Bogdan
4.7 years ago by
Bogdan570
Palo Alto, CA, USA
Bogdan570 wrote:

Dear Herve,

many thanks for your reply: it is very helpful. happy weekend !

 

-- bogdan

ADD COMMENTlink written 4.7 years ago by Bogdan570
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 282 users visited in the last hour