Skip to contents

Treats each sample as a major genome and a second genome of the other sex with a DNA share `f` below 0.5. `x_copies` and `y_copies` are twice the depth ratio of the kept X or Y bins to the kept autosomal bins. For an XX major genome `x_copies = 2 - f` and `y_copies = f`; for an XY one `x_copies = 1 + f` and `y_copies = 1 - f`. The major genome is XX when `x_copies >= 1.5`. `fraction_x` and `fraction_y` are the two estimates, unclipped; `fraction` combines them by inverse variance and `agreement_z` compares them, since an X or Y copy change of the major genome moves them differently.

Usage

rduckhts_depth_sex_fraction(
  con,
  bins_table,
  reference_table = NULL,
  table_name = NULL,
  overwrite = FALSE
)

Arguments

con

A DuckDB connection with DuckHTS loaded.

bins_table

Name of a table or view of corrected bins, as for [rduckhts_depth_region_fraction()].

reference_table

Optional name of a table or view of corrected bins of reference samples with a `sex` column, `XX` or `XY`.

table_name

Optional table to create instead of returning a data frame.

overwrite

Whether `table_name` may replace an existing table.

Value

A data frame (or invisible `TRUE` when `table_name` is given) with one row per sample: `sample_id`, `autosomal_bins`, `x_bins`, `y_bins`, `dispersion`, `x_copies`, `x_copies_se`, `y_copies`, `y_copies_se`, `major_sex`, `fraction_x`, `fraction_x_se`, `fraction_y`, `fraction_y_se`, `fraction`, `fraction_se`, `fraction_low`, `fraction_high`, `agreement_z`, `reference_samples`, `status` (`ok`, `no_autosomal_bins`, `no_xy_reference`, `bad_reference`, `no_sex_bins`, `no_x_bins`, `no_y_bins` or `x_y_disagree`) and `method`.

Details

Without `reference_table`, one X copy is 1, two are 2, one Y copy is 1 and a genome without Y has no Y depth. With `reference_table`, its XY samples set the depth of one X and one Y copy, and its XX samples, when present, set two X copies and the Y background; the tested sample is left out of its own reference. Remove the pseudo-autosomal regions of X, and Y bins that are not single-copy, in SQL first.

Examples

con <- rduckhts_connect()
DBI::dbExecute(con, paste(
  "CREATE TEMP TABLE bins AS SELECT 'S1' AS sample_id, chrom,",
  "b * 1000000 AS start, (b + 1) * 1000000 AS \"end\",",
  "0.35 + 0.25 * ((b * 37 + length(chrom) * 11) % 100) / 100.0 AS gc,",
  "CAST((600 + 4 * ((b * 13) % 11)) * CASE chrom WHEN 'X' THEN 0.95 WHEN 'Y' THEN 0.05",
  "ELSE 1 END AS BIGINT) AS reads",
  "FROM (SELECT CAST(c AS VARCHAR) AS chrom FROM range(1, 23) t(c)",
  "UNION ALL SELECT 'X' UNION ALL SELECT 'Y') g, range(0, 60) r(b)"
))
#> [1] 1440
rduckhts_depth_bins(con, "bins", table_name = "corrected")
rduckhts_depth_sex_fraction(con, "corrected")[, c("major_sex", "fraction", "status")]
#>   major_sex  fraction status
#> 1        XX 0.1000021     ok
DBI::dbDisconnect(con, shutdown = TRUE)