Skip to content

EGenetics · genetics.qc

Genotype quality control

Which markers and people are reliable enough to analyze?

The checks to run before any association or population analysis. Per marker: call rate, minor-allele frequency, observed and expected heterozygosity and the Hardy–Weinberg exact test. Per person: call rate and the inbreeding coefficient F from homozygosity, which flags contamination (F well below 0) or relatedness and inbreeding (F well above 0). Thresholds follow PLINK's common defaults and can be changed; nothing is removed, the card lists what fails. Research use only.

Experimental Python and R agree to 10 decimals Also called genotype QC, SNP QC, quality control…

A real result

Example “People common”, recorded in both engines. In the app, “Try this example” loads it into Analyze.

Genotype quality controlRecorded · Python and R agreeRecorded 2026-11-17 · 21 values compared
R

Quality control: 21 of 24 markers and 273 of 300 people passed

Markers passing
21
People passing
273
Overall call rate
0.9772

Plots

Minor-allele frequenciesHow is one numeric variable distributed?
View data
MAF
0.4379
0.4799
0.4521
0.4524
0.4291
0.4711
0.4555
0.4493
0.2908
0.2045
0.0017
0.3670
0.1156
0.0889
0.1814
0.4983
0.3259
0.4916
0.2024
0.0859
0.4966
0.1803
0.0814
0.4017
Genotype shares and Hardy–Weinberg proportionsDo the markers' genotype frequencies sit on the Hardy–Weinberg curve, or do some have too few or too many heterozygotes?
View data
Homozygous (other)HeterozygousHomozygous (counted)HWEMarker
5813894Passes HWEv01
8214670Passes HWEv02
8814460Passes HWEv03
6413892Passes HWEv04
9714455Passes HWEv05
6614583Passes HWEv06
6214288Passes HWEv07
8515655Passes HWEv08
1746951Fails HWEv09
165909Passes HWEv10
29410Passes HWEv11
11614437Passes HWEv12
229623Passes HWEv13
250435Passes HWEv14
1998511Passes HWEv15
7414573Passes HWEv16
14211140Passes HWEv17
7514480Passes HWEv18
1889313Passes HWEv19
246405Passes HWEv20
7315071Passes HWEv21
2037615Passes HWEv22
249442Passes HWEv23
9915541Passes HWEv24

Assumptions

  • One population — If people come from several populations, run QC within each: mixing them fails HWE and lowers heterozygosity.
  • Unrelated people — 0 people have F above 0.2, as relatives or inbreeding produce.
  • Biallelic markers — All 24 markers show at most two alleles.

Markers

MarkerCounted alleleCall rateMAFObserved het.Expected het.HWE exact pFails
v01B0.96670.43790.47590.49230.5533
v02B0.99330.47990.48990.49920.7293
v03B0.97330.45210.49320.49541
v04B0.98000.45240.46940.49550.4096
v05B0.98670.42910.48650.48990.9058
v06B0.98000.47110.49320.49830.9068
v07B0.97330.45550.48630.49600.7247
v08B0.98670.44930.52700.49490.2918
v09B0.98000.29080.23470.41259.998e-13HWE
v10B0.88000.20450.34090.32540.5702call rate
v11B0.98330.00170.00340.00341MAF
v12B0.99000.36700.48480.46460.5323
v13B0.98000.11560.21090.20450.7791
v14B0.99330.08890.14430.16200.0671
v15B0.98330.18140.28810.29690.5607
v16B0.97330.49830.49660.50000.9071
v17B0.97670.32590.37880.43940.0233
v18B0.99670.49160.48160.49990.5629
v19B0.98000.20240.31630.32280.7186
v20B0.97000.08590.13750.15710.0465
v21B0.98000.49660.51020.50000.8155
v22B0.98000.18030.25850.29550.0457
v23B0.98330.08140.14920.14951
v24B0.98330.40170.52540.48070.1455

People who fail

