# The IndexedFst class

Pierre-Luc Germain

#### 18 May 2026

#### Abstract

Indexed wrapper for fst objects, enabling indexed fast random access to on-disk data.frames or GRanges

#### Package

scanMiRApp 1.19.0

# 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.5 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    O 0.89621558
    ## 2        A    Q 0.30836866
    ## 3        A    J 0.60303788
    ## 4        A    E 0.62568532
    ## 5        A    B 0.31029333
    ## 6        A    Q 0.09265434

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
    ## 2402 2483 2531 2584

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

    catB <- d2$B
    head(catB)

    ##   category var2      var3
    ## 1        B    G 0.8106230
    ## 2        B    C 0.9947604
    ## 3        B    A 0.6912962
    ## 4        B    A 0.4787958
    ## 5        B    P 0.8848703
    ## 6        B    W 0.1856249

# 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

    ## 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: Seqinfo

    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      * |         e
    ##     [2]        B       2-3      * |         a
    ##     [3]        C       3-4      * |         c
    ##     [4]        B       4-5      * |         b
    ##     [5]        B       5-6      * |         a
    ##     ...      ...       ...    ... .       ...
    ##   [196]        A   196-197      * |         d
    ##   [197]        C   197-198      * |         d
    ##   [198]        B   198-199      * |         c
    ##   [199]        C   199-200      * |         a
    ##   [200]        A   200-201      * |         a
    ##   -------
    ##   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" "B" "A"

    head(d1$A)

    ## GRanges object with 6 ranges and 1 metadata column:
    ##       seqnames    ranges strand | propertyA
    ##          <Rle> <IRanges>  <Rle> |  <factor>
    ##   [1]        A       7-8      * |         c
    ##   [2]        A      9-10      * |         b
    ##   [3]        A     10-11      * |         c
    ##   [4]        A     11-12      * |         e
    ##   [5]        A     17-18      * |         a
    ##   [6]        A     21-22      * |         b
    ##   -------
    ##   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.6.0 Patched (2026-05-01 r89994)
    ## Platform: aarch64-apple-darwin23
    ## Running under: macOS Tahoe 26.3.1
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] GenomicRanges_1.65.0 Seqinfo_1.3.0        IRanges_2.47.1
    ##  [4] S4Vectors_0.51.2     BiocGenerics_0.59.2  generics_0.1.4
    ##  [7] fstcore_0.10.0       scanMiRApp_1.19.0    scanMiR_1.19.0
    ## [10] BiocStyle_2.41.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] RColorBrewer_1.1-3          jsonlite_2.0.0
    ##   [3] magrittr_2.0.5              shinyjqui_0.4.1
    ##   [5] GenomicFeatures_1.65.0      farver_2.1.2
    ##   [7] rmarkdown_2.31              BiocIO_1.23.3
    ##   [9] vctrs_0.7.3                 memoise_2.0.1
    ##  [11] Rsamtools_2.29.0            RCurl_1.98-1.18
    ##  [13] htmltools_0.5.9             S4Arrays_1.13.0
    ##  [15] BiocBaseUtils_1.15.1        progress_1.2.3
    ##  [17] AnnotationHub_4.3.0         curl_7.1.0
    ##  [19] SparseArray_1.13.2          sass_0.4.10
    ##  [21] bslib_0.11.0                htmlwidgets_1.6.4
    ##  [23] httr2_1.2.2                 plotly_4.12.0
    ##  [25] cachem_1.1.0                GenomicAlignments_1.49.0
    ##  [27] mime_0.13                   lifecycle_1.0.5
    ##  [29] pkgconfig_2.0.3             Matrix_1.7-5
    ##  [31] R6_2.6.1                    fastmap_1.2.0
    ##  [33] MatrixGenerics_1.25.0       shiny_1.13.0
    ##  [35] digest_0.6.39               AnnotationDbi_1.75.0
    ##  [37] shinycssloaders_1.1.0       RSQLite_3.52.0
    ##  [39] seqLogo_1.79.0              filelock_1.0.3
    ##  [41] httr_1.4.8                  abind_1.4-8
    ##  [43] compiler_4.6.0              bit64_4.8.0
    ##  [45] S7_0.2.2                    BiocParallel_1.47.0
    ##  [47] DBI_1.3.0                   biomaRt_2.69.0
    ##  [49] rappdirs_0.3.4              DelayedArray_0.39.2
    ##  [51] waiter_0.2.5.1              rjson_0.2.23
    ##  [53] tools_4.6.0                 otel_0.2.0
    ##  [55] httpuv_1.6.17               fst_0.9.8
    ##  [57] glue_1.8.1                  restfulr_0.0.16
    ##  [59] promises_1.5.0              grid_4.6.0
    ##  [61] gtable_0.3.6                tidyr_1.3.2
    ##  [63] ensembldb_2.37.0            hms_1.1.4
    ##  [65] data.table_1.18.4           XVector_0.53.0
    ##  [67] stringr_1.6.0               BiocVersion_3.24.0
    ##  [69] pillar_1.11.1               later_1.4.8
    ##  [71] rintrojs_0.3.4              dplyr_1.2.1
    ##  [73] BiocFileCache_3.3.0         lattice_0.22-9
    ##  [75] rtracklayer_1.73.0          bit_4.6.0
    ##  [77] tidyselect_1.2.1            Biostrings_2.81.1
    ##  [79] knitr_1.51                  bookdown_0.46
    ##  [81] ProtGenerics_1.45.0         SummarizedExperiment_1.43.0
    ##  [83] xfun_0.57                   shinydashboard_0.7.3
    ##  [85] Biobase_2.73.1              matrixStats_1.5.0
    ##  [87] DT_0.34.0                   stringi_1.8.7
    ##  [89] UCSC.utils_1.9.0            lazyeval_0.2.3
    ##  [91] yaml_2.3.12                 evaluate_1.0.5
    ##  [93] codetools_0.2-20            cigarillo_1.3.0
    ##  [95] tibble_3.3.1                BiocManager_1.30.27
    ##  [97] cli_3.6.6                   xtable_1.8-8
    ##  [99] jquerylib_0.1.4             dichromat_2.0-0.1
    ## [101] Rcpp_1.1.1-1.1              GenomeInfoDb_1.49.0
    ## [103] dbplyr_2.5.2                png_0.1-9
    ## [105] XML_3.99-0.23               parallel_4.6.0
    ## [107] ggplot2_4.0.3               blob_1.3.0
    ## [109] prettyunits_1.2.0           AnnotationFilter_1.37.0
    ## [111] bitops_1.0-9                pwalign_1.9.0
    ## [113] txdbmaker_1.9.0             viridisLite_0.4.3
    ## [115] scales_1.4.0                scanMiRData_1.19.0
    ## [117] purrr_1.2.2                 crayon_1.5.3
    ## [119] rlang_1.2.0                 cowplot_1.2.0
    ## [121] KEGGREST_1.53.0