OpenMS  2.4.0
FASTAFile.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2018.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Nico Pfeifer, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 
40 #include <functional>
41 #include <fstream>
42 #include <memory>
43 #include <utility>
44 #include <vector>
45 
46 namespace OpenMS
47 {
64  class OPENMS_DLLAPI FASTAFile
65  {
66 public:
76  struct FASTAEntry
77  {
81 
83  identifier(),
84  description(),
85  sequence()
86  {
87  }
88 
89  FASTAEntry(String id, String desc, String seq) :
90  identifier(id),
91  description(desc),
92  sequence(seq)
93  {
94  }
95 
96  FASTAEntry(const FASTAEntry& rhs)
97  :
98  identifier(rhs.identifier),
99  description(rhs.description),
100  sequence(rhs.sequence)
101  {
102  }
103 
104  FASTAEntry(FASTAEntry&& rhs) noexcept
105  :
106  identifier(::std::move(rhs.identifier)),
107  description(::std::move(rhs.description)),
108  sequence(::std::move(rhs.sequence))
109  {
110  }
111 
113  {
114  if (*this == rhs) return *this;
115  identifier = rhs.identifier;
116  description = rhs.description;
117  sequence = rhs.sequence;
118  return *this;
119  }
120 
121  bool operator==(const FASTAEntry& rhs) const
122  {
123  return identifier == rhs.identifier
124  && description == rhs.description
125  && sequence == rhs.sequence;
126  }
127 
128  bool headerMatches(const FASTAEntry& rhs) const
129  {
130  return identifier == rhs.identifier &&
131  description == rhs.description;
132  }
133 
134  bool sequenceMatches(const FASTAEntry& rhs) const
135  {
136  return sequence == rhs.sequence;
137  }
138  };
139 
141  FASTAFile();
142 
144  virtual ~FASTAFile();
145 
152  void readStart(const String& filename);
153 
163  bool readNext(FASTAEntry& protein);
164 
166  std::streampos position() const;
167 
169  bool atEnd() const;
170 
172  bool setPosition(const std::streampos& pos);
173 
179  void writeStart(const String& filename);
180 
188  void writeNext(const FASTAEntry& protein);
189 
194  void writeEnd();
195 
196 
205  void static load(const String& filename, std::vector<FASTAEntry>& data);
206 
214  void static store(const String& filename, const std::vector<FASTAEntry>& data);
215 
216 protected:
217  std::fstream infile_;
218  std::ofstream outfile_;
219  std::unique_ptr<void, std::function<void(void*) > > reader_;
221  };
222 
223 } // namespace OpenMS
224 
Size entries_read_
some internal book-keeping during reading
Definition: FASTAFile.h:220
A more convenient string class.
Definition: String.h:57
Size< TNeedle >::Type position(const PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:561
FASTAEntry()
Definition: FASTAFile.h:82
bool operator==(const FASTAEntry &rhs) const
Definition: FASTAFile.h:121
bool sequenceMatches(const FASTAEntry &rhs) const
Definition: FASTAFile.h:134
FASTAEntry(FASTAEntry &&rhs) noexcept
Definition: FASTAFile.h:104
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
std::fstream infile_
filestream for reading; init using FastaFile::readStart()
Definition: FASTAFile.h:217
bool headerMatches(const FASTAEntry &rhs) const
Definition: FASTAFile.h:128
FASTAEntry & operator=(const FASTAEntry &rhs)
Definition: FASTAFile.h:112
FASTAEntry(String id, String desc, String seq)
Definition: FASTAFile.h:89
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
String sequence
Definition: FASTAFile.h:80
std::ofstream outfile_
filestream for writing; init using FastaFile::writeStart()
Definition: FASTAFile.h:218
FASTA entry type (identifier, description and sequence)
Definition: FASTAFile.h:76
std::unique_ptr< void, std::function< void(void *) > > reader_
filestream for reading; init using FastaFile::readStart(); needs to be a pointer, since its not copy-...
Definition: FASTAFile.h:219
String identifier
Definition: FASTAFile.h:78
This class serves for reading in and writing FASTA files.
Definition: FASTAFile.h:64
String description
Definition: FASTAFile.h:79
FASTAEntry(const FASTAEntry &rhs)
Definition: FASTAFile.h:96