RowCall rateHeterozygosityFFails
60.58330.5714−0.3357call rate
70.45830.3636−0.0935call rate
120.91670.4091−0.0359call rate
180.91670.27270.2682call rate
230.91670.36360.1134call rate
270.91670.5000−0.3306call rate
460.87500.33330.0890call rate
480.91670.4091−0.0353call rate
520.91670.4545−0.2214call rate
760.91670.4545−0.2224call rate
860.91670.4545−0.1926call rate
1090.91670.5455−0.4337call rate
1140.91670.5000−0.3439call rate
1250.91670.18180.5244call rate
1290.91670.31820.1501call rate
1370.87500.6190−0.6550call rate
1420.91670.18180.5160call rate
1490.91670.31820.1678call rate
1530.91670.27270.2670call rate
1590.91670.5000−0.3169call rate
1770.91670.22730.3975call rate
1820.91670.4091−0.0995call rate
2090.91670.31820.1457call rate
2130.91670.6364−0.6038call rate
2500.91670.4091−0.0715call rate
2550.91670.4545−0.1500call rate
2850.91670.36360.0223call rate
  • 300 people have fewer than 100 polymorphic markers called, too few for a stable F; the |F| check skips them.
  • Research use only: not for clinical or diagnostic decisions. Genotypes are personal data.
  • Thresholds: marker call rate ≥ 0.95, MAF ≥ 0.01, HWE p ≥ 1e-06; person call rate ≥ 0.95, |F| ≤ 0.2.
  • F = (observed − expected homozygous markers) / (called markers − expected), over polymorphic markers (PLINK --het); checked from 100 markers.
  • Nothing is removed: filter the failing markers and people in Prepare or in the Genetics studio.
Python

Quality control: 21 of 24 markers and 273 of 300 people passed

Markers passing
21
People passing
273
Overall call rate
0.9772

To keep this page light, the plots are shown once, under the R tab.

Assumptions

  • One population — If people come from several populations, run QC within each: mixing them fails HWE and lowers heterozygosity.
  • Unrelated people — 0 people have F above 0.2, as relatives or inbreeding produce.
  • Biallelic markers — All 24 markers show at most two alleles.

Markers

MarkerCounted alleleCall rateMAFObserved het.Expected het.HWE exact pFails
v01B0.96670.43790.47590.49230.5533
v02B0.99330.47990.48990.49920.7293
v03B0.97330.45210.49320.49541
v04B0.98000.45240.46940.49550.4096
v05B0.98670.42910.48650.48990.9058
v06B0.98000.47110.49320.49830.9068
v07B0.97330.45550.48630.49600.7247
v08B0.98670.44930.52700.49490.2918
v09B0.98000.29080.23470.41259.998e-13HWE
v10B0.88000.20450.34090.32540.5702call rate
v11B0.98330.00170.00340.00341MAF
v12B0.99000.36700.48480.46460.5323
v13B0.98000.11560.21090.20450.7791
v14B0.99330.08890.14430.16200.0671
v15B0.98330.18140.28810.29690.5607
v16B0.97330.49830.49660.50000.9071
v17B0.97670.32590.37880.43940.0233
v18B0.99670.49160.48160.49990.5629
v19B0.98000.20240.31630.32280.7186
v20B0.97000.08590.13750.15710.0465
v21B0.98000.49660.51020.50000.8155
v22B0.98000.18030.25850.29550.0457
v23B0.98330.08140.14920.14951.0000
v24B0.98330.40170.52540.48070.1455

People who fail

RowCall rateHeterozygosityFFails
60.58330.5714−0.3357call rate
70.45830.3636−0.0935call rate
120.91670.4091−0.0359call rate
180.91670.27270.2682call rate
230.91670.36360.1134call rate
270.91670.5000−0.3306call rate
460.87500.33330.0890call rate
480.91670.4091−0.0353call rate
520.91670.4545−0.2214call rate
760.91670.4545−0.2224call rate
860.91670.4545−0.1926call rate
1090.91670.5455−0.4337call rate
1140.91670.5000−0.3439call rate
1250.91670.18180.5244call rate
1290.91670.31820.1501call rate
1370.87500.6190−0.6550call rate
1420.91670.18180.5160call rate
1490.91670.31820.1678call rate
1530.91670.27270.2670call rate
1590.91670.5000−0.3169call rate
1770.91670.22730.3975call rate
1820.91670.4091−0.0995call rate
2090.91670.31820.1457call rate
2130.91670.6364−0.6038call rate
2500.91670.4091−0.0715call rate
2550.91670.4545−0.1500call rate
2850.91670.36360.0223call rate
  • 300 people have fewer than 100 polymorphic markers called, too few for a stable F; the |F| check skips them.
  • Research use only: not for clinical or diagnostic decisions. Genotypes are personal data.
  • Thresholds: marker call rate ≥ 0.95, MAF ≥ 0.01, HWE p ≥ 1e-06; person call rate ≥ 0.95, |F| ≤ 0.2.
  • F = (observed − expected homozygous markers) / (called markers − expected), over polymorphic markers (PLINK --het); checked from 100 markers.
  • Nothing is removed: filter the failing markers and people in Prepare or in the Genetics studio.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Worked examples in the app

  • People commonge_people · variants: v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24
  • People vcf strictge_people · variants: gt1, gt2, gt3, gt4, v01, v09
  • People rare keepge_people · variants: r01, r02, r03, r04, r05, r06, r07, r08, r09, r10, r11, r12, r13, r14, r15
  • Degenerate infge_people · variants: v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24
  • Degenerate tiesge_people · variants: v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24

