gl.gen2fbm: Converts a gen-backed genlight object to a file-backed (FBM)...

View source: R/gl.gen2fbm.r

gl.gen2fbmR Documentation

Converts a gen-backed genlight object to a file-backed (FBM) one

Description

'gl.gen2fbm()' converts a 'dartR'/'genlight' object that stores genotypes in the standard list-of-'SNPbin' format ('@gen') into an 'FBM.code256' stored in the '@fbm' slot (and clears '@gen', enforcing XOR).

The genotypes are copied in **blocks of loci** read directly from the packed bits of the 'SNPbin' objects, so the full genotype matrix is never decoded in memory. Each value is held as one byte while it is copied. The block size follows the bigstatsr option bigstatsr.block.sizeGB (default 1): a block holds as many loci as bigstatsr::block_size(nInd(x)), and the peak memory use of the conversion is roughly that option in Gb. Lower it (e.g. options(bigstatsr.block.sizeGB = 0.1)) to convert with less memory.

Only diploid SNP data (genotypes 0, 1, 2 or missing) can be stored. The FBM uses the value 3 for a missing genotype, so data with higher dosages (e.g. polyploids) are refused with an error instead of being silently turned into missing values.

**Saving.** The FBM holds only a reference to its backing file, which by default is a temporary file deleted at the end of the R session. Save a file-backed object with gl.save (and reload it with gl.load), never with saveRDS(): saveRDS() stores only the reference, not the genotypes. After saving, check that the file size makes sense for the number of individuals and loci (a file of a few Kb for thousands of loci means the genotypes were not saved).

Usage

gl.gen2fbm(
  x,
  code = bigsnpr::CODE_012,
  backingfile = NULL,
  chunk = NULL,
  verbose = NULL
)

Arguments

x

A **'dartR'** (preferred) or **'genlight'** object that currently has genotype data only in '@gen' (i.e., no FBM yet). A plain 'genlight' is converted to 'dartR' first. Only SNP data are supported.

code

A 'bigsnpr' code mapping for 'FBM.code256'. Defaults to 'bigsnpr::CODE_012' (0/1/2 with 'NA' support).

backingfile

File stem for the FBM backing file. If NULL, a temporary file in the folder getOption("FBM.dir") (see utils.fbm.tempfile). A backing file named here is never removed automatically [default NULL].

chunk

Deprecated. Number of loci per block; if given, it replaces the block size derived from bigstatsr.block.sizeGB (rounded up to a multiple of 8). Kept for compatibility [default NULL].

verbose

Verbosity: 0, silent or fatal errors; 1, begin and end; 2, progress log; 3, progress and results summary; 5, full report [default NULL, adopting the global verbosity set by gl.set.verbosity(), or 2 if no global is set].

Value

A **'dartR'** object with '@fbm' populated and '@gen' emptied. An object that is already FBM-backed is returned unchanged.

Author(s)

Author(s): Luis Mijangos. Custodian: Luis Mijangos – Post to https://groups.google.com/d/forum/dartr

See Also

gl.fbm2gen

Other data manipulation: gl.add.indmetrics(), gl.define.pop(), gl.drop.ind(), gl.drop.loc(), gl.drop.pop(), gl.edit.recode.pop(), gl.fbm.cleanup(), gl.fbm2gen(), gl.impute(), gl.join(), gl.keep.ind(), gl.keep.loc(), gl.keep.pop(), gl.make.recode.ind(), gl.make.recode.pop(), gl.merge.pop(), gl.reassign.ind(), gl.reassign.pop(), gl.recode.ind(), gl.recode.pop(), gl.rename.pop(), gl.sample(), gl.sim.genotypes(), gl.sort(), gl.subsample.ind(), gl.subsample.loc(), gl.subsample.loci()

Examples

x <- gl.gen2fbm(testset.gl)
nInd(x); nLoc(x)            # dimensions via FBM
as.matrix(x)[1:5, 1:5]      # decodes from FBM

dartR.base documentation built on Nov. 18, 2026, 9:06 a.m.