Skip to content

Saving Loading partitions

ddarriba edited this page Oct 7, 2016 · 3 revisions

In the following example, we will save the current partition and tree to a sequential binary file. We define the following variables:

FILE * bin_file;
pll_binary_header_t bin_header;
const char * bin_fname = "test.bin";

First of all, we will create the file using the flag PLLMOD_BIN_ACCESS_SEQUENTIAL. bin_header is an output variable, so we do not need to initialize any value. It returns the file pointer.

bin_file = pllmod_binary_create(bin_fname,
                                &bin_header,
                                PLLMOD_BIN_ACCESS_SEQUENTIAL,
                                0);

For writing the data structures to the binary file, we should use pllmod_binary_%_dump functions. If there are available attributes for the data structure, decide whether you want to use them or not. For example, we use PLLMOD_BIN_ATTRIB_PARTITION_DUMP_CLV and PLLMOD_BIN_ATTRIB_PARTITION_DUMP_WGT together such that the CLVs and pattern weights (WGT) are stored together with the partition.

pllmod_binary_partition_dump(bin_file,
                             BLOCK_ID_PARTITION,
                             partition,
                             PLLMOD_BIN_ATTRIB_PARTITION_DUMP_CLV |
                              PLLMOD_BIN_ATTRIB_PARTITION_DUMP_WGT);

pllmod_binary_utree_dump(bin_file,
                         BLOCK_ID_TREE,
                         tree,
                         tip_nodes_count,
                         0); /* attributes */

Finally, we close the file

pllmod_binary_close(bin_file);

For restoring the values, we need to open again the file, but this time for reading only:

pll_binary_header_t input_header;

bin_file = pllmod_binary_open(bin_fname, &input_header);

attributes is now an output argument such that we can now.

unsigned int bin_attributes;

partition = pllmod_binary_partition_load(bin_file,
                                         BLOCK_ID_PARTITION,
                                         NULL, /* create a new partition */
                                         &bin_attributes,
                                         0);

tree = pllmod_binary_utree_load(bin_file,
                                BLOCK_ID_TREE,
                                &bin_attributes,
                                0);