In the guides

Where it sits in the decision guides

  • Goodness of fit · Statistics Guide · E — Compare proportions / frequencies

What you need

Markersnumeric / categorical · 1–10000 columns
One column per marker, one row per person: doses 0/1/2, VCF calls (0/1, 1|1, ./.) or two alleles.

Choices you make

statistics

Lowest marker call ratedefault 0.95

Markers called in a smaller share of people fail (PLINK --geno 0.05).

Lowest minor-allele frequencydefault 0.01

Rarer markers fail for common-variant analyses (PLINK --maf 0.01); set 0 to keep rare variants.

HWE exact-test thresholddefault 0.000001

Markers with a Hardy–Weinberg exact p below it fail, as calling errors often do (PLINK --hwe 1e-6).

Lowest person call ratedefault 0.95

People called at a smaller share of markers fail (PLINK --mind 0.05).

Largest |F| per persondefault 0.2

People further from 0 fail (low F: contamination; high: inbreeding). Needs 100 polymorphic markers per person.

Checks and remedies

CodeSplash checks each testable assumption on every run and shows the result on the card; premises the data can’t test are marked. If a check fails, these are the ways forward.

  • One population

    Mixed ancestry lowers heterozygosity and fails HWE without any error; run QC within populations.

  • Unrelated people

    Relatives raise F; check relatedness separately.

  • Biallelic markers

    Each marker is read with two alleles; split multi-allelic sites first.

What it reports

n_samples
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
n_variants
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
n_monomorphic
The frequency of the less common allele at a marker, between 0 and 0.5.
n_fail_call
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
n_fail_maf
The frequency of the less common allele at a marker, between 0 and 0.5.
n_fail_hwe
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
n_fail_any
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
n_pass
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
n_samples_fail_call
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
n_samples_fail_f
Each person's F compares observed with expected homozygous markers. Far below 0 suggests contamination; far above 0, inbreeding or kin.
n_samples_pass
Each person's F compares observed with expected homozygous markers. Far below 0 suggests contamination; far above 0, inbreeding or kin.
call_rate
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
variant_call_rate
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
variant_maf
The frequency of the less common allele at a marker, between 0 and 0.5.
variant_hwe_p
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
variant_het_obs
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
variant_het_exp
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
variant_pass
In QC, markers whose genotype counts fail the Hardy–Weinberg exact test at a strict threshold (often 10⁻⁶) are treated as calling errors.
sample_call_rate
The share of genotypes the assay could read, per marker or per person. Low call rates flag failed assays or poor DNA.
sample_f
Each person's F compares observed with expected homozygous markers. Far below 0 suggests contamination; far above 0, inbreeding or kin.
sample_pass
Each person's F compares observed with expected homozygous markers. Far below 0 suggests contamination; far above 0, inbreeding or kin.

Python and R

Python and R agree to 10 decimals (relative tolerance 1e-10). Python uses pandas, numpy, scipy, codesplash; R uses stats, nanoparquet, codesplashr.

Python

The exact script CodeSplash ran for example “People common” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0

# Genotype quality control
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method genetics.qc 1.0.1 · spec 3cb6a5d0c445a953
# Dataset: ge_people (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
import math

import numpy as np
import pandas as pd

import codesplash as cs
from codesplash.stats import genetics as gen  # per-marker and per-person QC, HWE exact (twins of cs_gen_*)

