The IndexedFst class

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    C 0.6858121
## 2        A    M 0.2796231
## 3        A    H 0.8063033
## 4        A    A 0.1947903
## 5        A    D 0.5580516
## 6        A    X 0.3292102

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 
## 2542 2486 2470 2502

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

catB <- d2$B
head(catB)
##   category var2       var3
## 1        B    E 0.37763935
## 2        B    H 0.47184643
## 3        B    L 0.89081450
## 4        B    O 0.08550839
## 5        B    T 0.92876504
## 6        B    A 0.30703750

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
## Loading required package: generics
## 
## Attaching package: 'generics'
## The following objects are masked from 'package:base':
## 
##     as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
##     setequal, union
## 
## 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, is.unsorted, lapply,
##     mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     rank, rbind, rownames, sapply, saveRDS, table, tapply, 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]        B       1-2      * |         b
##     [2]        C       2-3      * |         d
##     [3]        B       3-4      * |         b
##     [4]        A       4-5      * |         b
##     [5]        C       5-6      * |         d
##     ...      ...       ...    ... .       ...
##   [196]        B   196-197      * |         c
##   [197]        A   197-198      * |         d
##   [198]        A   198-199      * |         b
##   [199]        A   199-200      * |         e
##   [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] "B" "C" "A"
head(d1$A)
## GRanges object with 6 ranges and 1 metadata column:
##       seqnames    ranges strand | propertyA
##          <Rle> <IRanges>  <Rle> |  <factor>
##   [1]        A       4-5      * |         b
##   [2]        A     11-12      * |         d
##   [3]        A     13-14      * |         a
##   [4]        A     19-20      * |         c
##   [5]        A     21-22      * |         a
##   [6]        A     24-25      * |         e
##   -------
##   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"

More…

Multithreading

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

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.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        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: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] GenomicRanges_1.59.1 GenomeInfoDb_1.43.1  IRanges_2.41.1      
##  [4] S4Vectors_0.45.2     BiocGenerics_0.53.3  generics_0.1.3      
##  [7] fstcore_0.9.18       scanMiRApp_1.13.0    scanMiR_1.13.0      
## [10] BiocStyle_2.35.0    
## 
## loaded via a namespace (and not attached):
##   [1] sys_3.4.3                   jsonlite_1.8.9             
##   [3] magrittr_2.0.3              shinyjqui_0.4.1            
##   [5] GenomicFeatures_1.59.1      rmarkdown_2.29             
##   [7] BiocIO_1.17.0               zlibbioc_1.52.0            
##   [9] vctrs_0.6.5                 memoise_2.0.1              
##  [11] Rsamtools_2.23.0            RCurl_1.98-1.16            
##  [13] htmltools_0.5.8.1           S4Arrays_1.7.1             
##  [15] progress_1.2.3              AnnotationHub_3.15.0       
##  [17] curl_6.0.1                  SparseArray_1.7.2          
##  [19] sass_0.4.9                  bslib_0.8.0                
##  [21] htmlwidgets_1.6.4           httr2_1.0.6                
##  [23] plotly_4.10.4               cachem_1.1.0               
##  [25] buildtools_1.0.0            GenomicAlignments_1.43.0   
##  [27] mime_0.12                   lifecycle_1.0.4            
##  [29] pkgconfig_2.0.3             Matrix_1.7-1               
##  [31] R6_2.5.1                    fastmap_1.2.0              
##  [33] GenomeInfoDbData_1.2.13     MatrixGenerics_1.19.0      
##  [35] shiny_1.9.1                 digest_0.6.37              
##  [37] colorspace_2.1-1            AnnotationDbi_1.69.0       
##  [39] shinycssloaders_1.1.0       RSQLite_2.3.8              
##  [41] seqLogo_1.73.0              filelock_1.0.3             
##  [43] fansi_1.0.6                 httr_1.4.7                 
##  [45] abind_1.4-8                 compiler_4.4.2             
##  [47] bit64_4.5.2                 BiocParallel_1.41.0        
##  [49] DBI_1.2.3                   biomaRt_2.63.0             
##  [51] rappdirs_0.3.3              DelayedArray_0.33.2        
##  [53] waiter_0.2.5                rjson_0.2.23               
##  [55] tools_4.4.2                 httpuv_1.6.15              
##  [57] fst_0.9.8                   glue_1.8.0                 
##  [59] restfulr_0.0.15             promises_1.3.0             
##  [61] grid_4.4.2                  gtable_0.3.6               
##  [63] tidyr_1.3.1                 ensembldb_2.31.0           
##  [65] data.table_1.16.2           hms_1.1.3                  
##  [67] xml2_1.3.6                  utf8_1.2.4                 
##  [69] XVector_0.47.0              stringr_1.5.1              
##  [71] BiocVersion_3.21.1          pillar_1.9.0               
##  [73] later_1.3.2                 rintrojs_0.3.4             
##  [75] dplyr_1.1.4                 BiocFileCache_2.15.0       
##  [77] lattice_0.22-6              rtracklayer_1.67.0         
##  [79] bit_4.5.0                   tidyselect_1.2.1           
##  [81] maketools_1.3.1             Biostrings_2.75.1          
##  [83] knitr_1.49                  ProtGenerics_1.39.0        
##  [85] SummarizedExperiment_1.37.0 xfun_0.49                  
##  [87] shinydashboard_0.7.2        Biobase_2.67.0             
##  [89] matrixStats_1.4.1           DT_0.33                    
##  [91] stringi_1.8.4               UCSC.utils_1.3.0           
##  [93] lazyeval_0.2.2              yaml_2.3.10                
##  [95] evaluate_1.0.1              codetools_0.2-20           
##  [97] tibble_3.2.1                BiocManager_1.30.25        
##  [99] cli_3.6.3                   xtable_1.8-4               
## [101] munsell_0.5.1               jquerylib_0.1.4            
## [103] Rcpp_1.0.13-1               dbplyr_2.5.0               
## [105] png_0.1-8                   XML_3.99-0.17              
## [107] parallel_4.4.2              ggplot2_3.5.1              
## [109] blob_1.2.4                  prettyunits_1.2.0          
## [111] AnnotationFilter_1.31.0     bitops_1.0-9               
## [113] pwalign_1.3.0               txdbmaker_1.3.0            
## [115] viridisLite_0.4.2           scales_1.3.0               
## [117] scanMiRData_1.12.0          purrr_1.0.2                
## [119] crayon_1.5.3                rlang_1.1.4                
## [121] cowplot_1.1.3               KEGGREST_1.47.0