Chapter 5: Filtering

Preamble

Learning Outcomes

professor darter iconThe objective of this chapter is to introduce you to the options available for filtering your data based on an assessment of quality using the report functions introduced in Chapter 4.

After completing this chapter you should have a basic understanding of the fundamentals of the dartRverse functions for filtering SNPs and SilicoDArT calls based on such criteria as call rate, reproducibility, read depth, monomorphic loci and many other attributes. In some cases, individuals may be filtered.

Most importantly, you should be empowered to expand your knowledge beyond the basics presented here by exploring the help on these topics and associated scripts, and by applying the knowledge in your work.

Prerequisites

r studio logo

RStudio is an integrated development environment (IDE) with a graphical user interface (GUI) used to manage R workflow and so to work through this chapter, you will need to have already completed Chapter 1. RStudio Refresher. For more advanced treatment refer to the official documentation.

a bird running

 

You should have completed Chapter 3 on data entry and data structures and Chapter 4 on data manipulations before attempting this chapter.

 

a bird listeningYou may also find it helpful to listen to the podcast summary of the overview material in this chapter.

Basic Filtering (mp3)

Workflow

a bird running

We assume that you have established the RStudio project eBook1 and downloaded the datasets required for all chapters. Package eBook_Startup has been installed. All ready to go.

The workflow from this point again follows a formula.

  • Theory, unencumbered by competing options and technical asides;
  • Worked Examples where you are led through analyses with sample code;
  • More Theory, this time covering caveats and nuances.
  • Exercises where you are left on your own to integrate the knowledge you have gained in a problem-solving context.
  • Review the learning outcomes and the section on Where have we come? to confirm that you are moving to the next chapter with all that is required under your belt.

Accessing Worked Examples and Exercises

We covered how to access the worked examples and exercises in Chapter 1. Briefly, if you have not done so already:

 

