cgdist 0.1.1

Ultra-fast SNP/indel-level distance calculator for core genome MLST analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// profile.rs - Allelic profile and matrix data structures

use crate::hashers::AlleleHash;
use rayon::prelude::*;
use regex::Regex;
use std::collections::{HashMap, HashSet};

/// Genetic diversity metrics for the allelic matrix
#[derive(Debug, Clone)]
pub struct DiversityMetrics {
    pub avg_unique_alleles: f64,
    pub diversity_index: f64,
    pub diversity_category: &'static str,
    pub total_unique_pairs: usize,
}

/// Represents a single sample's allelic profile
#[derive(Debug)]
pub struct AllelicProfile {
    pub sample_id: String,
    pub loci_hashes: HashMap<String, AlleleHash>,
}

/// Collection of allelic profiles with associated metadata
#[derive(Debug)]
pub struct AllelicMatrix {
    pub samples: Vec<AllelicProfile>,
    pub loci_names: Vec<String>,
}

impl Default for AllelicMatrix {
    fn default() -> Self {
        Self::new()
    }
}

impl AllelicMatrix {
    /// Create a new empty matrix
    pub fn new() -> Self {
        Self {
            samples: Vec::new(),
            loci_names: Vec::new(),
        }
    }

    /// Load matrix from file with specified hasher
    #[allow(clippy::too_many_arguments)]
    pub fn from_file_with_hasher(
        file_path: &std::path::Path,
        missing_char: &str,
        hasher_type: &str,
        sample_threshold: f64,
        locus_threshold: f64,
        sample_include: Option<&Regex>,
        sample_exclude: Option<&Regex>,
        samples_include: Option<&HashSet<String>>,
        samples_exclude: Option<&HashSet<String>>,
        loci_include: Option<&Regex>,
        loci_exclude: Option<&Regex>,
        loci_include_set: Option<&HashSet<String>>,
        loci_exclude_set: Option<&HashSet<String>>,
    ) -> Result<Self, String> {
        println!(
            "📊 Loading allelic matrix with {} hasher: {}",
            hasher_type.to_uppercase(),
            file_path.display()
        );

        // Get the hasher from registry
        let registry = crate::hashers::HasherRegistry::new();
        let hasher = registry
            .get_hasher(hasher_type)
            .ok_or_else(|| format!("Unknown hasher type: {hasher_type}"))?;

        let extension = file_path
            .extension()
            .and_then(|s| s.to_str())
            .unwrap_or("tsv");

        // Load the matrix with the specific hasher
        let mut matrix = match extension {
            "csv" => Self::from_csv_with_hasher(file_path, missing_char, hasher)?,
            _ => Self::from_tsv_with_hasher(file_path, missing_char, hasher)?,
        };

        // Show initial statistics
        Self::print_matrix_statistics(&matrix, "INITIAL MATRIX");

        // Apply filtering
        matrix.apply_sample_filtering(
            sample_include,
            sample_exclude,
            samples_include,
            samples_exclude,
        )?;
        if sample_include.is_some()
            || sample_exclude.is_some()
            || samples_include.is_some()
            || samples_exclude.is_some()
        {
            Self::print_matrix_statistics(&matrix, "AFTER SAMPLE FILTERING");
        }

        matrix.apply_quality_filters(
            sample_threshold,
            locus_threshold,
            loci_include,
            loci_exclude,
            loci_include_set,
            loci_exclude_set,
        )?;
        Self::print_matrix_statistics(&matrix, "FINAL MATRIX");

        Ok(matrix)
    }

    /// Sample filtering function
    pub fn apply_sample_filtering(
        &mut self,
        sample_include: Option<&Regex>,
        sample_exclude: Option<&Regex>,
        samples_include: Option<&HashSet<String>>,
        samples_exclude: Option<&HashSet<String>>,
    ) -> Result<(), String> {
        let initial_samples = self.samples.len();

        // Apply sample filtering
        if sample_include.is_some()
            || sample_exclude.is_some()
            || samples_include.is_some()
            || samples_exclude.is_some()
        {
            self.samples.retain(|sample| {
                let sample_id = &sample.sample_id;

                // Include regex filter
                if let Some(regex) = sample_include {
                    if !regex.is_match(sample_id) {
                        return false;
                    }
                }

                // Exclude regex filter
                if let Some(regex) = sample_exclude {
                    if regex.is_match(sample_id) {
                        return false;
                    }
                }

                // Include set filter
                if let Some(set) = samples_include {
                    if !set.contains(sample_id) {
                        return false;
                    }
                }

                // Exclude set filter
                if let Some(set) = samples_exclude {
                    if set.contains(sample_id) {
                        return false;
                    }
                }

                true
            });

            let filtered_samples = self.samples.len();
            if initial_samples != filtered_samples {
                println!(
                    "Sample filters: kept {} samples (removed {})",
                    filtered_samples,
                    initial_samples - filtered_samples
                );
            }
        }

        Ok(())
    }

