@@ -3815,16 +3815,16 @@ test_simplest_mutation_edges(void)
38153815 "1 2 2 0\n" ;
38163816 const char * sites = "0.5 0\n"
38173817 "1.5 0\n" ;
3818- const char * mutations = "0 1 1\n"
3818+ const char * mutations = "0 2 1\n"
3819+ "0 1 1\n"
38193820 "0 0 1\n"
3820- "0 2 1\n"
3821- "1 0 1\n"
3821+ "1 2 1\n"
38223822 "1 1 1\n"
3823- "1 2 1\n" ;
3823+ "1 0 1\n" ;
38243824 tsk_treeseq_t ts ;
38253825 tsk_tree_t tree ;
38263826 /* We have mutations over roots, samples and just isolated nodes */
3827- tsk_id_t mutation_edges [] = { -1 , 0 , -1 , 1 , -1 , -1 };
3827+ tsk_id_t mutation_edges [] = { -1 , -1 , 0 , -1 , -1 , 1 };
38283828 tsk_size_t i , j , k , t ;
38293829 tsk_mutation_t mut ;
38303830 tsk_site_t site ;
@@ -4167,7 +4167,7 @@ test_single_tree_bad_mutations(void)
41674167 ret = tsk_treeseq_init (& ts , & tables , load_flags );
41684168 CU_ASSERT_EQUAL (ret , TSK_ERR_MUTATION_PARENT_AFTER_CHILD );
41694169 tsk_treeseq_free (& ts );
4170- tables .mutations .parent [3 ] = TSK_NULL ;
4170+ tables .mutations .parent [3 ] = 2 ;
41714171
41724172 /* time < node time */
41734173 tables .mutations .time [2 ] = 0 ;
@@ -8559,6 +8559,112 @@ test_init_take_ownership_no_edge_metadata(void)
85598559 tsk_treeseq_free (& ts );
85608560}
85618561
8562+ static void
8563+ test_init_compute_mutation_parents (void )
8564+ {
8565+ int ret ;
8566+ tsk_table_collection_t * tables , * tables2 ;
8567+ tsk_treeseq_t ts ;
8568+ const char * sites = "0 0\n" ;
8569+ /* Make a mutation on a parallel branch the parent*/
8570+ const char * bad_mutations = "0 0 1 -1\n"
8571+ "0 1 1 0\n" ;
8572+
8573+ tables = tsk_malloc (sizeof (tsk_table_collection_t ));
8574+ CU_ASSERT_NOT_EQUAL_FATAL (tables , NULL );
8575+ tables2 = tsk_malloc (sizeof (tsk_table_collection_t ));
8576+ CU_ASSERT_NOT_EQUAL_FATAL (tables2 , NULL );
8577+
8578+ CU_ASSERT_FATAL (tables != NULL );
8579+ ret = tsk_table_collection_init (tables , 0 );
8580+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
8581+
8582+ tables -> sequence_length = 1 ;
8583+ parse_nodes (single_tree_ex_nodes , & tables -> nodes );
8584+ CU_ASSERT_EQUAL_FATAL (tables -> nodes .num_rows , 7 );
8585+ parse_edges (single_tree_ex_edges , & tables -> edges );
8586+ CU_ASSERT_EQUAL_FATAL (tables -> edges .num_rows , 6 );
8587+ parse_sites (sites , & tables -> sites );
8588+ CU_ASSERT_EQUAL_FATAL (tables -> sites .num_rows , 1 );
8589+ parse_mutations (bad_mutations , & tables -> mutations );
8590+ CU_ASSERT_EQUAL_FATAL (tables -> mutations .num_rows , 2 );
8591+ tables -> sequence_length = 1.0 ;
8592+ ret = tsk_table_collection_copy (tables , tables2 , 0 );
8593+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
8594+
8595+ ret = tsk_treeseq_init (& ts , tables , TSK_TS_INIT_BUILD_INDEXES );
8596+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_MUTATION_PARENT );
8597+ tsk_treeseq_free (& ts );
8598+
8599+ ret = tsk_treeseq_init (
8600+ & ts , tables , TSK_TS_INIT_BUILD_INDEXES | TSK_TS_INIT_COMPUTE_MUTATION_PARENTS );
8601+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
8602+ tsk_treeseq_free (& ts );
8603+
8604+ /* When we use take ownership, the check of parents shouldn't overwrite them*/
8605+ ret = tsk_treeseq_init (& ts , tables , TSK_TAKE_OWNERSHIP | TSK_TS_INIT_BUILD_INDEXES );
8606+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_BAD_MUTATION_PARENT );
8607+ CU_ASSERT_EQUAL (tables -> mutations .parent [0 ], -1 );
8608+ CU_ASSERT_EQUAL (tables -> mutations .parent [1 ], 0 );
8609+ tsk_treeseq_free (& ts );
8610+
8611+ /* When we use take ownership and compute, the tables are overwritten*/
8612+ ret = tsk_treeseq_init (& ts , tables2 ,
8613+ TSK_TAKE_OWNERSHIP | TSK_TS_INIT_BUILD_INDEXES
8614+ | TSK_TS_INIT_COMPUTE_MUTATION_PARENTS );
8615+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
8616+ CU_ASSERT_EQUAL (tables2 -> mutations .parent [0 ], -1 );
8617+ CU_ASSERT_EQUAL (tables2 -> mutations .parent [1 ], -1 );
8618+
8619+ /* Don't need to free tables as we took ownership */
8620+ tsk_treeseq_free (& ts );
8621+ }
8622+
8623+ static void
8624+ test_init_compute_mutation_parents_errors (void )
8625+ {
8626+ int ret ;
8627+ tsk_id_t row_ret ;
8628+ tsk_table_collection_t tables ;
8629+ tsk_treeseq_t ts ;
8630+ const char * sites = "0.5 0\n"
8631+ "0 0\n" ;
8632+
8633+ ret = tsk_table_collection_init (& tables , 0 );
8634+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
8635+
8636+ tables .sequence_length = 1 ;
8637+ parse_nodes (single_tree_ex_nodes , & tables .nodes );
8638+ CU_ASSERT_EQUAL_FATAL (tables .nodes .num_rows , 7 );
8639+ parse_edges (single_tree_ex_edges , & tables .edges );
8640+ CU_ASSERT_EQUAL_FATAL (tables .edges .num_rows , 6 );
8641+ parse_sites (sites , & tables .sites );
8642+ CU_ASSERT_EQUAL_FATAL (tables .sites .num_rows , 2 );
8643+ tables .sequence_length = 1.0 ;
8644+
8645+ ret = tsk_treeseq_init (
8646+ & ts , & tables , TSK_TS_INIT_BUILD_INDEXES | TSK_TS_INIT_COMPUTE_MUTATION_PARENTS );
8647+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_UNSORTED_SITES );
8648+ tsk_treeseq_free (& ts );
8649+
8650+ tsk_site_table_clear (& tables .sites );
8651+ row_ret = tsk_site_table_add_row (& tables .sites , 0.5 , "A" , 1 , NULL , 0 );
8652+ CU_ASSERT_EQUAL_FATAL (row_ret , 0 );
8653+ row_ret = tsk_mutation_table_add_row (
8654+ & tables .mutations , 0 , 0 , TSK_NULL , TSK_UNKNOWN_TIME , "A" , 1 , NULL , 0 );
8655+ CU_ASSERT_EQUAL_FATAL (row_ret , 0 );
8656+ row_ret = tsk_mutation_table_add_row (
8657+ & tables .mutations , 0 , 4 , TSK_NULL , TSK_UNKNOWN_TIME , "A" , 1 , NULL , 0 );
8658+ CU_ASSERT_EQUAL_FATAL (row_ret , 1 );
8659+
8660+ ret = tsk_treeseq_init (
8661+ & ts , & tables , TSK_TS_INIT_BUILD_INDEXES | TSK_TS_INIT_COMPUTE_MUTATION_PARENTS );
8662+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MUTATION_PARENT_AFTER_CHILD );
8663+ tsk_treeseq_free (& ts );
8664+
8665+ tsk_table_collection_free (& tables );
8666+ }
8667+
85628668int
85638669main (int argc , char * * argv )
85648670{
@@ -8759,6 +8865,9 @@ main(int argc, char **argv)
87598865 test_extend_haplotypes_conflicting_times },
87608866 { "test_init_take_ownership_no_edge_metadata" ,
87618867 test_init_take_ownership_no_edge_metadata },
8868+ { "test_init_compute_mutation_parents" , test_init_compute_mutation_parents },
8869+ { "test_init_compute_mutation_parents_errors" ,
8870+ test_init_compute_mutation_parents_errors },
87628871 { NULL , NULL },
87638872 };
87648873
0 commit comments