This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Subsetting a VCF by a logical vector

Hello,

I'd like to subset variants in my VCF using a logical vector with indices corresponding to variant indices. I started familiarising myself with the vcfR package: I load my VCF into R, perform some custom operations to obtain the logical vector (note: the point is NOT simple filtering by QUAL, DP etc.), and then I'm stuck. Any suggestions?

(I'm not providing a reproducible example since this should be a general feature of the package I'm probably missing?)

Thanks in advance!

vcf vcfr r

1 answer

We can subset the same way as we do with data.frames, for example:

# load example vcf
library(vcfR)
pkg <- "pinfsc50"
vcf_file <- system.file("extdata", "pinf_sc50.vcf.gz", package = pkg)
vcf <- read.vcfR( vcf_file, verbose = FALSE )

# check - we have 22031 variants
vcf
# ***** Object of Class vcfR *****
# 18 samples
# 1 CHROMs
# 22,031 variants
# Object size: 22.4 Mb
# 7.929 percent missing data
# *****        *****         *****

# rows to subset
rowNumber <- c(1, 3, 5)

# subset, keep only 3 rows
vcf@fix <- vcf@fix[ rowNumber, ]
vcf@gt <- vcf@gt[ rowNumber, ]

# check - now we have 3 variants
vcf
# ***** Object of Class vcfR *****
# 18 samples
# 1 CHROMs
# 3 variants
# Object size: 0 Mb
# 7.407 percent missing data
# *****        *****         *****

Log in to answer this question.