    /// Apply quality filters based on completeness thresholds
    pub fn apply_quality_filters(
        &mut self,
        sample_threshold: f64,
        locus_threshold: f64,
        loci_include: Option<&Regex>,
        loci_exclude: Option<&Regex>,
        loci_include_set: Option<&HashSet<String>>,
        loci_exclude_set: Option<&HashSet<String>>,
    ) -> Result<(), String> {
        println!("\n=== APPLYING QUALITY FILTERS ===");

        let original_samples = self.samples.len();
        let original_loci = self.loci_names.len();

        // Apply loci filtering first
        let mut loci_to_keep = self.loci_names.clone();

        // Regex filters
        if let Some(include_regex) = loci_include {
            loci_to_keep.retain(|locus| include_regex.is_match(locus));
        }
        if let Some(exclude_regex) = loci_exclude {
            loci_to_keep.retain(|locus| !exclude_regex.is_match(locus));
        }

        // Set filters
        if let Some(include_set) = loci_include_set {
            loci_to_keep.retain(|locus| include_set.contains(locus));
        }
        if let Some(exclude_set) = loci_exclude_set {
            loci_to_keep.retain(|locus| !exclude_set.contains(locus));
        }

        self.loci_names = loci_to_keep;
        let before = original_loci;
        let after = self.loci_names.len();
        if before != after {
            println!(
                "Loci filters: kept {} loci (removed {})",
                after,
                before - after
            );
        }

        // Update sample hashes in parallel
        let loci_names_set: HashSet<_> = self.loci_names.iter().cloned().collect();
        self.samples.par_iter_mut().for_each(|sample| {
            sample
                .loci_hashes
                .retain(|locus, _| loci_names_set.contains(locus));
        });

        // Apply sample quality threshold
        if sample_threshold > 0.0 {
            let removed_samples = self.samples.len();
            self.samples.retain(|sample| {
                let total_loci = self.loci_names.len();
                let present_loci = sample
                    .loci_hashes
                    .values()
                    .filter(|hash| !hash.is_missing())
                    .count();
                let completeness = present_loci as f64 / total_loci as f64;
                completeness >= sample_threshold
            });
            let removed_samples = removed_samples - self.samples.len();
            if removed_samples > 0 {
                println!(
                    "Sample completeness filter (threshold {:.1}%): removed {} samples",
                    sample_threshold * 100.0,
                    removed_samples
                );
            }
        }

        // Apply locus quality threshold
        if locus_threshold > 0.0 {
            let loci_to_keep: Vec<String> = self
                .loci_names
                .iter()
                .filter(|locus_name| {
                    let total_samples = self.samples.len();
                    let present_samples = self
                        .samples
                        .iter()
                        .filter(|sample| {
                            sample
                                .loci_hashes
                                .get(*locus_name)
                                .map(|hash| !hash.is_missing())
                                .unwrap_or(false)
                        })
                        .count();
                    let completeness = present_samples as f64 / total_samples as f64;
                    completeness >= locus_threshold
                })
                .cloned()
                .collect();

            if loci_to_keep.len() != self.loci_names.len() {
                let removed_loci = self.loci_names.len() - loci_to_keep.len();
                println!(
                    "Locus completeness filter (threshold {:.1}%): removed {} loci",
                    locus_threshold * 100.0,
                    removed_loci
                );
                self.loci_names = loci_to_keep;
                let loci_names_set: HashSet<_> = self.loci_names.iter().cloned().collect();
                self.samples.par_iter_mut().for_each(|sample| {
                    sample
                        .loci_hashes
                        .retain(|locus, _| loci_names_set.contains(locus));
                });
            }
        }

        let final_samples = self.samples.len();
        let final_loci = self.loci_names.len();
        println!("Filter summary:");
        println!(
            "  Samples: {}{} (removed {})",
            original_samples,
            final_samples,
            original_samples - final_samples
        );
        println!(
            "  Loci: {}{} (removed {})",
            original_loci,
            final_loci,
            original_loci - final_loci
        );

        if final_samples == 0 {
            return Err("No samples remain after filtering".to_string());
        }
        if final_loci == 0 {
            return Err("No loci remain after filtering".to_string());
        }

        Ok(())
    }

