From 64d9a68789a2ee44d94b56f5b3b590e71e691bf4 Mon Sep 17 00:00:00 2001 From: "L.C. Karssen" Date: Fri, 13 Nov 2015 14:45:48 +0100 Subject: [PATCH 1/2] Some code layout changes Removed some tabs, added some spaces here and there. --- src/gendata.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/gendata.cpp b/src/gendata.cpp index ed8f639..f417d4d 100644 --- a/src/gendata.cpp +++ b/src/gendata.cpp @@ -251,13 +251,16 @@ void gendata::re_gendata(const char * fname, G.reinit(nids, (nsnps * ngpreds)); - std::ifstream file(fname, std::ios_base::in | std::ios_base::binary); - boost::iostreams::filtering_istream infile; - std::string filename=fname; - if (filename.compare(filename.length()-2,2,"gz")== 0){ - infile.push(boost::iostreams::gzip_decompressor()); - }else{ - std::cout << "no gziped:"<< ":\n"; + std::ifstream file(fname, std::ios_base::in | std::ios_base::binary); + boost::iostreams::filtering_istream infile; + std::string filename = fname; + if (filename.compare(filename.length() - 2, 2, "gz") == 0) + { + infile.push(boost::iostreams::gzip_decompressor()); + } + else + { + std::cout << "no gziped:" << ":\n"; } infile.push(file); From b5ff7cd0be424ba6301b43440e7582b1d88b0d48 Mon Sep 17 00:00:00 2001 From: "L.C. Karssen" Date: Fri, 13 Nov 2015 15:13:41 +0100 Subject: [PATCH 2/2] Proper Autotools use for Boost library for gzip input - Add a ./configure option --with-boost-iostreams (enabled by default) - Add a ./configure option --with-boost-include-path so the user can tell configure where to find the libraries if they are not in the default locations - Add an Automake option WITH_BOOST_IOSTREAMS that is true if the lib can be found (well, if the gzip.hpp file can be found) and use this to add the LDADD flags for the binaries that need it. This option is also used in the C++ code to switch between compiling with or without Boost::iostreams. - Print the version of the Boost library if Boost is used - In gendata.cpp keep the option open to read files when the library is not present. Unfortunately this does leave the code more ugly with a bunch of #ifdefs... --- configure.ac | 31 +++++++++++++++++++++++++++++++ src/Makefile.am | 15 +++++++++++++-- src/gendata.cpp | 26 +++++++++++++++++++++++++- src/usage.cpp | 8 ++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 8f86a61..c00dbed 100644 --- a/configure.ac +++ b/configure.ac @@ -88,6 +88,37 @@ if test x$ac_cv_header_Eigen_Dense = xno; then fi +# See if we can use the Boost IOstreams libraries for reading gzipped +# files +AC_ARG_WITH([boost-iostreams], + AS_HELP_STRING([--with-boost-iostreams], [Use the Boost Iostreams library to + allow reading from gzipped files. This is enabled by default] + ) +) + +if test "x$with_boost_iostreams" != "xno"; then + AC_MSG_NOTICE([building using the Boost IOstreams library enabled]) + + AC_ARG_WITH([boost-include-path], + [AS_HELP_STRING([--with-boost-include-path], + [location of the Boost headers, defaults to /usr/include/boost])], + [CPPFLAGS+=" -I${withval}"], + [CPPFLAGS+=' -I/usr/include/']) + + # Check for the Boost IOstreams header files + AC_CHECK_HEADER([boost/iostreams/filter/gzip.hpp], + [], + [AC_MSG_ERROR([Could not find the Boost IOstreams header files. Did \ +you specify --with-boost-include-path correctly? Or use --without-boost \ +to disable the reading of gzipped files.])] + ) +else + AC_MSG_NOTICE([not using the Boost IOstreams libraries, so input \ +from gzipped files is not possible]) +fi +AM_CONDITIONAL([WITH_BOOST_IOSTREAMS], test "x$with_boost_iostreams" != "xno") + + # Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL AC_C_INLINE diff --git a/src/Makefile.am b/src/Makefile.am index 33ffa60..62c67df 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -37,8 +37,6 @@ RHEADERS = include/R.h include/Rmath.h include/R_ext/Arith.h \ include/R_ext/Print.h include/R_ext/Random.h include/R_ext/Utils.h \ include/R_ext/RS.h -LDADD = -lboost_iostreams -lz - bin_PROGRAMS = if BUILD_palinear bin_PROGRAMS += palinear @@ -62,10 +60,12 @@ endif palinear_SOURCES = $(REGFILES) $(FVSRC) $(FVHEADERS) palinear_CPPFLAGS = -DLINEAR $(AM_CPPFLAGS) +palinear_LDADD = palinear_SOURCES += $(EIGENFILES) palogist_SOURCES = $(REGFILES) $(FVSRC) $(FVHEADERS) palogist_CPPFLAGS = -DLOGISTIC $(AM_CPPFLAGS) +palogist_LDADD = palogist_SOURCES += $(EIGENFILES) pacoxph_SOURCES = $(COXSRC) $(REGFILES) $(FVSRC) $(FVHEADERS) \ @@ -73,8 +73,19 @@ pacoxph_SOURCES = $(COXSRC) $(REGFILES) $(FVSRC) $(FVHEADERS) \ pacoxph_CXXFLAGS = -I $(top_srcdir)/src/include $(AM_CXXFLAGS) pacoxph_CPPFLAGS = -DCOXPH $(AM_CPPFLAGS) pacoxph_CFLAGS = -I $(top_srcdir)/src/include $(AM_CFLAGS) +pacoxph_LDADD = pacoxph_SOURCES += $(EIGENFILES) +if WITH_BOOST_IOSTREAMS +palinear_CPPFLAGS += -DWITH_BOOST_IOSTREAMS +palinear_LDADD += -lboost_iostreams -lz +palogist_CPPFLAGS += -DWITH_BOOST_IOSTREAMS +palogist_LDADD += -lboost_iostreams -lz +pacoxph_CPPFLAGS += -DWITH_BOOST_IOSTREAMS +pacoxph_LDADD += -lboost_iostreams -lz +endif + + extract_snp_SOURCES = extract-snp.cpp $(FVSRC) $(FVHEADERS) ## Install these scripts in the bin directory as well: diff --git a/src/gendata.cpp b/src/gendata.cpp index f417d4d..da4e2d1 100644 --- a/src/gendata.cpp +++ b/src/gendata.cpp @@ -36,9 +36,11 @@ #include "utilities.h" #include #include + +#if WITH_BOOST_IOSTREAMS #include #include - +#endif void gendata::mldose_line_to_matrix(const int k, const char *all_numbers, @@ -251,9 +253,15 @@ void gendata::re_gendata(const char * fname, G.reinit(nids, (nsnps * ngpreds)); +#if WITH_BOOST_IOSTREAMS std::ifstream file(fname, std::ios_base::in | std::ios_base::binary); boost::iostreams::filtering_istream infile; std::string filename = fname; + // Note: a better check would be to read the first two bytes of + // the file and check for the gzip signature: 0x1f8b + // W.r.t. endianness and bytt width: compare each byte separately, + // see the comment to this SE answer: + // http://stackoverflow.com/a/6059342/881084 if (filename.compare(filename.length() - 2, 2, "gz") == 0) { infile.push(boost::iostreams::gzip_decompressor()); @@ -263,8 +271,16 @@ void gendata::re_gendata(const char * fname, std::cout << "no gziped:" << ":\n"; } infile.push(file); +#else + std::ifstream infile; + infile.open(fname); +#endif +#if WITH_BOOST_IOSTREAMS if (!file) +#else + if (!infile) +#endif { std::cerr << "gendata: cannot open file " << fname << endl; exit(1); @@ -297,7 +313,11 @@ void gendata::re_gendata(const char * fname, cerr << "phenotype file and dose or probability file " << "did not match at line " << i + 2 << " (" << tmpid << " != " << idnames[k] << ")" << endl; +#if WITH_BOOST_IOSTREAMS file.close(); +#else + infile.close(); +#endif exit(1); } } @@ -327,7 +347,11 @@ void gendata::re_gendata(const char * fname, } } +#if WITH_BOOST_IOSTREAMS file.close(); +#else + infile.close(); +#endif } // HERE NEED A NEW CONSTRUCTOR BASED ON DATABELBASECPP OBJECT diff --git a/src/usage.cpp b/src/usage.cpp index 77387cb..53440ef 100644 --- a/src/usage.cpp +++ b/src/usage.cpp @@ -31,6 +31,10 @@ #include "config.h" #endif +#if WITH_BOOST_IOSTREAMS +#include +#endif + #include "eigen_mematrix.h" @@ -103,6 +107,10 @@ void print_version(void) { cout << "Using EIGEN version " << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION << " for matrix operations\n"; +#if WITH_BOOST_IOSTREAMS + cout << "Using Boost libraries version " << BOOST_LIB_VERSION << endl; +#endif + }