@@ -124,3 +124,37 @@ def test_plot_haplotype_sharing_chord(fixture, api: AnophelesHapClustAnalysis):
124124 show = False ,
125125 )
126126 assert fig is not None
127+
128+
129+ @parametrize_with_cases ("fixture,api" , cases = "." )
130+ def test_haplotype_pairwise_distances (fixture , api : AnophelesHapClustAnalysis ):
131+ import xarray as xr
132+
133+ all_sample_sets = api .sample_sets ()["sample_set" ].to_list ()
134+ region = fixture .random_region_str (region_size = 5000 )
135+ sample_sets = [str (np .random .choice (all_sample_sets ))]
136+
137+ # Test legacy tuple return.
138+ dist , phased_samples , n_snps = api .haplotype_pairwise_distances (
139+ region = region ,
140+ sample_sets = sample_sets ,
141+ )
142+ assert isinstance (dist , np .ndarray )
143+ assert isinstance (phased_samples , np .ndarray )
144+ assert isinstance (n_snps , int )
145+ assert dist .ndim == 1 # condensed form
146+ # Each phased sample contributes 2 haplotypes.
147+ n_haps = 2 * len (phased_samples )
148+ assert dist .shape [0 ] == int ((n_haps * (n_haps - 1 )) / 2 )
149+
150+ # Test dataset return mode.
151+ ds = api .haplotype_pairwise_distances (
152+ region = region ,
153+ sample_sets = sample_sets ,
154+ return_dataset = True ,
155+ )
156+ assert isinstance (ds , xr .Dataset )
157+ assert "dist" in ds
158+ assert "sample_id" in ds .coords
159+ assert ds ["dist" ].shape == (n_haps , n_haps )
160+ assert ds .attrs ["n_snps" ] == n_snps
0 commit comments