10 #include "stream_tools.h"
12 #include "stream_tools.h"
13 #include "file_index.h"
14 #include "error_codes.h"
20 #include "linkage_data.h"
23 #include "sample_gof.h"
24 #include "relatedness_data.h"
25 #include "population.h"
26 #include "pooled_data.h"
27 #include "sample_name.h"
30 #include "tmp_buffer.h"
32 #define READ std::ios::in
33 #define WRITE std::ios::out
34 #define BINARY std::ios::binary
54 void write_text(
const Data *);
100 const std::fstream::openmode&
openmode();
110 void open(
const std::ios::openmode&);
112 void open(std::iostream*,
const std::ios_base::openmode &);
114 void open(std::istream*,
const std::ios_base::openmode &);
116 void open(std::ostream*,
const std::ios_base::openmode &);
119 std::istream*
get_in(
void);
140 void write_header(
const Data *);
155 void seek(
const std::streampos &pos);
157 void seekg(
const std::streampos &pos);
159 void seekp(
const std::streampos &pos);
161 void seek(std::streampos pos, std::ios_base::seekdir way);
162 void seekg(std::streampos off, std::ios_base::seekdir way);
163 void seekp(std::streampos off, std::ios_base::seekdir way);
165 std::streampos
tellp(
void);
166 std::streampos
tellg(
void);
186 size_t size(
void)
const;
188 bool indexed(
void)
const;
189 bool concatenated(
void);
190 bool check_concatenated(
void);
191 bool check_compressed(
void);
195 template <
class Data>
202 virtual void read_text(
Data &){};
203 virtual void write_text(
const Data&){};
221 void open(
const char *,
const std::ios_base::openmode &);
224 void open_extention(
const char *,
const std::ios_base::openmode &);
260 using Base_file::concatenated;
280 using Base_file::concatenated;
289 id1_t reference_size_;
296 void read_binary(T &);
297 void write_binary(
const T &);
311 using Base_file::concatenated;
325 id1_t get_pos(
const T &)
const;
335 void read_binary(T &);
336 void write_binary(
const T &);
351 using Base_file::concatenated;
380 std::vector<std::string> line;
383 std::cerr << __FILE__ <<
":" << __LINE__ <<
": " <<
typeid(
this).name() <<
" is already open." << std::endl;
386 if ( mode & std::ios::in ){
387 line=split_last(filename,
'.');
389 if (line.back()!=T::file_name) {
391 std::cerr << line.back() <<
"!=" << T::file_name;
392 std::cerr <<
"opening " <<
filename_ <<
" in concatenated mode\n";
405 std::cerr <<
"opening "<<
filename_ <<
" in split mode\n";
414 std::string temp_filename=std::string(filename)+T::file_name;
416 std::cerr << __LINE__ <<
"opening w extension "<< temp_filename <<
"\n";
425 if (file.
openmode() & std::ios::in){
427 std::cerr <<
"opening from...\n";
431 }
else if (file.
openmode() & std::ios::out) {
442 return data.get_abs_pos();
453 else read_text(data);
455 std::cerr << __FILE__<<
":" <<__LINE__ <<
": file not open for reading. The methods Flat_file<type>::open() and Flat_file<type>::read_header(<type>) should be called.";
467 if (binary_) read_binary(data);
468 else read_text(data);
470 std::cerr << __FILE__<<
":" <<__LINE__ <<
": file not open for reading. The methods Flat_file<type>::open() and Flat_file<type>::read_header(<type>) should be called.";
482 if (binary_) read_binary(data);
483 else read_text(data);
485 std::cerr << __FILE__<<
":" <<__LINE__ <<
": file not open for reading. The methods Flat_file<type>::open() and Flat_file<type>::read_header(<type>) should be called.";
494 std::cerr <<
"an error has occurred during reading.\n";
497 if (in_->peek()==
'@') {
499 std::getline(*in_, line);
500 if (line!=
"@END_TABLE") {
501 std::cerr << __FILE__ <<
":" << __LINE__ <<
": file not closed correctly, exiting.\n";
502 std::cerr << line << std::endl;
505 if (!concatenated_) {
519 std::string scaffold;
521 std::cerr <<
"an error has occurred during reading.\n";
524 if (in_->peek()==
'@') {
527 if (line!=
"@END_TABLE") {
528 std::cerr << line << std::endl;
529 std::cerr << __FILE__ <<
":" << __LINE__ <<
": file not closed correctly, exiting.\n";
532 if (!concatenated_) {
539 std::cerr << (char)(in_->peek()) << std::endl;
544 data.set_abs_pos(file_index_.get_abs_pos(scaffold, pos) );
553 std::string scaffold1, scaffold2;
555 std::cerr <<
"an error has occurred during reading.\n";
558 if (in_->peek()==
'@') {
561 if (line!=
"@END_TABLE") {
562 std::cerr << line << std::endl;
563 std::cerr << __FILE__ <<
":" << __LINE__ <<
": file not closed correctly, exiting.\n";
566 if (!concatenated_) {
573 std::cerr << (char)(in_->peek()) << std::endl;
580 data.set_abs_pos1(file_index_.get_abs_pos(scaffold1, pos1) );
581 data.set_abs_pos2(file_index_.get_abs_pos(scaffold2, pos2) );
589 if (
in_->peek()!=
'@'){
591 data.read_binary(*
in_);
605 if (in_->peek()==
'@')
607 if (!concatenated_) {
616 data.read_binary(*in_);
625 if (in_->peek()==
'@')
627 if (!concatenated_) {
636 data.read_binary(*in_);
644 *out_ << data << std::endl;
650 *out_ << file_index_.get_string(file_index_.get_id0(data.get_abs_pos()) ) <<
'\t' << file_index_.get_id1(data.get_abs_pos() ) <<
'\t' << data << std::endl;
656 *out_ << file_index_.get_string(file_index_.get_id0(data.get_abs_pos()) ) <<
'\t' << file_index_.get_id1(data.get_abs_pos() ) <<
'\t' << file_index_.get_string(file_index_.get_id0(data.get_abs_pos2()) ) <<
'\t' << file_index_.get_id1(data.get_abs_pos2() ) <<
'\t' << data << std::endl;
665 data.write_binary(*
out_);
673 data.write_pos(*out_);
674 data.write_binary(*out_);
682 data.write_pos(*out_);
683 data.write_binary(*out_);
690 else write_text(data);
698 if (binary_) write_binary(data);
699 else write_text(data);
707 if (binary_) write_binary(data);
708 else write_text(data);
717 *out_ <<
"@NAME:" << T::table_name <<
"\tVERSION:" << VERSION;
718 if (try_binary_ && T::binary)
720 *out_ <<
"\tFORMAT:BINARY";
723 *out_ <<
"\tFORMAT:TEXT";
726 if (concatenated_) *out_ <<
"\tCONCATENATED";
728 *out_ << data.header();
731 std::cerr << __FILE__ <<
":" << __LINE__ <<
": file not open for writing. Exiting.\n"; exit(0);
739 std::vector <std::string> columns;
740 std::getline(*in_, line);
741 columns=split(line,
'\t');
742 if (columns.size()>2){
743 if (columns[0]==
"@NAME:"+T::table_name ){
744 binary_=std::find(columns.begin(), columns.end(),
"FORMAT:BINARY")!=columns.end();
745 concatenated_=std::find(columns.begin(), columns.end(),
"CONCATENATED")!=columns.end();
746 std::getline(*in_, line);
747 columns=split(line,
'\t');
752 std::cerr << __FILE__ <<
":" << __LINE__ <<
" attempted to open incorrect header.\n";
753 std::cerr << line << std::endl;
756 std::cerr << __FILE__ <<
":" << __LINE__ <<
" could not initilize " <<
typeid(T).name() <<
"\n";
757 std::cerr << line << std::endl;
769 index.
read(file_index_);
773 std::vector <std::string> columns;
775 std::cerr << in_->peek() << std::endl;
781 std::getline(*in_, line);
783 columns=split(line,
'\t');
784 if (columns.size()>2){
785 if (columns[0]==
"@NAME:"+T::table_name){
786 binary_=std::find(columns.begin(), columns.end(),
"FORMAT:BINARY")!=columns.end();
787 concatenated_=std::find(columns.begin(), columns.end(),
"CONCATENATED")!=columns.end();
788 std::getline(*in_, line);
789 columns=split(line,
'\t');
794 std::cerr << __FILE__ <<
":" << __LINE__ <<
" attempted to open incorrect header.\n";
795 std::cerr << line << std::endl;
796 std::cerr << T::table_name << std::endl;
798 std::cerr << __FILE__ <<
":" << __LINE__ <<
" could not initilize " <<
typeid(T).name() <<
"\n";
799 std::cerr << line << std::endl;
802 reference_size_=file_index_.get_reference_size();
810 std::cerr << __FILE__ <<
":" << __LINE__ <<
" file not open for writing. Exiting \n";
815 std::cerr <<
"Writing header of " << filename_ << std::endl;
816 std::cerr <<
"aka: " << this->filename() << std::endl;
820 std::cerr << __FILE__ <<
":" << __LINE__ <<
" cannot open for writing. Exiting \n";
824 index.
write(file_index_);
826 *out_ <<
"@NAME:" << T::table_name <<
"\tVERSION:" << VERSION;
827 if (try_binary_ && T::binary){
828 *out_ <<
"\tFORMAT:BINARY";
831 *out_ <<
"\tFORMAT:TEXT";
834 if (concatenated_) *out_ <<
"\tCONCATENATED";
835 *out_ <<
"\tINDEXED\n";
836 *out_ << data.header();
bool is_open(void) const
Returns true iff Base_file is open.
Definition: map_file.cc:369
void write_text(const T &)
Write in text mode. DONE.
Definition: map_file.h:642
igzstream gzin_
compressed istream.
Definition: map_file.h:92
Definition: gzstream.h:103
Definition: map_file.h:287
bool binary(void) const
Returns true iff a table is open in binary mode.
Definition: map_file.cc:442
T read_header(void)
Reads a file header.
Definition: map_file.h:763
void seek(const std::streampos &pos)
sets in and out (for reading and writing) to position pos.
void seekp(const std::streampos &pos)
sets out (for writing) to position pos.
Definition: map_file.cc:242
Definition: gzstream.h:92
std::streampos tellg(void)
Tells streampos of in (reading)
Definition: map_file.cc:257
std::streampos tellp(void)
Tells streampos of out (writing)
Definition: map_file.cc:252
void open_from(Base_file &)
! Appends to a file.
void write_text(const T &)
Write in text mode. DONE.
Definition: map_file.h:648
Tmp_buffer buffer_
a buffer which can be rewound.
Definition: map_file.h:88
void open_extention(const char *, const std::ios_base::openmode &)
! Doesn't check extension.
void write_header(const T &)
Writes a file header.
Definition: map_file.h:807
std::ostream * get_out(void)
returns the ostream.
Definition: map_file.cc:224
bool indexed_
Indexed mode flag.
Definition: map_file.h:83
File_index get_index(void) const
Returns the File_index.
Definition: map_file.h:847
void read_text(T &)
Read file in text mode. DONE.
Definition: map_file.h:491
const std::string & filename()
returns the filename.
Definition: map_file.cc:262
void set_index(const File_index &)
Sets the File_index.
Definition: map_file.h:841
Data which has an absolute position.
Definition: data.h:144
Definition: map_file.h:196
Base_file & read(Data *)
Reads from the istream.
Definition: map_file.cc:287
void open_no_extention(const char *, const std::ios::openmode &)
The function that opens a indexed_file (if file).
Definition: map_file.cc:59
bool try_binary_
Attempt to set binary mode flag.
Definition: map_file.h:82
An interface that transforms pairs of name and position keys into record numbers. ...
Definition: file_index.h:23
void write_header(const T &)
Writes a file header.
Definition: map_file.h:714
bool binary_
Binary mode flag.
Definition: map_file.h:81
bool compressed_
indicates whether the input/output stream is compressed.
Definition: map_file.h:74
Data * read_header(void)
Returns a pointer to a new instance of the derived data class.
Definition: map_file.cc:382
bool table_is_open(void) const
Returns true iff a table is open in Base_file.
Definition: map_file.cc:374
char delim_column_
The delimiter which separates columns.
Definition: map_file.h:77
void open(const char *, const std::ios_base::openmode &)
! The function that opens a Data_file (if file).
void read_text(T &)
Read file in text mode. DONE.
Definition: map_file.h:515
void seekg(const std::streampos &pos)
sets in (for reading) to position pos.
Definition: map_file.cc:247
Base_file()
returns the concatenated flag.
Definition: map_file.cc:5
std::ios::openmode openmode_
stores information about the mode in which the file was opened.
Definition: map_file.h:96
bool table_open_
indicates whether the header has been read successfully
Definition: map_file.h:68
bool read_
File is open for reading.
Definition: map_file.h:79
std::istream * in_
All data is read from in.
Definition: map_file.h:85
void write_text(const T &)
Write in text mode. DONE.
Definition: map_file.h:654
size_t size(void) const
Returns the number of rows in the file.
ogzstream gzout_
compressed ostream.
Definition: map_file.h:93
Base_file & write(const Data *)
Writes to the ostream.
Definition: map_file.cc:487
void write_binary(const Data *)
Write in binary mode.
Definition: map_file.cc:460
bool write_
File is open for writing.
Definition: map_file.h:80
Definition: map_file.h:264
std::ostream * out_
All data is written is written to out.
Definition: map_file.h:86
void open(const std::ios::openmode &)
Opens a Base_file to the cin/cout.
std::fstream file_
The file to read data from.
Definition: map_file.h:90
T read_header(void)
Reads a file header.
Definition: map_file.h:736
void write_binary(const Data &)
Write in binary mode.
Definition: tmp_buffer.h:37
void close(void)
Close iostreams, writes tail, etc.
Definition: map_file.cc:275
Data_file & read(Data &)
Reads a row from the file and advances one row.
File_index file_index_
A file_index which turns (id0, id1)->pos.
Definition: map_file.h:291
bool open_
indicates whether the iostream opened succesfully
Definition: map_file.h:60
std::string filename_
The name of the file if opened.
Definition: map_file.h:97
A templet which stores data associated with specific locations in a genome.
Definition: map_file.h:50
A class which can be written as flat text file or into an SQL database.
Definition: data.h:34
bool concatenated_
indicates whether multiple tables are included in the file
Definition: map_file.h:71
std::istream * get_in(void)
returns the istream.
Definition: map_file.cc:219
const std::fstream::openmode & openmode()
returns the open mode.
Definition: map_file.cc:229
void open_header(Base_file &)
Opens a header for a Flat_file.
Indexed_file & write(const T &)
Returns the position in the file.
Definition: map_file.h:696
void read_text(T &)
Read file in text mode. DONE.
Definition: map_file.h:549
void read_binary(Data &)
Read file in binary mode.
Definition: map_file.h:331
void close_table(void)
Ends table w/o closing iostreams.
Definition: map_file.cc:267
Data_file & write(const Data &)
Writes a row to the file and advances one row.