A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fasta_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 
38 #include <fstream>
39 #include <sstream>
40 #include <stdexcept>
41 
42 namespace genesis {
43 namespace sequence {
44 
45 // =================================================================================================
46 // Writing
47 // =================================================================================================
48 
49 void FastaWriter::write_sequence( Sequence const& seq, std::ostream& os ) const
50 {
51  // Write label.
52  os << ">" << seq.label() << "\n";
53 
54  // Write sequence. If needed, add new line at every line_length_ position.
55  if (line_length_ > 0) {
56  for (size_t i = 0; i < seq.length(); i += line_length_) {
57  // Write line_length_ many characters.
58  // (If the string is shorter, as many characters as possible are used.)
59  os << seq.sites().substr(i, line_length_) << "\n";
60  }
61  } else {
62  os << seq.sites() << "\n";
63  }
64 }
65 
66 void FastaWriter::to_stream( SequenceSet const& sset, std::ostream& os ) const
67 {
68  for( Sequence const& seq : sset ) {
69  write_sequence( seq, os );
70  }
71 }
72 
73 void FastaWriter::to_file( SequenceSet const& sset, std::string const& filename ) const
74 {
75  std::ofstream ofs;
76  utils::file_output_stream( filename, ofs );
77  to_stream( sset, ofs );
78 }
79 
80 std::string FastaWriter::to_string ( SequenceSet const& sset ) const
81 {
82  std::ostringstream oss;
83  to_stream( sset, oss );
84  return oss.str();
85 }
86 
87 // =================================================================================================
88 // Properties
89 // =================================================================================================
90 
92 {
93  line_length_ = value;
94  return *this;
95 }
96 
98 {
99  return line_length_;
100 }
101 
102 } // namespace sequence
103 } // namespace genesis
void to_stream(SequenceSet const &sset, std::ostream &os) const
Write Sequences of a SequenceSet to a stream, using the Fasta format.
void to_file(SequenceSet const &sset, std::string const &fn) const
Write Sequences of a SequenceSet to a file, using the Fasta format.
size_t line_length() const
Get the current line length.
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 length() const
Return the length (number of sites) of this sequence.
Definition: sequence.cpp:92
std::string const & sites() const
Definition: sequence.cpp:58
std::string const & label() const
Definition: sequence.cpp:44
Provides functions for accessing the file system.
Store a set of Sequences.
void write_sequence(Sequence const &seq, std::ostream &os) const
Write a single Sequence to an output stream in Fasta format.
std::string to_string(SequenceSet const &sset) const
Return Sequences of a SequenceSet in form of a Fasta formatted string.