Contents

1 IndexedFst

The IndexedFst class provides fast named random access to indexed fst files. It is based on the fst package, which provides fast random reading of data frames. This is particularly useful to manipulate large collections of binding sites without loading them all in memory.

Creating an indexed fst file from a data.frame is very simple:

library(scanMiRApp)
# we create a temporary directory in which the files will be saved
tmp <- tempdir()
f <- file.path(tmp, "test")
# we create a dummy data.frame
d <- data.frame( category=sample(LETTERS[1:4], 10000, replace=TRUE),
                 var2=sample(LETTERS, 10000, replace=TRUE),
                 var3=runif(10000) )

saveIndexedFst(d, index.by="category", file.prefix=f)

The file can then be loaded (without having all the data in memory) in the following way:

d2 <- loadIndexedFst(f)
class(d2)
## [1] "IndexedFst"
## attr(,"package")
## [1] "scanMiRApp"
summary(d2)
## <fst file>
## 10000 rows, 3 columns (test.fst)
## 
## * 'category': character
## * 'var2'    : character
## * 'var3'    : double

We can see that d2 is considerably smaller than the original d:

format(object.size(d),units="Kb")
## [1] "237 Kb"
format(object.size(d2),units="Kb")
## [1] "2.4 Kb"

Nevertheless, a number of functions can be used normally on the object:

nrow(d2)
## [1] 10000
ncol(d2)
## [1] 3
colnames(d2)
## [1] "category" "var2"     "var3"
head(d2)
##   category var2       var3
## 1        A    N 0.00406786
## 2        A    T 0.08341514
## 3        A    T 0.84870076
## 4        A    K 0.85914634
## 5        A    B 0.81178248
## 6        A    N 0.14172419

In addition, the object can be accessed as a list (using the indexed variable). Since in this case the file is indexed using the category column, the different categories can be accessed as names of the object:

names(d2)
## [1] "A" "B" "C" "D"
lengths(d2)
##    A    B    C    D 
## 2502 2487 2505 2506

We can read specifically the rows pertaining to one category using:

catB <- d2$B
head(catB)
##   category var2      var3
## 1        B    S 0.7022208
## 2        B    K 0.9728924
## 3        B    I 0.1390059
## 4        B    B 0.6130365
## 5        B    M 0.7992307
## 6        B    C 0.2756233

2 Storing GRanges as IndexedFst

In addition to data.frames, GRanges can be saved as indexed Fst. To demonstrate this, we first create a dummy GRanges object:

library(GenomicRanges)
## Loading required package: stats4
## Loading required package: BiocGenerics
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
##     as.data.frame, basename, cbind, colnames, dirname, do.call,
##     duplicated, eval, evalq, get, grep, grepl, intersect, is.unsorted,
##     lapply, mapply, match, mget, order, paste, pmax, pmax.int, pmin,
##     pmin.int, rank, rbind, rownames, sapply, setdiff, sort, table,
##     tapply, union, unique, unsplit, which.max, which.min
## Loading required package: S4Vectors
## 
## Attaching package: 'S4Vectors'
## The following objects are masked from 'package:base':
## 
##     I, expand.grid, unname
## Loading required package: IRanges
## Loading required package: GenomeInfoDb
gr <- GRanges(sample(LETTERS[1:3],200,replace=TRUE), IRanges(seq_len(200), width=2))
gr$propertyA <- factor(sample(letters[1:5],200,replace=TRUE))
gr
## GRanges object with 200 ranges and 1 metadata column:
##         seqnames    ranges strand | propertyA
##            <Rle> <IRanges>  <Rle> |  <factor>
##     [1]        C       1-2      * |         a
##     [2]        C       2-3      * |         a
##     [3]        C       3-4      * |         a
##     [4]        A       4-5      * |         d
##     [5]        C       5-6      * |         b
##     ...      ...       ...    ... .       ...
##   [196]        A   196-197      * |         d
##   [197]        B   197-198      * |         a
##   [198]        A   198-199      * |         e
##   [199]        A   199-200      * |         b
##   [200]        B   200-201      * |         e
##   -------
##   seqinfo: 3 sequences from an unspecified genome; no seqlengths

Again the file can then be loaded (without having all the data in memory) in the following way:

f2 <- file.path(tmp, "test2")
saveIndexedFst(gr, index.by="seqnames", file.prefix=f2)
d1 <- loadIndexedFst(f2)
names(d1)
## [1] "C" "A" "B"
head(d1$A)
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand | propertyA
##          <Rle> <IRanges>  <Rle> |  <factor>
##   [1]        A       4-5      * |         d
##   [2]        A       7-8      * |         b
##   [3]        A      9-10      * |         d
##   [4]        A     10-11      * |         b
##   [5]        A     15-16      * |         e
##   [6]        A     16-17      * |         d
##   -------
##   seqinfo: 3 sequences from an unspecified genome; no seqlengths

Similarly, we could index using a different column:

saveIndexedFst(gr, index.by="propertyA", file.prefix=f2)
d2 <- loadIndexedFst(f2)
names(d2)
## [1] "a" "b" "c" "d" "e"

3 More…

3.1 Multithreading

