Skip to content
Prev 10772 / 21312 Next

[Bioc-devel] Iterating over BSgenomeViews returns DNAString instead of BSgenomeViews

Hi Pariksheet,

This is the expected behavior.

Some background: BSgenomeViews are list-like objects where the *list
elements* (i.e. the elements one extracts with [[) are the DNA
sequences from the views:

   > views
   BSgenomeViews object with 2 views and 0 metadata columns:
         seqnames         ranges strand                       dna
            <Rle>      <IRanges>  <Rle>            <DNAStringSet>
     [1]     chr1 [25001, 28000]      * [GCTTCAGCCT...TTATTTATTG]
     [2]     chr2 [30001, 31000]      * [GACCCTCCTG...AGCAGGTGGT]
     -------
     seqinfo: 93 sequences (1 circular) from hg19 genome

   > views[[1]]
     3000-letter "DNAString" instance
   seq: 
GCTTCAGCCTGCACAGATAGGGGAGTAGGGGACAGA...CTGTCGTCTGAATTCCAAGCTTTTTGTTATTTATTG
   > views[[2]]
     1000-letter "DNAString" instance
   seq: 
GACCCTCCTGTTGGCTTTTTTACAAGACTGCTATGT...TGCTGGGACAGTCATGGGCAAACCAGAGCAGGTGGT

When subsetting with [ I get:

   > views[1]
   BSgenomeViews object with 1 view and 0 metadata columns:
         seqnames         ranges strand                       dna
            <Rle>      <IRanges>  <Rle>            <DNAStringSet>
     [1]     chr1 [25001, 28000]      * [GCTTCAGCCT...TTATTTATTG]
     -------
     seqinfo: 93 sequences (1 circular) from hg19 genome

   > views[2]
   BSgenomeViews object with 1 view and 0 metadata columns:
         seqnames         ranges strand                       dna
            <Rle>      <IRanges>  <Rle>            <DNAStringSet>
     [1]     chr2 [30001, 31000]      * [GACCCTCCTG...AGCAGGTGGT]
     -------
     seqinfo: 93 sequences (1 circular) from hg19 genome

The important difference is that with [[ I get a DNAString object
(the content of the view) and with [ I get a BSgenomeViews object
of length 1.

The semantic of lapply() is to always use [[ internally to extract
elements. This is why 'lapply(views, class)' returns "DNAString".
If you want to operate on the length-one Views objects, you need to
do:

   lapply(seq_along(views), function(i) { do something on views[i] })

Hope this helps,
H.
On 04/05/2017 08:13 PM, Pariksheet Nanda wrote: