VCF Filtering and PCA Workflow

Create a symbolic link to the variants file:

ln -s /data/PwaniGenomics/variants.vcf.gz

The variants file can also be downloaded from the Wildlife Genomics 4 Zenodo dataset .

1. Apply Base Quality, Genotype Quality, INDELS, and "PASS" Filters

Apply quality-based filters to retain high-confidence variants. Remove all variants with Q < 30, GQ < 30, remove all INDELs, and retain only variants that are flagged as "PASS".

2. Remove Non-neutral Loci

Remove any variant that deviates from HWE with a chi-square P value of less than 0.01.

3. Remove Alleles That Appear Less Than 3 Times

Extremely rare alleles can sometimes be sequencing artefacts. For example, an allele that appears only once or twice in the dataset could be caused by a sequencing error. Removing alleles that occur fewer than 3 times helps reduce the influence of extremely rare variants.

4. Apply Individual Missingness Filter

Remove individuals with more than 98% missing data.

5. Record Variant Counts for Different Site Missingness Thresholds

Extract and save the number of variants remaining at each missingness threshold from 0.1 to 0.9.

6. Apply Site Missingness Filter

Filter out variants that are not commonly genotyped across individuals.

7. Plot variant_counts.txt Using ggplot2 in R

8. Apply Final Missingness Filter (Max Missing = 0.5)

This retains variants that are present in at least 50% of individuals.

9. Extract Depth Values Between 2.5th and 97.5th Percentiles

Use R or Python to compute the 0.025 and 0.975 quantiles and filter sites accordingly.

10. Use Site Mean Depth Filter

Calculate the mean depth per variant.

11. Apply Depth Filters to Retain Only Mid 95% Variants

12. Perform PCA

Convert the filtered VCF file to PLINK format and perform PCA.

Extra Note

The Wildlife Genomics 4 dataset contains the pre-generated variants file used for this exercise.

Variants file: variants.vcf.gz

Zenodo dataset: https://zenodo.org/records/21871083