source("http://georges.biomatix.org/storage/app/media/eBook1/eBook_data_download.txt")
install.packages("dartRverse")
library(dartRverse)
gl.set.verbosity(3)
install.packages(c("remotes", "learner")
remotes::install_github("Biomatix/eBook1")
library(eBook1)

Session 5-1: Basic Filtering

Overview

professor darter icon

Calling SNPs in genotyping by sequencing is not an exact science. Judgements need to be made at various points in the workflow to increase the likelihood of a correct call at a particular locus. Diversity Arrays Technology Pty Ltd (DArT) has already done much of the filtering of the sequences used to generate your SNPs that would normally be undertaken by researchers who generate their own ddRAD data. Here we present some other filters that you might wish to apply to increase the reliability of the data retained in your SNP or SilicoDArT dataset.

For each filter parameter offered by dartRverse, we provide both a report function and a filtering function (e.g. gl.report.reproducibility is associated with gl.filter.reproducibility). The reporting function provides descriptive statistics for the parameter of choice. Inspection of these outputs will assist you to identify appropriate filter thresholds. Hence, as a standard workflow, it is a good idea to run the gl.report functions in advance of applying each gl.filter function to provide a foundation for selecting thresholds.

Several filters are available to improve the quality of the data represented in your genlight object. Some of these are specific to Diversity Arrays data (e.g. gl.filter.reproducibility), but most will work on any data provided they are in the appropriate genlight format.

The basic ones, in no particular order, are:

gl <- gl.filter.reproducibility() filter out loci for which the reproducibility (strictly repeatability) is less than a specified threshold, say threshold = 0.99
gl <- gl.filter.callrate() filter out loci or individuals for which the call rate (rate of non-missing values) is less than a specified threshold, say threshold = 0.95
gl <- gl.filter.monomorphs() filter out monomorphic loci and loci that are scored all NA
gl <- gl.filter.allna() filter out loci that are all missing values (NA)
gl <- gl.filter.secondaries() filter out SNPs that share a sequence tag, except one retained at random [or the best based on reproducibility (RepAvg) and information content (AvgPIC)].
gl <- gl.filter.hamming() filter out loci where the corresponding sequence tags differ from each other by less than a specified number of base pairs
gl <- gl.filter.rdepth() filter out loci with exceptionally low or high read depth (coverage)
gl <- gl.filter.taglength() filter out loci for which the tag length is less than a threshold
gl <- gl.filter.overshoot() filter out loci where the SNP location lies outside the trimmed sequence tag
gl <- gl.filter.sexlinked() filter out loci that are likely to be in sex chromosomes

The order of filtering can be important and requires some thought. As an example, filtering on call rate by individual before filtering on call rate by locus or choosing the alternative order will depend on the weight placed on losing individuals versus losing loci. The filtering steps may also change depending on the analysis you intend to run. There is no one-fits-all approach to SNP filtering. Refer to Session 2.

An Example: Filtering on reproducibility

professor darter icon

As an additional quality control measure, a selection of samples is processed twice by DArT using independent adaptors from DNA through to allelic calls. The consistency of scoring across these technical replicates is referred to as reproducibility. Let’s examine the distribution of reproducibility measures (RepAvg) in our dataset.

 

gl.report.reproducibility(testset.gl,plot=TRUE)

Starting gl.report.reproducibility

Processing SNP data

Reporting Repeatability by Locus

No. of loci = 255

No. of individuals = 250

Minimum : 0.959459

1st quartile : 1

Median : 1

Mean : 0.9981525

3r quartile : 1

Maximum : 1

Missing Rate Overall: 0.12

Quantile Threshold Retained Percent Filtered Percent
1 100% 1.0000000 214 83.9 41 16.1
2 95% 1.0000000 214 83.9 41 16.1
3 90% 1.0000000 214 83.9 41 16.1
4 85% 1.0000000 214 83.9 41 16.1
5 80% 1.0000000 214 83.9 41 16.1
6 75% 1.0000000 214 83.9 41 16.1
7 70% 1.0000000 214 83.9 41 16.1
8 65% 1.0000000 214 83.9 41 16.1
9 60% 1.0000000 214 83.9 41 16.1
10 55% 1.0000000 214 83.9 41 16.1
11 50% 1.0000000 214 83.9 41 16.1
12 45% 1.0000000 214 83.9 41 16.1
13 40% 1.0000000 214 83.9 41 16.1
14 35% 1.0000000 214 83.9 41 16.1
15 30% 1.0000000 214 83.9 41 16.1
16 25% 1.0000000 214 83.9 41 16.1
17 20% 1.0000000 214 83.9 41 16.1
18 15% 0.9945940 216 84.7 39 15.3
19 10% 0.9883732 229 89.8 26 10.2
20 5% 0.9883732 242 94.9 13 5.1
21 0% 0.9594590 255 100.0 0 0.0

Completed: gl.report.reproducibility

 

This output is useful in that it provides an indication of how much data will be lost, when filtering, for each choice of a threshold value (Figure 5-1). Clearly, with a minimum reproducibility of 0.96 and a maximum of 1 across loci, we can be fairly stringent in our choice of a threshold without shedding many loci. A value of 0.99 will not result in the loss of much data (5%). The judgement can also be made on the basis of the graphical output.

You may be tempted on the basis of this graph to filter very stringently on a threshold of 1 (perfect reproducibility). The DArT team recommend against this as it could potentially introduce an ascertainment bias.

Figure 5-1. Standard graphic produced by the report reproducibility function showing the quality of the SNP loci based on technical replicates.

We now filter by selecting a threshold value of 0.99 (Figure 5-2).

gl <- gl.filter.reproducibility( testset.gl, threshold=0.99, verbose = 3)
Starting gl.filter.reproducibility
  Processing a SNP dataset
  Identifying loci with repeatability below : 0.99
  Removing loci with repeatability less than 0.99

  Summary of filtered dataset
  Retaining loci with repeatability >= 0.99
  Original no. of loci: 255
  No. of loci discarded: 14
  No. of loci retained: 241
  No. of individuals: 250
  No. of populations: 30

Completed: gl.filter.reproducibility

Figure 5-2. Standard graphic produced by the filter on reproducibility function showing the quality of the SNP loci based on technical replicates before and after filtering.

Only 14 loci out of 255 were removed.

Worked Examples: Basic Filtering

a bird running

This worked example uses the earless dragon dataset that we input and then saved in Chapter 3. In this example, we illustrate the application of functions filtering SNP and SilicoDArT data.

Open the RStudio project you set up in Chapter 3, namely eBook1.

Set the global verbosity to 3 so you get to see the full progress log when you submit commands.

You will be asked to load the earless dragon dataset that was created in Chapter 3, namely tympo_SNP_raw.RData.

To access the Worked Examples, navigate to the RStudio Tutorial Pane and then open the session eBook.

Undertake Worked Example 5-1.

Where have we come?

a bird celebrating

The above Session was designed to give you an overview of the scripts in dartRverse for filtering your data.

Having completed this Session, you should now be able to:

  • Filter on call rate, reproducibility, secondaries, read depth, sex linkage and hamming distance.
  • Recalculate locus metrics after deleting individuals or populations as part of the filtering process.
  • Filter out resultant monomorphic loci.

Having played with the various filters, you should also have a good appreciation of how to use the gl.report functions to select appropriate thresholds for filtering and how to introduce a sequence of filtering steps into your workflows.

Session 5-2: Nuances of Filtering

professor darter icon

dartRverse is designed to streamline workflows and analyses. This does not relieve the user from applying judgement based on sound biological and scientific understanding. Analyses rest on assumptions, models and limitations. This applies to filtering. There is no one recipe for applying filters to SNP and SilicoDArT data. Context is important, both in terms of the scenario in which the data were collected and plans for future analysis. This section attempts to alert you to the considerations that are required in pre-filtering using dartRverse.

Pre-dartRverse Filtering

Sequences generated by DArT are processed using proprietary analytical pipelines before the report (containing the SNP and SilicoDArT markers) is provided to the client and the data are passed to dartRverse.

In the DArT workflow, poor quality sequences are first filtered, applying more stringent selection criteria to the barcode region than to the rest of the sequence (minimum barcode Phred score 30, pass percentage 75; minimum whole-read Phred score 10, pass percentage 50). In that way, assignment of the sequences to specific samples in the sample disaggregation step is very reliable. In a report based on full sequence volume (high), approximately 2,500,000 (±7%) sequences per sample are identified and used in marker calling.

These sequences are truncated to 69 bp (including some adaptor sequence where the fragments were shorter than 69 bp) and aggregated into clusters by the DART fast clustering algorithm. A Hamming distance threshold of 3 bp is used as a threshold for identifying distinct sequence, taking advantage of the fixed fragment length.

The sequences are then error-corrected using an algorithm that corrects a low-quality base (Phred score <20) to a corresponding high-quality singleton tag (Phred score >25); where there is more than one distinct high-quality tag, the sequence with the low-quality base is discarded.

Identical sequences are then collapsed. These error-corrected sequences are analysed using DART software (DArT DS14 pipeline) to output candidate SNP markers. SNP markers are identified within each cluster by examining parameters calculated for each sequence across all samples – primarily average and variance of sequencing depth, the average counts for each SNP allele and the call rate (proportion of samples for which the marker is scored).

Where three sequences survive filtering to this point, the two variants with the highest read depth are selected. The final average read depth per locus depends on the genome size, the degree of representation delivered by the chosen restriction enzymes and the sequencing intensity (sequence volume).

As an additional quality control, a selection of samples (30 to 100% of samples depending on the number of plates submitted) is processed twice using independent adaptors from DNA through to allelic calls. These are technical replicates. Scoring consistency (reproducibility or more strictly, repeatability) is used as the main selection criterion for high quality/low error rate markers.

The DArT analysis pipelines (DS14) have been tested against hundreds of controlled crosses to verify the Mendelian behaviour of the resultant SNPs, and the parameter thresholds have been fine tuned as part of their commercial operations.

The bottom line is that, although analytical pipelines are proprietary and commercial in confidence, a substantial amount of filtering of sequence tags is undertaken before you receive the SNP and SilicoDArT markers and report from DArT, filtering that ensures quality outcomes in terms of the veracity of the resultant SNPs.

Package dartR picks up the analysis at this point. The additional filtering that you may choose to undertake using dartR depends upon the research questions and other considerations as outlined below.

Stacks Pre-Filtering

professor darter icon

A full protocol for calling SNPs from reduced representation data has been published — Rochette N and Catchen J (2017) ‘Deriving genotypes from RAD-seq short-read data using Stacks’, Nature Protocols, 12(11):2640–2659. Please refer to that article and the Stacks V2 Manual.

Package dartR picks up the analysis after the SNPs have been called in Stacks. The additional filtering that you may choose to undertake using dartR depends upon the research questions and other considerations as outlined below.

Filtering Order

professor darter icon

It is often unclear as to what order the filtering steps should be performed in your dartR workflow. Does one filter on call rate by individual first, then call rate by locus, or the other way around? There is no correct answer to this, as it depends on whether you value retaining loci over retaining individuals.

Usually, you would not want to lose individuals from your dataset, so filtering out those loci with low call rates would come first, filtering on individuals second, though this is not always the case. A starting point for considering a workflow might be:

  1. Filter on sex linkage.
  2. Optionally filter on read depth if the Diversity Arrays threshold is considered too lenient.
  3. Filter out loci with a reproducibility below a particular threshold (say 0.98).
  4. Filter out loci with a call rate below a particular threshold (say 0.95).
  5. Filter out individuals with a call rate below a particular threshold (say 0.80).
  6. Filter out secondaries (all but one SNP retained per sequence tag).
  7. Optionally filter for monomorphs created by the removal of individuals.

An example of a filtering sequence might be

gl <- gl.filter.sexlinked(gl, system="xy")
gl <- gl.filter.rdepth(gl)
gl <- gl.filter.reproducibility(gl)
gl <- gl.filter.callrate(gl, method="loc")
gl <- gl.filter.callrate(gl, method="ind")

gl <- gl.filter.secondaries(gl)
gl <- gl.filter.monomorphs(gl)

Filtering Thresholds

Strategy

professor darter icon

The take-home message from this section is to be careful not to over-filter. The objective is to achieve an appropriate balance between signal-to-noise ratio and not eliminating noise altogether at the expense of losing some signal. This balance will depend on sequence intensity (volume in the context of genome size), sample quality and most importantly, downstream applications.

There is no right answer. A good strategy is to undertake an exploratory analysis first, whereby you experiment with filtering. Perhaps start with very stringent filtering, examine the impact on the final analysis, then progressively relax the stringency to select the minimal filtering regime that still delivers a stable outcome. Play with your data to get a feel for the balance between signal to noise ratio, in the context of the analyses you propose to subsequently undertake.

What is sequence volume?

Three services are routinely offered by DArT, differing in the volume of sequence generated. For genomes larger than 1 Gbp, a sequence volume of 2.5 million is recommended (high). For genomes ranging from 300 Mbp to 1 Gbp, a sequence volume of 1.2 million is recommended (medium). For genomes less than 300 Mbp, a sequence volume of 800,000 is recommended (low).

The average read depth of the sequence tags depends upon the genome size, the sequencing volume (low, medium, high), the proportion of the genome that is represented after the restriction enzymes are applied and after size selection, and the proportion of mis-targeted sequence in the sample (e.g. arising from bacterial contamination). Average read depth per sequence tag is calculated by dartRverse using gl.report.rdepth().

Average read depth can be obtained using:

mean(gl@other$loc.metrics$rdepth)

This is reported also by gl.report.basics().

Filtering on Reproducibility

As mentioned above, a proportion of samples is processed twice, using independent adaptors, from DNA through to allelic calls. These are technical replicates and their scoring consistency (repeatability) is used as the main selection criterion for high-quality/low error-rate markers. Reproducibility values may depend on the number of plates run in a particular service, because the proportion of samples selected for technical replication will vary (usually 60-70%, but as high as 100% (partial plate) or as low as 30%).

While it is tempting to filter on 100% reproducibility, this is typically over-kill. One needs to appreciate that the reproducibility of technical replicates can depend on the quality of the input DNA. If it is contaminated, say with bacterial DNA or a blood parasite, then the target sequence volume can be dramatically reduced, the error-rate inflated and the reproducibility of technical replicates across samples for a locus substantially affected. Furthermore, the presence of inhibitors can affect the efficiency of restriction enzymes in a context specific way, again leading to inflation of the error rate and systematic differences between technical replicates.

These factors, contamination and presence of PCR inhibitors, have a greater influence on heterozygous than homozygous sites, and so differentially affect loci that are more polymorphic than others. Furthermore, experience shows that 90% of the resultant error rates occur in 10% of samples; filtering too stringently will lead to loss of loci that are otherwise reproducible for 90% of samples.

DArT has already used technical replicates in their earlier pipelines to select reliable markers to yield a basic level of reproducibility in the data provided to you.

So by all means filter on reproducibility, but consider the consequential loss of loci and do not filter so stringently as to decimate your locus count, as this can lead to systematic biases and unnecessary loss of informative loci. You should carefully consider the plot of the distribution of reproducibility values in the exploratory analysis (gl.report.reproducibility).

Filtering on Call Rate

When filtering loci on Call Rate, one needs to be aware that failure to call a SNP at a given locus is typically a result of a mutation at one or both of the restriction sites. That is, a missing value typically represents a null allele, captured in the companion SilicoDArT dataset. In the SNP dataset, highly polymorphic regions of the genome, those with an abundance of informative SNPs, are also most prone to null alleles. Hence, SNPs in highly polymorphic regions will be differentially eliminated by overzealous filtering on Call Rate.

Again, by all means filter loci on Call Rate, but consider the consequential loss of loci and do not filter so stringently as to decimate your locus count, as this can lead to systematic biases (highly polymorphic loci differentially eliminated) and unnecessary loss of informative loci. Carefully consider the plot of the distribution of Call Rate values in the exploratory analyses (gl.report.callrate). Depending upon the subsequent analyses (see below), a Call Rate threshold of 0.90 might be considered appropriate for filtering loci.

There is no strict rule when filtering individuals on Call Rate. One should examine which individuals are likely to be lost for a given threshold and consider the impact of their loss on the subsequent analysis and interpretation. Of course, if individuals have exceptionally low Call Rates, their deletion would be highly recommended. If such individuals are of particular importance to the study, re-extraction and re-running the samples should be considered.

Imputation

Some analyses are intolerant of any missing values (e.g. classical Principal Components Analysis). Removing all missing values is often too wasteful of data – one must either remove all loci that contain one or more missing values, or remove all individuals that contain one or more missing values. An alternative is to impute the missing values. There are a number of approaches to imputation.

  • Global Imputation: Missing values are replaced by an estimate of the allelic value generated from the average allele frequencies for all the data taken collectively.
  • Local Imputation: Missing values are replaced by an estimate of the allelic value generated from the average allele frequencies or from a Hardy-Weinberg model at that locus in the population from which the individual was drawn.
  • Nearest Neighbour: Missing values are replaced by the value at the corresponding locus for the nearest neighbouring individual (nearest using Euclidean genetic distance)
  • Random: Missing data are substituted by random values (0, 1 or 2).

The appropriate statement is

gl <- gl.impute(gl)

Filtering on Read Depth

Read depth has a considerable influence on the veracity of SNP calls. Read depth estimates for sequence tags generated from high-volume sequencing typically range from 3 to 1000 for single-copy sequence. The low values arise because of chance variation in the coverage of particular bases. The high values arise because of the differential efficiency of the PCR steps. Under some circumstances, it might be sensible to push the lower threshold for read depth higher than has been used by DArT in their report. Analyses that rely heavily on the accuracy of calls of heterozygotes may require a higher threshold for read depth, say 10x, for example. The call is

gl.filter.rdepth(gl,threshold=10)

Of course, how far you can push the read depth threshold depends very much on the sequencing volume of the service taken in the context of the genome size. You should examine the average read depth for your data in making this decision.

Influence of Planned Analyses

Possibly the most influential consideration on filtering is the purpose for which the filtered dataset is to be used.

If you are planning to generate high-resolution linkage maps, then highly stringent filtering is warranted, and DArT would adjust their pipelines accordingly upon request. Similarly, if you are contemplating an analysis of relatedness or inbreeding, then stringent filtering might be warranted. In any case, it would be wise to start out with high stringency and then progressively relax the stringency and examine the impact on outcomes.

If your focus is on identifying sex-linked markers, which rely heavily on identifying markers that are heterozygous in XY individuals and homozygous in XX individuals, then a filtering threshold for read depth of 10x would be sensible. Sequencing volume in the context of genome size will provide an upper limit to how far you can push the read depth threshold, so in some cases, a high sequence volume service will need to be requested. Because the pipelines of DArT depend in part on Mendelian behaviour in the selection of SNP markers, and sex-linked loci do not behave in Mendelian fashion, you might also ask them to relax the stringency of some aspects of their filtering in generating the report.

Obviously, if your focus is on identifying sex-linked markers, then do no filtering before using our functions to identify sex linked loci.

The proof is in the pudding when searching for sex linked markers, so if you get the balance of considerations wrong, the cost lies in the number of false positives that will be generated, and additional work at the validation stage.

If your focus is on Genome-wide Association Studies (GWAS) or identifying loci under selection, then noise in the data will not associate with phenotype, and so filtering can be less stringent in order to maximise the chances of identifying promising associations.

Principal Components Analysis (PCA) and, to a lesser extent, Principal Coordinates Analysis (PCoA), rely on fully populated data matrices (no missing values). Classical PCA for example, cannot easily accommodate missing values. To overcome this, a balance must be struck between filtering on Call Rate and imputing the values of those missing values that remain. More stringent filtering on Call Rate, and less imputation; but more stringent filtering on Call Rate, more loss of potentially useful information or valuable samples. Refer to Georges et al. (2023, BioRxiv) for further discussion of this point.

Conclusion

There is no hard and fast rule to guide decisions on filtering SNP datasets prior to a substantive analysis. The decisions are based on comparing the distribution of the parameters to be filtered (using one of the gl.report functions) and the purpose to which the filtered dataset is to be put. Sequencing volume can constrain options, and the likelihood of some level of contamination of samples or presence of inhibitors of the restriction enzymes can have a bearing on the decisions. It is important not to over-filter because of the risk of introducing a level of systematic bias and because of the unnecessary loss of informative loci or individuals. An experimental approach is recommended, whereby different filtering regimes are tried (from stringent to less stringent) and checked for influence on analysis outcomes.

Exercises

a bird exercising

 

Now is a good time to try some exercises and to try out what you have learned so far on your own data. These exercises ask you to analyse some SNP and SilicoDArT datasets to see what you have learned so far.

 

Navigate to the RStudio Tutorial Pane and then open the session eBook. Navigate to the Exercises for Chapter 5. Undertake the exercises.

Winding up

Where have we come?

a bird celebrating

The above Session was designed to give you an overview of the considerations that come into play when filtering your data using dartRverse.

Having completed this Session, you should:

  • Appreciate to some degree the pre-processing that is undertaken by DArT before you receive your report.
  • Understand some of the considerations that come into play in deciding the order in which to apply dartRverse filters.
  • Be able to apply the filtering in a nuanced manner to avoid over-filtering, which depends of course on the particular research question you are addressing as much as attributes of the data.

Further reading

a bird reading

O’Leary SJ, Puritz JB, Willis SC, Hollenbeck CM and Portnoy DS (2018) ‘These aren’t the loci you’re looking for: Principles of effective SNP filtering for molecular ecologists’, Molecular Ecology, 27(16):3193-3206, https://doi.org/10.1111/mec.14792.

 

 

Ende

Licence

Icon for the Creative Commons Attribution-NonCommercial 4.0 International License

Getting started with dartRverse Copyright © by University of Canberra is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where otherwise noted.