| Title: | Fast and Memory-Efficient Manipulation and Conversion of VCF Files |
| Version: | 1.0.0 |
| Description: | Reads Variant Call Format (VCF) files into an S4 object whose genotypes are stored on disk as an 'Apache Arrow' dataset and loaded lazily, allowing large datasets to be processed with little memory. Provides functions to filter variants and samples, subset and merge datasets, compute summary statistics and diagnostic plots, write VCF files, and convert data to input formats of population genetic and phylogenetic software such as 'STRUCTURE', 'ADMIXTURE', 'PLINK', 'EIGENSTRAT', 'Arlequin', 'GENEPOP', 'BayeScan', 'TreeMix', 'SNAPP' and 'adegenet'. |
| URL: | https://legallab.github.io/VCFArrow/, https://github.com/legalLab/VCFArrow |
| BugReports: | https://github.com/legalLab/VCFArrow/issues |
| Language: | en-US |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| Depends: | R (≥ 4.1.0) |
| LinkingTo: | Rcpp |
| Imports: | Rcpp, arrow (≥ 10.0.0), cli, dplyr, ggplot2, methods, parallel, stats, stringr, tibble, utils, vctrs |
| Suggests: | adegenet, knitr, rmarkdown, svglite |
| VignetteBuilder: | knitr |
| SystemRequirements: | zlib |
| Config/roxygen2/version: | 8.1.1 |
| NeedsCompilation: | yes |
| Packaged: | 2026-11-25 05:05:22 UTC; tomas |
| Author: | Tomas Hrbek |
| Maintainer: | Tomas Hrbek <hrbek@evoamazon.net> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-25 05:50:02 UTC |
VCFArrow: Fast and Memory-Efficient Manipulation and Conversion of VCF Files
Description
Reads Variant Call Format (VCF) files into an S4 object whose genotypes are stored on disk as an 'Apache Arrow' dataset and loaded lazily, allowing large datasets to be processed with little memory. Provides functions to filter variants and samples, subset and merge datasets, compute summary statistics and diagnostic plots, write VCF files, and convert data to input formats of population genetic and phylogenetic software such as 'STRUCTURE', 'ADMIXTURE', 'PLINK', 'EIGENSTRAT', 'Arlequin', 'GENEPOP', 'BayeScan', 'TreeMix', 'SNAPP' and 'adegenet'.
Author(s)
Maintainer: Tomas Hrbek hrbek@evoamazon.net (ORCID) [copyright holder]
Authors:
Tomas Hrbek hrbek@evoamazon.net (ORCID) [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/legalLab/VCFArrow/issues
VCFArrow S4 class
Description
S4 class holding VCF data. Variant metadata are kept in memory while
genotypes are stored on disk as an Apache Arrow dataset and loaded lazily.
Objects are created with read_vcf().
Slots
headerComplete VCF header (character).
infoINFO field of each variant (character).
formatFORMAT field of each variant (data.frame).
variantsCHROM, POS, ID, REF, ALT, QUAL, FILTER and precalculated per-variant metrics (data.frame).
gtArrow dataset of genotypes in long format.
samplesSample names (character).
groupsGroup assignment of each sample (character).
pathLocation of the on-disk Arrow dataset (character).
finalizer_envEnvironment used to clean up
pathon garbage collection.lociCHROM, POS, REF and ALT of every variant of the source VCF, row i describing .row_id i; never filtered, so variants can be identified (e.g. by vcf_bind()) even after they were removed from
variants(data.frame).invariant_removedVariants removed as invariant, with their metadata (data.frame).
Author(s)
Tomas Hrbek April 2026
Subset method for VCFArrow
Description
Subset a VCFArrow object by variants (rows) and samples (columns)
Usage
## S4 method for signature 'VCFArrow'
x[i, j, ..., drop = FALSE]
Arguments
x |
A VCFArrow object |
i |
Variant (row) positions, numeric or logical |
j |
Column indices: numeric, logical, or sample name character vector |
... |
Ignored |
drop |
Ignored; kept for S4 compatibility |
Details
This function is a method of the VCFArrow S4 class Method to subset by row and column of GT
Value
A new VCFArrow object containing the selected variants and samples
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f)
vcf[1:100, 1:5]
assess_vcf_coverage
Description
Quantifying read depth of all samples in VCF
Usage
assess_vcf_coverage(
vcf_arrow,
res_path,
species,
project,
details = TRUE,
max_points_per_sample = 5000L
)
Arguments
vcf_arrow |
-> VCFArrow object |
res_path |
-> path to results (directory for output dataframe and plots) |
species |
-> species name, the plot title (character) |
project |
-> project name / base output file name (character) |
details |
-> flag for adding project name into figure title, default TRUE (Boolean) |
max_points_per_sample |
-> maximum number of SNVs from which to plot coverage, default 5000 (integer) |
Details
This function generates a violin plot of read depths (DP) per sample, from the sample's called genotypes; the DP of missing genotypes is not used. Each violin ends at the sample's lowest and highest plotted depth.
Value
Invisibly returns the input VCFArrow object; called for its side
effect of writing a violin plot of the read depth of each sample to
<project>_coverage.pdf, .svg and .png in res_path.
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
assess_vcf_coverage(vcf, res_path = tempdir(),
species = "Phyllomedusa vaillantii",
project = "vaillantii")
assess_vcf_missing_data
Description
Quantifying missing data of all samples in VCF
Usage
assess_vcf_missing_data(vcf_arrow, res_path, species, project, details = TRUE)
Arguments
vcf_arrow |
-> VCFArrow object |
res_path |
-> path to results (directory for output dataframe and plots) |
species |
-> species name, the plot title (character) |
project |
-> project name / base output file name (character) |
details |
-> flag for adding project name into figure title, default TRUE (Boolean) |
Details
This function generates a table of absolute and relative missing data per sample (columns sample, group, missing_n, missing_p and total_loci, missing_p being the proportion of missing genotypes), and a bar plot of the proportion of missing data per sample.
Value
Invisibly returns the input VCFArrow object; called for its side
effects of writing a table of the missing data of each sample to
<project>_missingness.csv and a bar plot of it to
<project>_missingness.pdf, .svg and .png in res_path.
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
assess_vcf_missing_data(vcf, res_path = tempdir(),
species = "Phyllomedusa vaillantii",
project = "vaillantii")
read_vcf
Description
Read VCF file and store its content within a VCFArrow object
Usage
read_vcf(vcf_file, chunk_size = 50000)
Arguments
vcf_file |
-> VCF file |
chunk_size |
-> number of variants to read in at a time, default 50000 (integer) |
Details
This function read a VCF file into an S4 class object in chunks, returning a VCFArrow object. It accepts both uncompressed and gz compressed files. The GT field is stored as an Apache Arrow in Long format. Genotypes are read from the GT field, wherever it is in FORMAT, as two allele numbers (0 = REF, 1 = first ALT, ...; alleles of any number of digits). Haploid genotypes (e.g. "1") are stored as homozygous ("1/1"). Genotypes with one missing allele (e.g. "./1"), with more than two alleles, or that cannot be read are stored as missing, as are all genotypes of lines without a GT field; a warning reports how many there were, and how many lines had fewer sample columns than samples. The allele depth ratio ADR = ALT / (REF + ALT) is computed from AD at biallelic sites only (NA elsewhere). Various metrics are precalculated for fast and easy filtering. GT slot content stored in a TEMP directory for lazy loading.
Value
VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f)
vcf
rename_vcf_samples
Description
Rename samples of a VCFArrow object
Usage
rename_vcf_samples(vcf_arrow, new_names, update_groups = FALSE)
Arguments
vcf_arrow |
-> VCFArrow object |
new_names |
-> the new sample names: a named character vector (c(old = "new", ...)), a data frame, or the path of a tab- or space-separated file with a header line; the data frame or file has the columns id (current name) and new_id (new name), and optionally pop (group) (character, data.frame or file path) |
update_groups |
-> also update the group assignments, from the pop column of new_names, default FALSE (keep the current group of each sample) (Boolean) |
Details
This function renames samples of a VCFArrow object, returning a new VCFArrow object. Samples not listed in new_names keep their names; names in new_names that are not samples of the object are ignored, with a warning. The new names must be unique (also with respect to the samples not renamed) and must not be empty or contain tabs or line breaks; a sample listed twice with different new names is an error. Names can also be exchanged (e.g. c(A = "B", B = "A")). By default (update_groups = FALSE) each sample keeps its group. With update_groups = TRUE, the samples listed get the group in the pop column of new_names (a listed sample with an empty pop keeps its group); this needs a data frame or file with a pop column. A file is read as in set_vcf_groups(): tab-separated if its header line has a tab (fields may then contain spaces), otherwise space-separated, all columns as text. Sample names are stored with every genotype, so the genotype files are rewritten (in parallel with workers, see vcf_set_workers()); the genotypes themselves are unchanged.
Value
VCFArrow object with the samples renamed
Author(s)
Tomas Hrbek November 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2 <- rename_vcf_samples(vcf, c(Pv120 = "WA_Pv120", Pv13 = "WA_Pv13"))
head(vcf2@samples)
# rename and move a sample to another group
new <- data.frame(id = "Pv120", new_id = "OG_Pv120", pop = "OG")
vcf3 <- rename_vcf_samples(vcf, new, update_groups = TRUE)
set_vcf_groups
Description
Associate samples with group info based on a "strt" file or data frame
Usage
set_vcf_groups(vcf_arrow, data_path, strt = "strata")
Arguments
vcf_arrow |
-> VCFArrow object |
data_path |
-> path to data (where strata are located); not needed when strt is a data frame |
strt |
-> file with 2+ columns, one id column and one pop column (tab- or space-separated, with a header line), or a data frame with these columns, default 'strata' (character or data.frame) |
Details
This function assigns each sample of a VCFArrow object to a group, based on a strata file or a data frame. The strt is a file with a header line and 2+ columns, two of them named 'id' (sample name) and 'pop' (group); other columns are ignored. The file is read as tab-separated when its header line contains a tab, so fields may contain spaces and may be empty; otherwise its columns are separated by spaces (any whitespace). All columns are read as text, so ids such as '007' keep their leading zeros. Ids in the file that are not samples of the object are ignored. An id listed more than once with different groups is an error. Samples not in the file, or with an empty group, get the group NA and a warning lists them; functions that need groups (e.g. the exporters) stop until every sample has one. By default strt is named 'strata' and found in the datapath where the VCF file is found. strt can also be a data frame (or tibble) with the columns 'id' and 'pop', e.g. a strata file already read into R; data_path is then not needed. Its columns are used as text, as for a file: factors and numbers are converted to text (numbers lose leading zeros, so keep such ids as text), spaces at either end are removed, and empty values are missing.
Value
vcf_arrow object with groups slot filled
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- set_vcf_groups(read_vcf(f), data_path = dirname(f),
strt = "strata")
table(vcf@groups)
# groups from a data frame
strata_df <- data.frame(id = vcf@samples, pop = substr(vcf@samples, 1, 2))
vcf <- set_vcf_groups(vcf, strt = strata_df)
table(vcf@groups)
Show method for VCFArrow
Description
Print a summary of a VCFArrow object
Usage
## S4 method for signature 'VCFArrow'
show(object)
Arguments
object |
A VCFArrow object |
Details
This function is a method of the VCFArrow S4 class. It prints the numbers of variants and samples, the numbers of biallelic SNVs, multiallelic variants and indels, the samples per group, the percentage of missing genotypes and the number of phased genotypes, the storage location, and the first five variants with their INFO and FORMAT fields and the first five samples. Missing and phased genotypes are counted in one genotype chunk file only, the first that holds current variants and samples, so that printing stays fast.
Value
Invisibly returns object; called for its side effect of printing
a summary.
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f)
show(vcf)
vcf2admixture
Description
Converts a VCFArrow object to a PLINK .bed format infile plus ADMIXTURE-specific .pop file
Usage
vcf2admixture(
vcf_arrow,
out_file,
keep_groups = NULL,
sex = NULL,
pheno = NULL,
chrom_code = c("zero", "auto", "index", "keep"),
supervised = FALSE,
reference_groups = NULL
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> output file prefix; extensions are added automatically, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
sex |
-> vector of sexes of samples, default NULL (character) |
pheno |
-> vector of phenotypes of samples, default NULL (character) |
chrom_code |
-> how CHROM is written: "auto" (keep numeric CHROM, otherwise 0), "index" (1..n by first appearance), "zero" (all 0) or "keep" (verbatim, only if every CHROM is a number), default "zero" (character) |
supervised |
-> flag to generate .pop for use in ADMIXTURE's supervised mode, default FALSE (Boolean) |
reference_groups |
-> groups whose samples are of known ancestry in a supervised run; their names must begin with a letter or digit, default NULL (all groups, a fully supervised run, with a warning) (character) |
Details
This function converts a VCFArrow object to an external PLINK .bed formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Sex and phenotype vectors are optional. If not defined sex = 0, pheno = -9. For supervised analyses, the supervised flag needs to be true to generate .pop file needed for ADMIXTURE's supervised mode. The .pop file has one line per sample, in the order of the .fam file: the sample's group, its known ancestry, if the group is in reference_groups, and '-' (ancestry to be estimated) otherwise. With reference_groups = NULL (the default) every sample's group is written, which is a fully supervised run: the ancestry of every sample is fixed and only the allele frequencies are estimated, so a warning is given. reference_groups can only be given with supervised = TRUE, must be groups of the exported samples, and must begin with a letter or digit, as ADMIXTURE reads a .pop line beginning with any other character as unknown ancestry. In a supervised run, K (given to ADMIXTURE) must equal the number of reference groups. ADMIXTURE (from version 1.3) requires numeric chromosome names, so chrom_code = "keep" is only allowed if every CHROM is a number; with "zero" (the default), "auto" or "index" the names are always numeric. Haploid analyses (ADMIXTURE's –haploid option) of sex chromosomes need their numeric codes (e.g. 23 and 24 in humans), which "auto" and "keep" retain. ADMIXTURE does not model linkage disequilibrium, and its manual recommends thinning linked markers, e.g. keeping one SNV per locus with vcf_filter_oneSNV() before export.
Value
Invisibly returns the input VCFArrow object; called for
its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2admixture(vcf, out_file = file.path(tempdir(), "admixture_in"))
vcf2admixture(vcf, out_file = file.path(tempdir(), "admixture_sup"),
supervised = TRUE,
reference_groups = c("GS", "BS", "WA"))
vcf2apparent
Description
Converts a VCFArrow object to an Apparent format infile
Usage
vcf2apparent(vcf_arrow, out_file, keep_groups = NULL, key = "All")
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
key |
-> relationship type (All, Pa, Mo, Fa, Off), default All (character) |
Details
This function converts a VCFArrow object to an external Apparent formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Possible relationships defined by the parameter 'key' are All, Pa, Mo, Fa, Off. Each line (no header) holds the sample name, the key and one genotype per SNP as two nucleotides separated by '/', with '-/-' for a missing genotype, as apparent expects. The same key is given to every sample.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2apparent(vcf, out_file = tempfile(fileext = ".txt"))
vcf2arlequin
Description
Converts a VCFArrow object to an Arlequin infile
Usage
vcf2arlequin(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external Arlequin formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2arlequin(vcf, out_file = tempfile(fileext = ".arp"))
vcf2bayesass
Description
Converts a VCFArrow object to BayesAss format infile
Usage
vcf2bayesass(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external BayesAss formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. The file is in BA3's native format: one line per individual and locus, 'indivID popID locID allele1 allele2', separated by spaces, with the alleles labeled 1 (REF) and 2 (ALT) and a missing genotype as '0 0'. Sample names, group labels and locus names must not contain spaces. BA3 accepts at most 100,000 loci, 100 populations and 20,000 individuals; a warning is given above these limits.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2bayesass(vcf, out_file = tempfile(fileext = ".immanc"))
vcf2bayescan
Description
Converts a VCFArrow object to Bayescan format infile
Usage
vcf2bayescan(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external BayeScan formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. For each population, each line gives the locus number, the number of called alleles (genes), the number of alleles (2) and the counts of the REF and ALT alleles, as BayeScan's SNP format requires.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2bayescan(vcf, out_file = tempfile(fileext = ".txt"))
vcf2eigenstrat
Description
Converts a VCFArrow object to Eigenstrat format infile
Usage
vcf2eigenstrat(
vcf_arrow,
out_file,
keep_groups = NULL,
sex = NULL,
chrom_code = c("auto", "index", "keep")
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> output file prefix; extensions are added automatically, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
sex |
-> sex of the individual, default = U (undefined) (character) |
chrom_code |
-> how CHROM is written in the .snp file: "auto" (keep CHROM if it is numeric, otherwise number the contigs 1, 2, 3, ...), "index" (number the contigs 1, 2, 3, ... in order of first appearance) or "keep" (verbatim), default "auto" (character) |
Details
This function converts a VCFArrow object to an external EIGENSTRAT formatted fileset (.geno, .ind and .snp). Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. In the .geno file, each digit is the number of copies of the reference allele, as EIGENSTRAT defines it: the REF allele, given in column 5 of the .snp file (2 = REF homozygote, 1 = heterozygote, 0 = ALT homozygote, 9 = missing). EIGENSOFT needs integer chromosome codes and removes SNPs on chromosome 0, so contig names (e.g. from de novo SNP calling) are numbered 1, 2, 3, ... in order of first appearance (chrom_code = "auto" or "index"), and the correspondence is written to <out_file>.chrommap. EIGENSOFT reads chromosomes above its numchrom parameter (default 22) as X, Y, ..., so with more than 22 codes set numchrom to at least the largest code in the parameter file; a warning gives the value.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2eigenstrat(vcf, out_file = file.path(tempdir(), "eigenstrat_in"))
vcf2fasta
Description
Converts a VCFArrow object to a FASTA format infile
Usage
vcf2fasta(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external FASTA formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL.
Each sample is one sequence of its genotypes at the SNVs: the REF or ALT nucleotide for homozygotes, the IUPAC ambiguity code of the two nucleotides for heterozygotes (e.g. R for A/G), and '?' if missing.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2fasta(vcf, out_file = tempfile(fileext = ".fas"))
vcf2fineradstructure
Description
Converts a VCFArrow object to FineRadStructure format infile
Usage
vcf2fineradstructure(
vcf_arrow,
out_file,
keep_groups = NULL,
block_size = 10000
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
block_size |
-> size in bp of the blocks that define RAD loci, as in vcf_filter_multiSNV(), default 10000 (integer) |
Details
This function converts a VCFArrow object to an external fineRADstructure formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. The first line holds the sample names; each further line is one RAD locus, defined as in vcf_filter_multiSNV(): the SNVs on one CHROM within the same block of block_size bases (counted from the CHROM's first SNV). Each cell holds a sample's two haplotypes over the locus's SNVs in RADpainter's syntax, e.g. 'TGAT/CGAT', or 'TGAT' if both are the same; a sample missing at any SNV of the locus has an empty cell. The genotypes are taken as phased: the first allele of each SNV (a1) forms one haplotype and the second (a2) the other. This holds for phased VCFs; for unphased genotypes with heterozygotes at several SNVs of a locus the haplotypes are arbitrary. vcf_filter_multiSNV() selects the SNVs of each locus but does not phase them. Use the same block_size as in vcf_filter_multiSNV(). All genotypes are held in memory while writing (2 bytes per sample and SNV, see vcf_memory_estimate(format = "individual")).
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_linked <- vcf_filter_multiSNV(vcf)
vcf2fineradstructure(vcf_linked, out_file = tempfile(fileext = ".txt"))
vcf2fstructure
Description
Converts a VCFArrow object to a FastStructure infile
Usage
vcf2fstructure(vcf_arrow, out_file, keep_groups = NULL, method = "F")
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
method |
-> flag for Structure/FastStructure formats, default 'F' (character) |
Details
This function is an alias of vcf2structure() that writes the FastStructure format by default (method = 'F'); see vcf2structure() for the file layout. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. FastStructure adds the extension '.str' to the name given with –input, so give out_file the extension .str (e.g. "data.str", read with –input=data –format=str).
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek November 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2fstructure(vcf, out_file = tempfile(fileext = ".str"))
vcf2genepop
Description
Converts a VCFArrow object to a Genepop format infile
Usage
vcf2genepop(vcf_arrow, out_file, keep_groups = NULL, pop_names = FALSE)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
pop_names |
-> write the group name after Pop on the line that starts each population ('pop WA') instead of Pop alone, default FALSE (Boolean) |
Details
This function converts a VCFArrow object to an external Genepop formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Each population starts with a line holding the word Pop alone, as the GENEPOP documentation shows it; GENEPOP itself names each population after its last individual. Some readers, e.g. adegenet::read.genepop(), accept only this form. With pop_names = TRUE, the line also holds the group name ('pop WA'), which other programs may read as the population name.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2genepop(vcf, out_file = tempfile(fileext = ".gen"))
vcf2genlight
Description
Converts a VCFArrow object to Genlight format infile
Usage
vcf2genlight(
vcf_arrow,
out_file = NULL,
keep_groups = NULL,
ploidy = 2L,
save = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of .rds file to write, required when save = TRUE, default NULL (character) |
keep_groups |
-> groups to retain, default NULL (character) |
ploidy |
-> ploidy level; the genotypes are always diploid, so only 2 is used, default = 2 (integer) |
save |
-> save as R data object, default = FALSE (Boolean) |
Details
This function converts a VCFArrow object to an adegenet Genlight object.
It requires the 'adegenet' package, which VCFArrow only suggests: install
it with install.packages("adegenet") to use this function.
Genotypes are read in chunks whose size is determined by the read_vcf() function.
If no groups are defined, the default behavior is to use all groups.
Only biallelic SNVs are exported (multiallelic sites and indels are left
out), and every sample must be assigned to a group (see set_vcf_groups()),
also when keep_groups is NULL.
Genlight objects can encode polyploid genomes, but VCFArrow stores diploid
genotypes, so the ploidy is always 2.
Genlight objects are in-memory S4 objects and are always returned; if
save = TRUE, the object is also written to out_file with saveRDS().
Unlike the other exporters, out_file is optional because nothing is written
unless save = TRUE.
Value
An adegenet genlight object.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
gl <- vcf2genlight(vcf)
gl
gl <- vcf2genlight(vcf, out_file = tempfile(fileext = ".rds"),
save = TRUE)
vcf2gt_long
Description
Converts a VCFArrow object to tidy long format infile
Usage
vcf2gt_long(
vcf_arrow,
out_file,
keep_groups = NULL,
format = c("feather", "parquet", "csv"),
col_select = NULL
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
format |
-> output format, "feather" (Arrow IPC), "parquet" or "csv", default "feather" (character) |
col_select |
-> optional selection of columns to save, default ALL |
Details
This function converts a VCFArrow object to an external tidy long-format genotype file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. The tidy data can be saved in either Arrow, Parquet or CSV formats. File extension is added automatically if missing. Optionally, specific columns can be saved, by default all columns are saved. The gt long slot contains pre-calculated metrics in addition to just genotypes.
Value
Invisibly returns the path of the written file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2gt_long(vcf, out_file = tempfile(), format = "csv")
vcf2migrate
Description
Converts a VCFArrow object to a MIGRATE-N format infile
Usage
vcf2migrate(
vcf_arrow,
out_file,
keep_groups = NULL,
block_size = 100L,
method = c("S", "N", "C")
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
block_size |
-> number of SNPs (method S) or base pairs (method N) per linked block, default 100 (integer) |
method |
-> "C" (allele counts, HapMap format), "S" (sequences, loci of block_size SNPs, "(sK)") or "N" (SNPs, loci of block_size bp per CHROM, "(nK)"), default "S" (character) |
Details
This function converts a VCFArrow object to an external MIGRATE-N formatted file.
Writing occurs in chunks whose size is determined by the read_vcf() function.
Larger chunks result in faster writing speeds.
If no groups are defined, the default behavior is to use all groups.
Only biallelic SNVs are exported (multiallelic sites and indels are left
out), and every sample must be assigned to a group (see set_vcf_groups()),
also when keep_groups is NULL.
The data file follows the Migrate-N manual (section 4, "Data file
specification",
https://peterbeerli.com/downloads/migrate/docs/migratedoc.pdf).
Method "S" writes sequence data: each sample as two haplotypes
(<name>_1, <name>_2) of the SNPs' nucleotides ('?' if missing), cut into
loci of block_size consecutive SNPs, labeled "(sK)" (a sequence locus of
K sites).
Method "N" writes the same sequences cut into loci of the SNPs on one
CHROM within the same interval of block_size bp, labeled "(nK)" (K linked
SNPs), with the data type N (SNPs) on the first line.
Method "C" writes the allele counts of each population in Migrate-N's
HapMap format (data type H): for each SNP its position, REF and ALT
nucleotides with their counts, and the total; the manual describes this
format as preliminary. SNPs without called alleles in some population are
left out (with a warning), as Migrate-N cannot compute their allele
frequencies there.
The first line gives the number of populations and of loci. Individual
names are written in a fixed field of 10 characters, Migrate-N's default;
if longer names are needed, a warning gives the nmlength to set in the
parmfile. Population names must not start with a number.
Whether loci of SNPs should be treated as linked depends on whether the
SNPs were mapped (so that linkage is known) or called de novo.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2migrate(vcf, out_file = tempfile(fileext = ".txt"))
vcf2nexus
Description
Converts a VCFArrow object to a NEXUS format infile
Usage
vcf2nexus(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external NEXUS formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL.
Each sample is one sequence of its genotypes at the SNVs: the REF or ALT nucleotide for homozygotes, the IUPAC ambiguity code of the two nucleotides for heterozygotes (e.g. R for A/G), and '?' if missing. The matrix is declared as DNA data (FORMAT DATATYPE=DNA MISSING=?).
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2nexus(vcf, out_file = tempfile(fileext = ".nex"))
vcf2plink_bed
Description
Converts a VCFArrow object to a PLINK .bed format infile
Usage
vcf2plink_bed(
vcf_arrow,
out_file,
keep_groups = NULL,
sex = NULL,
pheno = NULL,
chrom_code = c("auto", "index", "zero", "keep")
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> output file prefix; extensions are added automatically, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
sex |
-> vector of sexes of samples, default NULL (character) |
pheno |
-> vector of phenotypes of samples, default NULL (character) |
chrom_code |
-> how CHROM is written: "auto" (keep numeric CHROM, otherwise 0), "index" (1..n by first appearance), "zero" (all 0) or "keep" (verbatim), default "auto" (character) |
Details
This function converts a VCFArrow object to an external PLINK .bed formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Sex and phenotype vectors are optional. If not defined sex = 0, pheno = -9. Allele 1 (column 5 of the .bim file) is ALT and allele 2 (column 6) is REF, as in the files PLINK writes from a VCF (plink –vcf), so PLINK's A1 counts and frequencies refer to ALT. PLINK 1.9 makes the minor allele A1 when it loads the files unless run with –keep-allele-order; PLINK 2 keeps this order.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2plink_bed(vcf, out_file = file.path(tempdir(), "plink_out"))
vcf2plink_ped
Description
Converts a VCFArrow object to a PLINK .ped format infile
Usage
vcf2plink_ped(
vcf_arrow,
out_file,
keep_groups = NULL,
sex = NULL,
pheno = NULL,
chrom_code = c("auto", "index", "zero", "keep"),
allele_code = c("nucleotide", "12")
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> output file prefix; extensions are added automatically, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
sex |
-> vector of sexes of samples, default NULL (character) |
pheno |
-> vector of phenotypes of samples, default NULL (character) |
chrom_code |
-> how CHROM is written: "auto" (keep numeric CHROM, otherwise 0), "index" (1..n by first appearance), "zero" (all 0) or "keep" (verbatim), default "auto" (character) |
allele_code |
-> how alleles are written in the .ped file: "nucleotide" (the REF and ALT bases) or "12" (1 = ALT, 2 = REF, as ADMIXTURE requires for .ped input), default "nucleotide" (character) |
Details
This function converts a VCFArrow object to an external PLINK .ped formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Sex and phenotype vectors are optional. If not defined sex = 0, pheno = -9. Missing alleles are written as 0. In the "12" coding allele 1 is ALT and allele 2 is REF, as in vcf2plink_bed(). With either coding, PLINK 1.9 makes the minor allele A1 when it loads a .ped file; PLINK 2 (and PLINK 1.9 with –keep-allele-order) take the "12" coding's 1 as A1. PLINK reads both allele codings; ADMIXTURE reads .ped files only in the "12" coding (allele_code = "12"), and needs integer chromosome codes (chrom_code), as for .bed input.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2plink_ped(vcf, out_file = file.path(tempdir(), "plink_out"))
vcf2related
Description
Converts a VCFArrow object to Related format infile
Usage
vcf2related(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external Related formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Each line holds a sample name followed by two columns per locus, the alleles coded A = 1, C = 2, G = 3, T = 4 (written 01-04); a missing genotype is '0 0', as related's readgenotypedata() expects.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2related(vcf, out_file = tempfile(fileext = ".txt"))
vcf2smartsnp
Description
Converts a VCFArrow object to smartsnp tabular format infile
Usage
vcf2smartsnp(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external SmartSNP formatted file.
Writing occurs in chunks whose size is determined by the read_vcf() function.
Larger chunks result in faster writing speeds.
If no groups are defined, the default behavior is to use all groups.
Only biallelic SNVs are exported (multiallelic sites and indels are left
out), and every sample must be assigned to a group (see set_vcf_groups()),
also when keep_groups is NULL.
The genotype file has one line per SNP and one column per sample,
separated by spaces: the number of ALT alleles (0, 1, 2), 9 if missing.
It has no header line, as smartsnp requires ("without row names or
column headings").
The samples and their groups, in the order of the columns, are written to
a separate tab-separated file with a header line (sample, group), named
after out_file without its extension plus '_samples.smartsnp' (e.g.
'data.txt' gives 'data_samples.smartsnp'). Its group column is what
smartsnp's analyses take as sample_group, e.g.
s <- read.delim("data_samples.smartsnp") then
smartsnp::smart_pca("data.txt", sample_group = s$group) or
smartsnp::smart_permanova("data.txt", sample_group = s$group).
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file
and its sample file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2smartsnp(vcf, out_file = tempfile(fileext = ".txt"))
vcf2snapp
Description
Converts a VCFArrow object to a SNAPP encoded NEXUS format infile
Usage
vcf2snapp(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external SNAPP/SNAPPER formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. Genotypes are coded as SNAPP expects for diploid data: 0 (REF homozygote), 1 (heterozygote), 2 (ALT homozygote), ? (missing), in a NEXUS file declared as integer data with symbols 0, 1 and 2 (FORMAT DATATYPE=INTEGERDATA SYMBOLS="012"), which BEAUti imports with 3 states.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2snapp(vcf, out_file = tempfile(fileext = ".nex"))
vcf2snmf
Description
Converts a VCFArrow object to an sNMF format infile
Usage
vcf2snmf(vcf_arrow, out_file, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function converts a VCFArrow object to an external sNMF formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2snmf(vcf, out_file = tempfile(fileext = ".geno"))
vcf2structure
Description
Converts a VCFArrow object to a Structure or FastStructure infile
Usage
vcf2structure(vcf_arrow, out_file, keep_groups = NULL, method = c("S", "F"))
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
method |
-> flag for Structure/FastStructure formats, default 'S' (character) |
Details
This function converts a VCFArrow object to an external Structure or FastStructure formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. The flag parameter controls whether Structure (flag = 'S') or FastStructure (flag = 'F') formatted output is written out. Each sample is written on two lines (one per allele copy): its name, its group number and, for FastStructure, four columns of 0 (FastStructure ignores the first six columns), then one allele per SNP: 0 (REF), 1 (ALT) or -9 (missing). FastStructure adds the extension '.str' to the name given with –input, so give out_file the extension .str (e.g. "data.str", read with –input=data –format=str).
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2structure(vcf, out_file = tempfile(fileext = ".str"))
vcf2structure(vcf, out_file = tempfile(fileext = ".str"),
method = "F")
vcf2treemix
Description
Converts a VCFArrow object to Treemix format infile
Usage
vcf2treemix(
vcf_arrow,
out_file,
keep_groups = NULL,
gzip = grepl("\\.gz$", out_file)
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of file to output, no default (character) |
keep_groups |
-> groups to retain, default NULL (character) |
gzip |
-> compress the file with gzip, as TreeMix expects, default TRUE if out_file ends in ".gz" (Boolean) |
Details
This function converts a VCFArrow object to an external Treemix formatted file. Writing occurs in chunks whose size is determined by the read_vcf() function. Larger chunks result in faster writing speeds. If no groups are defined, the default behavior is to use all groups. Only biallelic SNVs are exported (multiallelic sites and indels are left out), and every sample must be assigned to a group (see set_vcf_groups()), also when keep_groups is NULL. The first line holds the group names; each further line one SNP, with the REF and ALT allele counts of each group as "ref,alt", separated by spaces. TreeMix reads gzip-compressed input (treemix -i file.gz): name out_file with the extension .gz to compress it.
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing out_file.
Author(s)
Tomas Hrbek May 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf2treemix(vcf, out_file = tempfile(fileext = ".gz"))
vcf_bind
Description
Bind two or more VCFArrow objects into a new VCFArrow object
Usage
vcf_bind(
...,
mode = c("intersect", "union"),
absent_as = c("missing", "hom_ref"),
outgroup = FALSE,
recover_loci = FALSE,
.check = NULL
)
Arguments
... |
-> a collection of VCFArrow objects |
mode |
-> "intersect" keeps variants present in all objects, "union" keeps variants present in any object (and variants removed from them as invariant, see Details), default "intersect" (character) |
absent_as |
-> for mode = "union" or outgroup = TRUE, how the genotypes a source object does not provide are coded, "missing" or "hom_ref", default "missing" (character); these are genotypes neither in the object nor recovered from its genotype files (with recover_loci = TRUE or outgroup = TRUE), e.g. of variants the object removed by filtering (without recover_loci) or never had (objects from different VCF files) |
outgroup |
-> add the other objects (e.g. outgroups) to the first object (e.g. a filtered ingroup), keeping exactly the variants of the first object; mode and recover_loci are then not used, default FALSE (Boolean) |
recover_loci |
-> for mode = "union", restore the genotypes of variants an object removed by filtering, where its genotype files still hold them, instead of coding them according to absent_as, default FALSE (Boolean) |
.check |
-> deprecated and ignored; objects no longer need to share the same variants |
Details
This function binds two or more VCFArrow objects with different sets of individuals, returning a new VCFArrow object. Sample names must be unique across objects. The objects need not have the same variants: mode = "intersect" keeps the variants present in all objects, mode = "union" keeps the variants present in any object, or removed from it as invariant, and codes the genotypes an object lacks according to absent_as. For objects with the same variants both modes give the same result. In mode = "union", variants an object removed by filtering stay removed for it: a variant that one object removed (e.g. with vcf_filter_maf()) but that another object contains is included, with the genotypes of the object that removed it coded according to absent_as. Variants an object removed as invariant (e.g. after extracting samples, see vcf_filter_invariant()) are the exception: they count as the object's own and are included with their real genotypes, also when every object removed them, so that differences fixed between separately extracted groups (e.g. an ingroup and an outgroup) are kept. Binding in steps and binding all objects in one call then give the same result. With recover_loci = TRUE, the real genotypes of such variants are restored instead, read from the genotype files of the object that removed them (which keep every variant the object had when its files were written). This suits joining separately filtered datasets; binding in steps can then differ from binding in one call.
outgroup = TRUE is meant for adding outgroups to an ingroup that was filtered on its own (so that properties of the outgroup, such as its missingness, cannot remove ingroup variants): the result keeps exactly the variants of the first object, and variants removed from it stay removed. The other objects contribute their genotypes for these variants, read from their genotype files wherever present (including variants they removed, for example as invariant); genotypes they do not have are coded according to absent_as.
Variants are matched by CHROM, POS, REF and ALT, so the objects may come
from different VCF files (e.g. separate sequencing runs); REF and ALT must
be written the same way in all of them (including the order of alternative
alleles). This is assured when all datasets were called against the same
reference genome assembly, which gives the same CHROM, POS and REF for the
same locus. It is generally not possible for de novo (reference-free) SNP
calling, for example with DiscoSnp, where locus names and positions are
generated anew in each calling run, so the same locus will usually not
match between separately called datasets; such datasets should be called
together in a single run instead. Multi-allelic sites can list their ALT
alleles in a different order in different files, so it is best to
normalize the VCFs first, for example by splitting multi-allelic sites with
bcftools norm -m -any. Variants keep the .row_id of the first object where its source
VCF has them, other variants get new ids; the variant metadata of the first
object and, in mode = "intersect", its record of removed invariant variants
are kept, so bound objects can be bound again.
Value
VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
ingroup <- vcf_extract_groups(vcf, c("GS", "BS", "WA"))
outgroup <- vcf_extract_groups(vcf, "OG")
vcf_bind(ingroup, outgroup, mode = "intersect")
vcf_bind(ingroup, outgroup, mode = "union", absent_as = "missing")
vcf_bind(vcf_filter_missingness(ingroup, 0.2), outgroup, outgroup = TRUE,
absent_as = "missing")
vcf_bind_sparse
Description
Deprecated: use vcf_bind(), which now binds objects with different variants (see its mode and absent_as arguments). vcf_bind_sparse(...) is vcf_bind(..., recover_loci = TRUE).
Usage
vcf_bind_sparse(
...,
mode = c("intersect", "union"),
absent_as = c("missing", "hom_ref")
)
Arguments
... |
-> a collection of VCFArrow objects |
mode |
-> see vcf_bind() |
absent_as |
-> see vcf_bind() |
Value
VCFArrow object
vcf_copy
Description
Copy a VCFArrow object
Usage
vcf_copy(vcf_arrow)
Arguments
vcf_arrow |
-> VCFArrow object |
Details
This function copies a VCFArrow object. It makes an actual physical copy of the object.
Value
VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_copy(vcf_filter_maf(vcf, 0.05))
vcf_extract_groups
Description
Extract groups of samples from a VCFArrow object
Usage
vcf_extract_groups(
vcf_arrow,
groups,
keep = TRUE,
f_invar = TRUE,
verbose = TRUE,
strict_poly = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
groups |
-> groups to retain/drop (character) |
keep |
-> retain (TRUE) or drop (FALSE) individuals flag, default TRUE (Boolean) |
f_invar |
-> filter invariant loci flag, default TRUE (Boolean) |
verbose |
-> report filtering stats, default TRUE (Boolean) |
strict_poly |
-> when removing invariant loci, polymorphism at the genotype level (TRUE) or the allele level (FALSE), see vcf_filter_invariant(), default FALSE (Boolean) |
Details
This function removes groups of samples from a VCFArrow object, returning a new VCFArrow object. It uses the 'keep' flag to either keep or drop the groups in the list. By default will remove any loci that may have become invariant as the result of the removal of samples. By default will report removed samples, final % missing data, and number of retained samples after sample filtering.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_extract_groups(vcf, groups = c("GS", "BS"))
vcf_extract_groups(vcf, groups = "OG", keep = FALSE)
vcf_extract_samples
Description
Extract samples from a VCFArrow object
Usage
vcf_extract_samples(
vcf_arrow,
samples,
keep = TRUE,
f_invar = TRUE,
verbose = TRUE,
strict_poly = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
samples |
-> individuals to retain/drop (character) |
keep |
-> retain (TRUE) or drop (FALSE) individuals flag, default TRUE (Boolean) |
f_invar |
-> filter invariant loci flag, default TRUE (Boolean) |
verbose |
-> report filtering stats, default TRUE (Boolean) |
strict_poly |
-> when removing invariant loci, polymorphism at the genotype level (TRUE) or the allele level (FALSE), see vcf_filter_invariant(), default FALSE (Boolean) |
Details
This function removes samples from a VCFArrow object, returning a new VCFArrow object. It uses the 'keep' flag to either keep or drop the samples in the list. By default will remove any loci that may have become invariant as the result of the removal of samples. By default will report removed samples, final % missing data, and number of retained samples after sample filtering.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_extract_samples(vcf, samples = c("Pv14", "Pv27", "Pv28"))
vcf_extract_samples(vcf, samples = c("Pb2Jp", "Pb2Scx", "Pb1Rd"),
keep = FALSE)
vcf_filter_adr
Description
Correct or remove genotypes with allele imbalance greater than a threshold from a VCFArrow object
Usage
vcf_filter_adr(
vcf_arrow,
threshold = 0.1,
mode = c("correct", "remove"),
f_invar = TRUE,
strict_poly = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> allele depth ratio (ADR) threshold, strictly between 0 and 0.5, default 0.1 (numeric) |
mode |
-> switch between 'correct' or 'remove' mode, default 'correct' (character) |
f_invar |
-> filter invariant loci flag, default TRUE (Boolean) |
strict_poly |
-> when removing invariant loci, polymorphism at the genotype level (TRUE) or the allele level (FALSE), see vcf_filter_invariant(), default FALSE (Boolean) |
Details
This function either changes heterozygous genotypes or makes them missing in a VCFArrow object if they have above/below threshold normalized ADR ratio, returning a new VCFArrow object. The ADR is calculated as ADR = ALT / (REF + ALT), from the AD field. Only called heterozygous genotypes are affected; homozygous and missing genotypes are left unchanged, even where AD gives them an ADR. In 'correct' mode, a heterozygote with ADR < threshold becomes REF homozygous, and one with 1 - ADR < threshold becomes ALT homozygous. In 'remove' mode, these heterozygotes become missing. The number of genotypes corrected or removed is reported. By default will remove any loci that may have become invariant as the result of the change/removal of genotypes.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_adr(vcf, mode = "correct")
vcf_filter_biallelic
Description
Remove non-biallelic loci from a VCFArrow object
Usage
vcf_filter_biallelic(vcf_arrow)
Arguments
vcf_arrow |
-> VCFArrow object |
Details
This function removes non-biallelic loci from a VCFArrow object, returning a new VCFArrow object.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_biallelic(vcf)
vcf_filter_coverage
Description
Set genotypes below a read depth (DP) threshold to missing, or remove variants without enough genotypes at or above it, in a VCFArrow object
Usage
vcf_filter_coverage(
vcf_arrow,
threshold = 10,
strict = TRUE,
strict_poly = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> DP threshold, default 10 (integer) |
strict |
-> set called genotypes with DP below the threshold to missing (TRUE) or remove variants with fewer than two polymorphic called genotypes with DP >= threshold (FALSE), default TRUE (Boolean) |
strict_poly |
-> with strict = FALSE, polymorphism at the genotype level (TRUE) or the allele level (FALSE), default FALSE (Boolean) |
Details
With strict = TRUE (the default), called genotypes with DP below the threshold, or without DP, are set to missing (as vcftools –minDP does), so every called genotype of the result has DP >= threshold; the number of genotypes set to missing is reported. No variants are removed: variants can become invariant or have much missing data, so follow with e.g. vcf_filter_invariant() and vcf_filter_missingness(). With strict = FALSE the genotypes are not changed, and only the variants that have at least two called genotypes with DP >= threshold and are polymorphic among them are kept; the kept variants can still contain called genotypes with DP below the threshold. With strict_poly = FALSE (the default), polymorphic means that not all alleles of these genotypes are the same, e.g. both REF and ALT alleles occur. With strict_poly = TRUE, it means that not all of these genotypes are the same, so a variant whose genotypes with DP >= threshold are all heterozygous is removed although both alleles occur; genotypes are compared as in vcf_filter_invariant(). strict_poly is ignored with strict = TRUE.
Value
VCFArrow object with genotypes set to missing (strict = TRUE) or subsetted VCFArrow object (strict = FALSE)
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
# set genotypes with DP < 10 to missing
vcf_filter_coverage(vcf, threshold = 10)
# keep the genotypes, remove variants with fewer than two polymorphic
# genotypes with DP >= 10
vcf_filter_coverage(vcf, threshold = 10, strict = FALSE)
# also remove variants whose well-covered genotypes are all heterozygous
vcf_filter_coverage(vcf, threshold = 10, strict = FALSE, strict_poly = TRUE)
vcf_filter_hets
Description
Remove loci above a heterozygosity threshold from a VCFArrow object
Usage
vcf_filter_hets(vcf_arrow, threshold = 0.5)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> heterozygosity threshold; loci with a proportion of heterozygous called genotypes above it are removed, those at it are kept, default 0.5 (numeric) |
Details
This function removes loci above a heterozygosity threshold from a VCFArrow object (loci at the threshold are kept), as well as loci without called genotypes, returning a new VCFArrow object. High heterozygosities are indicative of potential paralogs.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_hets(vcf, threshold = 0.5)
vcf_filter_indels
Description
Remove indels from a VCFArrow object
Usage
vcf_filter_indels(vcf_arrow)
Arguments
vcf_arrow |
-> VCFArrow object |
Details
This function removes indel loci from a VCFArrow object, returning a new VCFArrow object.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_indels(vcf)
vcf_filter_invariant
Description
Remove invariant loci from a VCFArrow object
Usage
vcf_filter_invariant(vcf_arrow, strict_poly = FALSE)
Arguments
vcf_arrow |
-> VCFArrow object |
strict_poly |
-> polymorphism at the genotype level (TRUE) or the allele level (FALSE), default FALSE (Boolean) |
Details
This function removes invariant loci from a VCFArrow object, returning a new VCFArrow object. This might be desirable after subsetting a VCFArrow object by individuals. A variant is kept if it has at least one called genotype and is polymorphic among its called genotypes. With strict_poly = FALSE (the default), polymorphic means that not all called alleles are the same, e.g. both REF and ALT alleles occur; a variant with a single called heterozygote is kept. With strict_poly = TRUE, polymorphic means that not all called genotypes are the same, so a variant where every called sample is heterozygous is removed although both alleles occur (such sites are often paralogs). Genotypes are compared without regard to allele order or phase (0/1, 1/0, 0|1 and 1|0 are the same genotype), and at multiallelic sites every genotype is distinct, e.g. 0/2 differs from 1/1 and 0/3 from 1/2.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_invariant(vcf)
# also remove variants where all called genotypes are heterozygous
vcf_filter_invariant(vcf, strict_poly = TRUE)
vcf_filter_maf
Description
Remove loci below MAF threshold from a VCFArrow object
Usage
vcf_filter_maf(vcf_arrow, threshold = 0.01)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> decimal MAF threshold, default 0.01 (numeric) |
Details
This function removes loci below MAF threshold from a VCFArrow object, returning a new VCFArrow object. Loci at the threshold are kept.
The default threshold of 0.01 follows the conventional definition of a
polymorphism as a variant whose minor allele has a frequency of at least
1% in a population (Karki et al. 2015). Karki et al. note that this
cutoff is a convention rather than a biological boundary, so set
threshold to suit the data and the analysis.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
References
Karki R, Pandya D, Elston RC, Ferlini C (2015). Defining "mutation" and "polymorphism" in the era of personal genomics. BMC Medical Genomics 8:37. doi:10.1186/s12920-015-0115-z
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_maf(vcf, threshold = 0.05)
vcf_filter_missing
Description
Remove samples with more than a threshold of missing data from a VCFArrow object
Usage
vcf_filter_missing(
vcf_arrow,
threshold = 0.5,
f_invar = TRUE,
verbose = TRUE,
strict_poly = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> decimal missing threshold; samples with a proportion of missing genotypes above it are removed, those at it are kept, default 0.5 (numeric) |
f_invar |
-> filter invariant loci flag, default TRUE (Boolean) |
verbose |
-> report removed samples, numbers of retained variants and samples, and the final % missing data, default TRUE (Boolean) |
strict_poly |
-> when removing invariant loci, polymorphism at the genotype level (TRUE) or the allele level (FALSE), see vcf_filter_invariant(), default FALSE (Boolean) |
Details
This function removes samples from a VCFArrow object if they have a proportion of missing genotypes above the threshold (samples at the threshold are kept), returning a new VCFArrow object. By default will remove any loci that may have become invariant as the result of the removal of samples. By default will report removed samples, final % missing data, and number of retained samples after sample filtering. The final % missing data is the percentage of missing genotypes over the retained variants and samples; computing it reads the genotypes once more, which verbose = FALSE avoids.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf |>
vcf_filter_missingness(threshold = 0.2) |>
vcf_filter_missing(threshold = 0.3)
vcf_filter_missingness
Description
Remove loci above missingness threshold from a VCFArrow object
Usage
vcf_filter_missingness(vcf_arrow, threshold = 0.1, verbose = TRUE)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> decimal missingness threshold, default 0.1 (numeric) |
verbose |
-> flag to report total % missing data after filtering, default TRUE (Boolean) |
Details
This function removes loci above missingness threshold from a VCFArrow object (loci at the threshold are kept), returning a new VCFArrow object. Missingness is a locus focused metric, i.e. missing data per locus. With verbose = TRUE, the number of retained variants and the final % missing data (missing genotypes over the retained variants and samples) are reported.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_missingness(vcf, threshold = 0.2)
vcf_filter_multiSNV
Description
Subset a VCFArrow object keeping only loci with several linked SNVs
Usage
vcf_filter_multiSNV(
vcf_arrow,
block_size = 10000,
minSNV = 2,
maxSNV = 5,
over_max = c("remove", "truncate")
)
Arguments
vcf_arrow |
-> VCFArrow object |
block_size |
-> size of linked SNV blocks, default 10000 bp (integer) |
minSNV |
-> minimum number of SNVs per block; blocks with fewer are removed, default 2 (integer) |
maxSNV |
-> maximum number of SNVs per block, default 5 (integer); Inf keeps every SNV of each block |
over_max |
-> what to do with blocks with more than maxSNV SNVs: "remove" the whole block or "truncate" it to its first maxSNV SNVs (by position), default "remove" (character) |
Details
This function subsets a VCFArrow object keeping only linked SNVs, i.e. blocks (loci) with at least minSNV SNVs, returning a new VCFArrow object. A block is a CHROM, or within a CHROM a window of block_size bp counted from its first SNV; with de novo RAD data, where each locus is its own CHROM, a block is a locus. Blocks with fewer than minSNV SNVs are removed. Blocks with more than maxSNV SNVs are removed (over_max = "remove") or truncated to their first maxSNV SNVs (over_max = "truncate"); blocks with minSNV to maxSNV SNVs are kept whole. With RAD loci and de novo SNV discovery, an unusually high number of SNVs in a short locus often indicates misassembled paralogs, so such loci are best removed (the default; fineRADstructure's Stacks2fineRAD.py script likewise removes loci with more than a maximum number of SNPs). With SNVs from whole genome data mapped to a reference, a block is an arbitrary window and many SNVs indicate a variable region rather than an artefact; over_max = "truncate" keeps the first, most tightly linked, maxSNV SNVs of such blocks, which suits unphased data, where the haplotypes of loci with several heterozygous SNVs are arbitrary, and maxSNV = Inf keeps all SNVs, e.g. for phased data.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_multiSNV(vcf, block_size = 10000, minSNV = 2, maxSNV = 5)
# whole genome data: truncate blocks with many SNVs instead of removing them
vcf_filter_multiSNV(vcf, minSNV = 2, maxSNV = 5, over_max = "truncate")
vcf_filter_oneSNV
Description
Subset a VCFArrow object keeping only 1 SNV per locus
Usage
vcf_filter_oneSNV(vcf_arrow, block_size = 10000, by_quality = FALSE)
Arguments
vcf_arrow |
-> VCFArrow object |
block_size |
-> size of linked SNV blocks, default 10000 bp (integer) |
by_quality |
-> keep the SNV with the highest QUAL of each block instead of the first, default FALSE (Boolean) |
Details
This function subsets a VCFArrow object keeping only 1 SNV per locus, returning a new VCFArrow object. Locus is defined as a different chromosome or a block of the 'block_size' parameter value within a chromosome. By default the first SNV (lowest POS) of each locus is kept, whatever its quality. With by_quality = TRUE the SNV with the highest QUAL is kept; SNVs without QUAL ('.') rank below all others, and ties (or a locus without any QUAL) go to the first SNV. VCFs without QUAL values, such as those of DiscoSnpRad, therefore give the same result as the default, with a warning.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_oneSNV(vcf)
# keep the SNV with the highest QUAL of each locus
vcf_filter_oneSNV(vcf, by_quality = TRUE)
vcf_filter_paralogs
Description
Remove loci above REPEAT and below RANK threshold from a VCFArrow object
Usage
vcf_filter_paralogs(
vcf_arrow,
threshold_rk = 0.2,
threshold_rpt = 0.5,
keep_na = FALSE
)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold_rk |
-> decimal RANK threshold, loci below it are removed, default 0.2 (numeric) |
threshold_rpt |
-> decimal REPEAT threshold, loci above it are removed, default 0.5 (numeric) |
keep_na |
-> retain loci without a RANK or REPEAT value, default FALSE (Boolean) |
Details
This function removes loci below the RANK threshold and above the REPEAT threshold from a VCFArrow object, returning a new VCFArrow object. It applies vcf_filter_rank() and then vcf_filter_rpt(). If only one of the Rk and RPT fields is present, only that filter is applied (the other returns its input unfiltered, with a warning); if neither is present, the object is returned unfiltered, with a warning. RANK and REPEAT metrics focus on different paralog signals, and used jointly maximize true positive and minimize false positive detections. Simulations indicate that removing loci with Rk < 0.2 or RPT > 0.5 gives the best results, with Rk < 0.4 or RPT > 0.5 being slightly more conservative.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek September 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_paralogs(vcf, threshold_rk = 0.2, threshold_rpt = 0.5)
vcf_filter_pass
Description
Remove loci that did not PASS FILTER in a VCFArrow object
Usage
vcf_filter_pass(vcf_arrow)
Arguments
vcf_arrow |
-> VCFArrow object |
Details
This function removes loci that did not PASS the FILTER in a VCFArrow object, returning a new VCFArrow object. PASS indicates that a variant has successfully passed all applied quality control filters. When no filters were applied and the FILTER field is '.' (Dot), vcf_filter_pass() defaults to passing the locus.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_pass(vcf)
vcf_filter_quality
Description
Remove loci below QUAL threshold from a VCFArrow object
Usage
vcf_filter_quality(vcf_arrow, threshold = 30)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> QUAL threshold, default 30 (integer) |
Details
This function removes loci below QUAL threshold from a VCFArrow object, returning a new VCFArrow object. QUAL is a Phred-scaled quality metric assessing whether any variant exists at that position across all samples. QUAL is different from PL which represents the confidence in a genotype call of a specific sample. When QUAL not reported '.' (Dot), QUAL is assumed to be above threshold.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_quality(vcf, threshold = 30)
vcf_filter_rank
Description
Remove loci below RANK threshold from a VCFArrow object
Usage
vcf_filter_rank(vcf_arrow, threshold = 0.4, keep_na = FALSE)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> decimal rank threshold, default 0.4 (numeric) |
keep_na |
-> retain loci without a rank value, default FALSE (Boolean) |
Details
This function removes loci below RANK threshold from a VCFArrow object, returning a new VCFArrow object. RANK is calculated by kissreads2 in DiscoSNP-RAD (Gauthier et. al. 2020) and registered as Rk in INFO. RANK is calculated as sqrt(chi-sqr/n) of allele read counts, and used for paralog detection -> very low rank values (<0.4) are indicative of paralogs. If no variant has an Rk value (e.g. VCFs not from DiscoSNP-RAD), the object is returned unfiltered, with a warning.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_rank(vcf, threshold = 0.5)
vcf_filter_rpt
Description
Remove loci above REPEAT threshold from a VCFArrow object
Usage
vcf_filter_rpt(vcf_arrow, threshold = 0.5, keep_na = FALSE)
Arguments
vcf_arrow |
-> VCFArrow object |
threshold |
-> decimal REPEAT threshold, loci above it are removed, default 0.5 (numeric) |
keep_na |
-> retain loci without a REPEAT value, default FALSE (Boolean) |
Details
This function removes loci above REPEAT threshold from a VCFArrow object, returning a new VCFArrow object. REPEAT is calculated by disco_haplotypes in DiscoSNP-RAD (Gauthier et. al. 2020) and registered as RPT in INFO. REPEAT is calculated as max of overlap, depth score and is 1 if in a giant-component bubble, and is used for paralog detection -> high repeat values (>0.5) are indicative of paralogs. If no variant has an RPT value (e.g. VCFs not from DiscoSNP-RAD, or not processed by disco_haplotypes), the object is returned unfiltered, with a warning.
Value
subsetted VCFArrow object
Author(s)
Tomas Hrbek September 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_filter_rpt(vcf, threshold = 0.5)
Garbage-collect VCFArrow temp directories
Description
Triggers R's garbage collector (three full passes to handle the finalizer -> pending-queue -> unlink chain), then flushes any directories whose reference count has already reached zero.
Usage
vcf_gc(force = FALSE, verbose = TRUE)
Arguments
force |
Logical. If TRUE, also force-deregister and delete directories
for objects that are still nominally live in the registry. Use this when
you have called |
verbose |
Logical. Print a status message. |
Value
Invisibly returns NULL; called for its side effect of deleting
temporary directories.
Typical workflow
rm(vcf1, vcf2, vcf3) vcf_gc() # usually sufficient vcf_gc(force = TRUE) # if directories remain after rm()
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f)
rm(vcf)
vcf_gc()
Estimate RAM required for a VCFArrow export operation
Description
Estimate RAM required for a VCFArrow export operation
Usage
vcf_memory_estimate(
vcf_arrow,
keep_groups = NULL,
format = c("individual", "pop", "chunk"),
chunk_size = 50000L,
lowmem = FALSE
)
Arguments
vcf_arrow |
A VCFArrow object. |
keep_groups |
Groups to export (NULL = all). |
format |
One of "individual" (Structure, Arlequin, FASTA, ...) or "pop" (BayesScan, Treemix, Migrate-N C, ...) or "chunk" (SmartSNP, sNMF, EIGENSTRAT, ...). |
chunk_size |
Feather chunk size used at read_vcf() time, default 50000 (as in read_vcf()). |
lowmem |
For format = "individual": if TRUE, count the genotype matrices at 1 byte per cell, the raw storage used while the chunks are read; if FALSE (default), at 4 bytes per cell, the integer matrices the exporters create to write the file, which gives their peak memory. |
Value
Invisibly, a list with elements:
-
n_var: number of variants -
n_samples: number of samples retained -
n_pops: number of populations -
chunk_arrow_bytes: Arrow memory per chunk read -
matrix_bytes: size of the accumulation matrices -
peak_bytes: estimated peak memory
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_memory_estimate(vcf, format = "individual")
vcf_set_workers
Description
Set the number of parallel worker processes used by VCFArrow
Usage
vcf_set_workers(workers = 1L)
Arguments
workers |
-> number of worker processes, default 1 (no parallelism) (integer) |
Details
Functions that process the on-disk genotype chunks independently
distribute the chunks over workers background R processes: read_vcf(),
the genotype-based filters, sample and group extraction, vcf_stats() and
the assess functions, vcf_bind(), write_vcf(), the vcf2*() exporters and
vcf2gt_long() with CSV output (with feather or Parquet output it runs
serially). Results do not depend on the number of workers. The workers are started once and reused
until vcf_set_workers(1) is called or the session ends.
Each worker holds one chunk in memory at a time, so memory use grows with
the number of workers: about one chunk's memory per worker (reported as
chunk_arrow_bytes by vcf_memory_estimate()). vcf_suggest_chunk_size()
takes the number of workers into account. Useful values are up
to the number of physical CPU cores; disk speed limits the gain beyond that.
Value
Invisibly, the previous number of workers.
Author(s)
Tomas Hrbek September 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
# 2 workers (CRAN examples may use at most 2 cores)
vcf_set_workers(2)
vcf <- read_vcf(f)
vcf_set_workers(1)
vcf_stats
Description
Calculates basic stats of each samples from VCFArrow format data. Includes average read depth per individual, missing data per individual, the inbreeding coefficient F, Watterson's theta and pi.
Usage
vcf_stats(
vcf_arrow,
res_path,
project,
theta = FALSE,
f_ref = c("all", "group")
)
Arguments
vcf_arrow |
-> VCFArrow object |
res_path |
-> directory where to write results |
project |
-> base name of the project file |
theta |
-> flag to perform theta and pi calculation, default FALSE (Boolean) |
f_ref |
-> allele frequencies for the inbreeding coefficient F: "all" (all samples, default, as vcftools and PLINK) or "group" (the sample's own group) (character) |
Details
This function calculates average read depth, heterozygosity, number of heterozygotes, number of reference and alternative homozygotes, missing data and total number SNPs of each sample in an VCFArrow object. The average read depth is the mean DP of the sample's called genotypes. Optionally calls vcf_theta() to get total and group Watterson's theta and pi.
The inbreeding coefficient F of each sample is the method-of-moments estimate of vcftools –het and plink –het, F = (O - E) / (N - E). It uses the biallelic SNVs at which the sample is called, at least two samples of the reference set are called and the reference set is polymorphic (N sites; monomorphic sites would not change F); O is the number of homozygous genotypes of the sample at these sites, and E the expected number, the sum of 1 - n / (n - 1) * 2p(1 - p) over the sites, where p is the ALT allele frequency and n the number of called alleles in the reference set. With f_ref = "all" the reference set is all samples; with f_ref = "group" it is the sample's group, which avoids the excess homozygosity that differences in allele frequency between groups cause when they are pooled (Wahlund effect). F is NA for samples without such sites (e.g. a group of one sample with f_ref = "group", or a sample without a group). The table gives F with N, O and E (F_sites, F_hom_obs, F_hom_exp), and for each sample the mean F of its group (F_group_mean; over the group's samples with an F).
The CSV file has one row per sample, sorted by group and sample, with the columns: sample; read_depth (mean DP of the called genotypes); heterozygosity (heterozygotes / called genotypes); heterozygotes; homozygotes, homozygotes_ref and homozygotes_alt; missing_p (proportion of missing genotypes), missing_n and non_missing; total_loci; F and F_group_mean; F_sites, F_hom_obs and F_hom_exp (N, O and E above). With theta = TRUE they are followed by theta_total and pi_total (Watterson's theta and pi of all samples) and theta_w and pi (of the sample's group). Then follow group and, with theta = TRUE, n_ind (the number of samples in the group).
Value
Invisibly returns the input VCFArrow object; called for its side effect of writing <project>_stats.csv in res_path.
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_stats(vcf, res_path = tempdir(), project = "vaillantii",
theta = TRUE)
vcf_sub_SNVs
Description
Randomly subsets SNVs from a VCFArrow object
Usage
vcf_sub_SNVs(vcf_arrow, n_SNVs = 10000, seed = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
n_SNVs |
-> number of SNVs to subset, default 10000 (integer) |
seed |
-> random number generator seed, default NULL (integer) |
Details
This function subsets a VCFArrow object to specific number of SNVs, returning new VCFArrow object. The seed for random number generator is automatically generated unless otherwise specified.
Value
VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_sub_SNVs(vcf, n_SNVs = 1000, seed = 42)
vcf_sub_SNVs_stratified
Description
Randomly subsets SNVs from a VCFArrow object
Usage
vcf_sub_SNVs_stratified(vcf_arrow, n_SNVs = 1000, seed = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
n_SNVs |
-> number of SNVs to subset, default 1000 (integer) |
seed |
-> random number generator seed, default NULL (integer) |
Details
This function subsets a VCFArrow object to specific number of SNVs, returning new VCFArrow object. The subsampling is stratified, i.e. the same proportion of SNVs per CHROM. Each CHROM contributes its proportional share of n_SNVs, rounded down; the SNVs still needed to reach n_SNVs come one each from the CHROMs with the largest remainders (ties broken at random), so exactly n_SNVs SNVs are returned. When most CHROMs hold a single variant (e.g. DiscoSnp-RAD output), this amounts to simple random sampling. The seed for random number generator is automatically generated unless specified.
Value
VCFArrow object
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_sub_SNVs_stratified(vcf, n_SNVs = 1000, seed = 42)
Suggest a chunk_size for read_vcf() given available RAM
Description
Suggest a chunk_size for read_vcf() given available RAM
Usage
vcf_suggest_chunk_size(
available_gb,
n_samples,
fmt_chars = 25,
sample_chars = 10,
workers = .vcf_workers(),
n_columns = NULL
)
Arguments
available_gb |
RAM available in gigabytes. |
n_samples |
Number of samples. |
fmt_chars |
Average length in characters of a sample's FORMAT text in the VCF (e.g. 24 for "0/1:12,8:20:99:200,0,300"), default 25. Longer FORMAT fields (e.g. PL with many alleles) need more memory. |
sample_chars |
Average length of the sample names, default 10. |
workers |
Number of worker processes read_vcf() will use (see vcf_set_workers()), default the number currently set. |
n_columns |
Deprecated and ignored: the size of a genotype row is now computed from its columns. |
Details
Half of available_gb is left for the operating system, R and later
operations; the other half is shared by the chunks read_vcf() holds at
once (one per worker). A chunk holds one row per variant and sample, of
about 44 + sample_chars + fmt_chars bytes in memory: .row_id, a1 and a2
(4 bytes each), the sample name and the sample's FORMAT text (each 4
bytes plus its characters), phased (1 bit), and DP, GQ and ADR (8 bytes
each). The FORMAT text is kept so write_vcf() can write the sample fields
unchanged; it is often the largest column. With workers, each chunk's
raw VCF lines (about fmt_chars + 1 bytes per genotype) are also held, by
the main process and by the worker parsing them. The result is limited to
between 1,000 and 500,000 variants.
Value
Invisibly, the suggested chunk size (integer).
Examples
vcf_suggest_chunk_size(available_gb = 8, n_samples = 100)
vcf_theta
Description
Calculates basic Watterson's theta and pi for all samples and for sample groups from VCFArrow format data.
Usage
vcf_theta(vcf_arrow, keep_groups = NULL)
Arguments
vcf_arrow |
-> VCFArrow object |
keep_groups |
-> groups to retain, default NULL (character) |
Details
This function calculates Watterson's theta and pi for the entire VCFArrow object, and for groups of individuals whose grouping is indicated by the groups slot in the VCFArrow object. Every sample must be assigned to a group (see set_vcf_groups()). Only biallelic SNVs are used, and a site is used for a group (valid) when at least two of its samples are called. With n called alleles and ALT allele frequency p at a site, the site's nucleotide diversity is n / (n - 1) * 2p(1 - p), the proportion of differing pairs among all pairs of called alleles (the unbiased estimator, as in vcftools –site-pi), and its Watterson contribution is 1 / a1 if the site is segregating, with a1 = sum(1 / i) for i = 1 .. n - 1. pi and theta are the means of these values over the valid sites. The statistics are accumulated chunk by chunk as running sums per group, so memory use does not grow with the number of variants, and chunks are processed in parallel when workers are set with vcf_set_workers().
Value
A list with pi and theta_w, nucleotide diversity and
Watterson's theta of all samples, and theta_g, a data frame with one
row per sample (sample, group, n_ind = number of samples in the group,
theta_w and pi of the group).
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
vcf_theta(vcf)
write_vcf
Description
Write a VCFArrow object to an external VCF file
Usage
write_vcf(
vcf_arrow,
out_file,
gzip = grepl("\\.gz$", out_file),
update_info = TRUE
)
Arguments
vcf_arrow |
-> VCFArrow object |
out_file |
-> name of the VCF file to be written to, no default (character) |
gzip |
-> a flag to GZIP VCF when writing, default TRUE if out_file ends in ".gz" (Boolean) |
update_info |
-> recompute the INFO fields AC, AN, AF and NS from the genotypes written, where present, default TRUE (Boolean) |
Details
This function writes a VCFArrow object to an external VCF file.
Writing occurs in chunks whose size is determined by the read_vcf() function.
Larger chunks result in faster writing speeds.
It writes both uncompressed and gz compressed files. Compressing increases
writing time by about 50%.
For large files, it is recommended to output an uncompressed VCF file,
and then compress with GZIP or PIGZ (or BGZIP, which tabix needs to
index the file).
The sample fields are written as stored; genotypes changed by VCFArrow
(e.g. by vcf_filter_adr() or vcf_filter_coverage()) have their GT updated.
With update_info = TRUE (the default), the INFO fields AC (count of each
ALT allele), AN (number of called alleles), AF (AC / AN) and NS (number of
samples with a called genotype) are recomputed from the samples and
genotypes written, wherever a variant's INFO has them, so they stay
correct after samples or genotypes were removed or changed; other INFO
fields are written unchanged. Haploid genotypes, which read_vcf() stores
as homozygous, count as two alleles.
Variants without a valid position (POS) are not written, with a warning.
A warning is also given if the variants are not sorted by position (e.g.
after vcf_bind(mode = "union")), since indexing tools such as tabix need
sorted files; bcftools sort sorts them.
A '##source=VCFArrow' line with the package version is added to the header.
Value
Invisibly returns the path of the written file.
Author(s)
Tomas Hrbek April 2026
Examples
f <- system.file("extdata", "vaillantii_discosnp_sub.vcf.gz",
package = "VCFArrow")
vcf <- read_vcf(f) |> set_vcf_groups(dirname(f))
write_vcf(vcf, out_file = tempfile(fileext = ".vcf"))
write_vcf(vcf, out_file = tempfile(fileext = ".vcf.gz"))