Skip to content

Commit

Permalink
reader should have no knowledge of writer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus D. R. Klarqvist committed Aug 11, 2017
1 parent 3da2faf commit 1f6e798
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
31 changes: 6 additions & 25 deletions io/TomahawkOutput/TomahawkOutputReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ bool TomahawkOutputReader::view(const std::string& input){
}

bool TomahawkOutputReader::__viewRegion(void){
if(this->writer_output_type == WRITER_TYPE::natural)
if(this->writer_output_type == WRITER_TYPE::natural){
this->writer = new TomahawkOutputWriterNatural();
TomahawkOutputWriterNatural* temp = reinterpret_cast<TomahawkOutputWriterNatural*>(this->writer);
temp->setContigs(this->contigs);
}
else this->writer = new TomahawkOutputWriter();

this->writer->open();
this->__writeOutputHeaders();
this->writer->writeHeader();

if(this->interval_tree != nullptr){
const entry_type* entry;
Expand All @@ -60,31 +64,8 @@ bool TomahawkOutputReader::__viewRegion(void){
} // end while next variant
}

this->writer->flush();
this->__writeOutputEOF();
this->writer->close();

return true;
}

bool TomahawkOutputReader::__writeOutputHeaders(void){
if(this->writer_output_type == WRITER_TYPE::binary){
return true;
} else {
const std::string header = "FLAG\tAcontigID\tAposition\tBcontigID\tBpositionID\tp1\tp2\tq1\tq2\tD\tDprime\tR2\tP\tchiSqFisher\tchiSqModel\n";
this->writer->write(&header[0], header.length());
return true;
}
}

bool TomahawkOutputReader::__writeOutputEOF(void){
if(this->writer_output_type == WRITER_TYPE::binary){
return true;
} else {
return true;
}
}

bool TomahawkOutputReader::__checkRegion(const entry_type* const entry){
// If iTree for contigA exists
if(this->interval_tree[entry->AcontigID] != nullptr){
Expand Down
3 changes: 0 additions & 3 deletions io/TomahawkOutput/TomahawkOutputReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ class TomahawkOutputReader {
bool __viewRegion(void);
bool __checkRegion(const entry_type* const entry);

bool __writeOutputHeaders(void);
bool __writeOutputEOF(void);

public:
U64 filesize; // input file size
U32 position;
Expand Down
34 changes: 23 additions & 11 deletions io/TomahawkOutput/TomahawkOutputWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TomahawkOutputWriterInterface {
if(this->stream->open()){
return false;
}

return true;
}

Expand All @@ -38,19 +39,22 @@ class TomahawkOutputWriterInterface {
if(this->stream->open(output)){
return false;
}

return true;
}

virtual inline void flush(void){ this->stream->flush(); }
virtual inline bool close(void){ this->stream->close(); return true; }
virtual void operator<<(const entry_type* const entryentry) =0;
virtual void write(const entry_type* const entry, const void* support) =0;
inline void write(const char* data, const U32 length){ this->stream->write(&data[0], length); }

template<class T> void write(const T& data){
*reinterpret_cast<std::ofstream*>(&this->stream->getStream()) << data;
}

virtual void writeHeader(void) =0;
virtual void writeEOF(void) =0;

protected:
std::string outFile;
stream_type* stream;
Expand All @@ -74,15 +78,14 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface {
~TomahawkOutputWriter(){
// Flush upon termination
this->flush();
this->writeEOF();
this->close();
this->buffer.deleteAll();
}

inline void flush(void){ this->stream->flush(); }
inline bool close(void){ this->stream->close(); return true; }

void write(const entry_type* const entry, const void* support){}

void operator<<(const entry_type* const entry){
this->buffer.Add(reinterpret_cast<const char*>(entry), sizeof(entry_type));
if(this->buffer.size() > this->flush_limit){
Expand All @@ -93,6 +96,9 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface {
}
}

void writeHeader(void){ };
void writeEOF(void){};

private:
U32 flush_limit;
buffer_type buffer;
Expand All @@ -102,24 +108,30 @@ class TomahawkOutputWriter : public TomahawkOutputWriterInterface {
// case binary
class TomahawkOutputWriterNatural : public TomahawkOutputWriterInterface {
typedef TomahawkOutputWriterNatural self_type;
typedef Totempole::TotempoleContigBase contig_type;

public:
TomahawkOutputWriterNatural(){}
TomahawkOutputWriterNatural() : contigs(nullptr){}
~TomahawkOutputWriterNatural(){
// Flush upon termination
this->flush();
this->writeEOF();
this->close();
}

void write(const entry_type* const entry, const void* support){
*reinterpret_cast<std::ofstream*>(&this->stream->getStream()) << *entry;
this->stream->getStream() << '\n';
}

void operator<<(const entry_type* const entry){
*reinterpret_cast<std::ofstream*>(&this->stream->getStream()) << *entry;
this->stream->getStream() << '\n';
entry->write(*reinterpret_cast<std::ofstream*>(&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;
};

}
Expand Down

0 comments on commit 1f6e798

Please sign in to comment.