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)
## Loading required package: scanMiR
# 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.3774359
## 2        A    V 0.4191099
## 3        A    R 0.5728043
## 4        A    A 0.4558261
## 5        A    C 0.3823825
## 6        A    F 0.5088926

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 
## 2507 2487 2517 2489

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

catB <- d2$B
head(catB)
##   category var2      var3
## 1        B    I 0.2758966
## 2        B    S 0.3219729
## 3        B    F 0.8349981
## 4        B    P 0.9125072
## 5        B    O 0.0603726
## 6        B    M 0.2857095

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, table, tapply,
##     union, unique, unsplit, which.max, which.min
## Loading required package: S4Vectors
## 
## Attaching package: 'S4Vectors'
## The following object is masked from 'package:utils':
## 
##     findMatches
## 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]        A       1-2      * |         c
##     [2]        B       2-3      * |         e
##     [3]        C       3-4      * |         a
##     [4]        A       4-5      * |         c
##     [5]        B       5-6      * |         d
##     ...      ...       ...    ... .       ...
##   [196]        C   196-197      * |         d
##   [197]        C   197-198      * |         b
##   [198]        B   198-199      * |         d
##   [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)
## Warning in as.data.frame.factor(col, optional = optional): Direct call of
## 'as.data.frame.factor()' is deprecated.  Use 'as.data.frame.vector()' or
## 'as.data.frame()' instead
d1 <- loadIndexedFst(f2)
names(d1)
## [1] "A" "B" "C"
head(d1$A)
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand | propertyA
##          <Rle> <IRanges>  <Rle> |  <factor>
##   [1]        A       1-2      * |         c
##   [2]        A       4-5      * |         c
##   [3]        A     12-13      * |         e
##   [4]        A     14-15      * |         c
##   [5]        A     15-16      * |         e
##   [6]        A     19-20      * |         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)
## Warning in as.data.frame.factor(col, optional = optional): Direct call of
## 'as.data.frame.factor()' is deprecated.  Use 'as.data.frame.vector()' or
## 'as.data.frame()' instead
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 Under development (unstable) (2024-03-06 r86056)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## 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       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] GenomicRanges_1.55.3 GenomeInfoDb_1.39.9  IRanges_2.37.1      
## [4] S4Vectors_0.41.4     BiocGenerics_0.49.1  fstcore_0.9.18      
## [7] scanMiRApp_1.9.3     scanMiR_1.9.3        BiocStyle_2.31.0    
## 
## loaded via a namespace (and not attached):
##   [1] DBI_1.2.2                   bitops_1.0-7               
##   [3] rlang_1.1.3                 magrittr_2.0.3             
##   [5] shinydashboard_0.7.2        matrixStats_1.2.0          
##   [7] compiler_4.4.0              RSQLite_2.3.5              
##   [9] GenomicFeatures_1.55.4      png_0.1-8                  
##  [11] vctrs_0.6.5                 ProtGenerics_1.35.4        
##  [13] pkgconfig_2.0.3             crayon_1.5.2               
##  [15] fastmap_1.1.1               dbplyr_2.4.0               
##  [17] XVector_0.43.1              ellipsis_0.3.2             
##  [19] utf8_1.2.4                  shinyjqui_0.4.1            
##  [21] Rsamtools_2.19.3            promises_1.2.1             
##  [23] rmarkdown_2.26              scanMiRData_1.9.0          
##  [25] purrr_1.0.2                 bit_4.0.5                  
##  [27] xfun_0.42                   zlibbioc_1.49.3            
##  [29] cachem_1.0.8                jsonlite_1.8.8             
##  [31] blob_1.2.4                  later_1.3.2                
##  [33] DelayedArray_0.29.9         BiocParallel_1.37.1        
##  [35] parallel_4.4.0              R6_2.5.1                   
##  [37] bslib_0.6.1                 stringi_1.8.3              
##  [39] rtracklayer_1.63.1          jquerylib_0.1.4            
##  [41] Rcpp_1.0.12                 bookdown_0.38              
##  [43] SummarizedExperiment_1.33.3 knitr_1.45                 
##  [45] httpuv_1.6.14               Matrix_1.6-5               
##  [47] tidyselect_1.2.1            abind_1.4-5                
##  [49] yaml_2.3.8                  codetools_0.2-19           
##  [51] curl_5.2.1                  lattice_0.22-5             
##  [53] tibble_3.2.1                Biobase_2.63.0             
##  [55] shiny_1.8.0                 KEGGREST_1.43.0            
##  [57] evaluate_0.23               BiocFileCache_2.11.1       
##  [59] Biostrings_2.71.4           shinycssloaders_1.0.0      
##  [61] pillar_1.9.0                BiocManager_1.30.22        
##  [63] filelock_1.0.3              MatrixGenerics_1.15.0      
##  [65] DT_0.32                     plotly_4.10.4              
##  [67] generics_0.1.3              RCurl_1.98-1.14            
##  [69] ensembldb_2.27.1            BiocVersion_3.19.1         
##  [71] ggplot2_3.5.0               munsell_0.5.0              
##  [73] scales_1.3.0                xtable_1.8-4               
##  [75] glue_1.7.0                  lazyeval_0.2.2             
##  [77] seqLogo_1.69.0              tools_4.4.0                
##  [79] AnnotationHub_3.11.1        BiocIO_1.13.0              
##  [81] data.table_1.15.2           GenomicAlignments_1.39.4   
##  [83] XML_3.99-0.16.1             cowplot_1.1.3              
##  [85] grid_4.4.0                  tidyr_1.3.1                
##  [87] AnnotationDbi_1.65.2        colorspace_2.1-0           
##  [89] GenomeInfoDbData_1.2.11     restfulr_0.0.15            
##  [91] cli_3.6.2                   rappdirs_0.3.3             
##  [93] fst_0.9.8                   fansi_1.0.6                
##  [95] viridisLite_0.4.2           S4Arrays_1.3.6             
##  [97] dplyr_1.1.4                 AnnotationFilter_1.27.0    
##  [99] gtable_0.3.4                rintrojs_0.3.4             
## [101] sass_0.4.8                  digest_0.6.35              
## [103] SparseArray_1.3.4           waiter_0.2.5               
## [105] rjson_0.2.21                htmlwidgets_1.6.4          
## [107] memoise_2.0.1               htmltools_0.5.7            
## [109] lifecycle_1.0.4             httr_1.4.7                 
## [111] mime_0.12                   bit64_4.0.5