A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
phylip_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 
37 
38 #include <fstream>
39 #include <sstream>
40 #include <stdexcept>
41 
42 namespace genesis {
43 namespace sequence {
44 
45 // =================================================================================================
46 // Writing
47 // =================================================================================================
48 
49 // void PhylipWriter::to_stream_interleaved( SequenceSet const& sset, std::ostream& os ) const
50 // {
51 //
52 // }
53 
54 // void PhylipWriter::to_stream_sequential( SequenceSet const& sset, std::ostream& os ) const
55 // {
56 //
57 // }
58 
62 void PhylipWriter::to_stream( SequenceSet const& sset, std::ostream& os ) const
63 {
64  // Check if this is an alignment and get its sequence length.
65  size_t length = 0;
66  for( Sequence const& s : sset ) {
67  if (length == 0) {
68  length = s.length();
69  continue;
70  }
71  if (s.length() != length) {
72  throw std::runtime_error( "Sequences do not have the same length." );
73  }
74  }
75  if (length == 0) {
76  throw std::runtime_error( "Sequences are empty." );
77  }
78 
79  // Write sequences to stream.
80  os << sset.size() << " " << length << "\n";
81  for (Sequence const& s : sset) {
82  // Write label.
83  if( label_length_ == 0 ) {
84  os << s.label() << " ";
85  } else {
86  os << s.label().substr( 0, label_length_ );
87  if( label_length_ > s.label().length() ) {
88  os << std::string( label_length_ - s.label().length(), ' ' );
89  }
90  }
91 
92  // Write sequence. If needed, add new line at every line_length_ position.
93  if( line_length_ > 0 ) {
94  for( size_t i = 0; i < s.length(); i += line_length_ ) {
95  // Write line_length_ many characters.
96  // (If the string is shorter, as many characters as possible are used.)
97  os << s.sites().substr( i, line_length_ ) << "\n";
98  }
99  } else {
100  os << s.sites() << "\n";
101  }
102  }
103 }
104 
113 void PhylipWriter::to_file( SequenceSet const& sset, std::string const& filename ) const
114 {
115  std::ofstream ofs;
116  utils::file_output_stream( filename, ofs );
117  to_stream( sset, ofs );
118 }
119 
126 std::string PhylipWriter::to_string ( SequenceSet const& sset ) const
127 {
128  std::ostringstream oss;
129  to_stream( sset, oss );
130  return oss.str();
131 }
132 
133 // =================================================================================================
134 // Properties
135 // =================================================================================================
136 
153 {
154  label_length_ = value;
155  return *this;
156 }
157 
164 {
165  return label_length_;
166 }
167 
176 {
177  line_length_ = value;
178  return *this;
179 }
180 
187 {
188  return line_length_;
189 }
190 
191 } // namespace sequence
192 } // 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.
size_t line_length() const
Get the current line length.
std::string to_string(SequenceSet const &sset) const
Return Sequences of a SequenceSet in form of a Phylip formatted string.
Provides functions for accessing the file system.
Store a set of Sequences.
void to_stream(SequenceSet const &sset, std::ostream &os) const
Write Sequences of a SequenceSet to a stream in Phylip format.
void to_file(SequenceSet const &sset, std::string const &fn) const
Write Sequences of a SequenceSet to a file in Phylip format.
size_t label_length() const
Return the currently set label length.
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.