-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvcf_cluster_explore.py
More file actions
1704 lines (1554 loc) · 78.8 KB
/
Copy pathvcf_cluster_explore.py
File metadata and controls
1704 lines (1554 loc) · 78.8 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
Explores the deeper genetic clusters (lineages) in a `.vcf` dataset in a way
that is NOT biased by the presence of clones. Where `vcf_clone_detect.py` asks
"which samples are near-identical (clones)", this script asks "what are the
genetic groups, how many of them are there, and how differentiated are they".
Clones are non-independent samples that distort per-group allele frequencies and
therefore bias essentially every population-genetic measure (Fst, private and
fixed-private alleles, heterozygosity, ordination, and even the clustering
itself). By default the script does NOT remove clones - it runs on all
samples as given (use this when the input is already clone-corrected, or to
inspect the raw structure). Optional CLONE-CORRECTION reduces each clonal genet
to a single representative ramet (the one with the least missing data) and runs
the entire analysis - similarity, tree, K-evaluation and all differentiation
statistics - on that clone-corrected (genet) set. Enable it with `--auto-clone`
(clonal genets detected internally by reusing `vcf_clone_detect.py` at an auto-
inferred threshold), `--clone-threshold PCT` (manual threshold), or
`--clone-list FILE` (external list of samples to drop, e.g. the "samples to
remove" output of `vcf_clone_detect.py`).
The script (1) computes pairwise genetic similarities (`--method`, default
`ibs`; `dosage` is the documented alternative), (2) builds a UPGMA / average-
linkage tree, (3) evaluates K = 2 .. `--max-k`, reporting for each K the
genetic-similarity cut-off that splits the tree into K groups, the merge-height
separation gap, and the silhouette width, and picks the best K by silhouette
(robust to between-cluster GD overlap, unlike the raw gap), (4) assigns every
sample to a cluster at each K, and (5) produces a two-page PDF. PAGE 1 is a
full-page-width tree with readable tip labels, the per-K cluster-assignment
columns, a % genotyped bar and - when a popfile (or `--pops-from-sample-id`) is
given - one dot-strip panel per metadata track (population from column 2 and,
if present, a lineage/species/region from column 3; or the 2nd/3rd/4th `_`-
delimited fields of the sample name) so each sample's category memberships line
up with its tip; the selected-K column is boxed, and tracks can be renamed with
`--fields`. PAGE 2 is a fixed A4 page (for supplementary docs), a row-based
grid: (row 1) a fanned circular tree with cluster-coloured branches/tips, then
ordination axes 1-vs-2 and 2-vs-3 with hulls; (row 2) the metric-vs-K support
curve and per-sample silhouette; (row 3, with tracks) one stacked bar per field
showing how the clusters distribute across that field's categories; (row 4)
white-yellow-red differentiation heatmaps - shared loci (or shared genotyped
SNPs without a `.loci` file; diagonal = each cluster's own total), pairwise
private alleles excluding singletons, and alternatively fixed SNPs (all counted
over sites with >= 2 samples per cluster).
The UPGMA linkage is the single source of truth: it is drawn as the tree AND
cut to give every K-assignment, so the tree and the columns are always coherent.
With `--tree nj` a neighbour-joining tree (as in `vcf_clone_detect.py`) is drawn
for display instead, but the cluster assignments still come from UPGMA.
Every PDF page is labelled with the VCF basename (top-left). With `--split-output
K` the dataset is split into the K clusters at that K and the whole analysis is
re-run separately for each (writing `_C<n>` csv/pdf outputs), to explore the
sub-structure within each cluster.
Example:
python3 vcf_cluster_explore.py --vcf vcf_file.vcf --pop pop_file.txt \
--output clusters.csv
"""
import sys
import os
import argparse
import numpy as np
import vcf_clone_detect
__author__ = 'Pim Bongaerts'
__copyright__ = 'Copyright (C) 2026 Pim Bongaerts'
__license__ = 'GPL'
DEFAULT_METHOD = 'ibs'
DEF_MAX_K = 10
DEF_MIN_CLUSTER_SIZE = 2
MAX_CATEGORIES = 20 # annotation tracks with more values are dropped
NAME_TRIM = 32 # tree tip labels are trimmed to this many chars
SPECIES_NAME_THRESHOLD = 0.75 # >this fraction one species -> name cluster by it
TRACK_COLORS = ['0.0', '0.45', '0.7'] # one grey per field (black -> light)
CLONE_FLOOR = vcf_clone_detect.DEF_THRESHOLD # only pairs >= this can be clones
CSV_SUFFIX = '_clusters.csv'
PDF_SUFFIX = '_clusters.pdf'
# --------------------------------------------------------------------------- #
# Clone-correction (the defining step)
# --------------------------------------------------------------------------- #
def read_clone_list(filename):
""" Read a list of sample names to drop (one per line, '#' comments). """
remove = set()
with open(filename, 'r') as handle:
for line in handle:
name = line.strip()
if name and not name.startswith('#'):
remove.add(name.split()[0])
return remove
def infer_clone_threshold(comparisons):
""" Quietly infer a clonal similarity threshold from the largest gap among
the highest matches (mirrors `vcf_clone_detect.output_highest_matches`).
Returns None when there is no distinct high-similarity (clonal) signal. """
last = highest_diff = max_p = min_p = 0.0
for row in np.nditer(comparisons):
match_perc = float(row[vcf_clone_detect.C_MATCH_PERC])
if match_perc < CLONE_FLOOR:
break
if last != 0.0:
diff = round(last - match_perc, 2)
if diff > highest_diff:
highest_diff = diff
min_p = match_perc
max_p = last
last = match_perc
if max_p == 0.0:
return None
if int(max_p) > min_p:
return float(int(max_p))
return (max_p + min_p) / 2.0
def subset_data(data, keep_idx):
""" Return a copy of the load_vcf_arrays dict restricted to sample columns
in `keep_idx` (sample-indexed arrays sliced; site-level fields unchanged). """
new = dict(data)
new['names'] = [data['names'][i] for i in keep_idx]
new['state_code'] = data['state_code'][:, keep_idx]
new['dosage'] = data['dosage'][:, keep_idx]
if data.get('ad_p') is not None:
new['ad_p'] = data['ad_p'][:, keep_idx]
if data.get('ad_present') is not None:
new['ad_present'] = data['ad_present'][:, keep_idx]
return new
def clone_correct(data, method, clone_list_file, clone_threshold, auto_clone):
""" Optionally reduce each clonal genet to one representative ramet. Clone-
correction only happens with --clone-list, --clone-threshold or --auto-clone;
otherwise the data is returned unchanged. Returns (data, removed_set, info). """
names = data['names']
if clone_list_file:
remove = read_clone_list(clone_list_file) & set(names)
info = ('supplied --clone-list `{0}` ({1} of its samples present)'
.format(clone_list_file, len(remove)))
elif clone_threshold is not None or auto_clone:
match_mat, both_mat, geno_count = vcf_clone_detect.compute_pair_matrices(
data, method)
comparisons = vcf_clone_detect.comparisons_for_indices(
names, match_mat, both_mat, geno_count, {}, range(len(names)))
if clone_threshold is not None:
threshold = float(clone_threshold)
source = 'manual --clone-threshold'
else:
threshold = infer_clone_threshold(comparisons)
source = 'auto-inferred threshold'
if threshold is None:
return data, set(), 'no distinct clonal signal detected (--auto-clone)'
groups = vcf_clone_detect.cluster_clones(comparisons, threshold)
remove = set()
for group in groups:
remove.update(group.get_samples_to_remove())
info = ('{0} = {1}%, {2} clonal group(s), {3} ramet(s) removed'
.format(source, round(threshold, 2), len(groups), len(remove)))
else:
return data, set(), ('clone-correction disabled (default; enable with '
'--auto-clone, --clone-threshold or --clone-list)')
if not remove:
return data, set(), info
keep_idx = [i for i, nm in enumerate(names) if nm not in remove]
return subset_data(data, keep_idx), remove, info
# --------------------------------------------------------------------------- #
# Similarity -> distance -> UPGMA linkage
# --------------------------------------------------------------------------- #
def compute_distance_matrix(data, method):
""" Build the NxN similarity (%) and distance (1 - sim/100) matrices.
Returns (sim, dist, n_no_overlap). """
match_mat, denom_mat = vcf_clone_detect._match_denom_matrices(data, method)
with np.errstate(divide='ignore', invalid='ignore'):
sim = np.where(denom_mat > 0, 100.0 * match_mat / denom_mat, np.nan)
np.fill_diagonal(sim, 100.0)
dist = 1.0 - sim / 100.0
# Pairs with no shared genotyped sites are NaN; scipy rejects those, so set
# them to the maximum distance (1.0) and count how many were affected.
nan_mask = ~np.isfinite(dist)
np.fill_diagonal(nan_mask, False)
n_no_overlap = int(nan_mask.sum() // 2)
dist[~np.isfinite(dist)] = 1.0
np.fill_diagonal(dist, 0.0)
dist = (dist + dist.T) / 2.0 # enforce exact symmetry for squareform
np.fill_diagonal(dist, 0.0)
return sim, dist, n_no_overlap
def build_linkage(dist):
""" UPGMA / average-linkage condensed-distance clustering (scipy). """
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage
return linkage(squareform(dist, checks=False), method='average')
# --------------------------------------------------------------------------- #
# K evaluation (cut-off + separation gap per K, with stopping rule)
# --------------------------------------------------------------------------- #
def silhouette_samples(dist, labels):
""" Per-sample silhouette width from a precomputed dissimilarity matrix.
s(i) compares how much better sample i fits its own cluster than the nearest
other cluster; robust to between-cluster distance overlap (unlike the raw
merge-height gap). Returns an array (one value per sample, range ~[-1, 1]). """
clusters = np.unique(labels)
n_clusters = len(clusters)
n = len(labels)
if n_clusters < 2:
return np.zeros(n)
mean_to = np.zeros((n, n_clusters)) # mean dist from sample to cluster c
counts = np.zeros(n_clusters)
own = np.zeros(n, dtype=int)
for ci, cluster in enumerate(clusters):
member = labels == cluster
counts[ci] = member.sum()
mean_to[:, ci] = dist[:, member].mean(axis=1)
own[member] = ci
own_count = counts[own]
own_mean = mean_to[np.arange(n), own]
# a(i): mean within-cluster distance, excluding self (self-distance is 0)
with np.errstate(divide='ignore', invalid='ignore'):
a = np.where(own_count > 1, own_mean * own_count / (own_count - 1), 0.0)
# b(i): smallest mean distance to any other cluster
other = mean_to.copy()
other[np.arange(n), own] = np.inf
b = other.min(axis=1)
denom = np.maximum(a, b)
return np.where(denom > 0, (b - a) / denom, 0.0)
def mean_silhouette(dist, labels):
""" Mean silhouette width (higher = better-separated clusters). """
return float(np.mean(silhouette_samples(dist, labels)))
def calinski_harabasz(dist, labels):
""" Calinski-Harabasz index from a precomputed distance matrix (via the
Huygens identity on squared distances). A second, variance-based K-selection
index that corroborates the silhouette; higher = better-separated. """
clusters = np.unique(labels)
k = len(clusters)
n = len(labels)
if k < 2 or n <= k:
return 0.0
sq = dist ** 2
total_ss = sq.sum() / (2.0 * n)
within_ss = 0.0
for cluster in clusters:
member = labels == cluster
nc = int(member.sum())
if nc > 0:
within_ss += sq[np.ix_(member, member)].sum() / (2.0 * nc)
between_ss = total_ss - within_ss
if within_ss <= 0:
return 0.0
return float((between_ss / (k - 1)) / (within_ss / (n - k)))
def evaluate_k(linkage_matrix, dist, n, max_k, min_cluster_size):
""" Evaluate every K from 2 to max_k: the similarity cut-off that yields K
clusters, the merge-height separation gap, and the silhouette width. Returns
(records, best_k). The whole range is scanned (no early break); best_k is the
K with the highest silhouette among splits whose clusters all meet
`min_cluster_size` (so that adding singletons at high K does not win).
Indexing: `Z[:,2]` are the n-1 merge heights ascending. K clusters occur for
a cut height in (Z[n-1-K, 2], Z[n-K, 2]); the gap supporting K is the width
of that interval. """
from scipy.cluster.hierarchy import fcluster
heights = linkage_matrix[:, 2]
root_height = heights[n - 2] if n >= 2 else 1.0
kmax = min(max_k, n - 1)
records = []
for k in range(2, kmax + 1):
h_low = heights[n - 1 - k]
h_high = heights[n - k]
cutoff_sim = 100.0 * (1.0 - 0.5 * (h_low + h_high))
abs_gap = h_high - h_low
rel_gap = abs_gap / root_height if root_height > 0 else 0.0
labels = fcluster(linkage_matrix, k, criterion='maxclust')
sizes = np.bincount(labels)[1:]
records.append({'K': k, 'labels': labels, 'cutoff': cutoff_sim,
'abs_gap': abs_gap, 'rel_gap': rel_gap,
'silhouette': mean_silhouette(dist, labels),
'ch': calinski_harabasz(dist, labels),
'min_size': int(sizes.min()),
'n_clusters': int((sizes > 0).sum())})
valid = [r for r in records if r['min_size'] >= min_cluster_size]
pool = valid if valid else records
best_k = max(pool, key=lambda r: r['silhouette'])['K']
return records, best_k
def select_record(records, k):
""" Return the record for a given K (or the closest available). """
for record in records:
if record['K'] == k:
return record
return records[0]
# --------------------------------------------------------------------------- #
# Per-cluster / pairwise differentiation statistics (clone-corrected data)
# --------------------------------------------------------------------------- #
def cluster_allele_stats(dosage, labels):
""" Per-cluster x per-site allele summaries from the alt-dosage matrix. """
clusters = sorted(set(int(x) for x in labels))
n_clusters = len(clusters)
n_sites = dosage.shape[0]
freq = np.full((n_clusters, n_sites), np.nan)
ncall = np.zeros((n_clusters, n_sites))
altcnt = np.zeros((n_clusters, n_sites))
for ci, cluster in enumerate(clusters):
sub = dosage[:, labels == cluster]
valid = sub >= 0
nseg = valid.sum(axis=1)
alt = np.where(valid, sub, 0).sum(axis=1)
ncall[ci] = nseg
altcnt[ci] = alt
seg = nseg > 0
freq[ci, seg] = alt[seg] / (2.0 * nseg[seg])
return {'clusters': clusters, 'n_clusters': n_clusters, 'freq': freq,
'ncall': ncall, 'altcnt': altcnt,
'alt_present': altcnt > 0,
'ref_present': (2 * ncall - altcnt) > 0,
'has_data': ncall > 0}
def private_alleles(stats):
""" Per-cluster private alleles (present in this cluster, absent in all
others). Returns list of (total_private, fixed_private) per cluster. """
alt_present, ref_present = stats['alt_present'], stats['ref_present']
freq = stats['freq']
total_alt = alt_present.sum(axis=0)
total_ref = ref_present.sum(axis=0)
out = []
for ci in range(stats['n_clusters']):
priv_alt = alt_present[ci] & ((total_alt - alt_present[ci]) == 0)
priv_ref = ref_present[ci] & ((total_ref - ref_present[ci]) == 0)
total = int(priv_alt.sum() + priv_ref.sum())
fixed = int((priv_alt & (freq[ci] == 1.0)).sum() +
(priv_ref & (freq[ci] == 0.0)).sum())
out.append((total, fixed))
return out
def pairwise_matrices(stats):
""" Pairwise fixed-difference counts and directional private-allele counts.
Returns (fixed_diff[KxK], priv_pair[KxK]) where priv_pair[a, b] = alleles
present in cluster a but absent in cluster b. """
n_clusters = stats['n_clusters']
freq, has_data = stats['freq'], stats['has_data']
alt_present, ref_present = stats['alt_present'], stats['ref_present']
fixed_diff = np.zeros((n_clusters, n_clusters), dtype=int)
priv_pair = np.zeros((n_clusters, n_clusters), dtype=int)
for a in range(n_clusters):
for b in range(n_clusters):
if a == b:
continue
valid = has_data[a] & has_data[b]
if a < b:
fdiff = int((valid & (np.abs(freq[a] - freq[b]) == 1.0)).sum())
fixed_diff[a, b] = fixed_diff[b, a] = fdiff
priv_pair[a, b] = int(
(alt_present[a] & ~alt_present[b] & has_data[b]).sum() +
(ref_present[a] & ~ref_present[b] & has_data[b]).sum())
return fixed_diff, priv_pair
def pairwise_private_no_singletons(stats):
""" Pairwise private-allele counts EXCLUDING singletons: priv[a, b] = alleles
present in cluster a on >= 2 allele copies (so not a singleton) yet entirely
absent from cluster b (which has data at the site). KxK, NaN-free ints. """
n_clusters = stats['n_clusters']
altcnt, ncall = stats['altcnt'], stats['ncall']
alt_present, ref_present = stats['alt_present'], stats['ref_present']
has_data = stats['has_data']
refcnt = 2.0 * ncall - altcnt
alt_ok = altcnt >= 2 # >= 2 copies -> not a singleton
ref_ok = refcnt >= 2
priv = np.zeros((n_clusters, n_clusters), dtype=int)
for a in range(n_clusters):
for b in range(n_clusters):
if a == b:
continue
priv[a, b] = int(
(alt_ok[a] & ~alt_present[b] & has_data[b]).sum() +
(ref_ok[a] & ~ref_present[b] & has_data[b]).sum())
return priv
def pairwise_fixed_diff_min2(stats):
""" Pairwise fixed-difference counts requiring >= 2 genotyped samples in
BOTH clusters at a site (so a difference is never called off a single
sample). Symmetric KxK int matrix. """
n_clusters = stats['n_clusters']
freq, ncall = stats['freq'], stats['ncall']
fixed = np.zeros((n_clusters, n_clusters), dtype=int)
for a in range(n_clusters):
for b in range(a + 1, n_clusters):
valid = ((ncall[a] >= 2) & (ncall[b] >= 2)
& np.isfinite(freq[a]) & np.isfinite(freq[b]))
fdiff = int((valid & (np.abs(freq[a] - freq[b]) == 1.0)).sum())
fixed[a, b] = fixed[b, a] = fdiff
return fixed
def pairwise_shared_snps(stats):
""" Number of SNP sites genotyped in >= 2 samples of both clusters, for every
cluster pair (the SNP-based fallback for shared loci when no `.loci` file is
given; the >= 2 rule matches the other differentiation panels). Symmetric
KxK int matrix; the DIAGONAL is each cluster's own count (sites genotyped in
>= 2 of its samples). """
n_clusters = stats['n_clusters']
ncall = stats['ncall']
has2 = ncall >= 2 # cluster x site
shared = np.zeros((n_clusters, n_clusters), dtype=int)
for a in range(n_clusters):
for b in range(n_clusters):
shared[a, b] = int((has2[a] & has2[b]).sum())
return shared
def shared_loci_matrix(presence, labels, names):
""" Number of loci recovered in >= 2 samples of both clusters, for every
cluster pair (from an ipyrad `.loci` presence list; the >= 2 rule matches
the other differentiation panels). Symmetric KxK int; the DIAGONAL is each
cluster's own count (loci recovered in >= 2 of its samples). """
name_cluster = {nm: int(labels[i]) for i, nm in enumerate(names)}
clusters = sorted(set(name_cluster.values()))
cidx = {c: i for i, c in enumerate(clusters)}
k = len(clusters)
counts = np.zeros((k, len(presence)), dtype=int) # samples/cluster/locus
for li, locus in enumerate(presence):
for nm in locus:
counts[cidx[name_cluster[nm]], li] += 1
present2 = counts >= 2
shared = np.zeros((k, k), dtype=int)
for a in range(k):
for b in range(k):
shared[a, b] = int((present2[a] & present2[b]).sum())
return shared
def pairwise_fst(stats):
""" Hudson's Fst between every cluster pair (ratio of averages, allele-copy
counts so a single diploid still gives n-1 = 1). Returns KxK (NaN diag). """
n_clusters = stats['n_clusters']
freq, ncall = stats['freq'], stats['ncall']
fst = np.full((n_clusters, n_clusters), np.nan)
for a in range(n_clusters):
for b in range(a + 1, n_clusters):
pa, pb = freq[a], freq[b]
na, nb = 2.0 * ncall[a], 2.0 * ncall[b]
valid = (na >= 2) & (nb >= 2) & np.isfinite(pa) & np.isfinite(pb)
with np.errstate(invalid='ignore', divide='ignore'):
num = ((pa - pb) ** 2
- pa * (1 - pa) / np.where(na > 1, na - 1, np.nan)
- pb * (1 - pb) / np.where(nb > 1, nb - 1, np.nan))
den = pa * (1 - pb) + pb * (1 - pa)
use = valid & np.isfinite(num) & np.isfinite(den)
den_sum = den[use].sum()
value = num[use].sum() / den_sum if den_sum > 0 else np.nan
fst[a, b] = fst[b, a] = value
return fst
def pairwise_dxy(stats):
""" Absolute divergence dxy between every cluster pair: mean over sites (with
data in both) of the expected proportion of pairwise allelic differences
between a sequence drawn from each cluster, `pa*(1-pb) + pb*(1-pa)`. Unlike
Fst (relative), dxy is not deflated by low within-cluster diversity. Returns
KxK (NaN diag). """
n_clusters = stats['n_clusters']
freq, has_data = stats['freq'], stats['has_data']
dxy = np.full((n_clusters, n_clusters), np.nan)
for a in range(n_clusters):
for b in range(a + 1, n_clusters):
pa, pb = freq[a], freq[b]
use = has_data[a] & has_data[b] & np.isfinite(pa) & np.isfinite(pb)
if use.any():
value = float((pa[use] * (1 - pb[use])
+ pb[use] * (1 - pa[use])).mean())
else:
value = np.nan
dxy[a, b] = dxy[b, a] = value
return dxy
def cluster_diversity(state_code, state_is_hom, labels, stats):
""" Per-cluster observed heterozygosity and % polymorphic sites. """
present = state_code >= 0
safe = np.where(present, state_code, 0)
is_het = present & ~state_is_hom[safe]
het_obs, poly = [], []
for ci, cluster in enumerate(stats['clusters']):
member = labels == cluster
denom = present[:, member].sum()
het_obs.append(is_het[:, member].sum() / denom if denom > 0 else np.nan)
freq = stats['freq'][ci]
segregating = (freq > 0) & (freq < 1)
has_data = stats['has_data'][ci].sum()
poly.append(100.0 * segregating.sum() / has_data if has_data else np.nan)
return het_obs, poly
def parse_loci_presence(filename, names):
""" Parse an ipyrad `.loci` file into per-locus sample-presence sets.
Format: each locus is a block of `samplename<whitespace>sequence` lines,
terminated by a `//...|locus_id|` separator line. A sample line means that
sample recovered the locus. Returns a list (one set of sample names per
locus, restricted to `names`) and the count of `.loci` samples that matched
the VCF. """
name_set = set(names)
presence = []
current = set()
matched = set()
with open(filename, 'r') as handle:
for line in handle:
if line.startswith('//'):
if current:
presence.append(current)
current = set()
continue
sample = line.split(None, 1)[0] if line.strip() else ''
if sample in name_set:
current.add(sample)
matched.add(sample)
if current:
presence.append(current)
return presence, len(matched)
def locus_sharing_stats(presence, labels, names):
""" From per-locus sample presence + cluster labels, compute per-cluster
private loci (recovered in >=1 sample of only that cluster) and a pairwise
shared-loci Jaccard matrix. Returns (clusters, private_counts, jaccard). """
name_cluster = {nm: int(labels[i]) for i, nm in enumerate(names)}
clusters = sorted(set(name_cluster.values()))
cidx = {c: i for i, c in enumerate(clusters)}
k = len(clusters)
present_any = np.zeros((k, len(presence)), dtype=bool) # cluster x locus
for li, locus in enumerate(presence):
for nm in locus:
present_any[cidx[name_cluster[nm]], li] = True
n_present = present_any.sum(axis=0)
private = np.zeros(k, dtype=int)
for ci in range(k):
private[ci] = int((present_any[ci] & (n_present == 1)).sum())
jaccard = np.full((k, k), np.nan)
for a in range(k):
for b in range(k):
union = (present_any[a] | present_any[b]).sum()
inter = (present_any[a] & present_any[b]).sum()
jaccard[a, b] = inter / union if union > 0 else np.nan
return clusters, private, jaccard
def ordinate(dosage, dist, kind, n_comp=3):
""" Ordination of samples on the first `n_comp` axes. Returns
(coords[n, n_comp], pct_var[n_comp]). """
if kind == 'pca':
geno = dosage.T.astype(float) # samples x sites
missing = geno < 0
valid = ~missing
col_n = valid.sum(axis=0)
col_mean = np.where(col_n > 0,
np.where(valid, geno, 0).sum(axis=0)
/ np.maximum(col_n, 1), 0.0)
geno = np.where(missing, col_mean, geno)
geno = geno - geno.mean(axis=0)
u_mat, sing, _ = np.linalg.svd(geno, full_matrices=False)
coords = u_mat[:, :n_comp] * sing[:n_comp]
total = (sing ** 2).sum()
pct = (100.0 * sing[:n_comp] ** 2 / total if total > 0
else np.zeros(n_comp))
return coords, pct
# PCoA on the distance matrix (coherent with the tree, but D is non-Euclidean)
n = dist.shape[0]
centering = np.eye(n) - 1.0 / n
gram = -0.5 * centering @ (dist ** 2) @ centering
eigval, eigvec = np.linalg.eigh(gram)
order = np.argsort(eigval)[::-1]
eigval, eigvec = eigval[order], eigvec[:, order]
coords = eigvec[:, :n_comp] * np.sqrt(np.clip(eigval[:n_comp], 0, None))
positive = np.clip(eigval, 0, None).sum()
pct = (100.0 * np.clip(eigval[:n_comp], 0, None) / positive
if positive > 0 else np.zeros(n_comp))
return coords, pct
# --------------------------------------------------------------------------- #
# Output: assignment CSV, filenames
# --------------------------------------------------------------------------- #
def write_assignment_csv(names, records, filename):
""" Write per-sample cluster assignment across all evaluated K (popfile-like:
any single `K*` column is a usable 2-column popfile). """
headers = ['sample'] + ['K{0}'.format(r['K']) for r in records]
with open(filename, 'w') as handle:
handle.write(','.join(headers) + '\n')
for i, name in enumerate(names):
row = [name] + [str(int(r['labels'][i])) for r in records]
handle.write(','.join(row) + '\n')
print('Cluster assignments outputted to file: `{0}`'.format(filename))
def derive_outputs(output_filename, vcf_filename, pdf_output):
""" Decide the CSV and PDF output filenames. With `-o foo.csv` the PDF
mirrors it (`foo.pdf`); otherwise both derive from the vcf basename. """
if output_filename:
csv_filename = output_filename
pdf_base = os.path.splitext(output_filename)[0]
else:
base = os.path.splitext(vcf_filename)[0] if vcf_filename \
else 'cluster_explore'
csv_filename = base + CSV_SUFFIX
pdf_base = base + '_clusters'
pdf_filename = pdf_output if pdf_output else pdf_base + '.pdf'
return csv_filename, pdf_filename
# --------------------------------------------------------------------------- #
# PDF report
# --------------------------------------------------------------------------- #
def cluster_palette(n):
""" A list of n categorical colours. """
import matplotlib.pyplot as plt
if n <= 10:
cmap = plt.cm.tab10
cols = [cmap(i) for i in range(n)]
elif n <= 20:
cmap = plt.cm.tab20
cols = [cmap(i) for i in range(n)]
else:
cmap = plt.cm.gist_rainbow
cols = [cmap(i / max(n - 1, 1)) for i in range(n)]
return cols
def assign_hierarchical_colors(records):
""" Descent-consistent cluster colours across K. Because UPGMA cuts are
strictly nested, a cluster either persists or splits as K grows; we let a
colour follow a lineage so a new colour only appears at a genuine split (and
is never reused for an unrelated cluster). Returns {K: {cluster_id: rgba}}.
Per K (ascending), each cluster is a frozenset of member indices. Processing
new clusters largest-first: a cluster identical to a previous one keeps its
colour; otherwise its parent is the previous cluster that is its superset and
the first (largest) child to claim that parent's colour inherits it, while
later children draw the next unused palette colour. """
max_clusters = max(r['n_clusters'] for r in records)
palette = cluster_palette(max_clusters)
ordered = sorted(records, key=lambda r: r['K'])
colors_by_k = {}
prev = {} # frozenset(members) -> rgba (previous K)
next_color = 0
for record in ordered:
labels = record['labels']
members = {}
for i, lab in enumerate(labels):
members.setdefault(int(lab), set()).add(i)
parts = {cid: frozenset(s) for cid, s in members.items()}
current = {} # frozenset -> rgba (this K)
claimed = set() # previous framesets whose colour was taken
cid_color = {}
# Largest clusters first so the dominant child inherits a parent colour
for cid in sorted(parts, key=lambda c: (-len(parts[c]), min(parts[c]))):
part = parts[cid]
if part in prev: # cluster persisted
colour = prev[part]
else:
parent = next((p for p in prev if part <= p
and p not in claimed), None)
if parent is not None: # first child inherits
colour = prev[parent]
claimed.add(parent)
else: # new lineage -> new colour
colour = palette[next_color % len(palette)]
next_color += 1
current[part] = colour
cid_color[cid] = colour
colors_by_k[record['K']] = cid_color
prev = current
return colors_by_k
def build_upgma_tree(linkage_matrix, names):
""" Convert the scipy UPGMA linkage into a Bio.Phylo tree (so the drawn tree
is exactly the object that is cut for the K-assignments). """
import Bio.Phylo.BaseTree as BaseTree
from scipy.cluster.hierarchy import to_tree
root = to_tree(linkage_matrix, rd=False)
def build(node):
if node.is_leaf():
return BaseTree.Clade(name=names[node.id], branch_length=0.0)
left, right = build(node.get_left()), build(node.get_right())
left.branch_length = node.dist - node.get_left().dist
right.branch_length = node.dist - node.get_right().dist
return BaseTree.Clade(clades=[left, right])
tree = BaseTree.Tree(root=build(root))
tree.ladderize()
return tree
def build_nj_tree(dist, names):
""" Neighbour-joining tree for display (as in vcf_clone_detect). """
import Bio.Phylo.TreeConstruction as TC
lower = [[dist[i][j] for j in range(i + 1)] for i in range(len(names))]
dmatrix = TC._DistanceMatrix(names=list(names), matrix=lower)
tree = TC.DistanceTreeConstructor().nj(dmatrix)
tree.ladderize()
for clade in tree.get_nonterminals():
clade.name = None
return tree
def draw_circular_tree(ax, tree, sample_cluster, cluster_color, title,
span_deg=300.0):
""" Draw a Bio.Phylo tree as a FANNED radial dendrogram: radius = branch-
length distance from the root (centre), tips spread over a `span_deg` fan
(an open wedge, not a full circle, so the deep splits open up), with an extra
angular gap inserted between clusters so genetically differentiated groups
separate visually. Branches that sit entirely within one cluster are drawn in
that cluster's colour (grey where a branch still joins several clusters); tip
dots are coloured by cluster to match the PCA panels. """
tips = tree.get_terminals()
n = len(tips)
if n < 2:
ax.axis('off')
return
# slot positions along the fan, with a gap between adjacent clusters
gap = max(1.0, n / 40.0)
slots, pos, prev = {}, 0.0, None
for tip in tips:
cid = sample_cluster.get(tip.name)
if prev is not None and cid != prev:
pos += gap
slots[tip] = pos
pos += 1.0
prev = cid
total = max(pos - 1.0, 1.0)
span = np.radians(span_deg)
a0 = np.pi / 2.0 + span / 2.0 # fan centred on top, opens downward
tip_angle = {tip: a0 - span * (slots[tip] / total) for tip in tips}
depths = tree.depths() # branch-length distance from root
# post-order (reversed level order): mean tip angle + cluster set per clade
order = list(tree.find_clades(order='level'))
ang_sum, ang_cnt, clset = {}, {}, {}
for clade in reversed(order):
if clade.is_terminal():
ang_sum[clade], ang_cnt[clade] = tip_angle[clade], 1
clset[clade] = {sample_cluster.get(clade.name)}
else:
ang_sum[clade] = sum(ang_sum[c] for c in clade.clades)
ang_cnt[clade] = sum(ang_cnt[c] for c in clade.clades)
clset[clade] = set().union(*(clset[c] for c in clade.clades))
angle = {clade: ang_sum[clade] / ang_cnt[clade] for clade in ang_sum}
def edge_color(clade):
cids = clset[clade]
if len(cids) == 1:
return cluster_color.get(next(iter(cids)), '0.35')
return '0.4'
for clade in order:
if clade.is_terminal():
continue
r = depths[clade]
child_angles = [angle[c] for c in clade.clades]
thetas = np.linspace(min(child_angles), max(child_angles), 40)
ax.plot(r * np.cos(thetas), r * np.sin(thetas),
color=edge_color(clade), lw=0.6, zorder=1)
for child, a in zip(clade.clades, child_angles):
rc = depths[child]
ax.plot([r * np.cos(a), rc * np.cos(a)],
[r * np.sin(a), rc * np.sin(a)], color=edge_color(child),
lw=0.6, zorder=2)
xs, ys, cols = [], [], []
for tip in tips:
a, r = tip_angle[tip], depths[tip]
xs.append(r * np.cos(a))
ys.append(r * np.sin(a))
cols.append(cluster_color.get(sample_cluster.get(tip.name), '0.5'))
ax.scatter(xs, ys, s=14, color=cols, edgecolor='none', zorder=3)
ax.set_aspect('equal')
ax.axis('off')
ax.set_title(title, fontsize=8)
def make_ax_in(fig, fig_w, fig_h):
""" Return an `ax_in(x, ytop, w, h)` that places an axes by inches-from-the-
top-left on `fig` (whose size is fig_w x fig_h inches). """
def ax_in(x_in, ytop_in, w_in, h_in):
return fig.add_axes([x_in / fig_w,
(fig_h - ytop_in - h_in) / fig_h,
w_in / fig_w, h_in / fig_h])
return ax_in
def despine(ax):
""" Hide the top and right spines of an axes. """
for spine in ('top', 'right'):
ax.spines[spine].set_visible(False)
def draw_heatmap(fig, ax, mat, cmap, fmt, title, n_clusters, cluster_labels,
label_colors=None, show_diagonal=False):
""" Draw a KxK cluster-pair heatmap with cell annotations. The diagonal is
blanked (NaN) unless `show_diagonal` is set - used for the shared loci / SNPs
panels, whose diagonal is each cluster's own total. When `label_colors` (one
per cluster) is given, each C-axis tick label sits on a coloured chip in its
cluster's colour, so the heatmap ties directly to the cluster-coloured panels
above it. Annotation text colour follows each cell's background luminance, so
it stays legible on any colormap (including the light end of the map). """
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize, to_rgb
disp = np.array(mat, dtype=float)
if not show_diagonal:
np.fill_diagonal(disp, np.nan)
image = ax.imshow(disp, cmap=cmap, aspect='auto')
ax.set_xticks(range(n_clusters))
ax.set_yticks(range(n_clusters))
ax.set_xticklabels(cluster_labels, fontsize=6, rotation=90)
ax.set_yticklabels(cluster_labels, fontsize=6)
if label_colors is not None:
for ticklabels in (ax.get_xticklabels(), ax.get_yticklabels()):
for i, ticklabel in enumerate(ticklabels):
r, g, b = to_rgb(label_colors[i])
lum = 0.299 * r + 0.587 * g + 0.114 * b
ticklabel.set_color('black' if lum > 0.5 else 'white')
ticklabel.set_bbox(dict(facecolor=label_colors[i],
edgecolor='none',
boxstyle='round,pad=0.15'))
finite = disp[np.isfinite(disp)]
lo = float(finite.min()) if finite.size else 0.0
hi = float(finite.max()) if finite.size else 1.0
cmap_obj = plt.get_cmap(cmap)
norm = Normalize(vmin=lo, vmax=hi)
for a in range(n_clusters):
for b in range(n_clusters):
if np.isfinite(disp[a, b]):
r, g, bl, _ = cmap_obj(norm(disp[a, b]))
lum = 0.299 * r + 0.587 * g + 0.114 * bl
ax.text(b, a, fmt.format(mat[a][b]), ha='center', va='center',
fontsize=5, color='black' if lum > 0.5 else 'white')
ax.set_title(title, fontsize=8)
cbar = fig.colorbar(image, ax=ax, fraction=0.046, pad=0.04)
cbar.ax.tick_params(labelsize=4.5)
cbar.ax.yaxis.get_offset_text().set_fontsize(4.5)
# --------------------------------------------------------------------------- #
# Sample metadata annotation tracks (population / lineage, drawn by the tree)
# --------------------------------------------------------------------------- #
def _make_track(name, assign, names):
""" Package a sample->value mapping into an annotation track (with the
ordered set of distinct values), restricted to `names`. """
values = sorted(set(assign[nm] for nm in names if nm in assign))
return {'name': name, 'assign': assign, 'values': values}
def build_category_tracks(names, pop_filename, pops_from_sample_id,
field_names=None):
""" Build the metadata annotation tracks drawn beside the tree, each a
sample->category mapping visualised as one dot per sample on an x-axis of
that category's distinct values.
With `--pops-from-sample-id` three tracks come from the 2nd, 3rd and 4th
`_`-delimited fields of each sample name (the 1st field is skipped). From a
popfile: a `population` track (column 2) and, when a 3rd column is present, a
`lineage / region` track (column 3). `field_names` (e.g. from `--fields
location,depth`) overrides the default track titles positionally (name 1 =
field 2 / population, name 2 = field 3 / lineage, ...). Returns a (possibly
empty) list of tracks; tracks with no usable values, or with more than
MAX_CATEGORIES distinct values (e.g. a per-sample id field), are dropped
(with a note). """
field_names = field_names or []
def title_for(pos, default):
return field_names[pos] if pos < len(field_names) else default
tracks = []
if pops_from_sample_id:
for pos, (default, idx) in enumerate((('id field 2', 1),
('id field 3', 2),
('id field 4', 3))):
assign = {}
for nm in names:
parts = nm.split('_')
if len(parts) > idx:
assign[nm] = parts[idx]
if assign:
tracks.append(_make_track(title_for(pos, default), assign,
names))
elif pop_filename:
indivs_pops, indivs_lineages = \
vcf_clone_detect.get_assignments_from_popfile(pop_filename)
if any(nm in indivs_pops for nm in names):
tracks.append(_make_track(title_for(0, 'population'), indivs_pops,
names))
if any(nm in indivs_lineages for nm in names):
tracks.append(_make_track(title_for(1, 'lineage / region'),
indivs_lineages, names))
kept = []
for track in tracks:
n_values = len(track['values'])
if n_values == 0:
continue
if n_values > MAX_CATEGORIES:
sys.stderr.write('Note: annotation track `{0}` has {1} distinct '
'values (> MAX_CATEGORIES={2}); not drawn.\n'.format(
track['name'], n_values, MAX_CATEGORIES))
continue
kept.append(track)
return kept
def draw_category_track(ax, track, tip_y, ylim, dot_size, color):
""" Draw one annotation track aligned to the tree tips: for every sample a
dot at the x-position of its category value (categories along the x-axis,
samples along the shared tree y-axis). All dots of a track share one colour
(the value is read off the x-axis, not the colour). """
values = track['values']
vidx = {v: i for i, v in enumerate(values)}
for i in range(len(values)):
ax.axvline(i, color='0.92', lw=0.5, zorder=0)
xs, ys = [], []
for nm, y in tip_y.items():
value = track['assign'].get(nm)
if value is None:
continue
xs.append(vidx[value])
ys.append(y)
ax.scatter(xs, ys, s=dot_size, color=color, edgecolor='none', zorder=3)
ax.set_xlim(-0.5, len(values) - 0.5)
ax.set_ylim(ylim)
ax.set_yticks([])
ax.set_xticks(range(len(values)))
ax.set_xticklabels(values, fontsize=6, rotation=90)
ax.tick_params(length=0)
ax.set_title(track['name'], fontsize=7)
for spine in ('top', 'right', 'left'):
ax.spines[spine].set_visible(False)
# --------------------------------------------------------------------------- #
# PDF report (page 1: full-width tree + annotations; page 2: analysis panels)
# --------------------------------------------------------------------------- #
def write_tree_page(pdf, dist, linkage_matrix, names, display, selected,
perc_genotyped, method, tree_mode, colors_by_k, sel_color,
category_tracks, dataset_label=''):
""" PAGE 1 - a full-page-width tree with readable tip labels, followed (left
to right) by the per-K cluster-assignment columns, a %genotyped bar and one
dot-strip panel per metadata track (population / lineage or the sample-id
fields). Returns the tip y-positions dict, or None if tips can't be found. """
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
import Bio.Phylo
name_idx = {nm: i for i, nm in enumerate(names)}
n_tips = len(names)
n_cols = len(display)
left_margin, right_margin = 0.6, 0.6
top_margin, bottom_margin = 0.6, 0.6
tree_w = 7.5
kcol_w = max(0.9, 0.22 * n_cols)
bar_w = 1.0
track_gap = 0.15
track_ws = [max(0.7, 0.16 * len(t['values'])) for t in category_tracks]
per_tip = 0.16
dot_size = 16
tree_h = max(3.0, n_tips * per_tip)
fig_w = (left_margin + tree_w + 0.1 + kcol_w + 0.15 + bar_w
+ sum(w + track_gap for w in track_ws) + right_margin)
fig_h = top_margin + tree_h + bottom_margin
font_size = max(5.0, min(9.0, per_tip * 72 * 0.7))
fig = plt.figure(figsize=(fig_w, fig_h))
ax_in = make_ax_in(fig, fig_w, fig_h)
# --- tree ----------------------------------------------------------------
ax_tree = ax_in(left_margin, top_margin, tree_w, tree_h)
if tree_mode == 'nj':
tree = build_nj_tree(dist, names)
else:
tree = build_upgma_tree(linkage_matrix, names)
sys.stderr.write('Drawing {0} tree ({1} tips)...\n'.format(tree_mode, n_tips))