Haplotype Inference

Decomposes diploid copy number estimates into haplotype-specific values using an iterative IBD-based phasing algorithm. For each sample, haplotype copy numbers are updated proportionally to the mean of IBD-matched haplotype neighbors until convergence. This step is locus-agnostic — it requires only diploid CN estimates and a phased IBD neighbors file.

grid.utils.hi_inference._compute_imp(i, hap_IRRs, hap_nbrs, mean_IRRs)

Compute imputed haplotype neighbor means for sample i at the final iteration.

grid.utils.hi_inference._load_ibd_neighbors(ilash_file, IDtoInd, MAX_NBR, region_start, region_end, min_length=0.5, min_match=0.7, weighted=False, weight_scale=1000000)

Load IBD neighbors from iLASH output.

File format (no header, tab-separated):

FID1 HAP_ID1 FID2 HAP_ID2 CHR BP1 BP2 SNP_BP1 SNP_BP2 LENGTH MATCH

HAP_ID format: {FID}_{hap} where hap is 0 or 1 (0-indexed). LENGTH is in cM. MATCH is the IBD match score.

If weighted=True, each neighbor gets a Lorentzian distance weight:

w = (weight_scale / (distance_bp + weight_scale)) * match

Returns hap_nbrs: list of lists of (neighbor_hap_idx, weight).

grid.utils.hi_inference._load_ibs_neighbors(neighbors_file, IDtoInd, MAX_NBR)

Load IBS neighbors from computeIBSpbwt output.

File format (header + tab/space-separated rows):

ID hap nbrInd cMlen cMedge IDnbr hapNbr

Haplotypes are 1-indexed (1 or 2). Returns hap_nbrs: list of lists of (neighbor_hap_idx, weight=1.0).

grid.utils.hi_inference._read_dip_cn_file(dip_cn_file)

Read diploid CN file with string IDs, skipping non-data rows (e.g. header).

grid.utils.hi_inference._run_phasing(IRRs, hap_nbrs, MIN_NBR, N_ITERS, console)

Iterative haplotype phasing shared by both IBS and IBD methods.

hap_nbrs[h] is a list of (neighbor_hap_idx, weight). Returns (hap_IRRs, mean_IRRs).

grid.utils.hi_inference._segment_distance(bp1, bp2, region_start, region_end)

Physical distance from IBD segment [bp1, bp2] to target region. 0 if overlapping.

grid.utils.hi_inference.hi_inference(config, console)

Impute haplotype-specific IRRs from diploid IRRs and haplotype neighbors.

Supports two methods controlled by method in compute_haploid_genotypes:

  • "ibs" — uses computeIBSpbwt output (ibs_output key required)

  • "ibd" — uses iLASH output (ibd_output key required). Set weighted: True to apply Lorentzian distance weighting.

Output columns: ID IRRs hap1phased hap2phased hap1imp hap2imp