A toolkit for working with phylogenetic data.
v0.18.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-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 FastaWriter::write_sequence( Sequence const& seq, std::ostream& os ) const
50 {
51  // Write label.
52  os << ">" << seq.label();
53 
54  // Write metadata if available.
55  if( enable_metadata_ && seq.metadata().size() > 0 ) {
56  os << " " << seq.metadata();
57  }
58  os << "\n";
59 
60  // Write sequence. If needed, add new line at every line_length_ position.
61  if (line_length_ > 0) {
62  for (size_t i = 0; i < seq.length(); i += line_length_) {
63  // Write line_length_ many characters.
64  // (If the string is shorter, as many characters as possible are used.)
65  os << seq.sites().substr(i, line_length_) << "\n";
66  }
67  } else {
68  os << seq.sites() << "\n";
69  }
70 }
71 
72 void FastaWriter::to_stream( SequenceSet const& sset, std::ostream& os ) const
73 {
74  for( Sequence const& seq : sset ) {
75  write_sequence( seq, os );
76  }
77 }
78 
79 void FastaWriter::to_file( SequenceSet const& sset, std::string const& filename ) const
80 {
81  std::ofstream ofs;
82  utils::file_output_stream( filename, ofs );
83  to_stream( sset, ofs );
84 }
85 
86 std::string FastaWriter::to_string ( SequenceSet const& sset ) const
87 {
88  std::ostringstream oss;
89  to_stream( sset, oss );
90  return oss.str();
91 }
92 
93 // =================================================================================================
94 // Properties
95 // =================================================================================================
96 
98 {
99  line_length_ = value;
100  return *this;
101 }
102 
104 {
105  return line_length_;
106 }
107 
109 {
110  enable_metadata_ = value;
111  return *this;
112 }
113 
115 {
116  return enable_metadata_;
117 }
118 
119 } // namespace sequence
120 } // 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:107
std::string const & sites() const
Definition: sequence.cpp:72
std::string const & label() const
Definition: sequence.cpp:44
Provides functions for accessing the file system.
Store a set of Sequences.
std::string const & metadata() const
Definition: sequence.cpp:58
void write_sequence(Sequence const &seq, std::ostream &os) const
Write a single Sequence to an output stream in Fasta format.
bool enable_metadata()
Return whether currently the FastaWriter is set to write metadata.
std::string to_string(SequenceSet const &sset) const
Return Sequences of a SequenceSet in form of a Fasta formatted string.