    /// Print matrix statistics
    pub fn print_matrix_statistics(&self, phase: &str) {
        println!("\n📊 === MATRIX STATISTICS ({phase}) ===");
        let total_cells = self.samples.len() * self.loci_names.len();

        // Calculate missing data in parallel
        let sample_missing_counts: Vec<_> = self
            .samples
            .par_iter()
            .map(|sample| {
                let sample_missing = self
                    .loci_names
                    .iter()
                    .filter(|locus| {
                        sample
                            .loci_hashes
                            .get(*locus)
                            .map(|h| h.is_missing())
                            .unwrap_or(true)
                    })
                    .count();
                let missing_percent = 100.0 * sample_missing as f64 / self.loci_names.len() as f64;
                (sample.sample_id.clone(), missing_percent, sample_missing)
            })
            .collect();

        let locus_missing_counts: Vec<_> = self
            .loci_names
            .par_iter()
            .map(|locus| {
                let locus_missing = self
                    .samples
                    .iter()
                    .filter(|sample| {
                        sample
                            .loci_hashes
                            .get(locus)
                            .map(|h| h.is_missing())
                            .unwrap_or(true)
                    })
                    .count();
                let missing_percent = 100.0 * locus_missing as f64 / self.samples.len() as f64;
                (locus.clone(), missing_percent, locus_missing)
            })
            .collect();

        println!(
            "  📏 Dimensions: {} samples × {} loci = {} total cells",
            self.samples.len(),
            self.loci_names.len(),
            total_cells
        );

        // Global missing percentage
        let total_missing: usize = sample_missing_counts
            .iter()
            .map(|(_, _, missing)| missing)
            .sum();
        let global_missing_percent = 100.0 * total_missing as f64 / total_cells as f64;

        print!("  📊 Missing data: {global_missing_percent:.2}% ({total_missing} cells)");
        if global_missing_percent <= 5.0 {
            println!("  🟢 EXCELLENT: Very low missing data");
        } else if global_missing_percent <= 15.0 {
            println!("  🟡 GOOD: Acceptable missing data");
        } else if global_missing_percent <= 30.0 {
            println!(
                "  🟠 FAIR: High missing data ({global_missing_percent:.2}%) - consider quality filters"
            );
        } else {
            println!(
                "  🔴 POOR: Very high missing data ({global_missing_percent:.2}%) - quality filters recommended"
            );
        }

        let complete_samples = sample_missing_counts
            .iter()
            .filter(|(_, missing, _)| *missing == 0.0)
            .count();
        let complete_loci = locus_missing_counts
            .iter()
            .filter(|(_, missing, _)| *missing == 0.0)
            .count();

        println!(
            "  ✅ Complete samples: {} ({:.1}%)",
            complete_samples,
            100.0 * complete_samples as f64 / self.samples.len() as f64
        );
        println!(
            "  ✅ Complete loci: {} ({:.1}%)",
            complete_loci,
            100.0 * complete_loci as f64 / self.loci_names.len() as f64
        );

        // Calculate genetic diversity metrics
        let diversity_stats = self.calculate_diversity_metrics();
        println!(
            "  🧬 Avg unique alleles per locus: {:.1}",
            diversity_stats.avg_unique_alleles
        );
        println!(
            "  📈 Allelic diversity index: {:.3} ({}/{})",
            diversity_stats.diversity_index,
            diversity_stats.diversity_category,
            if diversity_stats.diversity_index < 0.3 {
                "🟢 Low diversity - clonal population"
            } else if diversity_stats.diversity_index < 0.6 {
                "🟡 Moderate diversity"
            } else {
                "🔴 High diversity - very heterogeneous"
            }
        );
        println!(
            "  🔢 Total unique CRC pairs: {}",
            diversity_stats.total_unique_pairs
        );
    }

    /// Calculate genetic diversity metrics for the matrix
    pub fn calculate_diversity_metrics(&self) -> DiversityMetrics {
        // Calculate unique alleles per locus in parallel
        let locus_unique_counts: Vec<usize> = self
            .loci_names
            .par_iter()
            .map(|locus| {
                let mut unique_alleles = HashSet::new();

                // Collect all non-missing alleles for this locus
                for sample in &self.samples {
                    if let Some(hash) = sample.loci_hashes.get(locus) {
                        if !hash.is_missing() {
                            if let Some(crc) = hash.as_crc32() {
                                unique_alleles.insert(crc);
                            }
                        }
                    }
                }

                unique_alleles.len()
            })
            .collect();

        // Calculate statistics
        let total_unique_alleles: usize = locus_unique_counts.iter().sum();
        let avg_unique_alleles = total_unique_alleles as f64 / self.loci_names.len() as f64;

        // Calculate diversity index (normalized average unique alleles per locus)
        // Ranges from 0 (all identical) to 1 (maximum diversity)
        let max_possible_unique = self.samples.len();
        let diversity_index = avg_unique_alleles / max_possible_unique as f64;

        // Categorize diversity
        let diversity_category = if diversity_index < 0.3 {
            "Low"
        } else if diversity_index < 0.6 {
            "Moderate"
        } else {
            "High"
        };

        // Calculate total unique pairs (approximation)
        let total_unique_pairs: usize = locus_unique_counts
            .iter()
            .map(|&unique_count| {
                if unique_count > 1 {
                    unique_count * (unique_count - 1) / 2 // C(n,2)
                } else {
                    0
                }
            })
            .sum();

        DiversityMetrics {
            avg_unique_alleles,
            diversity_index,
            diversity_category,
            total_unique_pairs,
        }
    }
}