The fst package supports multithreaded reading and writing. This can also be applied for IndexedFst, using the nthreads argument of loadIndexedFst and saveIndexedFst.

3.2 Under the hood

The IndexedFst class is simply a wrapper around the fst package. In addition to the fst file, an rds file is saved containing the index data. For example, for our last example, the following files have been saved:

list.files(tmp, "test2")
## [1] "test2.fst"     "test2.idx.rds"

Either file (or the prefix) can be used for loading, but both files need to have the same prefix.



Session info

## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] GenomicRanges_1.50.0 GenomeInfoDb_1.34.0  IRanges_2.32.0      
## [4] S4Vectors_0.36.0     BiocGenerics_0.44.0  fstcore_0.9.12      
## [7] scanMiRApp_1.4.0     BiocStyle_2.26.0    
## 
## loaded via a namespace (and not attached):
##   [1] colorspace_2.0-3              rjson_0.2.21                 
##   [3] ellipsis_0.3.2                XVector_0.38.0               
##   [5] DT_0.26                       bit64_4.0.5                  
##   [7] interactiveDisplayBase_1.36.0 AnnotationDbi_1.60.0         
##   [9] fansi_1.0.3                   scanMiR_1.4.0                
##  [11] xml2_1.3.3                    codetools_0.2-18             
##  [13] cachem_1.0.6                  knitr_1.40                   
##  [15] jsonlite_1.8.3                Rsamtools_2.14.0             
##  [17] scanMiRData_1.3.0             dbplyr_2.2.1                 
##  [19] png_0.1-7                     shinydashboard_0.7.2         
##  [21] shiny_1.7.3                   BiocManager_1.30.19          
##  [23] compiler_4.2.1                httr_1.4.4                   
##  [25] assertthat_0.2.1              Matrix_1.5-1                 
##  [27] fastmap_1.1.0                 lazyeval_0.2.2               
##  [29] cli_3.4.1                     later_1.3.0                  
##  [31] htmltools_0.5.3               prettyunits_1.1.1            
##  [33] tools_4.2.1                   gtable_0.3.1                 
##  [35] glue_1.6.2                    GenomeInfoDbData_1.2.9       
##  [37] dplyr_1.0.10                  rappdirs_0.3.3               
##  [39] Rcpp_1.0.9                    Biobase_2.58.0               
##  [41] jquerylib_0.1.4               vctrs_0.5.0                  
##  [43] Biostrings_2.66.0             shinyjqui_0.4.1              
##  [45] rtracklayer_1.58.0            rintrojs_0.3.2               
##  [47] ggseqlogo_0.1                 xfun_0.34                    
##  [49] stringr_1.4.1                 mime_0.12                    
##  [51] lifecycle_1.0.3               restfulr_0.0.15              
##  [53] ensembldb_2.22.0              shinycssloaders_1.0.0        
##  [55] XML_3.99-0.12                 waiter_0.2.5                 
##  [57] AnnotationHub_3.6.0           zlibbioc_1.44.0              
##  [59] scales_1.2.1                  ProtGenerics_1.30.0          
##  [61] hms_1.1.2                     promises_1.2.0.1             
##  [63] MatrixGenerics_1.10.0         parallel_4.2.1               
##  [65] SummarizedExperiment_1.28.0   AnnotationFilter_1.22.0      
##  [67] yaml_2.3.6                    curl_4.3.3                   
##  [69] memoise_2.0.1                 ggplot2_3.3.6                
##  [71] sass_0.4.2                    biomaRt_2.54.0               
##  [73] stringi_1.7.8                 RSQLite_2.2.18               
##  [75] BiocVersion_3.16.0            BiocIO_1.8.0                 
##  [77] GenomicFeatures_1.50.0        filelock_1.0.2               
##  [79] BiocParallel_1.32.0           rlang_1.0.6                  
##  [81] pkgconfig_2.0.3               bitops_1.0-7                 
##  [83] matrixStats_0.62.0            evaluate_0.17                
##  [85] lattice_0.20-45               purrr_0.3.5                  
##  [87] GenomicAlignments_1.34.0      htmlwidgets_1.5.4            
##  [89] cowplot_1.1.1                 bit_4.0.4                    
##  [91] tidyselect_1.2.0              magrittr_2.0.3               
##  [93] bookdown_0.29                 R6_2.5.1                     
##  [95] generics_0.1.3                DelayedArray_0.24.0          
##  [97] DBI_1.1.3                     pillar_1.8.1                 
##  [99] KEGGREST_1.38.0               RCurl_1.98-1.9               
## [101] tibble_3.1.8                  crayon_1.5.2                 
## [103] utf8_1.2.2                    plotly_4.10.0                
## [105] BiocFileCache_2.6.0           rmarkdown_2.17               
## [107] progress_1.2.2                grid_4.2.1                   
## [109] data.table_1.14.4             blob_1.2.3                   
## [111] digest_0.6.30                 xtable_1.8-4                 
## [113] tidyr_1.2.1                   httpuv_1.6.6                 
## [115] fst_0.9.8                     munsell_0.5.0                
## [117] viridisLite_0.4.1             bslib_0.4.0