# %% Data (no rows dropped here: genotype no-calls and missing values are handled by the analysis)
df = pd.read_parquet("tests/parity/fixtures/ge_people.parquet", columns=["v01", "v02", "v03", "v04", "v05", "v06", "v07", "v08", "v09", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24"])
n_input = len(df)
warnings = []
research_note = "Research use only: not for clinical or diagnostic decisions. Genotypes are personal data."

# %% Genotypes
variants = ["v01", "v02", "v03", "v04", "v05", "v06", "v07", "v08", "v09", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24"]
g, counted, other = gen.dose_matrix({v: df[v].to_numpy() for v in variants})
min_call_v = float(0.95)
min_maf = float(0.01)
hwe_thr = float(1e-6)
min_call_s = float(0.95)
max_f = float(0.2)

# %% Checks
q = gen.qc(g)
n, m = g.shape
fail_call = q["call_rate"] < min_call_v
fail_maf = np.nan_to_num(q["maf"], nan=0.0) < min_maf
fail_hwe = np.nan_to_num(q["hwe_p"], nan=1.0) < hwe_thr
fail_any = fail_call | fail_maf | fail_hwe
s_fail_call = q["sample_call_rate"] < min_call_s
# F from a few markers is mostly noise, so the |F| check needs 100 polymorphic markers called per person.
f_ok = q["sample_n_poly"] >= 100
s_fail_f = (np.abs(np.nan_to_num(q["sample_f"], nan=0.0)) > max_f) & f_ok
if not f_ok.all():
    warnings.append(f"{int((~f_ok).sum())} people have fewer than 100 polymorphic markers called, too few for a stable F; the |F| check skips them.")
s_pass = ~(s_fail_call | s_fail_f)
call_rate = float(np.sum(~np.isnan(g))) / (n * m)
if int(np.sum(q["monomorphic"])):
    warnings.append(f"{int(np.sum(q['monomorphic']))} markers are monomorphic here: they carry no information and are left out of each person's F.")

# %% Assumptions
assumptions = [
    {"key": "one_population", "label": "One population", "status": "info", "detail": "If people come from several populations, run QC within each: mixing them fails HWE and lowers heterozygosity."},
    {"key": "unrelated", "label": "Unrelated people", "status": "warn" if int(np.sum((np.nan_to_num(q["sample_f"]) > max_f) & f_ok)) else "info",
     "detail": f"{int(np.sum((np.nan_to_num(q['sample_f']) > max_f) & f_ok))} people have F above {max_f:g}, as relatives or inbreeding produce."},
    {"key": "biallelic", "label": "Biallelic markers", "status": "pass", "detail": f"All {m} markers show at most two alleles."},
]

# %% Result
summary = (f"{int(np.sum(~fail_any))} of {m} markers pass (call rate {int(fail_call.sum())} fail, MAF {int(fail_maf.sum())}, HWE {int(fail_hwe.sum())}); "
           f"{int(s_pass.sum())} of {n} people pass (call rate {int(s_fail_call.sum())} fail, |F| {int(s_fail_f.sum())}). Overall call rate {call_rate:.3f}.")
apa = f"Quality control: {int(np.sum(~fail_any))} of {m} markers and {int(s_pass.sum())} of {n} people passed"
marker_rows = [{"marker": v, "counted": counted[j], "call_rate": float(q["call_rate"][j]), "maf": float(q["maf"][j]), "het_obs": float(q["het_obs"][j]),
                "het_exp": float(q["het_exp"][j]), "hwe_p": float(q["hwe_p"][j]),
                "flags": ", ".join(f for f, on in (("call rate", fail_call[j]), ("MAF", fail_maf[j]), ("HWE", fail_hwe[j])) if on)} for j, v in enumerate(variants)]
person_rows = [{"row": i + 1, "call_rate": float(q["sample_call_rate"][i]), "het": float(q["sample_het"][i]), "f": float(q["sample_f"][i]),
                "flags": ", ".join(f for f, on in (("call rate", s_fail_call[i]), ("|F|", s_fail_f[i])) if on)} for i in range(n) if not s_pass[i]]
notes = [research_note,
         f"Thresholds: marker call rate ≥ {min_call_v:g}, MAF ≥ {min_maf:g}, HWE p ≥ {hwe_thr:g}; person call rate ≥ {min_call_s:g}, |F| ≤ {max_f:g}.",
         "F = (observed − expected homozygous markers) / (called markers − expected), over polymorphic markers (PLINK --het); checked from 100 markers.",
         "Nothing is removed: filter the failing markers and people in Prepare or in the Genetics studio."]
values = {
    "n_samples": n, "n_variants": m, "n_monomorphic": int(np.sum(q["monomorphic"])), "n_fail_call": int(fail_call.sum()),
    "n_fail_maf": int(fail_maf.sum()), "n_fail_hwe": int(fail_hwe.sum()), "n_fail_any": int(fail_any.sum()), "n_pass": int(np.sum(~fail_any)),
    "n_samples_fail_call": int(s_fail_call.sum()), "n_samples_fail_f": int(s_fail_f.sum()), "n_samples_pass": int(s_pass.sum()),
    "call_rate": call_rate, "variant_call_rate": q["call_rate"].tolist(), "variant_maf": q["maf"].tolist(), "variant_hwe_p": q["hwe_p"].tolist(),
    "variant_het_obs": q["het_obs"].tolist(), "variant_het_exp": q["het_exp"].tolist(), "variant_pass": (~fail_any).tolist(),
    "sample_call_rate": q["sample_call_rate"].tolist(), "sample_f": q["sample_f"].tolist(), "sample_pass": s_pass.tolist(),
}
cs.publish({
    "method": "genetics.qc",
    "methodVersion": "1.0.1",
    "title": "Genotype quality control",
    "engine": cs.engine_info("pandas", "numpy", "scipy"),
    "n": n,
    "summary": summary,
    "apa": apa,
    "statistics": [
        {"key": "n_pass", "label": "Markers passing", "value": int(np.sum(~fail_any))},
        {"key": "n_samples_pass", "label": "People passing", "value": int(s_pass.sum())},
        {"key": "call_rate", "label": "Overall call rate", "value": call_rate},
    ],
    "tables": [
        {"key": "markers", "title": "Markers",
         "columns": [{"key": "marker", "label": "Marker", "type": "string"}, {"key": "counted", "label": "Counted allele", "type": "string"},
                     {"key": "call_rate", "label": "Call rate", "type": "number"}, {"key": "maf", "label": "MAF", "type": "number"},
                     {"key": "het_obs", "label": "Observed het.", "type": "number"}, {"key": "het_exp", "label": "Expected het.", "type": "number"},
                     {"key": "hwe_p", "label": "HWE exact p", "type": "p"}, {"key": "flags", "label": "Fails", "type": "string"}],
         "rows": marker_rows},
        {"key": "people", "title": "People who fail",
         "columns": [{"key": "row", "label": "Row", "type": "integer"}, {"key": "call_rate", "label": "Call rate", "type": "number"},
                     {"key": "het", "label": "Heterozygosity", "type": "number"}, {"key": "f", "label": "F", "type": "number"},
                     {"key": "flags", "label": "Fails", "type": "string"}],
         "rows": person_rows},
    ],
    "assumptions": assumptions,
    "warnings": warnings,
    "notes": notes,
    "charts": [
        cs.chart("maf", "histogram", "Minor-allele frequencies", {"x": cs.col("MAF", q["maf"].tolist())}),
        cs.chart("definetti", "definetti", "Genotype shares and Hardy–Weinberg proportions", {
            "aa": cs.col("Homozygous (other)", np.sum(g == 0, axis=0).astype(int).tolist(), "numeric"),
            "ab": cs.col("Heterozygous", np.sum(g == 1, axis=0).astype(int).tolist(), "numeric"),
            "bb": cs.col("Homozygous (counted)", np.sum(g == 2, axis=0).astype(int).tolist(), "numeric"),
            "label": cs.col("Marker", variants, "categorical"),
            "group": cs.col("HWE", ["Fails HWE" if f else "Passes HWE" for f in fail_hwe], "categorical")}),
    ],
    "values": values,
})
R

The exact script CodeSplash ran for example “People common” · R version 4.5.1 (2025-06-13)

# Genotype quality control
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method genetics.qc 1.0.1 · spec 3cb6a5d0c445a953
# Dataset: ge_people (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
library(codesplashr) # cs_gen_qc (per-marker and per-person QC, HWE exact; twin of codesplash.stats.genetics)

# %% Data (no rows dropped here: genotype no-calls and missing values are handled by the analysis)
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/ge_people.parquet", col_select = c("v01", "v02", "v03", "v04", "v05", "v06", "v07", "v08", "v09", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24")))
n_input <- nrow(df)
warnings <- list()
research_note <- "Research use only: not for clinical or diagnostic decisions. Genotypes are personal data."

