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    X 0.07108186
## 2        A    E 0.93870653
## 3        A    B 0.71333377
## 4        A    S 0.37408545
## 5        A    W 0.53505048
## 6        A    E 0.45203886

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 
## 2463 2590 2470 2477

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

catB <- d2$B
head(catB)
##   category var2      var3
## 1        B    N 0.7342055
## 2        B    T 0.2264286
## 3        B    E 0.6063523
## 4        B    E 0.4317652
## 5        B    L 0.9638400
## 6        B    H 0.5743209

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':
## 
##     anyDuplicated, aperm, append, as.data.frame, basename, cbind,
##     colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
##     get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
##     match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     Position, rank, rbind, Reduce, 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':
## 
##     expand.grid, I, 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      * |         a
##     [2]        B       2-3      * |         e
##     [3]        B       3-4      * |         e
##     [4]        A       4-5      * |         d
##     [5]        C       5-6      * |         c
##     ...      ...       ...    ... .       ...
##   [196]        A   196-197      * |         e
##   [197]        A   197-198      * |         c
##   [198]        C   198-199      * |         a
##   [199]        C   199-200      * |         d
##   [200]        B   200-201      * |         c
##   -------
##   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] "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      * |         a
##   [2]        A       4-5      * |         d
##   [3]        A       6-7      * |         c
##   [4]        A      9-10      * |         e
##   [5]        A     13-14      * |         b
##   [6]        A     17-18      * |         a
##   -------
##   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.4.0 beta (2024-04-15 r86425)
## 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_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [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.56.0 GenomeInfoDb_1.40.0  IRanges_2.38.0      
## [4] S4Vectors_0.42.0     BiocGenerics_0.50.0  fstcore_0.9.18      
## [7] scanMiRApp_1.10.0    scanMiR_1.10.0       BiocStyle_2.32.0    
## 
## loaded via a namespace (and not attached):
##   [1] jsonlite_1.8.8              magrittr_2.0.3             
##   [3] shinyjqui_0.4.1             GenomicFeatures_1.56.0     
##   [5] rmarkdown_2.26              BiocIO_1.14.0              
##   [7] zlibbioc_1.50.0             vctrs_0.6.5                
##   [9] memoise_2.0.1               Rsamtools_2.20.0           
##  [11] RCurl_1.98-1.14             htmltools_0.5.8.1          
##  [13] S4Arrays_1.4.0              progress_1.2.3             
##  [15] AnnotationHub_3.12.0        curl_5.2.1                 
##  [17] SparseArray_1.4.0           sass_0.4.9                 
##  [19] bslib_0.7.0                 htmlwidgets_1.6.4          
##  [21] httr2_1.0.1                 plotly_4.10.4              
##  [23] cachem_1.0.8                GenomicAlignments_1.40.0   
##  [25] mime_0.12                   lifecycle_1.0.4            
##  [27] pkgconfig_2.0.3             Matrix_1.7-0               
##  [29] R6_2.5.1                    fastmap_1.1.1              
##  [31] GenomeInfoDbData_1.2.12     MatrixGenerics_1.16.0      
##  [33] shiny_1.8.1.1               digest_0.6.35              
##  [35] colorspace_2.1-0            AnnotationDbi_1.66.0       
##  [37] shinycssloaders_1.0.0       RSQLite_2.3.6              
##  [39] seqLogo_1.70.0              filelock_1.0.3             
##  [41] fansi_1.0.6                 httr_1.4.7                 
##  [43] abind_1.4-5                 compiler_4.4.0             
##  [45] bit64_4.0.5                 BiocParallel_1.38.0        
##  [47] DBI_1.2.2                   biomaRt_2.60.0             
##  [49] rappdirs_0.3.3              DelayedArray_0.30.0        
##  [51] waiter_0.2.5                rjson_0.2.21               
##  [53] tools_4.4.0                 httpuv_1.6.15              
##  [55] fst_0.9.8                   glue_1.7.0                 
##  [57] restfulr_0.0.15             promises_1.3.0             
##  [59] grid_4.4.0                  generics_0.1.3             
##  [61] gtable_0.3.5                tidyr_1.3.1                
##  [63] ensembldb_2.28.0            data.table_1.15.4          
##  [65] hms_1.1.3                   xml2_1.3.6                 
##  [67] utf8_1.2.4                  XVector_0.44.0             
##  [69] stringr_1.5.1               BiocVersion_3.19.1         
##  [71] pillar_1.9.0                later_1.3.2                
##  [73] rintrojs_0.3.4              dplyr_1.1.4                
##  [75] BiocFileCache_2.12.0        lattice_0.22-6             
##  [77] rtracklayer_1.64.0          bit_4.0.5                  
##  [79] tidyselect_1.2.1            Biostrings_2.72.0          
##  [81] knitr_1.46                  bookdown_0.39              
##  [83] ProtGenerics_1.36.0         SummarizedExperiment_1.34.0
##  [85] xfun_0.43                   shinydashboard_0.7.2       
##  [87] Biobase_2.64.0              matrixStats_1.3.0          
##  [89] DT_0.33                     stringi_1.8.3              
##  [91] UCSC.utils_1.0.0            lazyeval_0.2.2             
##  [93] yaml_2.3.8                  evaluate_0.23              
##  [95] codetools_0.2-20            tibble_3.2.1               
##  [97] BiocManager_1.30.22         cli_3.6.2                  
##  [99] xtable_1.8-4                munsell_0.5.1              
## [101] jquerylib_0.1.4             Rcpp_1.0.12                
## [103] dbplyr_2.5.0                png_0.1-8                  
## [105] XML_3.99-0.16.1             parallel_4.4.0             
## [107] ggplot2_3.5.1               blob_1.2.4                 
## [109] prettyunits_1.2.0           AnnotationFilter_1.28.0    
## [111] bitops_1.0-7                pwalign_1.0.0              
## [113] txdbmaker_1.0.0             viridisLite_0.4.2          
## [115] scales_1.3.0                scanMiRData_1.9.0          
## [117] purrr_1.0.2                 crayon_1.5.2               
## [119] rlang_1.1.3                 cowplot_1.1.3              
## [121] KEGGREST_1.44.0