RLElist to GRanges

library(GenomicRanges)
## Loading required package: BiocGenerics
## Loading required package: parallel
## 
## Attaching package: 'BiocGenerics'
## 
## The following objects are masked from 'package:parallel':
## 
##     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
##     clusterExport, clusterMap, parApply, parCapply, parLapply,
##     parLapplyLB, parRapply, parSapply, parSapplyLB
## 
## The following object is masked from 'package:stats':
## 
##     xtabs
## 
## The following objects are masked from 'package:base':
## 
##     Filter, Find, Map, Position, Reduce, anyDuplicated, append,
##     as.data.frame, as.vector, cbind, colnames, do.call,
##     duplicated, eval, evalq, get, intersect, is.unsorted, lapply,
##     mapply, match, mget, order, paste, pmax, pmax.int, pmin,
##     pmin.int, rank, rbind, rep.int, rownames, sapply, setdiff,
##     sort, table, tapply, union, unique, unlist, unsplit
## 
## Loading required package: S4Vectors
## Loading required package: stats4
## Creating a generic function for 'nchar' from package 'base' in package 'S4Vectors'
## Loading required package: IRanges
## Loading required package: GenomeInfoDb
example(GRanges)
## 
## GRangs> seqinfo <- Seqinfo(paste0("chr", 1:3), c(1000, 2000, 1500), NA, "mock1")
## 
## GRangs> gr <-
## GRangs+   GRanges(seqnames =
## GRangs+           Rle(c("chr1", "chr2", "chr1", "chr3"), c(1, 3, 2, 4)),
## GRangs+           ranges = IRanges(
## GRangs+             1:10, width = 10:1, names = head(letters,10)),
## GRangs+           strand = Rle(
## GRangs+             strand(c("-", "+", "*", "+", "-")),
## GRangs+             c(1, 2, 2, 3, 2)),
## GRangs+           score = 1:10,
## GRangs+           GC = seq(1, 0, length=10),
## GRangs+           seqinfo=seqinfo)
## 
## GRangs> gr
## GRanges object with 10 ranges and 2 metadata columns:
##     seqnames    ranges strand |     score                GC
##        <Rle> <IRanges>  <Rle> | <integer>         <numeric>
##   a     chr1  [ 1, 10]      - |         1                 1
##   b     chr2  [ 2, 10]      + |         2 0.888888888888889
##   c     chr2  [ 3, 10]      + |         3 0.777777777777778
##   d     chr2  [ 4, 10]      * |         4 0.666666666666667
##   e     chr1  [ 5, 10]      * |         5 0.555555555555556
##   f     chr1  [ 6, 10]      + |         6 0.444444444444444
##   g     chr3  [ 7, 10]      + |         7 0.333333333333333
##   h     chr3  [ 8, 10]      + |         8 0.222222222222222
##   i     chr3  [ 9, 10]      - |         9 0.111111111111111
##   j     chr3  [10, 10]      - |        10                 0
##   -------
##   seqinfo: 3 sequences from mock1 genome
## 
## GRangs> ## Summarizing elements
## GRangs> table(seqnames(gr))
## 
## chr1 chr2 chr3 
##    3    3    4 
## 
## GRangs> sum(width(gr))
## [1] 55
## 
## GRangs> summary(mcols(gr)[,"score"])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    3.25    5.50    5.50    7.75   10.00 
## 
## GRangs> ## Renaming the underlying sequences
## GRangs> seqlevels(gr)
## [1] "chr1" "chr2" "chr3"
## 
## GRangs> seqlevels(gr) <- sub("chr", "Chrom", seqlevels(gr))
## 
## GRangs> gr
## GRanges object with 10 ranges and 2 metadata columns:
##     seqnames    ranges strand |     score                GC
##        <Rle> <IRanges>  <Rle> | <integer>         <numeric>
##   a   Chrom1  [ 1, 10]      - |         1                 1
##   b   Chrom2  [ 2, 10]      + |         2 0.888888888888889
##   c   Chrom2  [ 3, 10]      + |         3 0.777777777777778
##   d   Chrom2  [ 4, 10]      * |         4 0.666666666666667
##   e   Chrom1  [ 5, 10]      * |         5 0.555555555555556
##   f   Chrom1  [ 6, 10]      + |         6 0.444444444444444
##   g   Chrom3  [ 7, 10]      + |         7 0.333333333333333
##   h   Chrom3  [ 8, 10]      + |         8 0.222222222222222
##   i   Chrom3  [ 9, 10]      - |         9 0.111111111111111
##   j   Chrom3  [10, 10]      - |        10                 0
##   -------
##   seqinfo: 3 sequences from mock1 genome
## 
## GRangs> seqlevels(gr) <- sub("Chrom", "chr", seqlevels(gr)) # revert
## 
## GRangs> ## Combining objects
## GRangs> gr2 <- GRanges(seqnames=Rle(c('chr1', 'chr2', 'chr3'), c(3, 3, 4)),
## GRangs+                IRanges(1:10, width=5), strand='-',
## GRangs+                score=101:110, GC = runif(10),
## GRangs+                seqinfo=seqinfo)
## 
## GRangs> gr3 <- GRanges(seqnames=Rle(c('chr1', 'chr2', 'chr3'), c(3, 4, 3)),
## GRangs+                IRanges(101:110, width=10), strand='-',
## GRangs+                score=21:30,
## GRangs+                seqinfo=seqinfo)
## 
## GRangs> some.gr <- c(gr, gr2)
## 
## GRangs> ## all.gr <- c(gr, gr2, gr3) ## (This would fail)
## GRangs> all.gr <- c(gr, gr2, gr3, ignore.mcols=TRUE)
## 
## GRangs> ## Subsetting a named list-like object *by* a GRanges subscript
## GRangs> x <- RleList(chr1=101:120, chr2=2:-8, chr3=31:40)
## 
## GRangs> x[gr]
## RleList of length 10
## $chr1
## integer-Rle of length 10 with 10 runs
##   Lengths:   1   1   1   1   1   1   1   1   1   1
##   Values : 101 102 103 104 105 106 107 108 109 110
## 
## $chr2
## integer-Rle of length 9 with 9 runs
##   Lengths:  1  1  1  1  1  1  1  1  1
##   Values :  1  0 -1 -2 -3 -4 -5 -6 -7
## 
## $chr2
## integer-Rle of length 8 with 8 runs
##   Lengths:  1  1  1  1  1  1  1  1
##   Values :  0 -1 -2 -3 -4 -5 -6 -7
## 
## $chr2
## integer-Rle of length 7 with 7 runs
##   Lengths:  1  1  1  1  1  1  1
##   Values : -1 -2 -3 -4 -5 -6 -7
## 
## $chr1
## integer-Rle of length 6 with 6 runs
##   Lengths:   1   1   1   1   1   1
##   Values : 105 106 107 108 109 110
## 
## ...
## <5 more elements>
## 
## GRangs> ## The number of lines displayed in the 'show' method
## GRangs> ## are controlled with two global options.
## GRangs> longGR <- c(gr[,"score"], gr2[,"score"], gr3)
## 
## GRangs> longGR
## GRanges object with 30 ranges and 1 metadata column:
##       seqnames     ranges strand   |     score
##          <Rle>  <IRanges>  <Rle>   | <integer>
##     a     chr1    [1, 10]      -   |         1
##     b     chr2    [2, 10]      +   |         2
##     c     chr2    [3, 10]      +   |         3
##     d     chr2    [4, 10]      *   |         4
##     e     chr1    [5, 10]      *   |         5
##   ...      ...        ...    ... ...       ...
##           chr2 [106, 115]      -   |        26
##           chr2 [107, 116]      -   |        27
##           chr3 [108, 117]      -   |        28
##           chr3 [109, 118]      -   |        29
##           chr3 [110, 119]      -   |        30
##   -------
##   seqinfo: 3 sequences from mock1 genome
## 
## GRangs> options("showHeadLines"=7)
## 
## GRangs> options("showTailLines"=2)
## 
## GRangs> longGR
## GRanges object with 30 ranges and 1 metadata column:
##       seqnames     ranges strand   |     score
##          <Rle>  <IRanges>  <Rle>   | <integer>
##     a     chr1    [1, 10]      -   |         1
##     b     chr2    [2, 10]      +   |         2
##     c     chr2    [3, 10]      +   |         3
##     d     chr2    [4, 10]      *   |         4
##     e     chr1    [5, 10]      *   |         5
##     f     chr1    [6, 10]      +   |         6
##     g     chr3    [7, 10]      +   |         7
##   ...      ...        ...    ... ...       ...
##           chr3 [109, 118]      -   |        29
##           chr3 [110, 119]      -   |        30
##   -------
##   seqinfo: 3 sequences from mock1 genome
## 
## GRangs> ## Revert to default values
## GRangs> options("showHeadLines"=NULL)
## 
## GRangs> options("showTailLines"=NULL)
mycov <- coverage(longGR, weight=score(longGR))
rle.i <- slice(mycov, lower=1)
gr <- as (rle.i, "GRanges")
gr
## GRanges object with 39 ranges and 2 metadata columns:
##        seqnames     ranges strand   |     score      view
##           <Rle>  <IRanges>  <Rle>   | <integer> <integer>
##    [1]     chr1     [1, 1]      *   |       102         1
##    [2]     chr1     [2, 2]      *   |       204         1
##    [3]     chr1     [3, 4]      *   |       307         1
##    [4]     chr1     [5, 5]      *   |       312         1
##    [5]     chr1     [6, 6]      *   |       217         1
##    ...      ...        ...    ... ...       ...       ...
##   [35]     chr3 [108, 108]      *   |        28         2
##   [36]     chr3 [109, 109]      *   |        57         2
##   [37]     chr3 [110, 117]      *   |        87         2
##   [38]     chr3 [118, 118]      *   |        59         2
##   [39]     chr3 [119, 119]      *   |        30         2
##   -------
##   seqinfo: 3 sequences from an unspecified genome; no seqlengths

Leave a Reply

  

  

  

%d 博主赞过: