diff --git a/io/TomahawkOutput/TomahawkOutputReader.cpp b/io/TomahawkOutput/TomahawkOutputReader.cpp index 1737e35..2f77634 100644 --- a/io/TomahawkOutput/TomahawkOutputReader.cpp +++ b/io/TomahawkOutput/TomahawkOutputReader.cpp @@ -15,7 +15,7 @@ TomahawkOutputReader::TomahawkOutputReader() : filesize(0), position(0), size(0), - writer_output_type(WRITER_TYPE::natural), + writer_output_type(WRITER_TYPE::binary), writer(nullptr), contigs(nullptr), contig_htable(nullptr), @@ -47,11 +47,9 @@ bool TomahawkOutputReader::view(const std::string& input){ bool TomahawkOutputReader::__viewRegion(void){ if(this->writer_output_type == WRITER_TYPE::natural){ - this->writer = new TomahawkOutputWriterNatural(); - TomahawkOutputWriterNatural* temp = reinterpret_cast(this->writer); - temp->setContigs(this->contigs); + this->writer = new TomahawkOutputWriterNatural(this->contigs, &this->header); } - else this->writer = new TomahawkOutputWriter(); + else this->writer = new TomahawkOutputWriter(this->contigs, &this->header); this->writer->open(); this->writer->writeHeader(); diff --git a/io/TomahawkOutput/TomahawkOutputWriter.h b/io/TomahawkOutput/TomahawkOutputWriter.h index fe291bb..d226761 100644 --- a/io/TomahawkOutput/TomahawkOutputWriter.h +++ b/io/TomahawkOutput/TomahawkOutputWriter.h @@ -16,9 +16,14 @@ class TomahawkOutputWriterInterface { protected: typedef TomahawkOutputEntry entry_type; typedef TomahawkOutputHeader header_type; + typedef Totempole::TotempoleContigBase contig_type; public: - TomahawkOutputWriterInterface() : stream(nullptr){} + TomahawkOutputWriterInterface(const contig_type* contigs, const header_type* header) : + stream(nullptr), + header(header), + contigs(contigs) + {} virtual ~TomahawkOutputWriterInterface(){ delete this->stream; } bool open(void){ @@ -58,6 +63,8 @@ class TomahawkOutputWriterInterface { protected: std::string outFile; stream_type* stream; + const header_type* header; + const contig_type* contigs; }; // case binary @@ -67,8 +74,9 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface { typedef IO::GZController tgzf_controller_type; public: - TomahawkOutputWriter() : flush_limit(524288){} - TomahawkOutputWriter(const U32 flush_limit) : + TomahawkOutputWriter(const contig_type* contigs, const header_type* header) : TomahawkOutputWriterInterface(contigs, header), flush_limit(524288){} + TomahawkOutputWriter(const contig_type* contigs, const header_type* header, const U32 flush_limit) : + TomahawkOutputWriterInterface(contigs, header), flush_limit(flush_limit), buffer(flush_limit + 524288) { @@ -83,7 +91,13 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface { this->buffer.deleteAll(); } - inline void flush(void){ this->stream->flush(); } + inline void flush(void){ + this->controller.Deflate(this->buffer); + this->stream->write(&this->controller.buffer_[0], this->controller.buffer_.size()); + this->controller.Clear(); + this->buffer.reset(); + this->stream->flush(); + } inline bool close(void){ this->stream->close(); return true; } void operator<<(const entry_type* const entry){ @@ -96,8 +110,15 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface { } } - void writeHeader(void){ }; - void writeEOF(void){}; + void writeHeader(void){ + *reinterpret_cast(&this->stream->getStream()) << *this->header; + for(U32 i = 0; i < this->header->n_contig; ++i) + *reinterpret_cast(&this->stream->getStream()) << this->contigs[i]; + + }; + void writeEOF(void){ + //this->stream->write(reinterpret_cast(&Tomahawk::Constants::eof[0]), Tomahawk::Constants::eof_length*sizeof(U64)); + }; private: U32 flush_limit; @@ -108,10 +129,9 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface { // case binary class TomahawkOutputWriterNatural : public TomahawkOutputWriterInterface { typedef TomahawkOutputWriterNatural self_type; - typedef Totempole::TotempoleContigBase contig_type; public: - TomahawkOutputWriterNatural() : contigs(nullptr){} + TomahawkOutputWriterNatural(const contig_type* contigs, const header_type* header) : TomahawkOutputWriterInterface(contigs, header){} ~TomahawkOutputWriterNatural(){ // Flush upon termination this->flush(); @@ -123,15 +143,12 @@ class TomahawkOutputWriterNatural : public TomahawkOutputWriterInterface { entry->write(*reinterpret_cast(&this->stream->getStream()), this->contigs); } - void setContigs(const contig_type* const contigs){ this->contigs = contigs; } void writeHeader(void){ const std::string header = "FLAG\tAcontigID\tAposition\tBcontigID\tBpositionID\tp1\tp2\tq1\tq2\tD\tDprime\tR2\tP\tchiSqFisher\tchiSqModel\n"; this->stream->getStream() << header; }; void writeEOF(void){}; -private: - const contig_type* contigs; }; }