Skip to content

Commit

Permalink
Merge pull request #1 from lckarssen/master
Browse files Browse the repository at this point in the history
A cleaner autotools implementation of the gzip reading code
  • Loading branch information
maarten-k committed Nov 15, 2015
2 parents 486ccda + b5ff7cd commit 01146ad
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
31 changes: 31 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,19 +60,32 @@ 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) \
$(RHEADERS) survS.h survproto.h coxph_data.h coxph_data.cpp
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:
Expand Down
43 changes: 35 additions & 8 deletions src/gendata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
#include "utilities.h"
#include <iostream>
#include <fstream>

#if WITH_BOOST_IOSTREAMS
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>

#endif

void gendata::mldose_line_to_matrix(const int k,
const char *all_numbers,
Expand Down Expand Up @@ -251,17 +253,34 @@ 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";
#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());
}
else
{
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);
Expand Down Expand Up @@ -294,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);
}
}
Expand Down Expand Up @@ -324,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
Expand Down
8 changes: 8 additions & 0 deletions src/usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "config.h"
#endif

#if WITH_BOOST_IOSTREAMS
#include <boost/version.hpp>
#endif

#include "eigen_mematrix.h"


Expand Down Expand Up @@ -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

}


Expand Down

0 comments on commit 01146ad

Please sign in to comment.