Skip to content

Commit 72b2977

Browse files
committed
feat: Set tables in collection from owned tables.
1 parent 3a8fa2c commit 72b2977

File tree

1 file changed

+300
-0
lines changed

1 file changed

+300
-0
lines changed

src/table_collection.rs

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,306 @@ impl TableCollection {
858858
/// # }
859859
/// ```
860860
=> add_provenance, self, (*self.inner).provenances);
861+
862+
/// Set the edge table from an [`OwnedEdgeTable`](`crate::OwnedEdgeTable`)
863+
///
864+
/// # Errors
865+
///
866+
/// Any errors from the C API propagate.
867+
///
868+
/// # Example
869+
///
870+
/// ```rust
871+
/// # use tskit::TableAccess;
872+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
873+
/// let mut edges = tskit::OwnedEdgeTable::default();
874+
/// edges.add_row(0., 1., 0, 12).unwrap();
875+
/// tables.set_edges(&edges).unwrap();
876+
/// assert_eq!(tables.edges().num_rows(), 1);
877+
/// assert_eq!(tables.edges().child(0).unwrap(), 12);
878+
/// # edges.clear().unwrap();
879+
/// # assert_eq!(edges.num_rows(), 0);
880+
/// ```
881+
pub fn set_edges(&mut self, edges: &crate::OwnedEdgeTable) -> TskReturnValue {
882+
// SAFETY: neither self nor edges are possible
883+
// to create with null pointers.
884+
let rv = unsafe {
885+
ll_bindings::tsk_edge_table_set_columns(
886+
&mut (*self.inner).edges,
887+
(*edges.as_ptr()).num_rows,
888+
(*edges.as_ptr()).left,
889+
(*edges.as_ptr()).right,
890+
(*edges.as_ptr()).parent,
891+
(*edges.as_ptr()).child,
892+
(*edges.as_ptr()).metadata,
893+
(*edges.as_ptr()).metadata_offset,
894+
)
895+
};
896+
handle_tsk_return_value!(rv)
897+
}
898+
899+
/// Set the node table from an [`OwnedNodeTable`](`crate::OwnedNodeTable`)
900+
///
901+
/// # Errors
902+
///
903+
/// Any errors from the C API propagate.
904+
///
905+
/// # Example
906+
///
907+
/// ```rust
908+
/// # use tskit::TableAccess;
909+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
910+
/// let mut nodes = tskit::OwnedNodeTable::default();
911+
/// nodes.add_row(0, 10.0, -1, -1).unwrap();
912+
/// tables.set_nodes(&nodes).unwrap();
913+
/// assert_eq!(tables.nodes().num_rows(), 1);
914+
/// assert_eq!(tables.nodes().time(0).unwrap(), 10.0);
915+
/// # nodes.clear().unwrap();
916+
/// # assert_eq!(nodes.num_rows(), 0);
917+
/// ```
918+
pub fn set_nodes(&mut self, nodes: &crate::OwnedNodeTable) -> TskReturnValue {
919+
// SAFETY: neither self nor nodes are possible
920+
// to create with null pointers.
921+
let rv = unsafe {
922+
ll_bindings::tsk_node_table_set_columns(
923+
&mut (*self.inner).nodes,
924+
(*nodes.as_ptr()).num_rows,
925+
(*nodes.as_ptr()).flags,
926+
(*nodes.as_ptr()).time,
927+
(*nodes.as_ptr()).population,
928+
(*nodes.as_ptr()).individual,
929+
(*nodes.as_ptr()).metadata,
930+
(*nodes.as_ptr()).metadata_offset,
931+
)
932+
};
933+
handle_tsk_return_value!(rv)
934+
}
935+
936+
/// Set the site table from an [`OwnedSiteTable`](`crate::OwnedSiteTable`)
937+
///
938+
/// # Errors
939+
///
940+
/// Any errors from the C API propagate.
941+
///
942+
/// # Example
943+
///
944+
/// ```rust
945+
/// # use tskit::TableAccess;
946+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
947+
/// let mut sites = tskit::OwnedSiteTable::default();
948+
/// sites.add_row(11.0, None).unwrap();
949+
/// tables.set_sites(&sites).unwrap();
950+
/// assert_eq!(tables.sites().num_rows(), 1);
951+
/// assert_eq!(tables.sites().position(0).unwrap(), 11.0);
952+
/// # sites.clear().unwrap();
953+
/// # assert_eq!(sites.num_rows(), 0);
954+
/// ```
955+
pub fn set_sites(&mut self, sites: &crate::OwnedSiteTable) -> TskReturnValue {
956+
// SAFETY: neither self nor nodes are possible
957+
// to create with null pointers.
958+
let rv = unsafe {
959+
ll_bindings::tsk_site_table_set_columns(
960+
&mut (*self.inner).sites,
961+
(*sites.as_ptr()).num_rows,
962+
(*sites.as_ptr()).position,
963+
(*sites.as_ptr()).ancestral_state,
964+
(*sites.as_ptr()).ancestral_state_offset,
965+
(*sites.as_ptr()).metadata,
966+
(*sites.as_ptr()).metadata_offset,
967+
)
968+
};
969+
handle_tsk_return_value!(rv)
970+
}
971+
972+
/// Set the mutation table from an [`OwnedMutationTable`](`crate::OwnedSiteTable`)
973+
///
974+
/// # Errors
975+
///
976+
/// Any errors from the C API propagate.
977+
///
978+
/// # Example
979+
///
980+
/// ```rust
981+
/// # use tskit::TableAccess;
982+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
983+
/// let mut mutations = tskit::OwnedMutationTable::default();
984+
/// mutations.add_row(14, 12, -1, 11.3, None).unwrap();
985+
/// tables.set_mutations(&mutations).unwrap();
986+
/// assert_eq!(tables.mutations().num_rows(), 1);
987+
/// assert_eq!(tables.mutations().site(0).unwrap(), 14);
988+
/// # mutations.clear().unwrap();
989+
/// # assert_eq!(mutations.num_rows(), 0);
990+
/// ```
991+
pub fn set_mutations(&mut self, mutations: &crate::OwnedMutationTable) -> TskReturnValue {
992+
// SAFETY: neither self nor nodes are possible
993+
// to create with null pointers.
994+
let rv = unsafe {
995+
ll_bindings::tsk_mutation_table_set_columns(
996+
&mut (*self.inner).mutations,
997+
(*mutations.as_ptr()).num_rows,
998+
(*mutations.as_ptr()).site,
999+
(*mutations.as_ptr()).node,
1000+
(*mutations.as_ptr()).parent,
1001+
(*mutations.as_ptr()).time,
1002+
(*mutations.as_ptr()).derived_state,
1003+
(*mutations.as_ptr()).derived_state_offset,
1004+
(*mutations.as_ptr()).metadata,
1005+
(*mutations.as_ptr()).metadata_offset,
1006+
)
1007+
};
1008+
handle_tsk_return_value!(rv)
1009+
}
1010+
1011+
/// Set the individual table from an [`OwnedIndividualTable`](`crate::OwnedSiteTable`)
1012+
///
1013+
/// # Errors
1014+
///
1015+
/// Any errors from the C API propagate.
1016+
///
1017+
/// # Example
1018+
///
1019+
/// ```rust
1020+
/// # use tskit::TableAccess;
1021+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
1022+
/// let mut individuals = tskit::OwnedIndividualTable::default();
1023+
/// individuals.add_row(0, [0.1, 10.0], None).unwrap();
1024+
/// tables.set_individuals(&individuals).unwrap();
1025+
/// assert_eq!(tables.individuals().num_rows(), 1);
1026+
/// let expected = vec![tskit::Location::from(0.1), tskit::Location::from(10.0)];
1027+
/// assert_eq!(tables.individuals().location(0).unwrap(), Some(expected.as_slice()));
1028+
/// # individuals.clear().unwrap();
1029+
/// # assert_eq!(individuals.num_rows(), 0);
1030+
/// ```
1031+
pub fn set_individuals(&mut self, individuals: &crate::OwnedIndividualTable) -> TskReturnValue {
1032+
// SAFETY: neither self nor nodes are possible
1033+
// to create with null pointers.
1034+
let rv = unsafe {
1035+
ll_bindings::tsk_individual_table_set_columns(
1036+
&mut (*self.inner).individuals,
1037+
(*individuals.as_ptr()).num_rows,
1038+
(*individuals.as_ptr()).flags,
1039+
(*individuals.as_ptr()).location,
1040+
(*individuals.as_ptr()).location_offset,
1041+
(*individuals.as_ptr()).parents,
1042+
(*individuals.as_ptr()).parents_offset,
1043+
(*individuals.as_ptr()).metadata,
1044+
(*individuals.as_ptr()).metadata_offset,
1045+
)
1046+
};
1047+
handle_tsk_return_value!(rv)
1048+
}
1049+
1050+
/// Set the migration table from an [`OwnedMigrationTable`](`crate::OwnedSiteTable`)
1051+
///
1052+
/// # Errors
1053+
///
1054+
/// Any errors from the C API propagate.
1055+
///
1056+
/// # Example
1057+
///
1058+
/// ```rust
1059+
/// # use tskit::TableAccess;
1060+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
1061+
/// let mut migrations = tskit::OwnedMigrationTable::default();
1062+
/// migrations.add_row((0.25, 0.37), 1, (0, 1), 111.0).unwrap();
1063+
/// tables.set_migrations(&migrations).unwrap();
1064+
/// assert_eq!(tables.migrations().num_rows(), 1);
1065+
/// assert_eq!(tables.migrations().time(0).unwrap(), 111.0);
1066+
/// # migrations.clear().unwrap();
1067+
/// # assert_eq!(migrations.num_rows(), 0);
1068+
/// ```
1069+
pub fn set_migrations(&mut self, migrations: &crate::OwnedMigrationTable) -> TskReturnValue {
1070+
// SAFETY: neither self nor edges are possible
1071+
// to create with null pointers.
1072+
let rv = unsafe {
1073+
ll_bindings::tsk_migration_table_set_columns(
1074+
&mut (*self.inner).migrations,
1075+
(*migrations.as_ptr()).num_rows,
1076+
(*migrations.as_ptr()).left,
1077+
(*migrations.as_ptr()).right,
1078+
(*migrations.as_ptr()).node,
1079+
(*migrations.as_ptr()).source,
1080+
(*migrations.as_ptr()).dest,
1081+
(*migrations.as_ptr()).time,
1082+
(*migrations.as_ptr()).metadata,
1083+
(*migrations.as_ptr()).metadata_offset,
1084+
)
1085+
};
1086+
handle_tsk_return_value!(rv)
1087+
}
1088+
1089+
/// Set the population table from an [`OwnedPopulationTable`](`crate::OwnedSiteTable`)
1090+
///
1091+
/// # Errors
1092+
///
1093+
/// Any errors from the C API propagate.
1094+
///
1095+
/// # Example
1096+
///
1097+
/// ```rust
1098+
/// # use tskit::TableAccess;
1099+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
1100+
/// let mut populations = tskit::OwnedPopulationTable::default();
1101+
/// populations.add_row().unwrap();
1102+
/// tables.set_populations(&populations).unwrap();
1103+
/// assert_eq!(tables.populations().num_rows(), 1);
1104+
/// # populations.clear().unwrap();
1105+
/// # assert_eq!(populations.num_rows(), 0);
1106+
/// ```
1107+
pub fn set_populations(&mut self, populations: &crate::OwnedPopulationTable) -> TskReturnValue {
1108+
// SAFETY: neither self nor edges are possible
1109+
// to create with null pointers.
1110+
let rv = unsafe {
1111+
ll_bindings::tsk_population_table_set_columns(
1112+
&mut (*self.inner).populations,
1113+
(*populations.as_ptr()).num_rows,
1114+
(*populations.as_ptr()).metadata,
1115+
(*populations.as_ptr()).metadata_offset,
1116+
)
1117+
};
1118+
handle_tsk_return_value!(rv)
1119+
}
1120+
1121+
#[cfg(any(doc, feature = "provenance"))]
1122+
/// Set the provenance table from an [`OwnedProvenanceTable`](`crate::provenance::OwnedSiteTable`)
1123+
///
1124+
/// # Errors
1125+
///
1126+
/// Any errors from the C API propagate.
1127+
///
1128+
/// # Example
1129+
///
1130+
/// ```rust
1131+
/// # #[cfg(feature="provenance")] {
1132+
/// # use tskit::TableAccess;
1133+
/// let mut tables = tskit::TableCollection::new(1.0).unwrap();
1134+
/// let mut provenances = tskit::provenance::OwnedProvenanceTable::default();
1135+
/// provenances.add_row("I like pancakes").unwrap();
1136+
/// tables.set_provenances(&provenances).unwrap();
1137+
/// assert_eq!(tables.provenances().num_rows(), 1);
1138+
/// assert_eq!(tables.provenances().record(0).unwrap(), "I like pancakes");
1139+
/// # provenances.clear().unwrap();
1140+
/// # assert_eq!(provenances.num_rows(), 0);
1141+
/// # }
1142+
/// ```
1143+
pub fn set_provenances(
1144+
&mut self,
1145+
provenances: &crate::provenance::OwnedProvenanceTable,
1146+
) -> TskReturnValue {
1147+
// SAFETY: neither self nor edges are possible
1148+
// to create with null pointers.
1149+
let rv = unsafe {
1150+
ll_bindings::tsk_provenance_table_set_columns(
1151+
&mut (*self.inner).provenances,
1152+
(*provenances.as_ptr()).num_rows,
1153+
(*provenances.as_ptr()).timestamp,
1154+
(*provenances.as_ptr()).timestamp_offset,
1155+
(*provenances.as_ptr()).record,
1156+
(*provenances.as_ptr()).record_offset,
1157+
)
1158+
};
1159+
handle_tsk_return_value!(rv)
1160+
}
8611161
}
8621162

8631163
impl TableAccess for TableCollection {

0 commit comments

Comments
 (0)