modified: myjupyterlab.sh
[GalaxyCodeBases.git] / c_cpp / etc / mVicuna / src / jaz / fasta_file.hpp
blob04a8930e99a07043caf20b1bfc7eed1068466195
1 /***
2 * $Id: fasta_file.hpp 120 2007-06-04 18:42:10Z zola $
3 **
4 * File: fasta_file.hpp
5 * Created: Jul 23, 2006
7 * Author: Jaroslaw Zola <jaroslaw.zola@gmail.com>
8 * Copyright (c) 2006-2009 Jaroslaw Zola
9 * Please see attached LICENSE file.
12 #ifndef BIO_IO_FASTA_FILE_HPP
13 #define BIO_IO_FASTA_FILE_HPP
15 #include <cstddef>
16 #include <algorithm>
17 #include <fstream>
18 #include <iterator>
19 #include <string>
20 #include <utility>
23 /** Main bIO namespace.
25 namespace bIO {
27 /** This class provides interface to read input in the FASTA format.
29 class FASTA_input {
30 public:
31 /**
33 typedef std::pair<std::string, std::string> value_type;
35 /** Constructs FASTA_input over the input stream @a in. Next it seeks
36 * the first sequence in the stream and reads its header (if possible).
37 * @param in is an input stream.
39 explicit FASTA_input(std::istream& in)
40 : owner_(false), input_(&in), val_(value_type("", "")), buf_("") {
41 pre_read_();
42 } // FASTA_input
44 /** Constructs FASTA_input for the file @a name. Next it seeks
45 * first sequence in the stream and reads its header (if possible).
46 * @param name is a name of the FASTA file to be read.
48 explicit FASTA_input(const std::string& name)
49 : owner_(true), val_(value_type("", "")), buf_("") {
50 input_ = new std::ifstream(name.c_str());
51 pre_read_();
52 } // FASTA_input
54 /**
56 virtual ~FASTA_input() { if (owner_ == true) delete input_; }
59 /** Resets stream (tries to rewind to its beginning). Next it seeks
60 * first sequence in the stream and reads its header (if possible).
61 * @return true on success and false otherwise.
63 bool reset() {
64 input_->clear();
65 input_->seekg(0, std::ios_base::beg);
66 val_ = value_type("", "");
67 buf_ = "";
68 pre_read_();
69 return input_->good();
70 } // reset
72 /** @return state of the FASTA_input underlying stream.
74 operator bool() const { return input_->good(); }
76 /** @return pair consisting of the last read sequence, that is
77 * containing sequence's header as the first argument, and
78 * sequence's chain as the second argument.
80 const value_type& operator*() const { return val_; }
82 /** Tries to read FASTA sequence from the input.
83 * @return true on success and false otherwise.
85 bool operator++() {
86 if (input_->good() == false) return false;
88 if (buf_.empty() == false) {
89 unsigned int len = buf_.size() - 1;
90 if (buf_[len] == '\r') buf_.resize(len);
93 val_.first = (buf_.c_str() + 1);
94 val_.second = "";
96 do {
97 std::getline(*input_, buf_);
98 if ((buf_.empty() == false) && (buf_[0] != ';') && (buf_[0] != '>')) {
99 unsigned int len = buf_.size() - 1;
100 if (buf_[len] == '\r') buf_.resize(len);
101 val_.second += buf_;
103 if ((buf_.empty() == false) && (buf_[0] == '>')) break;
105 while (input_->good());
107 return true;
108 } // operator++
111 private:
112 FASTA_input(const FASTA_input&);
113 void operator=(const FASTA_input&);
115 void pre_read_() {
116 while (input_->good()) {
117 std::getline(*input_, buf_);
118 if ((buf_.empty() == false) && (buf_[0] == '>')) break;
121 if (buf_.empty() == false) {
122 unsigned int len = buf_.size() - 1;
123 if (buf_[len] == '\r') buf_.resize(len);
125 } // pre_read_
127 bool owner_;
128 std::istream* input_;
130 value_type val_;
131 std::string buf_;
133 }; // class FASTA_input
137 /** This class provides interface to write output in the FASTA format.
139 class FASTA_output {
140 public:
143 typedef std::pair<std::string, std::string> value_type;
145 /** Constructs FASTA_output over the output stream @a out.
147 explicit FASTA_output(std::ostream& out)
148 : owner_(false), output_(&out),
149 out_iter_(std::ostream_iterator<char>(*output_)),
150 val_(value_type("", "")) {
151 } // FASTA_output
153 /** Constructs FASTA_output over the file @a name.
155 explicit FASTA_output(const std::string& name)
156 : owner_(true), output_(new std::ofstream(name.c_str())),
157 out_iter_(std::ostream_iterator<char>(*output_)),
158 val_(value_type("", "")) {
159 } // FASTA_output
163 virtual ~FASTA_output() { if (owner_ == true) delete output_; }
166 /** @return state of the underlying stream.
168 operator bool() const { return output_->good(); }
170 /** @return reference to the pair with sequence which will be written
171 * in the next call to operator++().
173 value_type& operator*() { return val_; }
175 /** Writes currently stored sequence to the output, performing
176 * required formatting.
177 * @return true on success, false otherwise.
179 bool operator++() {
180 *(++out_iter_) = '>';
181 std::copy(val_.first.begin(), val_.first.end(), out_iter_);
182 *(++out_iter_) = '\n';
184 const int L = 79;
186 std::string::iterator iter = val_.second.begin();
187 std::size_t l = val_.second.size() / L;
189 for (std::size_t i = 0; i < l; ++i, iter += L) {
190 std::copy(iter, iter + L, out_iter_);
191 *(++out_iter_) = '\n';
194 std::copy(iter, val_.second.end(), out_iter_);
195 *(++out_iter_) = '\n';
197 return output_->good();
198 } // operator
201 private:
202 FASTA_output(const FASTA_output&);
203 void operator=(const FASTA_output&);
205 bool owner_;
206 std::ostream* output_;
207 std::ostream_iterator<char> out_iter_;
209 value_type val_;
211 }; // class FASTA_output
213 } // namespace bIO
215 #endif // BIO_IO_FASTA_FILE_HPP