bpp-seq-omics  2.4.1
BedGraphFeatureReader.h
Go to the documentation of this file.
1 //
2 // File: BedGraphFeatureReader.h
3 // Created by: Julien Dutheil
4 // Created on: Tue Feb 2 2016
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for sequences analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _BEDGRAPHFEATUREREADER_H_
41 #define _BEDGRAPHFEATUREREADER_H_
42 
43 #include "../SequenceFeature.h"
44 #include "../FeatureReader.h"
45 
46 //From bpp-core:
47 #include <Bpp/Exceptions.h>
48 
49 //From the STL:
50 #include <string>
51 #include <vector>
52 
53 namespace bpp {
54 
67  public virtual FeatureReader
68 {
69  public:
70  static const std::string BED_VALUE;
71 
72  private:
73  std::istream& input_;
74  std::string nextLine_;
75  unsigned int id_;
76 
77  public:
78  BedGraphFeatureReader(std::istream& input):
79  input_(input), nextLine_(), id_(0)
80  {
81  bool start = false;
82  do {
83  getNextLine_();
84  if (nextLine_.size() >= 5 && nextLine_.substr(0, 5) == "track") {
85  start = true;
86  }
87  } while(!start && !input_.eof());
88  if (input_.eof())
89  throw Exception("BedGraphFeatureReader::constructor: Invalid BedGraph file, missing proper header.");
90  getNextLine_();
91  }
92 
93  public:
94  bool hasMoreFeature() const { return nextLine_ != ""; }
96 
98  while (hasMoreFeature()) {
99  features.addFeature(nextFeature());
100  }
101  }
102  void getFeaturesOfType(const std::string& type, SequenceFeatureSet& features) {
103  while (hasMoreFeature()) {
104  BasicSequenceFeature feature = nextFeature();
105  if (feature.getType() == type)
106  features.addFeature(feature);
107  }
108  }
109  void getFeaturesOfSequence(const std::string& seqId, SequenceFeatureSet& features) {
110  while (hasMoreFeature()) {
111  BasicSequenceFeature feature = nextFeature();
112  if (feature.getSequenceId() == seqId)
113  features.addFeature(feature);
114  }
115  }
116 
121  static std::string toString(const bpp::SequenceFeature& f);
122 
131  static void toString(const bpp::SequenceFeature& f, std::ostream& out) {
132  out << toString(f) << std::endl;
133  }
134 
135  static void toString(const bpp::SequenceFeatureSet& fs, std::ostream& out) {
136  for (unsigned int i = 0; i < fs.getNumberOfFeatures(); ++i) {
137  toString(fs[i], out);
138  }
139  }
140 
141  private:
142  void getNextLine_();
143 
144 };
145 
146 } //end of namespace bpp
147 
148 #endif //_BEDGRAPHFEATUREREADER_H_
149 
A very simple implementation of the SequenceFeature class.
const std::string & getSequenceId() const
const std::string & getType() const
A simple reader for features in the BedGraph format.
static void toString(const bpp::SequenceFeature &f, std::ostream &out)
Out put a string description of a feature to a stream.
void getAllFeatures(SequenceFeatureSet &features)
static const std::string BED_VALUE
void getFeaturesOfType(const std::string &type, SequenceFeatureSet &features)
static std::string toString(const bpp::SequenceFeature &f)
const BasicSequenceFeature nextFeature()
static void toString(const bpp::SequenceFeatureSet &fs, std::ostream &out)
BedGraphFeatureReader(std::istream &input)
void getFeaturesOfSequence(const std::string &seqId, SequenceFeatureSet &features)
Interface for feature readers.
Definition: FeatureReader.h:60
A simple ensemble of sequence features.
void addFeature(const SequenceFeature &feature)
Add a feature to the container. The feature will be copied and the copy owned by the container.
size_t getNumberOfFeatures() const
The base interface for sequence features.