bpp-seq-omics  2.4.1
BedGraphFeatureReader.cpp
Go to the documentation of this file.
1 //
2 // File: BedGraphFeatureReader.cpp
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 #include "BedGraphFeatureReader.h"
41 
42 //From bpp-core:
44 #include <Bpp/Text/TextTools.h>
45 #include <Bpp/Text/KeyvalTools.h>
47 
48 //From the STL:
49 #include <string>
50 #include <iostream>
51 
52 using namespace bpp;
53 using namespace std;
54 
55 const std::string BedGraphFeatureReader::BED_VALUE = "BedValue";
56 
57 
59  nextLine_ = "";
60  while (TextTools::isEmpty(nextLine_) || nextLine_.size() < 2 || nextLine_[0] == '#') {
61  if (input_.eof()) {
62  nextLine_ = "";
63  return;
64  }
65  getline(input_, nextLine_);
66  }
67 }
68 
70 {
71  if (!hasMoreFeature())
72  throw Exception("BedGraphFeatureReader::nextFeature(). No more feature in file.");
73 
74  //Parse current line:
75  StringTokenizer st(nextLine_, "\t");
76  if (st.numberOfRemainingTokens() != 4)
77  throw Exception("BedGraphFeatureReader::nextFeature(). Wrong BedGraph file format: should have 4 tab delimited columns.");
78 
79  //if ok, we can parse each column:
80  string seqId = st.nextToken();
81  unsigned int start = TextTools::to<unsigned int>(st.nextToken());
82  unsigned int end = TextTools::to<unsigned int>(st.nextToken());
83  string value = st.nextToken();
84  string id = "bed" + TextTools::toString(++id_);
85  BasicSequenceFeature feature(id, seqId, "bed_graph", "", start, end, '.', -1);
86 
87  //Set value attributes:
88  if (value != ".") feature.setAttribute(BED_VALUE, value);
89 
90  //Read the next line:
91  getNextLine_();
92 
93  return feature;
94 }
95 
97  std::vector< std::string > v;
98  v.push_back(f.getSequenceId());
99  v.push_back(bpp::TextTools::toString(f.getStart()));
100  v.push_back(bpp::TextTools::toString(f.getEnd()));
101  string value = f.getAttribute(BED_VALUE);
102  v.push_back(bpp::TextTools::toString(value == "" ? "." : value));
103  return bpp::VectorTools::paste(v, "\t");
104 }
105 
A very simple implementation of the SequenceFeature class.
void setAttribute(const std::string &attribute, const std::string &value)
Set the value of an attribute.
static const std::string BED_VALUE
static std::string toString(const bpp::SequenceFeature &f)
const BasicSequenceFeature nextFeature()
The base interface for sequence features.
virtual const size_t getEnd() const =0
virtual const std::string & getAttribute(const std::string &attribute) const =0
virtual const size_t getStart() const =0
virtual const std::string & getSequenceId() const =0
size_t numberOfRemainingTokens() const
const std::string & nextToken()
static std::string paste(const std::vector< T > &v, const std::string &delim=" ")
bool isEmpty(const std::string &s)
std::string toString(T t)