reodering a GRanges object by its metadata
1
0
Entering edit mode
d r ▴ 150
@d-r-5459
Last seen 6.8 years ago
Israel
Hi there I am looking for a way to reorder a GRanges object using its metadata. Here is a sample of my GRanges object: GRanges with 1122060 ranges and 4 metadata columns: seqnames ranges strand | ID TSS <rle> <iranges> <rle> | <character> <integer> [1] chr1 [66993729, 66993928] + | chr1:66998729-66999824 chr1:66999824-66999824 + 1 [2] chr1 [66993929, 66994128] + | chr1:66998729-66999824 chr1:66999824-66999824 + 1 [3] chr1 [66994129, 66994328] + | chr1:66998729-66999824 chr1:66999824-66999824 + 1 [4] chr1 [66994329, 66994528] + | chr1:66998729-66999824 chr1:66999824-66999824 + 1 [5] chr1 [66994529, 66994728] + | chr1:66998729-66999824 chr1:66999824-66999824 + 1 ... ... ... ... ... ... ... [1122056] chr22 [51226318, 51226517] + | chr22:51221773-51222156 chr22:51222156-51222156 + 29143 [1122057] chr22 [51226518, 51226717] + | chr22:51221773-51222156 chr22:51222156-51222156 + 29143 [1122058] chr22 [51226718, 51226917] + | chr22:51221773-51222156 chr22:51222156-51222156 + 29143 [1122059] chr22 [51226918, 51227117] + | chr22:51221773-51222156 chr22:51222156-51222156 + 29143 [1122060] chr22 [51227118, 51227317] + | chr22:51221773-51222156 chr22:51222156-51222156 + 29143 I want to order it so the ranges will be sorted first by the ID metadata,then by start (ranges with the same value for this metadata are all subsets of a larger range, so they are on the same chromosome). I have implemented that coercing the GRanges into a data frame, ordering its row and then coercing back to GRanges: grdf<-as.data.frame(CPGIs) grdf<-grdf[order(grdf$ID,grdf$start),] CPGIs<-GRanges(seqnames=grdf$seqnames, ranges=IRanges(grdf$start, grdf$end),strand=grdf$strand, ID=grdf$ID, TSS=grdf$TSS, HGNC_symbol=grdf$HGNC_symbol, refseq_symbol=grdf$refseq_symbol) Which works, but is somewhat cumbersome. Is there a way to reorder the Granges object without resorting to a data frame? Thanks in advance Dolev Rahat [[alternative HTML version deleted]]
• 3.2k views
ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 4 months ago
United States
On 02/13/2014 07:02 AM, d r wrote: > Hi there > > I am looking for a way to reorder a GRanges object using its metadata. > Here is a sample of my GRanges object: > > GRanges with 1122060 ranges and 4 metadata columns: > seqnames ranges strand | > ID TSS > <rle> <iranges> <rle> | > <character> <integer> > [1] chr1 [66993729, 66993928] + | > chr1:66998729-66999824 chr1:66999824-66999824 + 1 > [2] chr1 [66993929, 66994128] + | > chr1:66998729-66999824 chr1:66999824-66999824 + 1 > [3] chr1 [66994129, 66994328] + | > chr1:66998729-66999824 chr1:66999824-66999824 + 1 > [4] chr1 [66994329, 66994528] + | > chr1:66998729-66999824 chr1:66999824-66999824 + 1 > [5] chr1 [66994529, 66994728] + | > chr1:66998729-66999824 chr1:66999824-66999824 + 1 > ... ... ... ... ... > ... ... > [1122056] chr22 [51226318, 51226517] + | > chr22:51221773-51222156 chr22:51222156-51222156 + 29143 > [1122057] chr22 [51226518, 51226717] + | > chr22:51221773-51222156 chr22:51222156-51222156 + 29143 > [1122058] chr22 [51226718, 51226917] + | > chr22:51221773-51222156 chr22:51222156-51222156 + 29143 > [1122059] chr22 [51226918, 51227117] + | > chr22:51221773-51222156 chr22:51222156-51222156 + 29143 > [1122060] chr22 [51227118, 51227317] + | > chr22:51221773-51222156 chr22:51222156-51222156 + 29143 > > I want to order it so the ranges will be sorted first by the ID > metadata,then by start (ranges with the same value for this metadata > are all subsets of a larger range, so they are on the same > chromosome). > > I have implemented that coercing the GRanges into a data frame, > ordering its row and then coercing back to GRanges: > > grdf<-as.data.frame(CPGIs) > grdf<-grdf[order(grdf$ID,grdf$start),] > CPGIs<-GRanges(seqnames=grdf$seqnames, ranges=IRanges(grdf$start, > grdf$end),strand=grdf$strand, ID=grdf$ID, TSS=grdf$TSS, > HGNC_symbol=grdf$HGNC_symbol, refseq_symbol=grdf$refseq_symbol) I think you're looking for o = order(CPGIs$ID, start(CBGIs)) CBGIs[o] ? Martin > > > Which works, but is somewhat cumbersome. Is there a way to reorder the > Granges object without resorting to a data frame? > > Thanks in advance > > Dolev Rahat > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
ADD COMMENT
0
Entering edit mode
I was inspired by this to add an experimental (just for fun) "by" argument to sort,GenomicRanges that supports sorting by a formula in the environment of the GRanges. So now you can do: sort(CBGIs, by=~ID+start) Just an experiment; feel free to get rid of it. On Thu, Feb 13, 2014 at 10:14 AM, Martin Morgan <mtmorgan@fhcrc.org> wrote: > On 02/13/2014 07:02 AM, d r wrote: > >> Hi there >> >> I am looking for a way to reorder a GRanges object using its metadata. >> Here is a sample of my GRanges object: >> >> GRanges with 1122060 ranges and 4 metadata columns: >> seqnames ranges strand | >> ID TSS >> <rle> <iranges> <rle> | >> <character> <integer> >> [1] chr1 [66993729, 66993928] + | >> chr1:66998729-66999824 chr1:66999824-66999824 + 1 >> [2] chr1 [66993929, 66994128] + | >> chr1:66998729-66999824 chr1:66999824-66999824 + 1 >> [3] chr1 [66994129, 66994328] + | >> chr1:66998729-66999824 chr1:66999824-66999824 + 1 >> [4] chr1 [66994329, 66994528] + | >> chr1:66998729-66999824 chr1:66999824-66999824 + 1 >> [5] chr1 [66994529, 66994728] + | >> chr1:66998729-66999824 chr1:66999824-66999824 + 1 >> ... ... ... ... ... >> ... ... >> [1122056] chr22 [51226318, 51226517] + | >> chr22:51221773-51222156 chr22:51222156-51222156 + 29143 >> [1122057] chr22 [51226518, 51226717] + | >> chr22:51221773-51222156 chr22:51222156-51222156 + 29143 >> [1122058] chr22 [51226718, 51226917] + | >> chr22:51221773-51222156 chr22:51222156-51222156 + 29143 >> [1122059] chr22 [51226918, 51227117] + | >> chr22:51221773-51222156 chr22:51222156-51222156 + 29143 >> [1122060] chr22 [51227118, 51227317] + | >> chr22:51221773-51222156 chr22:51222156-51222156 + 29143 >> >> I want to order it so the ranges will be sorted first by the ID >> metadata,then by start (ranges with the same value for this metadata >> are all subsets of a larger range, so they are on the same >> chromosome). >> >> I have implemented that coercing the GRanges into a data frame, >> ordering its row and then coercing back to GRanges: >> >> grdf<-as.data.frame(CPGIs) >> grdf<-grdf[order(grdf$ID,grdf$start),] >> CPGIs<-GRanges(seqnames=grdf$seqnames, ranges=IRanges(grdf$start, >> grdf$end),strand=grdf$strand, ID=grdf$ID, TSS=grdf$TSS, >> HGNC_symbol=grdf$HGNC_symbol, refseq_symbol=grdf$refseq_symbol) >> > > I think you're looking for > > o = order(CPGIs$ID, start(CBGIs)) > CBGIs[o] > > ? > > Martin > > > >> >> Which works, but is somewhat cumbersome. Is there a way to reorder the >> Granges object without resorting to a data frame? >> >> Thanks in advance >> >> Dolev Rahat >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor@r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: http://news.gmane.org/gmane. >> science.biology.informatics.conductor >> >> > > -- > Computational Biology / Fred Hutchinson Cancer Research Center > 1100 Fairview Ave. N. > PO Box 19024 Seattle, WA 98109 > > Location: Arnold Building M1 B861 > Phone: (206) 667-2793 > > > _______________________________________________ > Bioconductor mailing list > Bioconductor@r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane. > science.biology.informatics.conductor > [[alternative HTML version deleted]]
ADD REPLY

Login before adding your answer.

Traffic: 862 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6