Skip to content

Commit b6f698d

Browse files
committed
improve error handlign when reading .trees files
1 parent a2849c3 commit b6f698d

1 file changed

Lines changed: 39 additions & 28 deletions

File tree

core/species.cpp

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10186,30 +10186,6 @@ void Species::ReadTreeSequenceMetadata(TreeSeqInfo &p_treeseq, slim_tick_t *p_ti
1018610186

1018710187
SLiM_json_struct_metadata_get_components((uint8_t *)p_tables.metadata, p_tables.metadata_length, &top_level_json_buffer, &top_level_json_length, &top_level_binary_buffer, &top_level_binary_length, "Species::ReadTreeSequenceMetadata");
1018810188

10189-
size_t trait_count = Traits().size();
10190-
size_t binary_row_length = sizeof(MutationTableMetadataRec) + sizeof(_MutationPerTraitMetadata) * (trait_count - 1);
10191-
10192-
// extract the encoded mutation metadata table row count
10193-
uint64_t mutation_table_row_count = *(uint64_t *)top_level_binary_buffer;
10194-
10195-
top_level_binary_buffer += sizeof(uint64_t);
10196-
top_level_binary_length -= sizeof(uint64_t);
10197-
10198-
if (top_level_binary_length % binary_row_length != 0)
10199-
EIDOS_TERMINATION << "ERROR (Species::ReadTreeSequenceMetadata): the top-level binary metadata does not comprise an integral number of rows (binary_row_length == " << binary_row_length << ", top_level_binary_length == " << top_level_binary_length << "); this file cannot be read." << EidosTerminate();
10200-
10201-
// pass information on the binary mutation metadata table back to the caller
10202-
p_mut_metadata_table.table_buffer = (uint8_t *)top_level_binary_buffer;
10203-
p_mut_metadata_table.row_size = binary_row_length;
10204-
p_mut_metadata_table.row_count = top_level_binary_length / binary_row_length;
10205-
10206-
if (mutation_table_row_count != p_mut_metadata_table.row_count)
10207-
EIDOS_TERMINATION << "ERROR (Species::ReadTreeSequenceMetadata): the top-level binary metadata's specified row count ( " << mutation_table_row_count << ") does not match the number of rows of data present (" << p_mut_metadata_table.row_count << "); this file cannot be read." << EidosTerminate();
10208-
10209-
#if DEBUG
10210-
//std::cout << "ReadTreeSequenceMetadata(): read binary mutation table with row size " << p_mut_metadata_table.row_size << " and " << p_mut_metadata_table.row_count << " rows." << std::endl;
10211-
#endif
10212-
1021310189
// Note: we *could* parse the metadata schema, but instead we'll just try parsing the metadata.
1021410190
// std::string metadata_schema_str(p_tables->metadata_schema, p_tables->metadata_schema_length);
1021510191
// nlohmann::json metadata_schema = nlohmann::json::parse(metadata_schema_str);
@@ -10309,15 +10285,22 @@ void Species::ReadTreeSequenceMetadata(TreeSeqInfo &p_treeseq, slim_tick_t *p_ti
1030910285
// The "chromosomes" key is optional, but if provided, it has to make sense
1031010286
if (top_level_json["SLiM"].contains("chromosomes"))
1031110287
{
10312-
chomosomes_key_present = true;
10313-
1031410288
// We validate the whole "chromosomes" key against the whole model, to make sure everything is as expected
1031510289
auto &chromosomes_metadata = top_level_json["SLiM"]["chromosomes"];
1031610290

1031710291
if (!chromosomes_metadata.is_array())
10292+
{
1031810293
SLIM_ERRSTREAM << "#WARNING (Species::ReadTreeSequenceMetadata): the 'chromosomes' metadata key must be an array." << std::endl;
10294+
goto noChromosomesKey;
10295+
}
10296+
1031910297
if (chromosomes_metadata.size() != Chromosomes().size())
10320-
SLIM_ERRSTREAM << "#WARNING (Species::ReadTreeSequenceMetadata): the number of entries in the 'chromosomes' metadata key does not match the number of chromosomes in the model." << std::endl;
10298+
{
10299+
SLIM_ERRSTREAM << "#WARNING (Species::ReadTreeSequenceMetadata): the number of entries in the 'chromosomes' metadata key (" << chromosomes_metadata.size() << ") does not match the number of chromosomes in the model (" << Chromosomes().size() << "). (The 'chromosomes' metadata key will be ignored.)" << std::endl;
10300+
goto noChromosomesKey;
10301+
}
10302+
10303+
chomosomes_key_present = true;
1032110304

1032210305
for (std::size_t chromosomes_index = 0; chromosomes_index < Chromosomes().size(); ++chromosomes_index)
1032310306
{
@@ -10342,6 +10325,9 @@ void Species::ReadTreeSequenceMetadata(TreeSeqInfo &p_treeseq, slim_tick_t *p_ti
1034210325
if (one_chromosome_type != chromosome->TypeString())
1034310326
SLIM_ERRSTREAM << "#WARNING (Species::ReadTreeSequenceMetadata): the type for the entry at index " << chromosomes_index << " in the 'chromosomes' metadata key does not match the corresponding chromosome in the model." << std::endl;
1034410327
}
10328+
10329+
noChromosomesKey:
10330+
;
1034510331
}
1034610332

1034710333
// The new "traits" key is required, and we need to check its contents
@@ -10350,7 +10336,7 @@ void Species::ReadTreeSequenceMetadata(TreeSeqInfo &p_treeseq, slim_tick_t *p_ti
1035010336
if (!traits_metadata.is_array())
1035110337
SLIM_ERRSTREAM << "#WARNING (Species::ReadTreeSequenceMetadata): the 'traits' metadata key must be an array." << std::endl;
1035210338
if (traits_metadata.size() != Traits().size())
10353-
SLIM_ERRSTREAM << "#WARNING (Species::ReadTreeSequenceMetadata): the number of entries in the 'traits' metadata key does not match the number of traits in the model." << std::endl;
10339+
EIDOS_TERMINATION << "ERROR (Species::ReadTreeSequenceMetadata): the number of entries in the 'traits' metadata key (" << traits_metadata.size() << ") does not match the number of traits in the model (" << Traits().size() << ")." << EidosTerminate();
1035410340

1035510341
for (size_t traits_index = 0; traits_index < Traits().size(); ++traits_index)
1035610342
{
@@ -10523,6 +10509,31 @@ void Species::ReadTreeSequenceMetadata(TreeSeqInfo &p_treeseq, slim_tick_t *p_ti
1052310509
else
1052410510
EIDOS_TERMINATION << "ERROR (Species::ReadTreeSequenceMetadata): the chromosome index provided in the 'this_chromosome' key (" << this_chromosome_index << ") does not match the index (" << (unsigned int)(chromosome->Index()) << ") of the corresponding chromosome in the model." << EidosTerminate();
1052510511
}
10512+
10513+
// Now that we know we're in sync with the file contents (same number of traits, etc.), check the mutation metadata table
10514+
size_t trait_count = Traits().size();
10515+
size_t binary_row_length = sizeof(MutationTableMetadataRec) + sizeof(_MutationPerTraitMetadata) * (trait_count - 1);
10516+
10517+
// extract the encoded mutation metadata table row count
10518+
uint64_t mutation_table_row_count = *(uint64_t *)top_level_binary_buffer;
10519+
10520+
top_level_binary_buffer += sizeof(uint64_t);
10521+
top_level_binary_length -= sizeof(uint64_t);
10522+
10523+
if (top_level_binary_length % binary_row_length != 0)
10524+
EIDOS_TERMINATION << "ERROR (Species::ReadTreeSequenceMetadata): the top-level binary metadata does not comprise an integral number of rows (binary_row_length == " << binary_row_length << ", top_level_binary_length == " << top_level_binary_length << "); this file cannot be read. (Was all mutation metadata generated with the correct schema?)" << EidosTerminate();
10525+
10526+
// pass information on the binary mutation metadata table back to the caller
10527+
p_mut_metadata_table.table_buffer = (uint8_t *)top_level_binary_buffer;
10528+
p_mut_metadata_table.row_size = binary_row_length;
10529+
p_mut_metadata_table.row_count = top_level_binary_length / binary_row_length;
10530+
10531+
if (mutation_table_row_count != p_mut_metadata_table.row_count)
10532+
EIDOS_TERMINATION << "ERROR (Species::ReadTreeSequenceMetadata): the top-level binary metadata's specified row count ( " << mutation_table_row_count << ") does not match the number of rows of data present (" << p_mut_metadata_table.row_count << "); this file cannot be read. (Do you need to run pyslim.add_mutation_metadata()?)" << EidosTerminate();
10533+
10534+
#if DEBUG
10535+
//std::cout << "ReadTreeSequenceMetadata(): read binary mutation table with row size " << p_mut_metadata_table.row_size << " and " << p_mut_metadata_table.row_count << " rows." << std::endl;
10536+
#endif
1052610537
}
1052710538

1052810539
void Species::_CreateDirectoryForMultichromArchive(std::string resolved_user_path, bool p_overwrite_directory)

0 commit comments

Comments
 (0)