|
| 1 | +//! Per-individual diploid genotype model (PR1: known reference alleles). |
| 2 | +use std::collections::HashMap; |
| 3 | + |
| 4 | +use crate::ir::Segment; |
| 5 | +use crate::refdata::{AlleleId, GeneId}; |
| 6 | + |
| 7 | +/// One carried allele in a haplotype gene slot. `copies` encodes |
| 8 | +/// gene-copy multiplicity for the *same* allele; two different alleles |
| 9 | +/// in a slot are two `GeneCopy` entries. `weight` is relative |
| 10 | +/// within-slot expression. |
| 11 | +#[derive(Clone, Copy, Debug, PartialEq)] |
| 12 | +pub struct GeneCopy { |
| 13 | + pub allele: AlleleId, |
| 14 | + pub copies: u8, |
| 15 | + pub weight: f32, |
| 16 | +} |
| 17 | + |
| 18 | +/// One chromosome's carried alleles, per V/D/J gene. An absent or empty |
| 19 | +/// slot means the gene is deleted on this chromosome. |
| 20 | +#[derive(Clone, Debug, Default)] |
| 21 | +pub struct Haplotype { |
| 22 | + v: HashMap<GeneId, Vec<GeneCopy>>, |
| 23 | + d: HashMap<GeneId, Vec<GeneCopy>>, |
| 24 | + j: HashMap<GeneId, Vec<GeneCopy>>, |
| 25 | +} |
| 26 | + |
| 27 | +impl Haplotype { |
| 28 | + pub fn new() -> Self { |
| 29 | + Self::default() |
| 30 | + } |
| 31 | + |
| 32 | + fn map(&self, seg: Segment) -> &HashMap<GeneId, Vec<GeneCopy>> { |
| 33 | + match seg { |
| 34 | + Segment::V => &self.v, |
| 35 | + Segment::D => &self.d, |
| 36 | + Segment::J => &self.j, |
| 37 | + _ => panic!("Haplotype: segment must be V/D/J, got {seg:?}"), |
| 38 | + } |
| 39 | + } |
| 40 | + fn map_mut(&mut self, seg: Segment) -> &mut HashMap<GeneId, Vec<GeneCopy>> { |
| 41 | + match seg { |
| 42 | + Segment::V => &mut self.v, |
| 43 | + Segment::D => &mut self.d, |
| 44 | + Segment::J => &mut self.j, |
| 45 | + _ => panic!("Haplotype: segment must be V/D/J, got {seg:?}"), |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// Set (replace) the copies carried for a gene on this chromosome. |
| 50 | + /// An empty `copies` vec means the gene is deleted here. |
| 51 | + pub fn set(&mut self, seg: Segment, gene: GeneId, copies: Vec<GeneCopy>) { |
| 52 | + self.map_mut(seg).insert(gene, copies); |
| 53 | + } |
| 54 | + pub fn slot(&self, seg: Segment, gene: GeneId) -> &[GeneCopy] { |
| 55 | + self.map(seg).get(&gene).map(Vec::as_slice).unwrap_or(&[]) |
| 56 | + } |
| 57 | + pub fn is_deleted(&self, seg: Segment, gene: GeneId) -> bool { |
| 58 | + self.slot(seg, gene).is_empty() |
| 59 | + } |
| 60 | + /// Genes with at least one carried copy on this chromosome, in |
| 61 | + /// ascending GeneId order (deterministic). |
| 62 | + pub fn present_genes(&self, seg: Segment) -> impl Iterator<Item = GeneId> + '_ { |
| 63 | + let mut genes: Vec<GeneId> = self |
| 64 | + .map(seg) |
| 65 | + .iter() |
| 66 | + .filter(|(_, v)| !v.is_empty()) |
| 67 | + .map(|(g, _)| *g) |
| 68 | + .collect(); |
| 69 | + genes.sort_by_key(|g| g.index()); |
| 70 | + genes.into_iter() |
| 71 | + } |
| 72 | + /// (GeneId, usage-weight) for each present gene, weight from `usage`. |
| 73 | + pub fn gene_weights<F: Fn(GeneId) -> f64>(&self, seg: Segment, usage: &F) -> Vec<(GeneId, f64)> { |
| 74 | + self.present_genes(seg).map(|g| (g, usage(g))).collect() |
| 75 | + } |
| 76 | + /// All carried allele ids for a segment across all present genes. |
| 77 | + pub fn carried_alleles(&self, seg: Segment) -> Vec<AlleleId> { |
| 78 | + let mut out = Vec::new(); |
| 79 | + for g in self.present_genes(seg) { |
| 80 | + for c in self.slot(seg, g) { |
| 81 | + out.push(c.allele); |
| 82 | + } |
| 83 | + } |
| 84 | + out |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// A diploid genotype: two chromosomes + draw weights + provenance. |
| 89 | +#[derive(Clone, Debug)] |
| 90 | +pub struct Genotype { |
| 91 | + haplotypes: [Haplotype; 2], |
| 92 | + chromosome_weights: [f32; 2], |
| 93 | + subject_id: Option<String>, |
| 94 | + source_refdata_hash: String, |
| 95 | +} |
| 96 | + |
| 97 | +impl Genotype { |
| 98 | + pub fn new( |
| 99 | + haplotypes: [Haplotype; 2], |
| 100 | + chromosome_weights: [f32; 2], |
| 101 | + subject_id: Option<String>, |
| 102 | + source_refdata_hash: String, |
| 103 | + ) -> Self { |
| 104 | + Self { |
| 105 | + haplotypes, |
| 106 | + chromosome_weights, |
| 107 | + subject_id, |
| 108 | + source_refdata_hash, |
| 109 | + } |
| 110 | + } |
| 111 | + pub fn haplotype(&self, c: usize) -> &Haplotype { |
| 112 | + &self.haplotypes[c] |
| 113 | + } |
| 114 | + pub fn chromosome_weights(&self) -> [f32; 2] { |
| 115 | + self.chromosome_weights |
| 116 | + } |
| 117 | + pub fn subject_id(&self) -> Option<&str> { |
| 118 | + self.subject_id.as_deref() |
| 119 | + } |
| 120 | + pub fn source_refdata_hash(&self) -> &str { |
| 121 | + &self.source_refdata_hash |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[cfg(test)] |
| 126 | +mod tests { |
| 127 | + use super::*; |
| 128 | + use crate::refdata::AlleleId; |
| 129 | + |
| 130 | + fn copy(id: u32) -> GeneCopy { |
| 131 | + GeneCopy { |
| 132 | + allele: AlleleId::new(id), |
| 133 | + copies: 1, |
| 134 | + weight: 1.0, |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn haplotype_reports_carried_alleles_per_gene_with_deletion_as_empty() { |
| 140 | + let mut h = Haplotype::new(); |
| 141 | + h.set(Segment::V, GeneId::new(0), vec![copy(10)]); // carried |
| 142 | + h.set(Segment::V, GeneId::new(1), vec![]); // deleted |
| 143 | + assert_eq!(h.slot(Segment::V, GeneId::new(0)).len(), 1); |
| 144 | + assert!(h.is_deleted(Segment::V, GeneId::new(1))); |
| 145 | + assert!(h.is_deleted(Segment::V, GeneId::new(2))); // absent == deleted |
| 146 | + let genes: Vec<GeneId> = h.present_genes(Segment::V).collect(); |
| 147 | + assert_eq!(genes, vec![GeneId::new(0)]); // only non-empty slots |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn genotype_carries_two_haplotypes_and_chromosome_weights() { |
| 152 | + let mut h0 = Haplotype::new(); |
| 153 | + let mut h1 = Haplotype::new(); |
| 154 | + h0.set(Segment::V, GeneId::new(0), vec![copy(10)]); |
| 155 | + h1.set(Segment::V, GeneId::new(0), vec![copy(11)]); // heterozygous |
| 156 | + let g = Genotype::new([h0, h1], [0.5, 0.5], Some("S1".into()), "sha256:x".into()); |
| 157 | + assert_eq!(g.chromosome_weights(), [0.5, 0.5]); |
| 158 | + assert_eq!(g.subject_id(), Some("S1")); |
| 159 | + assert_eq!( |
| 160 | + g.haplotype(0).slot(Segment::V, GeneId::new(0))[0].allele, |
| 161 | + AlleleId::new(10) |
| 162 | + ); |
| 163 | + assert_eq!( |
| 164 | + g.haplotype(1).slot(Segment::V, GeneId::new(0))[0].allele, |
| 165 | + AlleleId::new(11) |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn gene_weights_restrict_to_present_genes_and_apply_usage() { |
| 171 | + // chromosome 0 carries genes 0 and 1; usage favors gene 1. |
| 172 | + let mut h = Haplotype::new(); |
| 173 | + h.set(Segment::V, GeneId::new(0), vec![copy(10)]); |
| 174 | + h.set(Segment::V, GeneId::new(1), vec![copy(20)]); |
| 175 | + let usage = |g: GeneId| if g.index() == 1 { 3.0 } else { 1.0 }; |
| 176 | + let w = h.gene_weights(Segment::V, &usage); |
| 177 | + assert_eq!(w, vec![(GeneId::new(0), 1.0), (GeneId::new(1), 3.0)]); |
| 178 | + } |
| 179 | +} |
0 commit comments