A library for working with phylogenetic and population genetic data.
v0.32.0
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-2020 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::write( NexusDocument const& document, std::shared_ptr<utils::BaseOutputTarget> target ) const
50 {
51  auto& os = target->ostream();
52  os << "#NEXUS\n";
53 
54  for(auto& block : document) {
55  os << "\nBEGIN " << block->block_name() << ";\n";
56  os << *block;
57  os << "END;\n";
58  }
59 }
60 
61 std::string NexusWriter::to_string( NexusDocument const& document ) const
62 {
63  std::stringstream sstr;
64  write( document, utils::to_stream( sstr ));
65  return sstr.str();
66 }
67 
68 } // namespace utils
69 } // namespace genesis
genesis::utils::NexusWriter::write
void write(NexusDocument const &document, std::shared_ptr< utils::BaseOutputTarget > target) const
Write a NexusDocument to an output target, using the Nexus format.
Definition: utils/formats/nexus/writer.cpp:49
fs.hpp
Provides functions for accessing the file system.
genesis::utils::NexusDocument
Definition: nexus/document.hpp:54
genesis::utils::NexusWriter::to_string
std::string to_string(NexusDocument const &document) const
Directly return a NexusDocument as a string in Nexus format.
Definition: utils/formats/nexus/writer.cpp:61
output_stream.hpp
writer.hpp
genesis::utils::to_stream
std::shared_ptr< BaseOutputTarget > to_stream(std::ostream &target_stream, GzipCompressionLevel compression_level=GzipCompressionLevel::kNoCompression)
Obtain an output target for writing to a stream.
Definition: output_target.hpp:203
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
document.hpp