2 * $Id: fasta_file.hpp 120 2007-06-04 18:42:10Z zola $
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
23 /** Main bIO namespace.
27 /** This class provides interface to read input in the FASTA format.
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_("") {
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());
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.
65 input_
->seekg(0, std::ios_base::beg
);
66 val_
= value_type("", "");
69 return input_
->good();
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.
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);
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
);
103 if ((buf_
.empty() == false) && (buf_
[0] == '>')) break;
105 while (input_
->good());
112 FASTA_input(const FASTA_input
&);
113 void operator=(const FASTA_input
&);
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
);
128 std::istream
* input_
;
133 }; // class FASTA_input
137 /** This class provides interface to write output in the FASTA format.
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("", "")) {
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("", "")) {
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.
180 *(++out_iter_
) = '>';
181 std::copy(val_
.first
.begin(), val_
.first
.end(), out_iter_
);
182 *(++out_iter_
) = '\n';
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();
202 FASTA_output(const FASTA_output
&);
203 void operator=(const FASTA_output
&);
206 std::ostream
* output_
;
207 std::ostream_iterator
<char> out_iter_
;
211 }; // class FASTA_output
215 #endif // BIO_IO_FASTA_FILE_HPP