Skip to content

Instantly share code, notes, and snippets.

@gatoravi
Last active November 30, 2015 05:46
Show Gist options
  • Save gatoravi/c47d82a6267e1c8ae2e6 to your computer and use it in GitHub Desktop.
Save gatoravi/c47d82a6267e1c8ae2e6 to your computer and use it in GitHub Desktop.
capnp example using cmake

A simple example for using capnmp. I used HomeBrew to install capnp and dependencies on OSX. Compilation is done using CMake. See CMakeLists.txt for details. The code to read the capnp file is based on https://github.com/marbl/Mash/.

include_directories(../src/mash/capnp/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_MODULE_PATH
".;${CMAKE_MODULE_PATH}")
find_package(CapnProto REQUIRED)
include_directories(${CAPNP_INCLUDE_DIRS})
add_definitions(${CAPNP_DEFINITIONS})
capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS MinHash.capnp)
add_executable(test_capnp test_capnp.cpp)
target_link_libraries(test_capnp ${CAPNP_LIBRARIES})
/*
Copyright (c) <2015> <Avi Ramu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <iostream>
#include <capnp/message.h>
#include <capnp/serialize.h>
#include <fcntl.h>
#include "MinHash.capnp.h"
using namespace std;
int read_capnp(const char * file)
{
/*
* This function was derived from code copyrighted as follows:
* COPYRIGHT LICENSE
Copyright © 2015, Battelle National Biodefense Institute (BNBI);
all rights reserved. Authored by: Brian Ondov, Todd Treangen,
Sergey Koren, and Adam Phillippy
*/
int fd = open(file, O_RDONLY);
if (fd < 0) {
cerr << "ERROR: could not open \"" << file << "\" for reading." << endl;
exit(1);
}
capnp::ReaderOptions readerOptions;
capnp::StreamFdMessageReader * message = new capnp::StreamFdMessageReader(fd, readerOptions);
capnp::MinHash::Reader reader = message->getRoot<capnp::MinHash>();
cout << " kmerSize =" << reader.getKmerSize();
cout << " parameters.error = " << reader.getError();
cout << " parameters.minHashesPerWindow = " << reader.getMinHashesPerWindow();
cout << " parameters.windowSize = " << reader.getWindowSize();
cout << " parameters.concatenated = " << reader.getConcatenated();
cout << " parameters.noncanonical = " << reader.getNoncanonical();
cout << endl;
close(fd);
delete message;
return 0;
}
int main(int argc, char* argv[]) {
if (argc > 1) {
cerr << "Name of file is " << argv[1];
return read_capnp(argv[1]);
}
cerr << "Usage:test_capnp capnp_input";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment