bpp-seq-omics  2.4.1
GffFeatureReader.cpp
Go to the documentation of this file.
1 //
2 // File: GffFeatureReader.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Nov 21 2011
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 "GffFeatureReader.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 GffFeatureReader::GFF_PHASE = "GFF_PHASE";
56 const std::string GffFeatureReader::GFF_NAME = "Name";
57 const std::string GffFeatureReader::GFF_ALIAS = "GFF_ALIAS";
58 const std::string GffFeatureReader::GFF_PARENT = "Parent";
59 const std::string GffFeatureReader::GFF_TARGET = "Target";
60 const std::string GffFeatureReader::GFF_GAP = "Gap";
61 const std::string GffFeatureReader::GFF_DERIVES_FROM = "GFF_DERIVES_FROM";
62 const std::string GffFeatureReader::GFF_NOTE = "Note";
63 const std::string GffFeatureReader::GFF_DBXREF = "Dbxref";
64 const std::string GffFeatureReader::GFF_ONTOLOGY_TERM = "Ontology_term";
65 const std::string GffFeatureReader::GFF_IS_CIRCULAR = "Is_circular";
66 
67 
69  nextLine_ = "";
70  while (TextTools::isEmpty(nextLine_) || nextLine_.size() < 2 || nextLine_[0] == '#') {
71  if (input_.eof()) {
72  nextLine_ = "";
73  return;
74  }
75  getline(input_, nextLine_);
76  }
77 }
78 
80 {
81  if (!hasMoreFeature())
82  throw Exception("GffFeatureReader::nextFeature(). No more feature in file.");
83 
84  //Parse current line:
85  StringTokenizer st(nextLine_, "\t");
86  if (st.numberOfRemainingTokens() != 9)
87  throw Exception("GffFeatureReader::nextFeature(). Wrong GFF3 file format: should have 9 tab delimited columns.");
88 
89  //if ok, we can parse each column:
90  string seqId = st.nextToken();
91  string source = st.nextToken();
92  string type = st.nextToken();
93  unsigned int start = TextTools::to<unsigned int>(st.nextToken()) - 1;
94  unsigned int end = TextTools::to<unsigned int>(st.nextToken());
95  double score = TextTools::to<double>(st.nextToken());
96  string strand = st.nextToken();
97  string phase = st.nextToken();
98  string attrDesc = st.nextToken();
99  map<string, string> attributes;
100  KeyvalTools::multipleKeyvals(attrDesc, attributes, ";", false);
101  string id = attributes["ID"];
102  BasicSequenceFeature feature(id, seqId, source, type, start, end, strand[0], score);
103 
104  //Set phase attributes:
105  if (phase != ".") feature.setAttribute(GFF_PHASE, phase);
106 
107  //now check additional attributes:
108  for (map<string, string>::iterator it = attributes.begin(); it != attributes.end(); ++it) {
109  if (it->first != "ID")
110  feature.setAttribute(it->first, it->second); //We accept all attributes, even if they are not standard.
111  }
112 
113  //Read the next line:
114  getNextLine_();
115 
116  return feature;
117 }
118 
120  std::vector< std::string > v;
121  std::vector< std::string > attr;
122  std::set< std::string > attrNames = f.getAttributeList();
123  v.push_back(f.getSequenceId());
124  v.push_back(f.getSource());
125  v.push_back(f.getType());
126  v.push_back(bpp::TextTools::toString(f.getStart() + 1));
127  v.push_back(bpp::TextTools::toString(f.getEnd()));
128  v.push_back(bpp::TextTools::toString(f.getScore()));
129  if (f.isStranded()) {
130  if (f.isNegativeStrand()) {
131  v.push_back("-");
132  } else {
133  v.push_back("+");
134  }
135  } else {
136  v.push_back(".");
137  }
138  if (f.getAttribute(GFF_PHASE) == "") {
139  v.push_back(".");
140  } else {
141  v.push_back(f.getAttribute(GFF_PHASE));
142  }
143 
144  if (f.getId() != "") {
145  attr.push_back("ID=" + f.getId());
146  }
147  for (std::set< std::string >::iterator it = attrNames.begin() ; it != attrNames.end() ; it++) {
148  attr.push_back(*it + "=" + f.getAttribute(*it));
149  }
150  v.push_back(bpp::VectorTools::paste(attr, ";"));
151  return bpp::VectorTools::paste(v, "\t");
152 }
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 GFF_DBXREF
static const std::string GFF_ALIAS
static std::string toString(const bpp::SequenceFeature &f)
static const std::string GFF_NOTE
static const std::string GFF_PHASE
static const std::string GFF_DERIVES_FROM
static const std::string GFF_TARGET
static const std::string GFF_ONTOLOGY_TERM
static const std::string GFF_IS_CIRCULAR
static const std::string GFF_GAP
static const std::string GFF_PARENT
static const std::string GFF_NAME
const BasicSequenceFeature nextFeature()
static void multipleKeyvals(const std::string &desc, std::map< std::string, std::string > &keyvals, const std::string &split=",", bool nested=true)
The base interface for sequence features.
virtual std::set< std::string > getAttributeList() const =0
virtual const std::string & getType() const =0
virtual const std::string & getSource() const =0
virtual const size_t getEnd() const =0
virtual bool isStranded() const =0
virtual const std::string & getAttribute(const std::string &attribute) const =0
virtual const std::string & getId() const =0
virtual const size_t getStart() const =0
virtual const std::string & getSequenceId() const =0
virtual const double & getScore() const =0
virtual bool isNegativeStrand() 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)