install.packages(c("vcfR", "tidyverse"))
# Install toolone from GitHub
# install.packages("devtools")
devtools::install_github("Breeding-Insight/BIGr")Tutorial 1: Introduction
beginner
R
setup
Getting started with genomic data analysis
Learning Objectives
By the end of this tutorial, you will be able to:
- Set up your R environment for genomic analysis
- Load and inspect a VCF file
- Understand the basic structure of variant data
Prerequisites
- R (≥ 4.0) installed
- RStudio (recommended)
- ~30 minutes
Setup
First, install the required packages:
Load the packages:
library(vcfR)
library(tidyverse)
library(toolone)Loading Data
From a file
# Read a VCF file
vcf <- read.vcfR("path/to/your/file.vcf.gz")
# Check what you loaded
vcfExpected output:
***** Object of Class vcfR *****
1000 samples
50000 variants
Object size: 125.3 Mb
Using example data
For this tutorial, we’ll use built-in example data:
data("example_vcf", package = "toolone")
vcf <- example_vcfInspecting the Data
Basic information
# How many samples?
ncol(vcf@gt) - 1
# How many variants?
nrow(vcf@fix)
# What chromosomes are present?
unique(vcf@fix[, "CHROM"])The VCF structure
A VCF file has three main components:
- Meta information (header lines starting with
##) - Fixed fields (CHROM, POS, ID, REF, ALT, QUAL, FILTER, INFO)
- Genotype data (sample-level information)
# View the fixed fields
head(vcf@fix)
# View genotype data for first few samples
vcf@gt[1:5, 1:5]
NoteVCF Format Details
For a complete description of VCF format, see the VCF specification.
Extracting Information
Get genotypes as a matrix
# Extract genotypes (GT field)
gt_matrix <- extract.gt(vcf, element = "GT")
# View dimensions
dim(gt_matrix)
# Preview
gt_matrix[1:5, 1:5]Get depth information
# Extract read depth (DP field)
dp_matrix <- extract.gt(vcf, element = "DP", as.numeric = TRUE)
# Summary statistics
summary(as.vector(dp_matrix))Quick Visualization
Let’s make a simple plot of read depth distribution:
# Flatten the matrix and plot
dp_values <- as.vector(dp_matrix)
dp_values <- dp_values[!is.na(dp_values)]
hist(dp_values,
breaks = 50,
main = "Read Depth Distribution",
xlab = "Depth",
col = "steelblue")
Summary
In this tutorial, you learned how to:
- ✅ Install and load required packages
- ✅ Read VCF files into R
- ✅ Inspect basic properties of variant data
- ✅ Extract genotype and depth matrices
- ✅ Create a simple visualization
Next Steps
Continue to Tutorial 2: Working with Data to learn about filtering and manipulating variant data.
Exercises
Try these on your own:
- Load your own VCF file and check how many samples and variants it contains
- Extract the
GQ(genotype quality) field and plot its distribution - Find the chromosome with the most variants
TipExercise 3 Solution
# Count variants per chromosome
table(vcf@fix[, "CHROM"]) |>
sort(decreasing = TRUE) |>
head()