1
1
/*
2
2
* MIT License
3
3
*
4
- * Copyright (c) 2019-2023 Tskit Developers
4
+ * Copyright (c) 2019-2024 Tskit Developers
5
5
*
6
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
* of this software and associated documentation files (the "Software"), to deal
@@ -287,7 +287,8 @@ verify_divergence_matrix(tsk_treeseq_t *ts, tsk_flags_t options)
287
287
ts , n , sample_set_sizes , samples , n * n , index_tuples , 0 , NULL , options , D1 );
288
288
CU_ASSERT_EQUAL_FATAL (ret , 0 );
289
289
290
- ret = tsk_treeseq_divergence_matrix (ts , 0 , NULL , 0 , NULL , options , D2 );
290
+ ret = tsk_treeseq_divergence_matrix (
291
+ ts , n , sample_set_sizes , samples , 0 , NULL , options , D2 );
291
292
CU_ASSERT_EQUAL_FATAL (ret , 0 );
292
293
293
294
for (j = 0 ; j < n ; j ++ ) {
@@ -1057,19 +1058,42 @@ test_single_tree_divergence_matrix(void)
1057
1058
int ret ;
1058
1059
double result [16 ];
1059
1060
double D_branch [16 ] = { 0 , 2 , 6 , 6 , 2 , 0 , 6 , 6 , 6 , 6 , 0 , 4 , 6 , 6 , 4 , 0 };
1060
- double D_site [16 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
1061
+ double D_site [16 ] = { 0 , 1 , 1 , 0 , 1 , 0 , 2 , 1 , 1 , 2 , 0 , 1 , 0 , 1 , 1 , 0 };
1061
1062
1062
- tsk_treeseq_from_text (& ts , 1 , single_tree_ex_nodes , single_tree_ex_edges , NULL , NULL ,
1063
- NULL , NULL , NULL , 0 );
1063
+ tsk_size_t sample_set_sizes [] = { 2 , 2 };
1064
1064
1065
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1065
+ tsk_treeseq_from_text (& ts , 1 , single_tree_ex_nodes , single_tree_ex_edges , NULL ,
1066
+ single_tree_ex_sites , single_tree_ex_mutations , NULL , NULL , 0 );
1067
+
1068
+ ret = tsk_treeseq_divergence_matrix (
1069
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1066
1070
CU_ASSERT_EQUAL_FATAL (ret , 0 );
1067
1071
assert_arrays_almost_equal (16 , result , D_branch );
1068
1072
1069
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_SITE , result );
1073
+ ret = tsk_treeseq_divergence_matrix (
1074
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_SITE , result );
1070
1075
CU_ASSERT_EQUAL_FATAL (ret , 0 );
1071
1076
assert_arrays_almost_equal (16 , result , D_site );
1072
1077
1078
+ ret = tsk_treeseq_divergence_matrix (
1079
+ & ts , 2 , sample_set_sizes , NULL , 0 , NULL , TSK_STAT_SITE , result );
1080
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1081
+
1082
+ ret = tsk_treeseq_divergence_matrix (
1083
+ & ts , 2 , sample_set_sizes , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1084
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1085
+
1086
+ sample_set_sizes [0 ] = 3 ;
1087
+ sample_set_sizes [1 ] = 1 ;
1088
+ ret = tsk_treeseq_divergence_matrix (
1089
+ & ts , 2 , sample_set_sizes , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1090
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1091
+ ret = tsk_treeseq_divergence_matrix (
1092
+ & ts , 2 , sample_set_sizes , NULL , 0 , NULL , TSK_STAT_SITE , result );
1093
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1094
+
1095
+ /* assert_arrays_almost_equal(4, result, D_site); */
1096
+
1073
1097
verify_divergence_matrix (& ts , TSK_STAT_BRANCH );
1074
1098
verify_divergence_matrix (& ts , TSK_STAT_BRANCH | TSK_STAT_SPAN_NORMALISE );
1075
1099
verify_divergence_matrix (& ts , TSK_STAT_SITE );
@@ -1083,7 +1107,7 @@ test_single_tree_divergence_matrix_internal_samples(void)
1083
1107
{
1084
1108
tsk_treeseq_t ts ;
1085
1109
int ret ;
1086
- double result [ 16 ] ;
1110
+ double * result = malloc ( 16 * sizeof ( double )) ;
1087
1111
double D [16 ] = { 0 , 2 , 4 , 3 , 2 , 0 , 4 , 3 , 4 , 4 , 0 , 1 , 3 , 3 , 1 , 0 };
1088
1112
1089
1113
const char * nodes = "1 0 -1 -1\n" /* 2.00┊ 6 ┊ */
@@ -1109,14 +1133,35 @@ test_single_tree_divergence_matrix_internal_samples(void)
1109
1133
"3 3 T -1\n"
1110
1134
"4 4 T -1\n"
1111
1135
"5 5 T -1\n" ;
1136
+ tsk_id_t samples [] = { 0 , 1 , 2 , 5 };
1137
+ tsk_size_t sizes [] = { 1 , 1 , 1 , 1 };
1112
1138
1113
1139
tsk_treeseq_from_text (& ts , 1 , nodes , edges , NULL , sites , mutations , NULL , NULL , 0 );
1114
1140
1115
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1141
+ ret = tsk_treeseq_divergence_matrix (
1142
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1143
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1144
+ assert_arrays_almost_equal (16 , result , D );
1145
+ ret = tsk_treeseq_divergence_matrix (
1146
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_SITE , result );
1116
1147
CU_ASSERT_EQUAL_FATAL (ret , 0 );
1117
1148
assert_arrays_almost_equal (16 , result , D );
1118
1149
1119
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_SITE , result );
1150
+ ret = tsk_treeseq_divergence_matrix (
1151
+ & ts , 4 , sizes , samples , 0 , NULL , TSK_STAT_BRANCH , result );
1152
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1153
+ assert_arrays_almost_equal (16 , result , D );
1154
+ ret = tsk_treeseq_divergence_matrix (
1155
+ & ts , 4 , sizes , samples , 0 , NULL , TSK_STAT_SITE , result );
1156
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1157
+ assert_arrays_almost_equal (16 , result , D );
1158
+
1159
+ ret = tsk_treeseq_divergence_matrix (
1160
+ & ts , 4 , NULL , samples , 0 , NULL , TSK_STAT_BRANCH , result );
1161
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
1162
+ assert_arrays_almost_equal (16 , result , D );
1163
+ ret = tsk_treeseq_divergence_matrix (
1164
+ & ts , 4 , NULL , samples , 0 , NULL , TSK_STAT_SITE , result );
1120
1165
CU_ASSERT_EQUAL_FATAL (ret , 0 );
1121
1166
assert_arrays_almost_equal (16 , result , D );
1122
1167
@@ -1126,6 +1171,7 @@ test_single_tree_divergence_matrix_internal_samples(void)
1126
1171
verify_divergence_matrix (& ts , TSK_STAT_SITE | TSK_STAT_SPAN_NORMALISE );
1127
1172
1128
1173
tsk_treeseq_free (& ts );
1174
+ free (result );
1129
1175
}
1130
1176
1131
1177
static void
@@ -1164,7 +1210,8 @@ test_single_tree_divergence_matrix_multi_root(void)
1164
1210
1165
1211
tsk_treeseq_from_text (& ts , 1 , nodes , edges , NULL , sites , mutations , NULL , NULL , 0 );
1166
1212
1167
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1213
+ ret = tsk_treeseq_divergence_matrix (
1214
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
1168
1215
CU_ASSERT_EQUAL_FATAL (ret , 0 );
1169
1216
assert_arrays_almost_equal (16 , result , D_branch );
1170
1217
@@ -2424,73 +2471,84 @@ test_simplest_divergence_matrix(void)
2424
2471
"1 0 0\n"
2425
2472
"0 1 0\n" ;
2426
2473
const char * edges = "0 1 2 0,1\n" ;
2474
+ const char * sites = "0.1 A\n"
2475
+ "0.6 A\n" ;
2476
+ const char * mutations = "0 0 B -1\n"
2477
+ "1 0 B -1\n" ;
2427
2478
tsk_treeseq_t ts ;
2428
2479
tsk_id_t sample_ids [] = { 0 , 1 };
2429
2480
double D_branch [4 ] = { 0 , 2 , 2 , 0 };
2430
- double D_site [4 ] = { 0 , 0 , 0 , 0 };
2481
+ double D_site [4 ] = { 0 , 2 , 2 , 0 };
2431
2482
double result [4 ];
2432
2483
int ret ;
2433
2484
2434
- tsk_treeseq_from_text (& ts , 1 , nodes , edges , NULL , NULL , NULL , NULL , NULL , 0 );
2485
+ tsk_treeseq_from_text (& ts , 1 , nodes , edges , NULL , sites , mutations , NULL , NULL , 0 );
2435
2486
2436
2487
ret = tsk_treeseq_divergence_matrix (
2437
- & ts , 2 , sample_ids , 0 , NULL , TSK_STAT_BRANCH , result );
2488
+ & ts , 2 , NULL , sample_ids , 0 , NULL , TSK_STAT_BRANCH , result );
2438
2489
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2439
2490
assert_arrays_almost_equal (4 , D_branch , result );
2440
2491
2441
- ret = tsk_treeseq_divergence_matrix (
2442
- & ts , 2 , sample_ids , 0 , NULL , TSK_STAT_BRANCH | TSK_STAT_SPAN_NORMALISE , result );
2492
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , NULL ,
2493
+ TSK_STAT_BRANCH | TSK_STAT_SPAN_NORMALISE , result );
2443
2494
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2444
2495
assert_arrays_almost_equal (4 , D_branch , result );
2445
2496
2446
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 0 , NULL , 0 , result );
2497
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , NULL , 0 , result );
2447
2498
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2448
2499
assert_arrays_almost_equal (4 , D_site , result );
2449
2500
2450
2501
ret = tsk_treeseq_divergence_matrix (
2451
- & ts , 2 , sample_ids , 0 , NULL , TSK_STAT_SPAN_NORMALISE , result );
2502
+ & ts , 2 , NULL , sample_ids , 0 , NULL , TSK_STAT_SPAN_NORMALISE , result );
2452
2503
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2453
2504
assert_arrays_almost_equal (4 , D_site , result );
2454
2505
2455
2506
ret = tsk_treeseq_divergence_matrix (
2456
- & ts , 2 , sample_ids , 0 , NULL , TSK_STAT_SITE , result );
2507
+ & ts , 2 , NULL , sample_ids , 0 , NULL , TSK_STAT_SITE , result );
2457
2508
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2458
2509
assert_arrays_almost_equal (4 , D_site , result );
2459
2510
2460
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
2511
+ ret = tsk_treeseq_divergence_matrix (
2512
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_BRANCH , result );
2461
2513
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2462
2514
assert_arrays_almost_equal (4 , D_branch , result );
2463
2515
2464
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_SITE , result );
2516
+ ret = tsk_treeseq_divergence_matrix (
2517
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_SITE , result );
2465
2518
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2466
2519
assert_arrays_almost_equal (4 , D_site , result );
2467
2520
2468
- ret = tsk_treeseq_divergence_matrix (& ts , 0 , NULL , 0 , NULL , TSK_STAT_NODE , result );
2521
+ ret = tsk_treeseq_divergence_matrix (
2522
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_NODE , result );
2469
2523
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_UNSUPPORTED_STAT_MODE );
2470
2524
2471
2525
ret = tsk_treeseq_divergence_matrix (
2472
- & ts , 0 , NULL , 0 , NULL , TSK_STAT_POLARISED , result );
2526
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_POLARISED , result );
2473
2527
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_STAT_POLARISED_UNSUPPORTED );
2474
2528
2475
2529
ret = tsk_treeseq_divergence_matrix (
2476
- & ts , 0 , NULL , 0 , NULL , TSK_STAT_SITE | TSK_STAT_BRANCH , result );
2530
+ & ts , 0 , NULL , NULL , 0 , NULL , TSK_STAT_SITE | TSK_STAT_BRANCH , result );
2477
2531
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MULTIPLE_STAT_MODES );
2478
2532
2479
2533
sample_ids [0 ] = -1 ;
2480
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 0 , NULL , 0 , result );
2534
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , NULL , 0 , result );
2481
2535
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_NODE_OUT_OF_BOUNDS );
2482
2536
2483
2537
sample_ids [0 ] = 3 ;
2484
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 0 , NULL , 0 , result );
2538
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , NULL , 0 , result );
2485
2539
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_NODE_OUT_OF_BOUNDS );
2486
2540
2487
2541
sample_ids [0 ] = 1 ;
2488
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 0 , NULL , 0 , result );
2542
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , NULL , 0 , result );
2489
2543
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_DUPLICATE_SAMPLE );
2490
2544
ret = tsk_treeseq_divergence_matrix (
2491
- & ts , 2 , sample_ids , 0 , NULL , TSK_STAT_BRANCH , result );
2545
+ & ts , 2 , NULL , sample_ids , 0 , NULL , TSK_STAT_BRANCH , result );
2492
2546
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_DUPLICATE_SAMPLE );
2493
2547
2548
+ sample_ids [0 ] = 2 ;
2549
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , NULL , 0 , result );
2550
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_SAMPLES );
2551
+
2494
2552
tsk_treeseq_free (& ts );
2495
2553
}
2496
2554
@@ -2515,39 +2573,39 @@ test_simplest_divergence_matrix_windows(void)
2515
2573
2516
2574
tsk_treeseq_from_text (& ts , 1 , nodes , edges , NULL , sites , mutations , NULL , NULL , 0 );
2517
2575
2518
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 2 , windows , 0 , result );
2576
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 2 , windows , 0 , result );
2519
2577
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2520
2578
assert_arrays_almost_equal (8 , D_site , result );
2521
2579
ret = tsk_treeseq_divergence_matrix (
2522
- & ts , 2 , sample_ids , 2 , windows , TSK_STAT_BRANCH , result );
2580
+ & ts , 2 , NULL , sample_ids , 2 , windows , TSK_STAT_BRANCH , result );
2523
2581
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2524
2582
assert_arrays_almost_equal (8 , D_branch , result );
2525
2583
2526
2584
/* Windows for the second half */
2527
2585
ret = tsk_treeseq_divergence_matrix (
2528
- & ts , 2 , sample_ids , 1 , windows + 1 , TSK_STAT_SITE , result );
2586
+ & ts , 2 , NULL , sample_ids , 1 , windows + 1 , TSK_STAT_SITE , result );
2529
2587
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2530
2588
assert_arrays_almost_equal (4 , D_site , result );
2531
2589
ret = tsk_treeseq_divergence_matrix (
2532
- & ts , 2 , sample_ids , 1 , windows + 1 , TSK_STAT_BRANCH , result );
2590
+ & ts , 2 , NULL , sample_ids , 1 , windows + 1 , TSK_STAT_BRANCH , result );
2533
2591
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2534
2592
assert_arrays_almost_equal (4 , D_branch , result );
2535
2593
2536
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 0 , windows , 0 , result );
2594
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 0 , windows , 0 , result );
2537
2595
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_NUM_WINDOWS );
2538
2596
2539
2597
windows [0 ] = -1 ;
2540
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 2 , windows , 0 , result );
2598
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 2 , windows , 0 , result );
2541
2599
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_WINDOWS );
2542
2600
2543
2601
windows [0 ] = 0.45 ;
2544
2602
windows [2 ] = 1.5 ;
2545
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 2 , windows , 0 , result );
2603
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 2 , windows , 0 , result );
2546
2604
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_WINDOWS );
2547
2605
2548
2606
windows [0 ] = 0.55 ;
2549
2607
windows [2 ] = 1.0 ;
2550
- ret = tsk_treeseq_divergence_matrix (& ts , 2 , sample_ids , 2 , windows , 0 , result );
2608
+ ret = tsk_treeseq_divergence_matrix (& ts , 2 , NULL , sample_ids , 2 , windows , 0 , result );
2551
2609
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_WINDOWS );
2552
2610
2553
2611
tsk_treeseq_free (& ts );
@@ -2558,7 +2616,7 @@ test_simplest_divergence_matrix_internal_sample(void)
2558
2616
{
2559
2617
const char * nodes = "1 0 0\n"
2560
2618
"1 0 0\n"
2561
- "0 1 0\n" ;
2619
+ "1 1 0\n" ;
2562
2620
const char * edges = "0 1 2 0,1\n" ;
2563
2621
tsk_treeseq_t ts ;
2564
2622
tsk_id_t sample_ids [] = { 0 , 1 , 2 };
@@ -2570,12 +2628,12 @@ test_simplest_divergence_matrix_internal_sample(void)
2570
2628
tsk_treeseq_from_text (& ts , 1 , nodes , edges , NULL , NULL , NULL , NULL , NULL , 0 );
2571
2629
2572
2630
ret = tsk_treeseq_divergence_matrix (
2573
- & ts , 3 , sample_ids , 0 , NULL , TSK_STAT_BRANCH , result );
2631
+ & ts , 3 , NULL , sample_ids , 0 , NULL , TSK_STAT_BRANCH , result );
2574
2632
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2575
2633
assert_arrays_almost_equal (9 , D_branch , result );
2576
2634
2577
2635
ret = tsk_treeseq_divergence_matrix (
2578
- & ts , 3 , sample_ids , 0 , NULL , TSK_STAT_SITE , result );
2636
+ & ts , 3 , NULL , sample_ids , 0 , NULL , TSK_STAT_SITE , result );
2579
2637
CU_ASSERT_EQUAL_FATAL (ret , 0 );
2580
2638
assert_arrays_almost_equal (9 , D_site , result );
2581
2639
0 commit comments