# %% Genotypes
variants <- c("v01", "v02", "v03", "v04", "v05", "v06", "v07", "v08", "v09", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24")
dm <- cs_gen_dose_matrix(df[variants])
g <- dm$g
min_call_v <- 0.95
min_maf <- 0.01
hwe_thr <- 1e-6
min_call_s <- 0.95
max_f <- 0.2

# %% Checks
q <- cs_gen_qc(g)
n <- nrow(g)
m <- ncol(g)
nz <- function(x, v) ifelse(is.na(x), v, x)
fail_call <- q$call_rate < min_call_v
fail_maf <- nz(q$maf, 0) < min_maf
fail_hwe <- nz(q$hwe_p, 1) < hwe_thr
fail_any <- fail_call | fail_maf | fail_hwe
s_fail_call <- q$sample_call_rate < min_call_s
# F from a few markers is mostly noise, so the |F| check needs 100 polymorphic markers called per person.
f_ok <- q$sample_n_poly >= 100
s_fail_f <- (abs(nz(q$sample_f, 0)) > max_f) & f_ok
if (!all(f_ok)) {
  warnings <- c(warnings, sprintf("%d people have fewer than 100 polymorphic markers called, too few for a stable F; the |F| check skips them.", sum(!f_ok)))
}
s_pass <- !(s_fail_call | s_fail_f)
call_rate <- sum(!is.na(g)) / (n * m)
if (sum(q$monomorphic) > 0) {
  warnings <- c(warnings, sprintf("%d markers are monomorphic here: they carry no information and are left out of each person's F.", sum(q$monomorphic)))
}

# %% Assumptions
n_high_f <- sum((nz(q$sample_f, 0) > max_f) & f_ok)
assumptions <- list(
  list(key = "one_population", label = "One population", status = "info", detail = "If people come from several populations, run QC within each: mixing them fails HWE and lowers heterozygosity."),
  list(key = "unrelated", label = "Unrelated people", status = if (n_high_f > 0) "warn" else "info",
       detail = sprintf("%d people have F above %s, as relatives or inbreeding produce.", n_high_f, format(max_f))),
  list(key = "biallelic", label = "Biallelic markers", status = "pass", detail = sprintf("All %d markers show at most two alleles.", m))
)

# %% Result
summary <- sprintf("%d of %d markers pass (call rate %d fail, MAF %d, HWE %d); %d of %d people pass (call rate %d fail, |F| %d). Overall call rate %.3f.",
                   sum(!fail_any), m, sum(fail_call), sum(fail_maf), sum(fail_hwe), sum(s_pass), n, sum(s_fail_call), sum(s_fail_f), call_rate)
apa <- sprintf("Quality control: %d of %d markers and %d of %d people passed", sum(!fail_any), m, sum(s_pass), n)
flags <- function(...) {
  x <- c(...)
  paste(names(x)[x], collapse = ", ")
}
marker_rows <- lapply(seq_len(m), function(j) list(marker = variants[j], counted = dm$counted[j], call_rate = q$call_rate[j], maf = q$maf[j],
                                                   het_obs = q$het_obs[j], het_exp = q$het_exp[j], hwe_p = q$hwe_p[j],
                                                   flags = flags(`call rate` = fail_call[j], MAF = fail_maf[j], HWE = fail_hwe[j])))
person_rows <- lapply(which(!s_pass), function(i) list(row = i, call_rate = q$sample_call_rate[i], het = q$sample_het[i], f = q$sample_f[i],
                                                       flags = flags(`call rate` = s_fail_call[i], `|F|` = s_fail_f[i])))
notes <- list(research_note,
              sprintf("Thresholds: marker call rate ≥ %s, MAF ≥ %s, HWE p ≥ %s; person call rate ≥ %s, |F| ≤ %s.",
                      format(min_call_v), format(min_maf), format(hwe_thr), format(min_call_s), format(max_f)),
              "F = (observed − expected homozygous markers) / (called markers − expected), over polymorphic markers (PLINK --het); checked from 100 markers.",
              "Nothing is removed: filter the failing markers and people in Prepare or in the Genetics studio.")
result_values <- list(
  n_samples = n, n_variants = m, n_monomorphic = sum(q$monomorphic), n_fail_call = sum(fail_call), n_fail_maf = sum(fail_maf),
  n_fail_hwe = sum(fail_hwe), n_fail_any = sum(fail_any), n_pass = sum(!fail_any), n_samples_fail_call = sum(s_fail_call),
  n_samples_fail_f = sum(s_fail_f), n_samples_pass = sum(s_pass), call_rate = call_rate,
  variant_call_rate = I(q$call_rate), variant_maf = I(q$maf), variant_hwe_p = I(q$hwe_p), variant_het_obs = I(q$het_obs),
  variant_het_exp = I(q$het_exp), variant_pass = I(!fail_any), sample_call_rate = I(q$sample_call_rate), sample_f = I(q$sample_f),
  sample_pass = I(s_pass)
)
cs_publish(list(
  method = "genetics.qc",
  methodVersion = "1.0.1",
  title = "Genotype quality control",
  engine = cs_engine_info("stats", "nanoparquet"),
  n = n,
  summary = summary,
  apa = apa,
  statistics = list(
    list(key = "n_pass", label = "Markers passing", value = sum(!fail_any)),
    list(key = "n_samples_pass", label = "People passing", value = sum(s_pass)),
    list(key = "call_rate", label = "Overall call rate", value = call_rate)
  ),
  tables = list(
    list(key = "markers", title = "Markers",
         columns = list(list(key = "marker", label = "Marker", type = "string"), list(key = "counted", label = "Counted allele", type = "string"),
                        list(key = "call_rate", label = "Call rate", type = "number"), list(key = "maf", label = "MAF", type = "number"),
                        list(key = "het_obs", label = "Observed het.", type = "number"), list(key = "het_exp", label = "Expected het.", type = "number"),
                        list(key = "hwe_p", label = "HWE exact p", type = "p"), list(key = "flags", label = "Fails", type = "string")),
         rows = marker_rows),
    list(key = "people", title = "People who fail",
         columns = list(list(key = "row", label = "Row", type = "integer"), list(key = "call_rate", label = "Call rate", type = "number"),
                        list(key = "het", label = "Heterozygosity", type = "number"), list(key = "f", label = "F", type = "number"),
                        list(key = "flags", label = "Fails", type = "string")),
         rows = person_rows)
  ),
  assumptions = assumptions,
  warnings = warnings,
  notes = notes,
  charts = list(
    cs_chart("maf", "histogram", "Minor-allele frequencies", list(x = cs_col("MAF", q$maf))),
    cs_chart("definetti", "definetti", "Genotype shares and Hardy–Weinberg proportions",
             list(aa = cs_col("Homozygous (other)", unname(colSums(g == 0, na.rm = TRUE)), "numeric"),
                  ab = cs_col("Heterozygous", unname(colSums(g == 1, na.rm = TRUE)), "numeric"),
                  bb = cs_col("Homozygous (counted)", unname(colSums(g == 2, na.rm = TRUE)), "numeric"),
                  label = cs_col("Marker", variants, "categorical"),
                  group = cs_col("HWE", ifelse(fail_hwe, "Fails HWE", "Passes HWE"), "categorical")))
  ),
  values = result_values
))

Sources

  • Purcell, S., Neale, B., Todd-Brown, K., et al. (2007). PLINK: a tool set for whole-genome association and population-based linkage analyses. American Journal of Human Genetics, 81, 559–575.
  • Anderson, C. A., Pettersson, F. H., Clarke, G. M., Cardon, L. R., Morris, A. P., & Zondervan, K. T. (2010). Data quality control in genetic case-control association studies. Nature Protocols, 5, 1564–1573.
  • Wigginton, J. E., Cutler, D. J., & Abecasis, G. R. (2005). A note on exact tests of Hardy–Weinberg equilibrium. American Journal of Human Genetics, 76, 887–893.

From the CodeSplash method registry · genetics.qc v1.0.1.