A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxonomy_writer.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
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 
37 
42 
43 #include <cassert>
44 #include <fstream>
45 #include <sstream>
46 #include <stdexcept>
47 
48 namespace genesis {
49 namespace taxonomy {
50 
51 // =================================================================================================
52 // Writing
53 // =================================================================================================
54 
55 void TaxonomyWriter::to_stream( Taxonomy const& tax, std::ostream& os ) const
56 {
57  for( auto const& tit : preorder( tax )) {
58  os << tax_gen_.to_string( tit.taxon() );
59 
60  if( write_ids_ ) {
61  os << "\t" << tit.taxon().id();
62  }
63  if( write_ranks_ ) {
64  os << "\t" << tit.taxon().rank();
65  }
66 
67  os << "\n";
68  }
69 }
70 
71 void TaxonomyWriter::to_file( Taxonomy const& tax, std::string const& filename ) const
72 {
73  std::ofstream ofs;
74  utils::file_output_stream( filename, ofs );
75  to_stream( tax, ofs );
76 }
77 
78 std::string TaxonomyWriter::to_string( Taxonomy const& tax ) const
79 {
80  std::ostringstream oss;
81  to_stream( tax, oss );
82  return oss.str();
83 }
84 
85 } // namespace taxonomy
86 } // namespace genesis
utils::Range< IteratorPreorder< Taxonomy const, Taxon const > > preorder(TaxonomyType const &taxonomy)
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(Taxonomy const &tax, std::ostream &os) const
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Provides some valuable additions to STD.
void to_file(Taxonomy const &tax, std::string const &fn) const
Provides some commonly used string utility functions.
Provides functions for accessing the file system.
std::string to_string(Taxopath const &taxopath) const
Return a string representation of a Taxopath.
std::string to_string(Taxonomy const &tax) const