gl.read.vcf: Converts a vcf file into a genlight object

View source: R/gl.read.vcf.r

gl.read.vcfR Documentation

Converts a vcf file into a genlight object

Description

This function needs package vcfR, please install it.

Usage

gl.read.vcf(
  vcffile,
  ind.metafile = NULL,
  mode = "genotype",
  fbm = FALSE,
  read.counts = FALSE,
  likelihoods = FALSE,
  verbose = NULL,
  parser = "vcfR"
)

Arguments

vcffile

A vcf file (works only for diploid data) [required].

ind.metafile

Optional file in csv format with metadata for each individual (see details for explanation) [default NULL].

mode

"genotype" all heterozygous sites will be coded as 1 regardless ploidy level, dosage: sites will be codes as copy number of alternate allele [default genotype]

fbm

Logical, if TRUE the dartR object will contain a file-backed matrix. this is important for large data sets that do not fit into RAM.

read.counts

Logical, if TRUE the reference and alternative read counts of each genotype are stored in the dartR object (see details). They take much more memory than the genotypes [default FALSE].

likelihoods

Logical, if TRUE the likelihoods of the three genotypes (FORMAT PL, or GL) of each genotype call are stored in the dartR object (see details). Only in "genotype" mode. They take 12 bytes per genotype, about 190 MB for 171 individuals and 93,000 loci [default FALSE].

verbose

Verbosity: 0, silent or fatal errors; 1, begin and end; 2, progress log; 3, progress and results summary; 5, full report [default 2, unless specified using gl.set.verbosity].

parser

"vcfR" reads the vcf with package vcfR (the whole file is held in memory as text); "cpp" reads the genotypes with the C++ parser of package tidypopgen, chunk by chunk straight into a file-backed matrix, which is much faster and needs little memory for large files. tidypopgen is not installed with dartR.base; install it with install.packages("tidypopgen") to use "cpp". See details for the limits of "cpp" [default "vcfR"].

Details

The ind.metadata file needs to have very specific headings. First a heading called id. Here the ids have to match the ids in the dartR object. The following column headings are optional. pop: specifies the population membership of each individual. lat and lon specify spatial coordinates (in decimal degrees WGS1984 format). Additional columns with individual metadata can be imported (e.g. age, gender). Individuals in the SNP data that are absent from the ind.metafile are retained in the returned object with NA metadata; a warning lists them at verbose >= 1.

Genotypes are coded as the count of the ALT allele: 0 = homozygous REF, 2 (or the copy number in dosage mode) = homozygous ALT. Note that this is the opposite orientation to gl.read.PLINK, which counts allele.2 (the PLINK 1.x major allele).

In "genotype" mode the returned object is coded as diploid; if the vcf contains haploid or polyploid genotype calls a warning is issued at verbose >= 1 (haploid calls are recoded as diploid homozygotes, polyploid heterozygous calls as 1). In "dosage" mode the ploidy of the object is set to the data's maximum copy number of the alternate allele, uniform across individuals. Note also that this function checks to see if there are input of mode, missing input of mode will issue the user with an error. Please carefully check the data if "dosage" mode is used.

When the vcf holds FORMAT DP, the mean read depth of each locus over its called genotypes is stored in x@other$loc.metrics$rdepth, where gl.report.rdepth and gl.filter.rdepth find it. Missing and half-missing calls (e.g. ./., 0/.) are not counted; some callers report a depth of 0 for them.

With read.counts = TRUE, x@other$geno.metrics holds two integer matrices of individuals x loci: ref.count and alt.count, the reads of the REF and ALT allele of each genotype. They are read from FORMAT AD or, for genotypes without AD, from FORMAT BSDP (read counts of the bases A, C, G and T) at the REF and ALT bases. Counts are stored as the vcf reports them, also for uncalled genotypes.

With likelihoods = TRUE, x@other$geno.metrics also holds pl.ref, pl.het and pl.alt: the phred-scaled likelihoods of 0/0, 0/1 and 1/1 for each genotype, as integer matrices of individuals x loci. They are read from FORMAT PL or, for genotypes without PL, from FORMAT GL as round(-10 * GL). Each genotype's likelihoods are shifted so that the most likely genotype has 0, as some callers write PL whose lowest value is above 0; likelihoods are defined only up to a constant, so nothing is lost. A likelihood of 0 (GL of -inf) is stored as the largest integer. Genotypes whose likelihoods are missing, do not number three (haploid or polyploid calls) or are all equal (e.g. ./.:0,0,0) get NA; uncalled genotypes with informative likelihoods keep them. Likelihoods are not stored in "dosage" mode.

The matrices in x@other$geno.metrics have no row or column names: row i belongs to indNames(x)[i] and column j to locNames(x)[j], so renaming individuals or loci cannot leave stale names on them. The matrices follow the genotypes when individuals or loci are subset, sorted or combined, and gl.randomize.snps swaps the REF and ALT read counts and the 0/0 and 1/1 likelihoods of the loci it recodes; gl.compliance.check removes them if their dimensions no longer match the object.

With parser = "cpp" (package tidypopgen) the genotypes are read by tidypopgen's C++ parser into a file-backed matrix and copied in blocks of loci into the returned object (file-backed with fbm = TRUE, converted to @gen in blocks otherwise); the fixed columns of the vcf (CHROM to INFO) are read in chunks as well, so the whole file is never held in memory. For the variants it reads, the result is the same as with parser = "vcfR": genotypes (count of the ALT allele), locus names, alleles, chromosome, position and the QUAL, FILTER and INFO locus metrics. Limits: only single-base (SNP) variants are read, so indels are skipped (with a warning); only mode = "genotype"; no rdepth, read counts or likelihoods; and only diploid genotype calls – a vcf with haploid or polyploid calls (e.g. a sex chromosome in the heterogametic sex) stops with an error, as tidypopgen would code a haploid call like a heterozygote. Use parser = "vcfR" in these cases.

Value

A genlight object.

Author(s)

Author(s): Bernd Gruber, Ching Ching Lau. Custodian: Bernd Gruber (Post to https://groups.google.com/d/forum/dartr)

Examples

## Not run: 
# read in vcf and convert to format as DArT data
obj <- gl.read.vcf(system.file('extdata/test.vcf',package='dartR'),
ind.metafile="metafile.csv")
# read in vcf and convert to format as dosage
obj <- gl.read.vcf(system.file('extdata/test.vcf',package='dartR'),
ind.metafile="metafile.csv",mode="dosage")

## End(Not run)

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