@@ -20,7 +20,7 @@ use crate::TreeSequenceFlags;
20
20
use crate :: TskReturnValue ;
21
21
use crate :: TskitTypeAccess ;
22
22
use crate :: { tsk_flags_t, tsk_id_t, tsk_size_t, TSK_NULL } ;
23
- use crate :: { IndividualId , NodeId } ;
23
+ use crate :: { IndividualId , NodeId , PopulationId } ;
24
24
use ll_bindings:: tsk_table_collection_free;
25
25
26
26
/// A table collection.
@@ -241,11 +241,11 @@ impl TableCollection {
241
241
///
242
242
/// Migration tables are not currently supported
243
243
/// by tree sequence simplification.
244
- pub fn add_migration < N : Into < NodeId > > (
244
+ pub fn add_migration < N : Into < NodeId > , SOURCE : Into < PopulationId > , DEST : Into < PopulationId > > (
245
245
& mut self ,
246
246
span : ( f64 , f64 ) ,
247
247
node : N ,
248
- source_dest : ( tsk_id_t , tsk_id_t ) ,
248
+ source_dest : ( SOURCE , DEST ) ,
249
249
time : f64 ,
250
250
) -> TskReturnValue {
251
251
self . add_migration_with_metadata ( span, node, source_dest, time, None )
@@ -257,11 +257,15 @@ impl TableCollection {
257
257
///
258
258
/// Migration tables are not currently supported
259
259
/// by tree sequence simplification.
260
- pub fn add_migration_with_metadata < N : Into < NodeId > > (
260
+ pub fn add_migration_with_metadata <
261
+ N : Into < NodeId > ,
262
+ SOURCE : Into < PopulationId > ,
263
+ DEST : Into < PopulationId > ,
264
+ > (
261
265
& mut self ,
262
266
span : ( f64 , f64 ) ,
263
267
node : N ,
264
- source_dest : ( tsk_id_t , tsk_id_t ) ,
268
+ source_dest : ( SOURCE , DEST ) ,
265
269
time : f64 ,
266
270
metadata : Option < & dyn MetadataRoundtrip > ,
267
271
) -> TskReturnValue {
@@ -272,8 +276,8 @@ impl TableCollection {
272
276
span. 0 ,
273
277
span. 1 ,
274
278
node. into ( ) . 0 ,
275
- source_dest. 0 ,
276
- source_dest. 1 ,
279
+ source_dest. 0 . into ( ) . 0 ,
280
+ source_dest. 1 . into ( ) . 0 ,
277
281
time,
278
282
md. as_ptr ( ) ,
279
283
md. len ( ) ,
@@ -283,22 +287,22 @@ impl TableCollection {
283
287
}
284
288
285
289
/// Add a row to the node table
286
- pub fn add_node < I : Into < IndividualId > > (
290
+ pub fn add_node < I : Into < IndividualId > , POP : Into < PopulationId > > (
287
291
& mut self ,
288
292
flags : ll_bindings:: tsk_flags_t ,
289
293
time : f64 ,
290
- population : tsk_id_t ,
294
+ population : POP ,
291
295
individual : I ,
292
296
) -> Result < NodeId , TskitError > {
293
297
self . add_node_with_metadata ( flags, time, population, individual, None )
294
298
}
295
299
296
300
/// Add a row with metadata to the node table
297
- pub fn add_node_with_metadata < I : Into < IndividualId > > (
301
+ pub fn add_node_with_metadata < I : Into < IndividualId > , POP : Into < PopulationId > > (
298
302
& mut self ,
299
303
flags : ll_bindings:: tsk_flags_t ,
300
304
time : f64 ,
301
- population : tsk_id_t ,
305
+ population : POP ,
302
306
individual : I ,
303
307
metadata : Option < & dyn MetadataRoundtrip > ,
304
308
) -> Result < NodeId , TskitError > {
@@ -308,7 +312,7 @@ impl TableCollection {
308
312
& mut ( * self . as_mut_ptr ( ) ) . nodes ,
309
313
flags,
310
314
time,
311
- population,
315
+ population. into ( ) . 0 ,
312
316
individual. into ( ) . 0 ,
313
317
md. as_ptr ( ) ,
314
318
md. len ( ) ,
@@ -390,15 +394,15 @@ impl TableCollection {
390
394
}
391
395
392
396
/// Add a row to the population_table
393
- pub fn add_population ( & mut self ) -> TskReturnValue {
397
+ pub fn add_population ( & mut self ) -> Result < PopulationId , TskitError > {
394
398
self . add_population_with_metadata ( None )
395
399
}
396
400
397
401
/// Add a row with metadata to the population_table
398
402
pub fn add_population_with_metadata (
399
403
& mut self ,
400
404
metadata : Option < & dyn MetadataRoundtrip > ,
401
- ) -> TskReturnValue {
405
+ ) -> Result < PopulationId , TskitError > {
402
406
let md = EncodedMetadata :: new ( metadata) ?;
403
407
let rv = unsafe {
404
408
ll_bindings:: tsk_population_table_add_row (
@@ -408,7 +412,7 @@ impl TableCollection {
408
412
)
409
413
} ;
410
414
411
- handle_tsk_return_value ! ( rv)
415
+ handle_tsk_return_value ! ( rv, PopulationId :: from ( rv ) )
412
416
}
413
417
414
418
/// Build the "input" and "output"
@@ -1030,30 +1034,33 @@ mod test {
1030
1034
#[ test]
1031
1035
fn test_add_population ( ) {
1032
1036
let mut tables = TableCollection :: new ( 1000. ) . unwrap ( ) ;
1033
- tables. add_population ( ) . unwrap ( ) ;
1037
+ let pop_id = tables. add_population ( ) . unwrap ( ) ;
1038
+ assert_eq ! ( pop_id, 0 ) ;
1034
1039
assert_eq ! ( tables. populations( ) . num_rows( ) , 1 ) ;
1040
+
1041
+ tables
1042
+ . add_node ( crate :: TSK_NODE_IS_SAMPLE , 0.0 , pop_id, crate :: TSK_NULL )
1043
+ . unwrap ( ) ;
1044
+
1045
+ match tables. nodes ( ) . row ( NodeId :: from ( 0 ) ) {
1046
+ Ok ( x) => match x. population {
1047
+ PopulationId ( 0 ) => ( ) ,
1048
+ _ => panic ! ( "expected PopulationId(0)" ) ,
1049
+ } ,
1050
+ Err ( _) => panic ! ( "expected Ok(_)" ) ,
1051
+ } ;
1035
1052
}
1036
1053
1037
1054
#[ test]
1038
1055
fn test_dump_tables ( ) {
1039
1056
let treefile = "trees.trees" ;
1040
1057
let mut tables = TableCollection :: new ( 1000. ) . unwrap ( ) ;
1041
- tables. add_population ( ) . unwrap ( ) ;
1058
+ let pop_id = tables. add_population ( ) . unwrap ( ) ;
1042
1059
tables
1043
- . add_node (
1044
- crate :: TSK_NODE_IS_SAMPLE ,
1045
- 0.0 ,
1046
- crate :: TSK_NULL ,
1047
- crate :: TSK_NULL ,
1048
- )
1060
+ . add_node ( crate :: TSK_NODE_IS_SAMPLE , 0.0 , pop_id, crate :: TSK_NULL )
1049
1061
. unwrap ( ) ;
1050
1062
tables
1051
- . add_node (
1052
- crate :: TSK_NODE_IS_SAMPLE ,
1053
- 1.0 ,
1054
- crate :: TSK_NULL ,
1055
- crate :: TSK_NULL ,
1056
- )
1063
+ . add_node ( crate :: TSK_NODE_IS_SAMPLE , 1.0 , pop_id, crate :: TSK_NULL )
1057
1064
. unwrap ( ) ;
1058
1065
tables. add_edge ( 0. , tables. sequence_length ( ) , 1 , 0 ) . unwrap ( ) ;
1059
1066
tables
0 commit comments