A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils/formats/nexus/writer.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
36 
37 #include <stdexcept>
38 #include <fstream>
39 #include <ostream>
40 #include <sstream>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Nexus Writer
47 // =================================================================================================
48 
49 void NexusWriter::to_stream( NexusDocument const& doc, std::ostream& out ) const
50 {
51  out << "#NEXUS\n";
52 
53  for(auto& block : doc) {
54  out << "\nBEGIN " << block->block_name() << ";\n";
55  out << *block;
56  out << "END;\n";
57  }
58 }
59 
60 void NexusWriter::to_file( NexusDocument const& doc, std::string const& filename) const
61 {
62  std::ofstream ofs;
63  utils::file_output_stream( filename, ofs );
64  to_stream( doc, ofs );
65 }
66 
67 void NexusWriter::to_string( NexusDocument const& doc, std::string& output) const
68 {
69  std::stringstream sstr;
70  to_stream( doc, sstr );
71  output = sstr.str();
72 }
73 
74 std::string NexusWriter::to_string( NexusDocument const& doc) const
75 {
76  std::stringstream sstr;
77  to_stream( doc, sstr );
78  return sstr.str();
79 }
80 
81 } // namespace utils
82 } // namespace genesis
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out)
Helper function to obtain an output stream to a file.
void to_stream(NexusDocument const &doc, std::ostream &out) const
void to_string(NexusDocument const &doc, std::string &output) const
Provides functions for accessing the file system.
void to_file(NexusDocument const &doc, std::string const &filename) const