@@ -3514,10 +3514,10 @@ test_table_collection_subset_with_options(tsk_flags_t options)
3514
3514
// four nodes from two diploids; the first is from pop 0
3515
3515
ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , 0 , 0 , NULL , 0 );
3516
3516
CU_ASSERT_FATAL (ret >= 0 );
3517
- ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0 .0 , 0 , 0 , NULL , 0 );
3517
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 1 .0 , 0 , 0 , NULL , 0 );
3518
3518
CU_ASSERT_FATAL (ret >= 0 );
3519
3519
ret = tsk_node_table_add_row (
3520
- & tables .nodes , TSK_NODE_IS_SAMPLE , 0 .0 , TSK_NULL , 1 , NULL , 0 );
3520
+ & tables .nodes , TSK_NODE_IS_SAMPLE , 2 .0 , TSK_NULL , 1 , NULL , 0 );
3521
3521
CU_ASSERT_FATAL (ret >= 0 );
3522
3522
ret = tsk_node_table_add_row (
3523
3523
& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , TSK_NULL , 1 , NULL , 0 );
@@ -3544,6 +3544,8 @@ test_table_collection_subset_with_options(tsk_flags_t options)
3544
3544
ret = tsk_mutation_table_add_row (
3545
3545
& tables .mutations , 1 , 1 , TSK_NULL , NAN , NULL , 0 , NULL , 0 );
3546
3546
CU_ASSERT_FATAL (ret >= 0 );
3547
+ ret = tsk_table_collection_build_index (& tables , 0 );
3548
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3547
3549
3548
3550
// empty nodes should get empty tables
3549
3551
ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT | options );
@@ -3597,16 +3599,17 @@ test_table_collection_subset_errors(void)
3597
3599
3598
3600
ret = tsk_table_collection_init (& tables , 0 );
3599
3601
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3602
+ tables .sequence_length = 1 ;
3600
3603
ret = tsk_table_collection_init (& tables_copy , 0 );
3601
3604
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3602
3605
3603
3606
// four nodes from two diploids; the first is from pop 0
3604
3607
ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , 0 , 0 , NULL , 0 );
3605
3608
CU_ASSERT_FATAL (ret >= 0 );
3606
- ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0 .0 , 0 , 0 , NULL , 0 );
3609
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 1 .0 , 0 , 0 , NULL , 0 );
3607
3610
CU_ASSERT_FATAL (ret >= 0 );
3608
3611
ret = tsk_node_table_add_row (
3609
- & tables .nodes , TSK_NODE_IS_SAMPLE , 0 .0 , TSK_NULL , 1 , NULL , 0 );
3612
+ & tables .nodes , TSK_NODE_IS_SAMPLE , 2 .0 , TSK_NULL , 1 , NULL , 0 );
3610
3613
CU_ASSERT_FATAL (ret >= 0 );
3611
3614
ret = tsk_node_table_add_row (
3612
3615
& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , TSK_NULL , 1 , NULL , 0 );
@@ -3619,6 +3622,8 @@ test_table_collection_subset_errors(void)
3619
3622
CU_ASSERT_FATAL (ret >= 0 );
3620
3623
ret = tsk_edge_table_add_row (& tables .edges , 0.0 , 1.0 , 1 , 0 , NULL , 0 );
3621
3624
CU_ASSERT_FATAL (ret >= 0 );
3625
+ ret = tsk_table_collection_build_index (& tables , 0 );
3626
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3622
3627
3623
3628
/* Migrations are not supported */
3624
3629
ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
@@ -3629,17 +3634,250 @@ test_table_collection_subset_errors(void)
3629
3634
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MIGRATIONS_NOT_SUPPORTED );
3630
3635
3631
3636
// test out of bounds nodes
3637
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3638
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3632
3639
nodes [0 ] = -1 ;
3633
- ret = tsk_table_collection_subset (& tables , nodes , 4 );
3640
+ ret = tsk_table_collection_subset (& tables_copy , nodes , 4 );
3634
3641
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_NODE_OUT_OF_BOUNDS );
3635
3642
nodes [0 ] = 6 ;
3636
- ret = tsk_table_collection_subset (& tables , nodes , 4 );
3643
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3644
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3645
+ ret = tsk_table_collection_subset (& tables_copy , nodes , 4 );
3637
3646
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_NODE_OUT_OF_BOUNDS );
3638
3647
3648
+ // check integrity
3649
+ nodes [0 ] = 0 ;
3650
+ nodes [1 ] = 1 ;
3651
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3652
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3653
+ ret = tsk_node_table_truncate (& tables_copy .nodes , 3 );
3654
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3655
+ ret = tsk_node_table_add_row (
3656
+ & tables_copy .nodes , TSK_NODE_IS_SAMPLE , 0.0 , -2 , 0 , NULL , 0 );
3657
+ CU_ASSERT_FATAL (ret >= 0 );
3658
+ ret = tsk_table_collection_subset (& tables_copy , nodes , 4 );
3659
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_POPULATION_OUT_OF_BOUNDS );
3660
+
3639
3661
tsk_table_collection_free (& tables );
3640
3662
tsk_table_collection_free (& tables_copy );
3641
3663
}
3642
3664
3665
+ static void
3666
+ test_table_collection_union (void )
3667
+ {
3668
+ int ret ;
3669
+ tsk_table_collection_t tables ;
3670
+ tsk_table_collection_t tables_empty ;
3671
+ tsk_table_collection_t tables_copy ;
3672
+ tsk_id_t node_mapping [3 ];
3673
+
3674
+ memset (node_mapping , 0xff , sizeof (node_mapping ));
3675
+
3676
+ ret = tsk_table_collection_init (& tables , 0 );
3677
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3678
+ tables .sequence_length = 1 ;
3679
+ ret = tsk_table_collection_init (& tables_empty , 0 );
3680
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3681
+ tables_empty .sequence_length = 1 ;
3682
+ ret = tsk_table_collection_init (& tables_copy , 0 );
3683
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3684
+
3685
+ // does not error on empty tables
3686
+ ret = tsk_table_collection_union (
3687
+ & tables , & tables_empty , node_mapping , TSK_UNION_NO_CHECK_SHARED );
3688
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3689
+
3690
+ // three nodes, two pop, three ind, two edge, two site, two mut
3691
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , 0 , 0 , NULL , 0 );
3692
+ CU_ASSERT_FATAL (ret >= 0 );
3693
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , 1 , 1 , NULL , 0 );
3694
+ CU_ASSERT_FATAL (ret >= 0 );
3695
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.5 , 1 , 2 , NULL , 0 );
3696
+ CU_ASSERT_FATAL (ret >= 0 );
3697
+ ret = tsk_individual_table_add_row (& tables .individuals , 0 , NULL , 0 , NULL , 0 );
3698
+ CU_ASSERT_FATAL (ret >= 0 );
3699
+ ret = tsk_individual_table_add_row (& tables .individuals , 0 , NULL , 0 , NULL , 0 );
3700
+ CU_ASSERT_FATAL (ret >= 0 );
3701
+ ret = tsk_individual_table_add_row (& tables .individuals , 0 , NULL , 0 , NULL , 0 );
3702
+ CU_ASSERT_FATAL (ret >= 0 );
3703
+ ret = tsk_population_table_add_row (& tables .populations , NULL , 0 );
3704
+ CU_ASSERT_FATAL (ret >= 0 );
3705
+ ret = tsk_population_table_add_row (& tables .populations , NULL , 0 );
3706
+ CU_ASSERT_FATAL (ret >= 0 );
3707
+ ret = tsk_edge_table_add_row (& tables .edges , 0.0 , 1.0 , 2 , 0 , NULL , 0 );
3708
+ CU_ASSERT_FATAL (ret >= 0 );
3709
+ ret = tsk_edge_table_add_row (& tables .edges , 0.0 , 1.0 , 2 , 1 , NULL , 0 );
3710
+ CU_ASSERT_FATAL (ret >= 0 );
3711
+ ret = tsk_site_table_add_row (& tables .sites , 0.4 , "T" , 1 , NULL , 0 );
3712
+ CU_ASSERT_FATAL (ret >= 0 );
3713
+ ret = tsk_site_table_add_row (& tables .sites , 0.2 , "A" , 1 , NULL , 0 );
3714
+ CU_ASSERT_FATAL (ret >= 0 );
3715
+ ret = tsk_mutation_table_add_row (
3716
+ & tables .mutations , 0 , 0 , TSK_NULL , TSK_UNKNOWN_TIME , NULL , 0 , NULL , 0 );
3717
+ ret = tsk_mutation_table_add_row (
3718
+ & tables .mutations , 1 , 1 , TSK_NULL , TSK_UNKNOWN_TIME , NULL , 0 , NULL , 0 );
3719
+ CU_ASSERT_FATAL (ret >= 0 );
3720
+ ret = tsk_table_collection_build_index (& tables , 0 );
3721
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3722
+ ret = tsk_table_collection_sort (& tables , NULL , 0 );
3723
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3724
+
3725
+ // union with empty should not change
3726
+ // other is empty
3727
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3728
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3729
+ ret = tsk_table_collection_union (
3730
+ & tables_copy , & tables_empty , node_mapping , TSK_UNION_NO_CHECK_SHARED );
3731
+ CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & tables_copy ));
3732
+ // self is empty
3733
+ ret = tsk_table_collection_clear (& tables_copy );
3734
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3735
+ ret = tsk_table_collection_union (
3736
+ & tables_copy , & tables , node_mapping , TSK_UNION_NO_CHECK_SHARED );
3737
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3738
+ CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & tables_copy ));
3739
+
3740
+ // union all shared nodes + subset original nodes = original table
3741
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3742
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3743
+ ret = tsk_table_collection_union (
3744
+ & tables_copy , & tables , node_mapping , TSK_UNION_NO_CHECK_SHARED );
3745
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3746
+ node_mapping [0 ] = 0 ;
3747
+ node_mapping [1 ] = 1 ;
3748
+ node_mapping [2 ] = 2 ;
3749
+ ret = tsk_table_collection_subset (& tables_copy , node_mapping , 3 );
3750
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3751
+ CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & tables_copy ));
3752
+
3753
+ // union with one shared node
3754
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3755
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3756
+ node_mapping [0 ] = TSK_NULL ;
3757
+ node_mapping [1 ] = TSK_NULL ;
3758
+ node_mapping [2 ] = 2 ;
3759
+ ret = tsk_table_collection_union (& tables_copy , & tables , node_mapping , 0 );
3760
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3761
+ CU_ASSERT_EQUAL_FATAL (
3762
+ tables_copy .populations .num_rows , tables .populations .num_rows + 2 );
3763
+ CU_ASSERT_EQUAL_FATAL (
3764
+ tables_copy .individuals .num_rows , tables .individuals .num_rows + 2 );
3765
+ CU_ASSERT_EQUAL_FATAL (tables_copy .nodes .num_rows , tables .nodes .num_rows + 2 );
3766
+ CU_ASSERT_EQUAL_FATAL (tables_copy .edges .num_rows , tables .edges .num_rows + 2 );
3767
+ CU_ASSERT_EQUAL_FATAL (tables_copy .sites .num_rows , tables .sites .num_rows );
3768
+ CU_ASSERT_EQUAL_FATAL (tables_copy .mutations .num_rows , tables .mutations .num_rows + 2 );
3769
+
3770
+ // union with one shared node, but no add pop
3771
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3772
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3773
+ node_mapping [0 ] = TSK_NULL ;
3774
+ node_mapping [1 ] = TSK_NULL ;
3775
+ node_mapping [2 ] = 2 ;
3776
+ ret = tsk_table_collection_union (
3777
+ & tables_copy , & tables , node_mapping , TSK_UNION_NO_ADD_POP );
3778
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3779
+ CU_ASSERT_EQUAL_FATAL (tables_copy .populations .num_rows , tables .populations .num_rows );
3780
+ CU_ASSERT_EQUAL_FATAL (
3781
+ tables_copy .individuals .num_rows , tables .individuals .num_rows + 2 );
3782
+ CU_ASSERT_EQUAL_FATAL (tables_copy .nodes .num_rows , tables .nodes .num_rows + 2 );
3783
+ CU_ASSERT_EQUAL_FATAL (tables_copy .edges .num_rows , tables .edges .num_rows + 2 );
3784
+ CU_ASSERT_EQUAL_FATAL (tables_copy .sites .num_rows , tables .sites .num_rows );
3785
+ CU_ASSERT_EQUAL_FATAL (tables_copy .mutations .num_rows , tables .mutations .num_rows + 2 );
3786
+
3787
+ tsk_table_collection_free (& tables_copy );
3788
+ tsk_table_collection_free (& tables_empty );
3789
+ tsk_table_collection_free (& tables );
3790
+ }
3791
+
3792
+ static void
3793
+ test_table_collection_union_errors (void )
3794
+ {
3795
+ int ret ;
3796
+ tsk_table_collection_t tables ;
3797
+ tsk_table_collection_t tables_copy ;
3798
+ tsk_id_t node_mapping [] = { 0 , 1 };
3799
+
3800
+ ret = tsk_table_collection_init (& tables , 0 );
3801
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3802
+ tables .sequence_length = 1 ;
3803
+ ret = tsk_table_collection_init (& tables_copy , 0 );
3804
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3805
+
3806
+ // two nodes, two pop, two ind, one edge, one site, one mut
3807
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , 0 , 0 , NULL , 0 );
3808
+ CU_ASSERT_FATAL (ret >= 0 );
3809
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.5 , 1 , 1 , NULL , 0 );
3810
+ CU_ASSERT_FATAL (ret >= 0 );
3811
+ ret = tsk_individual_table_add_row (& tables .individuals , 0 , NULL , 0 , NULL , 0 );
3812
+ CU_ASSERT_FATAL (ret >= 0 );
3813
+ ret = tsk_individual_table_add_row (& tables .individuals , 0 , NULL , 0 , NULL , 0 );
3814
+ CU_ASSERT_FATAL (ret >= 0 );
3815
+ ret = tsk_population_table_add_row (& tables .populations , NULL , 0 );
3816
+ CU_ASSERT_FATAL (ret >= 0 );
3817
+ ret = tsk_population_table_add_row (& tables .populations , NULL , 0 );
3818
+ CU_ASSERT_FATAL (ret >= 0 );
3819
+ ret = tsk_edge_table_add_row (& tables .edges , 0.0 , 1.0 , 1 , 0 , NULL , 0 );
3820
+ CU_ASSERT_FATAL (ret >= 0 );
3821
+ ret = tsk_site_table_add_row (& tables .sites , 0.2 , "A" , 1 , NULL , 0 );
3822
+ CU_ASSERT_FATAL (ret >= 0 );
3823
+ ret = tsk_mutation_table_add_row (
3824
+ & tables .mutations , 0 , 0 , TSK_NULL , TSK_UNKNOWN_TIME , NULL , 0 , NULL , 0 );
3825
+ CU_ASSERT_FATAL (ret >= 0 );
3826
+
3827
+ // trigger diff histories error
3828
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3829
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3830
+ ret = tsk_mutation_table_add_row (
3831
+ & tables_copy .mutations , 0 , 1 , TSK_NULL , TSK_UNKNOWN_TIME , NULL , 0 , NULL , 0 );
3832
+ CU_ASSERT_FATAL (ret >= 0 );
3833
+ ret = tsk_table_collection_union (& tables_copy , & tables , node_mapping , 0 );
3834
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_UNION_DIFF_HISTORIES );
3835
+
3836
+ // Migrations are not supported
3837
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3838
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3839
+ tsk_migration_table_add_row (& tables_copy .migrations , 0 , 1 , 0 , 0 , 0 , 0 , NULL , 0 );
3840
+ CU_ASSERT_EQUAL_FATAL (tables_copy .migrations .num_rows , 1 );
3841
+ ret = tsk_table_collection_union (
3842
+ & tables_copy , & tables , node_mapping , TSK_UNION_NO_CHECK_SHARED );
3843
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MIGRATIONS_NOT_SUPPORTED );
3844
+
3845
+ // unsuported union - child shared parent not shared
3846
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3847
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3848
+ node_mapping [0 ] = 0 ;
3849
+ node_mapping [1 ] = TSK_NULL ;
3850
+ ret = tsk_table_collection_union (
3851
+ & tables_copy , & tables , node_mapping , TSK_UNION_NO_ADD_POP );
3852
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_UNION_NOT_SUPPORTED );
3853
+
3854
+ // test out of bounds node_mapping
3855
+ node_mapping [0 ] = -4 ;
3856
+ node_mapping [1 ] = 6 ;
3857
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3858
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3859
+ ret = tsk_table_collection_union (& tables_copy , & tables , node_mapping , 0 );
3860
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_UNION_BAD_MAP );
3861
+
3862
+ // check integrity
3863
+ node_mapping [0 ] = 0 ;
3864
+ node_mapping [1 ] = 1 ;
3865
+ ret = tsk_node_table_add_row (
3866
+ & tables_copy .nodes , TSK_NODE_IS_SAMPLE , 0.0 , -2 , 0 , NULL , 0 );
3867
+ CU_ASSERT_FATAL (ret >= 0 );
3868
+ ret = tsk_table_collection_union (& tables_copy , & tables , node_mapping , 0 );
3869
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_POPULATION_OUT_OF_BOUNDS );
3870
+ ret = tsk_table_collection_copy (& tables , & tables_copy , TSK_NO_INIT );
3871
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3872
+ ret = tsk_node_table_add_row (& tables .nodes , TSK_NODE_IS_SAMPLE , 0.0 , -2 , 0 , NULL , 0 );
3873
+ CU_ASSERT_FATAL (ret >= 0 );
3874
+ ret = tsk_table_collection_union (& tables , & tables_copy , node_mapping , 0 );
3875
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_POPULATION_OUT_OF_BOUNDS );
3876
+
3877
+ tsk_table_collection_free (& tables_copy );
3878
+ tsk_table_collection_free (& tables );
3879
+ }
3880
+
3643
3881
int
3644
3882
main (int argc , char * * argv )
3645
3883
{
@@ -3688,6 +3926,8 @@ main(int argc, char **argv)
3688
3926
test_table_collection_check_integrity },
3689
3927
{ "test_table_collection_subset" , test_table_collection_subset },
3690
3928
{ "test_table_collection_subset_errors" , test_table_collection_subset_errors },
3929
+ { "test_table_collection_union" , test_table_collection_union },
3930
+ { "test_table_collection_union_errors" , test_table_collection_union_errors },
3691
3931
{ NULL , NULL },
3692
3932
};
3693
3933
0 commit comments