Welcome to selectiontest’s documentation!

Overview

This repository contains python modules and command line scripts to support testing for selective neutrality using relative likelihood. It enables application of the methods described in Simon and Huttley 2021 A New Likelihood-based Test for Natural Selection bioRxiv doi = 10.1101/2021.07.04.451068. For other software supporting that paper, but not required for applications, see https://github.com/helmutsimon/NeutralityTest.

Functions available include:

  • calculate statistic for relative neutrality, \({\rho }\), which is a relative likelihood of two models;
  • generate variates corresponding to the Wright-Fisher model and the `uniform distribution’ model;
  • calculate Tajima’s D (for comparison purposes);
  • calibrate \({\rho }\), that is, find the threshold corresponding to a desired false positive (Type I error) rate; and
  • generate variates corresponding to a piece-wise constant demographic history.
  • compute SFS from variant data in vcf format.

Installation

Basic install:

pip install git+https://github.com/helmutsimon/selectiontest

Install with development related libraries

pip install git+https://github.com/helmutsimon/selectiontest.git#egg=selectiontest[dev]

Python modules

Access Python modules by the following command or variations thereof:

import selectiontest
selectiontest.selectiontest.test_neutrality(sfs, variates0=None, variates1=None, reps=10000)

Calculate \(\rho\), the log odds ratio of the data for the distribution given by variates0 over the distribution given by variates1.

Parameters:
  • sfs (list) – Site frequency spectrum, e.g. [1, 3, 0, 2, 1]
  • variates0 (numpy array) – Array of variates from null hypothesis distribution. Default uses Wright-Fisher model.
  • variates1 (numpy array) – Array of variates from alternative distribution. Default uses `uniform’ model.
  • reps (int) – Number of variates to generate if default is used.
Returns:

\(\rho\) (value of log odds ratio). Values can include inf, -inf or nan if one or both probabilities are zero due to underflow error.

Return type:

numpy.float64

selectiontest.selectiontest.calculate_D(sfs)

Calculate Tajima’s D from a site frequency spectrum.

Parameters:sfs (list) – Site frequency spectrum, e.g. [1, 3, 0, 2, 1]
Returns:Value of Tajima’s D.
Return type:numpy.float64
selectiontest.selectiontest.sample_wf_distribution(n, reps)

Calculate variates for the probability distribution Q under Wright Fisher model.

Parameters:
  • n (int) – Sample size
  • reps (int) – Number of variates to generate if default is used.
Yields:

numpy.ndarray – Array of variates (n-1)

selectiontest.selectiontest.sample_uniform_distribution(n, reps)

Calculate variates for the uniform probability distribution Q.

Parameters:
  • n (int) – Sample size
  • reps (int) – Number of variates to generate if default is used.
Returns:

Array of variates (reps, n-1)

Return type:

numpy.ndarray

selectiontest.selectiontest.compute_threshold(n, seg_sites, sreps=10000, wreps=10000, fpr=0.02)

Calculate threshold value of \(\rho\) corresponding to a given false positive rate (FPR). For values of \(\rho\) above the threshold we reject the null (by default neutral) hypothesis.

Parameters:
  • n (int) – Sample size
  • seg_sites (int) – Number of segregating sites in sample.
  • sreps (int) – Number of SFS configs and of uniform variates to generate if default is used.
  • wreps (int) – Number of Wright-Fisher variates to generate if default is used.
  • fpr (float) – Selected FPR tolerance.
Returns:

Threshold value for log odds ratio

Return type:

numpy.float64

selectiontest.selectiontest.piecewise_constant_variates(n, timepoints, pop_sizes, reps=10000)

Generate variates corresponding to a piecewise constant demographic history.

Parameters:
  • n (int) – Sample size
  • timepoints (array-like) – Times at which population changes (in generations, backward from the present).
  • pop_sizes (array-like) – Population sizes between timepoints (only relative sizes matter.)
  • reps (int) – Number of variates to generate.
Yields:

numpy.ndarray – Variates

selectiontest.selectiontest.vcf2sfs(vcf_file, panel, coord, start, end, select_chr=True)

Get SFS from vcf data for given population and sequence. The panel file is used to select samples.

Parameters:
  • vcf_file (pyvcf class: Reader (https://pyvcf.readthedocs.io/en/latest/)) – Variant details
  • panel (pandas DataFrame) – Proband details
  • coord (str) – Coordinate (e.g. chromosome).
  • start (int) – Start position of sequence.
  • end (int) – End position of sequence.
  • select_chr (bool) – If True, sample first chromosome. If False, use both.
Returns:

  • list – Site frequency spectrum
  • int – Sample size
  • list – Names of variants common to all elements of the sample.

The module vcf2sfs uses the pyVCF library for VCF processing: see https://pypi.org/project/PyVCF/.

Command line interface

The following python modules can be run from the bash command line, using the prefix command ``st’’ as shown:

st test-neutrality sfs -r reps

st calculate-d sfs

st compute-threshold n seg_sites -r reps -f fpr

For the command line interface, sfs is entered as a string of integers separated by spaces (variadic argument), e.g. 1 3 0 2 1.

For more information, use ``st –help’’, ``st test-neutrality –help’’ etc.

In addition, there is a command st test-neutrality-from-vcf to calculate \({\rho }\) directly from VCF data, combining the modules vcf2sfs and test_neutrality.

For details use command ``st test-neutrality-from-vcf –help’’.

This requires vcf data and sample panel details in the format used by the 1000 Genomes Project at ftp://ftp.1000genomes.ebi.ac.uk/vol1/ftp/release/20130502/.

License and disclaimer

Copyright (c) 2020, Helmut Simon All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name selectiontest nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Source

Source code is hosted at https://github.com/helmutsimon/selectiontest.

Citation

Bibtex:

@article{simon2021new,
title={A new likelihood-based test for natural selection},
author={Simon, Helmut and Huttley, Gavin A.},
journal={bioRxiv},
year={2021},
doi= {10.1101/2021.07.04.451068}
publisher={Cold Spring Harbor Laboratory}

}

Indices and tables