Algorithm for automatically building pedigrees using IBD, age, and sex information.
This repository contains Bonsai v3, the current version of Bonsai, in bonsaitree.v3. New users should use Bonsai v3, as described below.
Legacy Bonsai v2: The top-level
bonsaitreepackage (from bonsaitree import bonsai) is the earlier Bonsai v2. It remains available so that existing code that depends on it continues to work. Its documentation is in README_v2.md.
To install: in the bonsaitree directory containing the file Makefile, type
make install
at a command prompt. See the Troubleshooting section below for common errors.
To test: in the directory containing the file Makefile, type
make test
To build a pedigree for a group of related individuals:
from bonsaitree.v3.bonsai import build_pedigree
up_dict_log_like_list = build_pedigree(
bio_info=bio_info,
unphased_ibd_seg_list=unphased_ibd_seg_list,
)
# the most likely pedigree and its log likelihood
up_node_dict, log_like = up_dict_log_like_list[0]-
bio_info: (List of dicts) One dict per genotyped individual, of the form[{'genotype_id': <int>, 'age': <int or float>, 'sex': <'M' or 'F'>, 'coverage': <float>}, ...]genotype_id: (int) ID of the individual. Genotyped IDs must be positive integers. Ungenotyped individuals that Bonsai adds to a pedigree are given negative IDs.age: (int or float) Age in years, where a larger value means an older person. Bonsai uses only the differences between ages, so ages can be measured from any common reference point. Do not pass birth years, because they reverse the direction of every age difference. If an age is unknown, set it toNone.sex: (str)'M'or'F'.coverage: (Optional, float) Genome coverage of the individual. For full-coverage genotypes (e.g., from a genotyping array), set this tofloat('inf'). Ifcoverageis omitted, the individual is treated as full coverage.
-
unphased_ibd_seg_list: (List of lists) Unphased IBD segments, such as those detected by IBIS [1]. Unphased segments are preferred over phased segments because they tend to be less fragmented. Segments are of the form[[id1, id2, chrom, start_bp, end_bp, is_full, len_cm], ...], whereid1,id2: (int) Genotype IDs of the two individuals sharing the segment.chrom: (str or int) Chromosome on which the segment is found.start_bp,end_bp: (float) Physical start and end positions of the segment.is_full: (bool) Segment type.Truemeans a "full" segment: the region is IBD2 throughout.Falsemeans a "half or full" segment: the region is IBD1 or IBD2, and may contain IBD2 stretches. A region where IBD2 is embedded in IBD1 is therefore described by two overlapping segments (see the example below).len_cm: (float) Length of the segment in cM.
For example, if individuals 1 and 2 share a region of chromosome 1 that is IBD1 from 0 to 1 Mb, IBD2 from 1 to 2 Mb, and IBD1 again from 2 to 3 Mb, the region is described by these two segments:
[ [1, 2, '1', 0, 3_000_000, False, 3.0], # "half or full" segment spanning the whole region [1, 2, '1', 1_000_000, 2_000_000, True, 1.0], # "full" (IBD2) segment ]This differs from the output of IBD detection programs such as IBIS, which report non-overlapping IBD1 and IBD2 segments. To convert a directory of IBIS
.segfiles (one per chromosome) into this format:from bonsaitree.utils import read_ibis_ibd unphased_ibd_seg_list = read_ibis_ibd('ibis_output_dir')
read_ibis_ibdexpects IBIS IDs of the form<family_id>:<individual_id>, where<individual_id>is an integer; that integer is used as the genotype ID. If any full segment inunphased_ibd_seg_listis not contained in a "half or full" segment between the same pair, Bonsai issues a warning, because this usually means the segments were not converted to this format. -
phased_ibd_seg_list: (Optional, list of lists) Phased IBD segments of the form[[id1, id2, hap1, hap2, chrom, start_cm, end_cm, len_cm], ...], wherehap1andhap2(0or1) are the haplotypes inid1andid2on which the segment occurs. Eitherunphased_ibd_seg_listorphased_ibd_seg_listmust be provided.
Commonly adjusted parameters include min_seg_len (the minimum segment length, in cM, that your IBD detection method reports) and max_peds (the number of alternative pedigrees retained at each step). See the Bonsai v3 README for the full list.
build_pedigree() returns a list of the form [(up_node_dict, log_like), ...], sorted from most to least likely, where
up_node_dict: (dict) The inferred pedigree, mapping each individual to their parents:{node: {parent1: 1, parent2: 1}, ...}. A node may have zero, one, or two parents listed. Positive IDs are the genotyped individuals frombio_info; negative IDs are inferred ungenotyped ancestors and relatives.log_like: (float) The composite log likelihood of the pedigree.
Bonsai v3 can also add a new individual to an existing pedigree. These functions are in bonsaitree.v3.bonsai:
connect_new_node_many_ways(): Add a new individual to an existing pedigree and return the most likely resulting pedigrees.get_new_node_connections(): List the possible ways of connecting a new individual to an existing pedigree, with their likelihoods, without connecting them.connect_new_node(): Connect a new individual to an existing pedigree in a specified way.get_next_node(): Find the unplaced individual who shares the most IBD with someone already in the pedigree.
See the Bonsai v3 README for the inputs and outputs of each function.
- Seidman DN, Shenoy SA, Kim M, Babu R, Woods IG, Dyer TD, Lehman DM, Curran JE, Duggirala R, Blangero J, Williams AL. Rapid, phase-free detection of long identity-by-descent segments enables effective relationship classification. American Journal of Human Genetics. 2020;106(4):453–466. https://doi.org/10.1016/j.ajhg.2020.02.012
- If you experience an error running "make install", try upgrading pip before trying other things. To do this, type "pip install --upgrade pip" at a command prompt.
- If upgrading pip does not work, you can try installing Bonsai in a virtual environment. To create and activate a virtual environment, type "python3 -m venv venv && source venv/bin/activate" at a command prompt. Then try running "make install" again.