Calculate B

The Python API can be used to access core B calculation functions that take vectorized inputs (numpy arrays). Here we show how to calculate B from linked sites (calculateB_linear) and unlinked sites (calculateB_unlinked).

Python functions

Bvalcalc.get_params(params_path: str | None = None, gamma_dfe: bool = False, constant_dfe: bool = False)[source]

Loads DFE parameters from the provided population genetic parameters file. Caches on (params_path, gamma_dfe, constant_dfe) and rebuilds whenever any of those three inputs change.

Bvalcalc.calculateB_linear(distance_to_element: int, length_of_element: int, params: dict | None = None)[source]

Calculate B due to purifying selection acting on a linked selected element of arbitrary length, assuming a constant crossover and gene conversion rate (analytical solution).

Parameters:
  • distance_to_element (int) – Distance (bp) from the neutral site to the nearest edge of the selected element.

  • length_of_element (int) – Length (bp) of the selected element.

  • params (dict) – Required parameters from get_params(), only kept as default (None) when being called by CLI, in which case parameters are sourced from the params file directly.

Bvalcalc.calculateB_unlinked(unlinked_L: int, params: dict | None = None)[source]

Calculate B due to purifying selection at unlinked sites.

Parameters:
  • unlinked_L (float) – Cumulative count of selected sites in unlinked regions.

  • params (dict) – Required parameters from get_params(), only kept as default (None) when being called by CLI, in which case parameters are sourced from the params file directly.

Bvalcalc.calculateB_hri(distant_B, interfering_L, params: dict | None = None)[source]

Calculate B’ (B accounting for Hill-Robertson interference effects) for a non-recombining region containing selected sites.

This is a diploid implementation of Eq. 12 from Becher and Charlesworth (2025), see the relevant supplement in the B-value calculator manuscript.

Parameters:
  • distant_B (float or array-like) – The background B value in the HRI region, from less-linked selected sites outside the interference region (i.e. elsewhere on the chromosome, other chromosomes).

  • interfering_L (float or array-like) – The cumulative length (bp) of interfering selected sites in the HRI region.

  • params (dict) – Required parameters from get_params(), only kept as default (None) when being called by CLI, in which case parameters are sourced from the params file directly.

Example

from Bvalcalc import get_params, calculateB_unlinked, calculateB_linear, calculateB_hri

# Path to your Params.py file
params = get_params("./DroMel_Cds_Params.py")

calculateB_unlinked(unlinked_L = 200000, params = params)
# np.float64(0.9996953434292404)

calculateB_linear(distance_to_element = 500, length_of_element = 10000, params = params)
# array(0.98721754)

calculateB_hri(distant_B = 0.7, interfering_L = 10000, params = params)
# np.float64(0.22627504209854507)

This will import the relevant functions, get the popgen parameters from a relevant params file, see Generate Parameters.

Then B will be calculated from 200kb of unlinked sites using calculateB_unlinked, and B from a linked selected element of length 10kb, 500bp away is calculated using calculateB_linear.

Finally, B’ is calculated for a non-recombining region containing 10000bp of selected sites, with a prior B of 0.7 from distant selected sites outside the region.

Notes

Note that calculateB_linear assumes a consistent crossover and gene conversion rate across both the length of and distance to the selected element, in the CLI, variable recombination rates are accounted for with the more complex function calculateB_recmap; if accessing this function is important for your work feel free to raise it as an issue on GitHub as I could describe its API usage.