Skip to content

Commit

Permalink
Allow specification of output file names
Browse files Browse the repository at this point in the history
  • Loading branch information
John Hensley committed Sep 18, 2017
1 parent 7366ba9 commit 391fdef
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
cta
src/Version.hpp
38 changes: 24 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PROGRAM_NAME = cta
VERSION_MAJOR = 0
VERSION_MINOR = 1
VERSION_PATCH = 1
VERSION_PATCH = 2

VERSION = $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)

Expand All @@ -19,21 +19,9 @@ OBJECTS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRC_CPP))
CPPFLAGS = -pedantic -Wall -Wextra -Wwrite-strings -Wstrict-overflow -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong -fno-strict-aliasing -fPIC $(INCLUDES)
CXXFLAGS = -std=c++11 -O3 -g $(CPPFLAGS)

LDLIBS = -lboost_iostreams -lz
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
CXXFLAGS += -D LINUX $(INCLUDES) -O3 -g
ifdef BOOST_MODULE
INCLUDES += -I $(BOOST_MODULE)/include
LDFLAGS += -L $(BOOST_MODULE)/lib
else
ifdef BOOST_INCLUDE
INCLUDES += -I$(BOOST_INCLUDE)
endif
ifdef BOOST_LIB
LDFLAGS += -L$(BOOST_LIB)
endif
endif
endif
ifeq ($(UNAME_S),Darwin) # Mac with Homebrew
CXXFLAGS += -D OSX -O3
Expand All @@ -50,6 +38,27 @@ ifneq ($(filter arm%,$(UNAME_P)),)
CXXFLAGS += -D ARM
endif

ifdef BOOST_TAGGED
BOOST_LIBS = -lboost_filesystem-mt -lboost_iostreams-mt -lboost_system-mt -lboost_chrono-mt -lz
else
BOOST_LIBS = -lboost_filesystem -lboost_iostreams -lboost_system -lboost_chrono -lz
endif

ifdef BOOST_ROOT
CPPFLAGS += -I$(BOOST_ROOT)/include
LDFLAGS += -L$(BOOST_ROOT)/lib
else
ifdef BOOST_INCLUDE
CPPFLAGS += -I$(BOOST_INCLUDE
endif

ifdef BOOST_LIB
LDFLAGS += -L$(BOOST_LIB)
endif
endif

LDLIBS = $(BOOST_LIBS)

PREFIX = /usr/local

#
Expand All @@ -66,7 +75,8 @@ clean:
rm -rf $(BUILD_DIR)

install: $(BUILD_DIR)/cta
install -m 0755 $(BUILD_DIR)/cta $(PREFIX)/bin
install -d -m 0755 $(PREFIX)/bin
install -m 0755 $(BUILD_DIR)/cta $(PREFIX)/bin/cta

$(BUILD_DIR):
@mkdir -p $@
Expand Down
55 changes: 43 additions & 12 deletions src/cta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "Version.hpp"

namespace bio = boost::iostreams;


class FileException: public std::runtime_error {
public:
Expand Down Expand Up @@ -52,7 +54,7 @@ std::istream& operator>>(std::istream& is, FASTQRecord& record) {
///
/// Check for the GZIP header in a file
///
bool is_gzipped_file(std::string filename) {
bool is_gzipped_file(const std::string& filename) {
bool gzipped = false;
FILE* f = fopen(filename.c_str(), "rb");

Expand All @@ -67,8 +69,13 @@ bool is_gzipped_file(std::string filename) {
return gzipped;
}

bool is_gzipped_filename(const std::string& filename) {
std::string ext = ".gz";
return std::equal(ext.rbegin(), ext.rend(), filename.rbegin());
}

std::regex fastq_filename_re("\\.(fq|fastq)?(\\.gz)?$");
std::string make_trimmed_filename(std::string filename) {
std::string make_trimmed_filename(const std::string& filename) {
return std::regex_replace(filename, fastq_filename_re, ".trimmed.fq.gz");
}

Expand Down Expand Up @@ -241,10 +248,15 @@ std::string version_string() {
void print_usage() {
std::cout << version_string() << ": Trim adapters from paired-end HTS reads.\n\n"

<< "Usage:\n\n" << PROGRAM_NAME << " [options] file1 file2\n\n"
<< "Usage:\n\n" << PROGRAM_NAME << " [options] input_file1 input_file2 [output_file1 output_file2]\n\n"
<< "where:\n"
<< " file1 contains the first reads of all the pairs\n"
<< " file2 contains the second reads of all the pairs\n\n"
<< " input_file1 contains the first reads of all pairs\n"
<< " input_file2 contains the second reads of all pairs\n"
<< " output_file1 will contain the trimmed first reads of all pairs\n"
<< " output_file2 will contain the trimmed second reads of all pairs\n\n"

<< "You must supply both of the output file names, or neither. If not\n"
<< "specified, they will be inferred from the inputs.\n\n"

<< "Options may include:\n\n"

Expand Down Expand Up @@ -272,8 +284,6 @@ void print_usage() {

int main(int argc, char **argv)
{
namespace bio = boost::iostreams;

int c, option_index = 0;
bool verbose = 0;

Expand All @@ -283,6 +293,8 @@ int main(int argc, char **argv)
long long int rc_length = 20;
std::string input_filename1;
std::string input_filename2;
std::string output_filename1;
std::string output_filename2;

static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
Expand Down Expand Up @@ -333,14 +345,24 @@ int main(int argc, char **argv)
}
}

if ((optind + 2) > argc) {
if (argc < (optind + 2)) {
print_usage();
exit(1);
}

input_filename1 = argv[optind];
input_filename2 = argv[optind + 1];

if (argc > (optind + 2)) {
if (argc == optind + 4) {
output_filename1 = argv[optind + 2];
output_filename2 = argv[optind + 3];
} else {
print_usage();
exit(1);
}
}

bio::file_source input_file1(input_filename1);
bio::stream<bio::file_source> input_stream1(input_file1);

Expand All @@ -359,8 +381,13 @@ int main(int argc, char **argv)
}
in2.push(input_stream2);

std::string output_filename1 = make_trimmed_filename(input_filename1);
std::string output_filename2 = make_trimmed_filename(input_filename2);
if (output_filename1.empty()) {
output_filename1 = make_trimmed_filename(input_filename1);
}

if (output_filename2.empty()) {
output_filename2 = make_trimmed_filename(input_filename2);
}

if (verbose) {
std::cout << "Writing to " << output_filename1 << " and " << output_filename2 << "...\n";
Expand All @@ -370,11 +397,15 @@ int main(int argc, char **argv)
std::ofstream output_file2(output_filename2, std::ios_base::out | std::ios_base::binary);

bio::filtering_stream<bio::output> out1;
out1.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_speed)));
if (is_gzipped_filename(output_filename1)) {
out1.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_speed)));
}
out1.push(output_file1);

bio::filtering_stream<bio::output> out2;
out2.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_speed)));
if (is_gzipped_filename(output_filename2)) {
out2.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_speed)));
}
out2.push(output_file2);

FASTQRecord record1;
Expand Down

0 comments on commit 391fdef

Please sign in to comment.