Presentation
This page presents some code snippets related to the use of persistency API.
Some of the snippets presented below can be used online.
Additional snippets are available in directory: gatb-core/gatb-core/examples/storage.
Create and save a collection with a Storage object
This snippet shows how to use a Storage object for creating a collection of integers. We use the HDF5 format, so we can control the result of our snippet with HDF5 tools.
Code is from example storage1.cpp:
int main (int argc, char* argv[])
{
Storage* storage = StorageFactory(STORAGE_HDF5).create ("foo", true, false);
Group& root = storage->root();
Collection<NativeInt64>& myIntegers = root.getCollection<NativeInt64> ("myIntegers");
myIntegers.insert (1);
myIntegers.insert (2);
myIntegers.insert (3);
myIntegers.insert (5);
myIntegers.insert (8);
myIntegers.flush();
}
[go back to top]
Create and save two collections with a Storage object
This snippet shows how to use a Storage object for creating collections of integers. We use the HDF5 format, so we can control the result of our snippet with HDF5 tools.
Code is from example storage2.cpp:
int main (int argc, char* argv[])
{
Storage* storage = StorageFactory(STORAGE_HDF5).create ("foo", true, false);
Group& root = storage->root();
Group& group1 = root.getGroup("group1");
Group& group2 = root.getGroup("group2");
Collection<NativeInt64>& integers1 = group1.getCollection<NativeInt64> ("integers");
Collection<NativeInt64>& integers2 = group2.getCollection<NativeInt64> ("integers");
integers1.insert (1);
integers1.insert (2);
integers1.insert (3);
integers2.insert (5);
integers2.insert (8);
integers1.flush();
integers2.flush();
}
[go back to top]
Load a collection from a Storage object
This snippet shows how to load a Storage object and get a saved collection from it.
Code is from example storage3.cpp:
int main (int argc, char* argv[])
{
Storage* storage = StorageFactory(STORAGE_HDF5).load ("foo");
Group& root = storage->root();
Collection<NativeInt64>& myIntegers = root.getCollection<NativeInt64> ("myIntegers");
Iterator<NativeInt64>* iter = myIntegers.iterator();
for (iter->first(); !iter->isDone(); iter->next()) { cout << iter->item() << endl; }
}
[go back to top]
Load collections from a Storage object
This snippet shows how to load a Storage object and get saved collections from it. Note that we use lambda expressions in this example.
Code is from example storage4.cpp:
int main (int argc, char* argv[])
{
Storage* storage = StorageFactory(STORAGE_HDF5).load ("foo");
Group& root = storage->root();
Group& group1 = root.getGroup("group1");
Group& group2 = root.getGroup("group2");
group1.getCollection<NativeInt64> ("integers").iterate ([] (const NativeInt64& n) { cout << n << endl; });
cout << endl;
group2.getCollection<NativeInt64> ("integers").iterate ([] (const NativeInt64& n) { cout << n << endl; });
}
[go back to top]
Iterate solid kmers from a HDF5 file
This snippet shows how to use a HDF5 Storage object holding solid kmers and iterate the kmers.
It also uses a Model instance in order to convert the solid kmers values into the corresponding nucleotides sequence.
The input file is likely to have been generated by dbgh5 for instance, or by dsk.
If you want to know the structure of the HDF5 file, you can use the h5dump utility, for instance: h5dump -H file.h5
Code is from example storage6.cpp:
#include <iostream>
#include <memory>
int main (int argc, char* argv[])
{
OptionsParser parser ("StorageSnippet");
parser.push_back (new OptionOneParam (STR_URI_GRAPH, "graph input", true));
parser.push_back (new OptionOneParam (STR_VERBOSE, "verbosity (0:no display, 1: display kmers, 2: display distrib", false, "0"));
try
{
IProperties* options = parser.parse (argc, argv);
int display = options->getInt (STR_VERBOSE);
auto_ptr<Storage> storage (StorageFactory(STORAGE_HDF5).load (options->getStr(STR_URI_GRAPH)));
Group& dskGroup = storage->getGroup("dsk");
Partition<Kmer<>::Count>& solidKmers = dskGroup.getPartition<Kmer<>::Count> ("solid");
cout << dskGroup.getProperty("xml") << endl;
Properties props;
props.readXML (dskGroup.getProperty("xml"));
Properties configProps;
configProps.readXML (storage->getGroup("configuration").getProperty("xml"));
cout << "kmer size: " << configProps.getInt ("kmer_size") << endl;
cout << "nb solid kmers: " << props.getInt ("kmers_nb_solid") << endl;
Kmer<>::ModelCanonical model (configProps.getInt ("kmer_size"));
size_t nbKmers = 0;
ProgressIterator<Kmer<>::Count> iter (solidKmers);
Kmer<>::Type checksum;
map<u_int64_t,u_int64_t> distrib;
for (iter.first(); !iter.isDone(); iter.next())
{
Kmer<>::Count& count = iter.item();
checksum += count.value;
distrib [count.abundance] ++;
if (display==1)
{
cout << "[" << ++nbKmers << "] " << model.toString(count.value) << " " << count.value << " " << count.abundance << endl;
}
}
cout << "kmer checksum: " << checksum << endl;
if (display==2)
{
for (map<u_int64_t,u_int64_t>::iterator it = distrib.begin(); it != distrib.end(); ++it)
{
cout << it->first << " " << it->second << endl;
}
}
}
catch (OptionFailure& e)
{
return e.displayErrors (std::cout);
}
catch (Exception& e)
{
std::cerr << "EXCEPTION: " << e.getMessage() << std::endl;
}
}
[go back to top]
Associate metadata to HDF5 collections
This snippet shows how to associate metadata to HDF5 collections.
You can dump such values with h5dump: h5dump -a myIntegers/myData foo.h5
Code is from example storage7.cpp:
#include <iostream>
int main (int argc, char* argv[])
{
Storage* storage = StorageFactory(STORAGE_HDF5).create ("foo", true, false);
Group& root = storage->root();
Collection<NativeInt64>& myIntegers = root.getCollection<NativeInt64> ("myIntegers");
myIntegers.addProperty ("myData", "test_%d", 147);
cout << "metadata is " << myIntegers.getProperty("myData") << endl;
}
[go back to top]
Using C++ like streams with HDF5
This snippet shows how to use binary input/output streams with HDF5. There are two types:
- Storage::ostream : used for saving binary data into a HDF5 collection
- Storage::istream : used for retrieving binary data from a HDF5 collection
Code is from example storage8.cpp:
#include <iostream>
int main (int argc, char* argv[])
{
float table[] = { 0.577, 3.1415, 2.71 };
Storage* storage = StorageFactory(STORAGE_HDF5).create ("foo", true, false);
Group& root = storage->root();
Storage::ostream os (root, "data");
os.write (reinterpret_cast<char const*>(table), sizeof(table));
os.flush();
Collection<NativeInt8>& dataCollection = root.getCollection<NativeInt8> ("data");
size_t nbItems = dataCollection.getNbItems() / sizeof(table[0]);
cout << "nb items : " << nbItems << endl;
Storage::istream is (root, "data");
float* buffer = new float [nbItems];
is.read (reinterpret_cast<char*>(buffer), nbItems*sizeof(float));
cout << "check : " << (memcmp(buffer, table, nbItems*sizeof(float)) == 0) << endl;
delete[] buffer;
}
[go back to top]