bpp-seq-omics  2.4.1
GtfFeatureReader.cpp
Go to the documentation of this file.
1 //
2 // File: GtfFeatureReader.cpp
3 // Created by: Sylvain Gaillard
4 // Created on: Fry Jan 27 2012
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 "GtfFeatureReader.h"
41 
42 //From bpp-core:
44 #include <Bpp/Text/TextTools.h>
45 #include <Bpp/Text/KeyvalTools.h>
46 
47 //From the STL:
48 #include <string>
49 #include <iostream>
50 
51 using namespace bpp;
52 using namespace std;
53 
54 const string GtfFeatureReader::GTF_PHASE = "GTF_PHASE";
55 const string GtfFeatureReader::GTF_GENE_ID = "gene_id";
56 const string GtfFeatureReader::GTF_TRANSCRIPT_ID = "transcript_id";
57 
59  nextLine_ = "";
60  // Remove comments
61  /*
62  todo: remove characters between # and end-of-line taking care of double quotes
63  */
64  while (TextTools::isEmpty(nextLine_) || nextLine_.size() < 2 || nextLine_[0] == '#') {
65  if (input_.eof()) {
66  nextLine_ = "";
67  return;
68  }
69  getline(input_, nextLine_);
70  }
71 }
72 
74 {
75  if (!hasMoreFeature())
76  throw Exception("GtfFeatureReader::nextFeature(). No more feature in file.");
77 
78  //Parse current line:
79  StringTokenizer st(nextLine_, "\t");
80  if (st.numberOfRemainingTokens() != 9)
81  throw Exception("GtfFeatureReader::nextFeature(). Wrong GTF file format: should have 9 tab delimited columns.");
82 
83  //if ok, we can parse each column:
84  string seqId = st.nextToken();
85  string source = st.nextToken();
86  string type = st.nextToken();
87  unsigned int start = TextTools::to<unsigned int>(st.nextToken()) - 1;
88  unsigned int end = TextTools::to<unsigned int>(st.nextToken());
89  double score = TextTools::to<double>(st.nextToken());
90  string strand = st.nextToken();
91  string phase = st.nextToken();
92  string attrDesc = st.nextToken();
93  map<string, string> attributes;
94  StringTokenizer st1(attrDesc, ";");
95  while (st1.hasMoreToken()) {
96  string item(st1.nextToken());
97  if (TextTools::isEmpty(item)) continue;
99  string::size_type idx = item.find_first_of(' ');
100  string key(item.substr(0, idx));
101  string value(item.substr(idx));
102  // remove first "
103  while (
104  value.size() > 0
105  && (
106  value[0] == '"'
108  )
109  ) {
110  value.erase(value.begin());
111  }
112  // remove last "
113  while (
114  value.size() > 0
115  && (
116  value[value.size() - 1] == '"'
117  || TextTools::isWhiteSpaceCharacter(value[value.size() - 1])
118  )
119  ) {
120  value.erase(value.end() - 1);
121  }
122  attributes[key] = value;
123  //std::cout << "[" << key << "] = [" << value << "]" << std::endl;
124  }
125  //KeyvalTools::multipleKeyvals(attrDesc, attributes, ";", false);
126  //std::string id = attributes["ID"];
127  string id = "";
128  BasicSequenceFeature feature(id, seqId, source, type, start, end, strand[0], score);
129 
130  //Set phase attributes:
132  if (phase != ".") feature.setAttribute(GTF_PHASE, phase);
133 
134  //now check additional attributes:
135  for (map<std::string, std::string>::iterator it = attributes.begin(); it != attributes.end(); ++it) {
136  feature.setAttribute(it->first, it->second); //We accept all attributes, even if they are not standard.
137  //std::cout << "[" << it->first << "] = [" << it->second << "]" << std::endl;
138  //std::cout << "phase: " << phase << std::endl;
139  }
140 
141  //Read the next line:
142  getNextLine_();
143 
144  return feature;
145 }
146 
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 GTF_TRANSCRIPT_ID
static const std::string GTF_PHASE
static const std::string GTF_GENE_ID
const BasicSequenceFeature nextFeature()
size_t numberOfRemainingTokens() const
const std::string & nextToken()
bool hasMoreToken() const
std::string removeSurroundingWhiteSpaces(const std::string &s)
bool isWhiteSpaceCharacter(char c)
bool isEmpty(const std::string &s)