Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 77 additions & 121 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,165 +1,121 @@
# grapp

A library and command-line tool for tackling problems in statistical and population genetics, implemented
on top of the Genotype Representation Graph (GRG) format. [GRG](https://github.com/aprilweilab/grgl) is
a file format and data structure that losslessly represents a genetic dataset. It has the advantage of
compressing large datasets significantly, while also making calculations over that dataset extremely fast
(see [the paper](https://www.nature.com/articles/s43588-024-00739-9) and the
[main GRG documentation](https://grgl.readthedocs.io/en/latest/index.html)).
`grapp` provides statistical and population genetics analyses that natively use the [GRG](https://github.com/aprilweilab/grgl)
format. GRG is an order of magnitude smaller than other representations, and can be multiple orders of
magnitude faster for calculations, such as PCA.

**Statgen features**: GWAS, PCA, phenotype simulation (via [grg\_pheno\_sim](https://github.com/aprilweilab/grg_pheno_sim)), linear mixed models, filtering samples and variants.

**Popgen features**: Polarization, integration with [scikit-allel](https://scikit-allel.readthedocs.io/en/stable/index.html) for various SFS and population statistics, conversions between [tskit.TreeSequence](https://tskit.dev/tskit/docs/stable/python-api.html#tskit.TreeSequence) files and various other formats.

`grapp` provides a command-line interface (ala plink or GCTA), and also a Python API that integrates with [numpy](https://numpy.org/), [scipy](https://scipy.org/),
and [pandas](https://pandas.pydata.org/). Of particular interest to method developers, `grapp` provides [linear operators](https://grapp.readthedocs.io/en/latest/reference/grapp.html#linear-algebra)
that are compatible with [scipy.sparse.linalg](https://docs.scipy.org/doc/scipy/reference/sparse.linalg.html) and [other compatible libraries](https://grapp.readthedocs.io/en/latest/overview/compatible.html),
which enables iterative numerical algorithms to scale to datasets with a million samples and hundreds of millions of variants.
Some notes on usage are below, and you can also browse [the Python documentation for grapp](https://grapp.readthedocs.io/en/latest/).

Check out the [command-line cheatsheet](https://github.com/aprilweilab/grapp/blob/main/CHEATSHEET.md) for examples on how to perform many tasks.
There are also some tutorials in the [GRG documentation](https://grgl.readthedocs.io/en/latest/tutorials/index.html) (and correspond [Jupyter notebooks](https://github.com/aprilweilab/grgl/tree/main/doc/tutorials/notebooks)).

See [the GRG paper](https://www.nature.com/articles/s43588-024-00739-9) and the [main GRG documentation](https://grgl.readthedocs.io/en/latest/index.html)
for more details about Genotype Representation Graphs.

## Installation

```
pip install grapp
```

## Modules
## Functionality

### assoc
### GWAS

Perform association tests between phenotypes and genotypes.

#### Command Line
* Command: `grapp assoc [-h] [-p PHENOTYPES] [-b] [-c COVARIATES] [-o OUT_FILE] [-r] [-s] [-j JOBS] grg_input [grg_input ...]`
* Python: [grapp.assoc.linear\_assoc\_covar()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.assoc.linear_assoc_covar), [grapp.assoc.linear\_assoc\_no\_covar()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.assoc.linear_assoc_no_covar)

```
usage: grapp assoc [-h] [-p PHENOTYPES] [-c COVARIATES] [-o OUT_FILE] grg_input

positional arguments:
grg_input The input GRG file

options:
-h, --help show this help message and exit
-p PHENOTYPES, --phenotypes PHENOTYPES
The file containing the phenotypes. If no file is provided, random phenotype values are used.
-c COVARIATES, --covariates COVARIATES
Covariates text file to load
-o OUT_FILE, --out-file OUT_FILE
Tab-separated output file (with header); exported Pandas DataFrame. Default: <grg_input>.assoc.tsv
```
### PCA

#### Library
Principal Component Analysis (PCA) using iterative methods.

There are methods for GWAS with (`grapp.assoc.linear_assoc_covar`) and without covariates (`grapp.assoc.linear_assoc_no_covar`).
* Command: `grapp pca [-h] [-d DIMENSIONS] [-o PCS_OUT] [--normalize] [--pro-pca] grg_input`
* Python: [grapp.linalg.PCs()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.linalg.PCs), [grapp.linalg.eigs()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.linalg.eigs)

### linalg
### BOLT-LMM

Linear algebra functionality that integrates GRG with [numpy](https://numpy.org/) and [scipy](https://scipy.org/). The main workhorses behind this module are
the operators compatible with [scipy.sparse.linalg.LinearOperator](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.linalg.LinearOperator.html#scipy.sparse.linalg.LinearOperator).
[BOLT-LMM](https://alkesgroup.broadinstitute.org/BOLT-LMM/BOLT-LMM_manual.html) reimplemented on GRG.

#### Command Line
* Command: `grapp bolt-lmm [-h] -p PHENOTYPES [-c COVARIATES] [--covar-cols [COVAR_COLS ...]] [--q-covar-cols [Q_COVAR_COLS ...]] [--covar-max-levels COVAR_MAX_LEVELS] [--num-calib-snps NUM_CALIB_SNPS] [--mc-trials MC_TRIALS] [--cg-tol CG_TOL] [--max-iter MAX_ITER] [--seed SEED] [-j JOBS] [-o OUT_FILE] grg_input [grg_input ...]`
* Python: [grapp.assoc.bolt\_lmm\_inf()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#module-grapp.assoc)

The Principal Component Analysis (PCA) is available via the command line:
```
usage: grapp pca [-h] [-d DIMENSIONS] [-o PCS_OUT] [--normalize] [--pro-pca] grg_input

positional arguments:
grg_input The input GRG file

options:
-h, --help show this help message and exit
-d DIMENSIONS, --dimensions DIMENSIONS
The number of PCs to extract. Default: 10.
-o PCS_OUT, --pcs-out PCS_OUT
Output filename to write the PCs to. Default: "<grg_input>.pcs.tsv"
--normalize Normalize the PCs according to sqrt(eigenvalue) for each.
--pro-pca Use the ProPCA algorithm to compute principal components.
```
### Filtering

Filtering variants and samples, with syntax similar to that of `bcftools`.

* Command: `grapp filter [-h] [-j JOBS] [-S INDIVIDUALS | --hap-samples HAP_SAMPLES | -P POPULATIONS] [-r RANGE] [-c MIN_AC] [-C MAX_AC] [-q MIN_AF] [-Q MAX_AF] [-v TYPES] [-A] [-m MIN_ALLELES] [-M MAX_ALLELES] [-H HWE] [--multi-ref] grg_input grg_output`
* Python: [grapp.util.filter.grg\_save\_freq()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.util.filter.grg_save_freq), [grapp.util.filter.grg\_save\_individuals()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.util.filter.grg_save_individuals), [etc.](https://grapp.readthedocs.io/en/latest/reference/grapp.html#module-grapp.util.filter).

### Phenotype Simulation

Simulate phenotypes, for testing GWAS and association study scenarios.

* Command: `grapp pheno [-h] [-e HERITABILITY] [-n NUM_CAUSAL] [--seed SEED] [--no-normalize] [--save-effects SAVE_EFFECTS] [-o OUT_FILE] grg_input`
* Python: See [grg\_pheno\_sim](https://github.com/aprilweilab/grg_pheno_sim/)

### Data Conversion

GRG can be converted back to a tabular (sparse matrix) format, [IGD](https://picovcf.readthedocs.io/en/latest/igd_overview.html):

* Command: `grapp export [-h] [--igd IGD | --vcf VCF] [-f] [-j JOBS] [--temp-dir TEMP_DIR] [--contig CONTIG] grg_input`
* Python: [grapp.util.igd.export\_igd()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.util.igd.export_igd)

`grapp` also supports converting [tskit.TreeSequence](https://tskit.dev/tskit/docs/stable/python-api.html#tskit.TreeSequence) to IGD, since for really large
simulated `TreeSequence` files (e.g., from [msprime](https://tskit.dev/msprime/docs/stable/intro.html)) converting them to VCF can take days (or weeks):

#### Library
* Command: `grapp ts2igd [-h] [-f] [-j JOBS] [--temp-dir TEMP_DIR] ts_input igd_output`

You can also convert `TreeSequence` to _GRG_ using the core GRG library, via `grg convert ts_input igd_output`. In situations where converting a `TreeSequence`
to VCF takes days, converting to IGD takes hours, and converting to GRG takes minutes.

### Simple Computations

The following information can be easily displayed on the command line: GRG summary, HWE p-values, allele counts,
allele frequencies, sample identifiers, mapping between samples and populations.

* Command: `grapp show [-h] [-j JOBS] [-S | -P | -i | -f | -c | -H | --multi-sites] [--multi-ref] grg_input`
* Python: [grapp.util.simple.hwe()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.util.simple.hwe), [grapp.util.simple.get\_zygosities()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.util.simple.get_zygosities), [etc.](https://grapp.readthedocs.io/en/latest/reference/grapp.html#module-grapp.util.simple).

### LinearOperators

The core of the library are the `LinearOperator`s that operate on GRGs:
* `grapp.linalg.ops_scipy.SciPyXOperator`: An operator that performs matrix multiplication against the genotype matrix `X` (`NxM`) or its transpose (`MxN`).
* `grapp.linalg.ops_scipy.SciPyXTXOperator`: An operator that performs matrix multiplication against the `MxM` product `transpose(X) * X`.
* `grapp.linalg.ops_scipy.SciPyStdXOperator`: The same as `SciPyXOperator`, except the genotype matrix is standardized by using the allele frequencies (standard deviation via binomial distribution).
* `grapp.linalg.ops_scipy.SciPyStdXTXOperator`: The same as `SciPyXTXOperator`, except the genotype matrix is standardized by using the allele frequencies (standard deviation via binomial distribution).

Additionally, there is a helpful method for eigen decomposition (`grapp.linalg.eigs`) and PCA (`grapp.linalg.PCs`).

### util
### scikit-allel Integration

Common utility functions for working with the GRG format.
The primary integration with [scikit-allel](https://scikit-allel.readthedocs.io/en/stable/index.html) is via the
[grapp.popgen.allele\_counts()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.popgen.allele_counts) and
[grapp.popgen.pop\_allele\_counts()](https://grapp.readthedocs.io/en/latest/reference/grapp.html#grapp.popgen.pop_allele_counts) methods,
which return output compatible with many `scikit-allel` functions, such as [allel.sequence\_diversity()](https://scikit-allel.readthedocs.io/en/stable/stats/diversity.html#allel.sequence_diversity),
[allel.watterson\_theta()](https://scikit-allel.readthedocs.io/en/stable/stats/diversity.html#allel.watterson_theta), [allel.hudson\_fst()](https://scikit-allel.readthedocs.io/en/stable/stats/fst.html#allel.hudson_fst),
and [many](https://scikit-allel.readthedocs.io/en/stable/stats/fst.html) [others](https://scikit-allel.readthedocs.io/en/stable/stats/sf.html).

#### Command Line
See the [grapp.popgen](https://grapp.readthedocs.io/en/latest/reference/grapp.html#population-genetics) documentation for more details.

GRG can be exported to tabular data formats. `.vcf.gz` is supported but slow. It is recommended to use [IGD](https://github.com/aprilweilab/picovcf?tab=readme-ov-file#indexable-genotype-data-igd) which is dramatically faster and more compact, while being similar to VCF in how it is structured. Export command:
```
usage: grapp export [-h] [--igd IGD | --vcf VCF] [-f] [-j JOBS] [--temp-dir TEMP_DIR] [--contig CONTIG] grg_input

positional arguments:
grg_input The input GRG file

options:
-h, --help show this help message and exit
--igd IGD Export the entire dataset to the given IGD filename.
--vcf VCF Export the entire dataset to the given VCF filename. Use '-' to write to stdout (and, e.g., pipe through bgzip). If the filename ends with .gz then the Python GZIP codec will be used
(not bgzip). Otherwise, a plaintext VCF file will be created.
-f, --force Force overwrite of the output file, if it exists.
-j JOBS, --jobs JOBS Number of processes/threads to use, if possible. Default: 1.
--temp-dir TEMP_DIR Put all temporary files in the given directory, instead of creating a directory in the system temporary location. WARNING: Intermediate/temporary files will not be cleaned up when
this is specified.
--contig CONTIG Use the given contig name when exporting to VCF. Default: "unknown".
```

GRG files can be filtered, prior to performing analysis on them. See the filter command:
```
sage: grapp filter [-h] [-S INDIVIDUALS | --hap-samples HAP_SAMPLES | -P POPULATIONS] [-r RANGE] [-c MIN_AC] [-C MAX_AC] [-q MIN_AF] [-Q MAX_AF] [-v TYPES] [-A] [-m MIN_ALLELES] [-M MAX_ALLELES]
grg_input grg_output

positional arguments:
grg_input The input GRG file
grg_output The output GRG file

options:
-h, --help show this help message and exit

sample filters:
-S INDIVIDUALS, --individuals INDIVIDUALS
Keep only the individuals with the IDs given as a comma-separated list or in the given filename.
--hap-samples HAP_SAMPLES
Keep only the haploid samples with the NodeIDs (indexes) given as a comma-separated list or in the given filename.
-P POPULATIONS, --populations POPULATIONS
Keep only the individuals with populations matching the comma-separated list or in the given filename.

mutation filters:
-r RANGE, --range RANGE
Keep only the variants within the given range, in base pairs. Example: "lower-upper", where both are integers and lower is inclusive, upper is exclusive.
-c MIN_AC, --min-ac MIN_AC
Minimum allele count to keep. All Mutations with count below this value will be dropped
-C MAX_AC, --max-ac MAX_AC
Maximum allele count to keep. All Mutations with count above this value will be dropped
-q MIN_AF, --min-af MIN_AF
Minimum allele frequency to keep. All Mutations with frequency below this value will be dropped
-Q MAX_AF, --max-af MAX_AF
Maximum allele frequency to keep. All Mutations with frequency above this value will be dropped
-v TYPES, --types TYPES
Comma-separated list of variant types to select. Site is selected if any of the ALT alleles is of the type requested. Types are determined by comparing the REF and ALT alleles.
-A, --apply-to-sites By default, all filters apply to each variant independently. This flag will cause an entire site to be dropped if any variants are filtered out.
-m MIN_ALLELES, --min-alleles MIN_ALLELES
Only keep sites with at least this many alleles. Counts all REF alleles as 1.
-M MAX_ALLELES, --max-alleles MAX_ALLELES
Only keep sites with at most this many alleles. Counts all REF alleles as 1. Use '-m 2 -M 2 -v snps' to view only biallelic SNPs.
```

#### Library

Library API reference document is [here](https://grapp.readthedocs.io/en/latest/). There are also some tutorials in the [GRG documentation](https://grgl.readthedocs.io/en/latest/tutorials/GWAS.html) (and correspond [Jupyter notebooks](https://github.com/aprilweilab/grgl/tree/main/doc/tutorials/notebooks)).

### nn
### Nearest Neighbors

Experimental library for nearest neighbors search over GRG.

The `nn` module lets you search a dataset stored as a GRG for nearest neighbors in a variety of ways:
The [grapp.nn](https://grapp.readthedocs.io/en/latest/reference/grapp.html#module-grapp.nn) module lets you search a dataset
stored as a GRG for nearest neighbors in a variety of ways:
* Similarity is either between samples (haplotypes) or mutations (variants). I.e., you can ask to find
samples that are similar to a given sample, or mutations that are similar to a given mutation.
* Similarity is defined as the Hamming distance between items. The Hamming distance is just the number
of differences, so for two samples their distance is defined as the number of mutations that either of
them has, but both of them do not. I.e., `Hamming(A, B) = |Muts(A)| + |Muts(B)| - 2*|Muts(A) intersect Muts(B)|`.
* Similarity is defined as the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between items. The Hamming
distance is just the number of differences, i.e., `Hamming(A, B) = |Muts(A)| + |Muts(B)| - 2*|Muts(A) intersect Muts(B)|`.
* There are APIs that let you query using a sample/mutation already in the dataset (GRG) or more generally
you can query using an external sample/mutation that is not in the dataset, though your options may
be slightly more limited in the latter case.


1 change: 1 addition & 0 deletions grapp/assoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from scipy.stats import t as t_distribution
from typing import List, Optional, Union, Tuple
from grapp.assoc.bolt_lmm import bolt_lmm_inf # noqa: F401
import itertools
import math
import numpy
Expand Down
Loading