source: trunk/yat/omic/GFF.cc @ 2703

Last change on this file since 2703 was 2703, checked in by Peter, 11 years ago

merge release 0.8.1 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1// $Id: GFF.cc 2703 2012-03-12 06:43:10Z peter $
2
3/*
4  Copyright (C) 2011, 2012 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "GFF.h"
23
24#include "yat/utility/Exception.h"
25#include "yat/utility/split.h"
26#include "yat/utility/stl_utility.h"
27#include "yat/utility/yat_assert.h"
28
29#include <cassert>
30#include <istream>
31#include <map>
32#include <ostream>
33#include <sstream>
34#include <string>
35#include <vector>
36
37namespace theplu {
38namespace yat {
39namespace omic {
40
41  GFF::GFF(void) {}
42
43  GFF::~GFF(void) {}
44
45  const std::string& GFF::attribute(const std::string& key) const
46  { 
47    try {
48      return utility::get(attributes(), key); 
49    }
50    catch (utility::runtime_error& e) {
51      std::stringstream ss;
52      ss << "GFF attribute `" << key << "' not found";
53      throw utility::runtime_error(ss.str());
54    }
55    assert(false && "we should never reach here since we throw above");
56    // return some nonsense to avoid compiler warning
57    return vec_[0];
58  }
59
60  const std::map<std::string, std::string>& GFF::attributes(void) const
61  { 
62    if (attributes_.empty()) {
63      typedef std::map<std::string, std::string> StrMap;
64      // since we create attributes map lazily, i.e., first here and
65      // not already in getline we need to const_cast here. Prefer
66      // const_cast over declaring variable mutable; the latter
67      // would allow modifications in the entire class.
68      StrMap& a = const_cast<StrMap&>(attributes_);
69      create_attributes(a);
70    }
71    return attributes_; 
72  }
73
74
75  const std::string& GFF::end(void) const
76  { 
77    assert(4 < vec_.size());
78    return vec_[4];
79  }
80
81
82  const std::string& GFF::phase(void) const
83  { 
84    assert(7 < vec_.size());
85    return vec_[7];
86  }
87
88
89  const std::string& GFF::score(void) const
90  { 
91    assert(5 < vec_.size());
92    return vec_[5];
93  }
94
95
96  const std::string& GFF::seqid(void) const
97  { 
98    assert(0 < vec_.size());
99    return vec_[0];
100  }
101
102
103  const std::string& GFF::source(void) const
104  { 
105    assert(1 < vec_.size());
106    return vec_[1];
107  }
108
109
110  const std::string& GFF::start(void) const
111  { 
112    assert(3 < vec_.size());
113    return vec_[3];
114  }
115
116
117  const std::string& GFF::strand(void) const
118  { 
119    assert(6 < vec_.size());
120    return vec_[6];
121  }
122 
123 
124  const std::string& GFF::type(void) const
125  { 
126    assert(2 < vec_.size());
127    return vec_[2];
128  }
129   
130  const std::vector<std::string>& GFF::vec(void) const { return vec_; }
131
132  void GFF::create_attributes(std::map<std::string, std::string>& m) const
133  {
134    assert(8 < vec_.size());
135    std::vector<std::string> v;
136    utility::split(v, vec_[8], ';');
137   
138    for (size_t i=0; i<v.size(); ++i) 
139      add_attribute(m, v[i]);
140  }
141
142
143  std::ostream& operator<<(std::ostream& os, const GFF& gff)
144  {
145    if (gff.vec_.empty())
146      return os;
147    os << gff.vec()[0];
148    for (size_t i=1; i<gff.vec().size(); ++i)
149      os << "n" << gff.vec()[i];
150    return os;
151  }
152
153
154  bool getline(std::istream& is, GFF& gff)
155  {
156    std::string line;
157    if (!getline(is, line))
158      return false;
159    // ignore comment
160    size_t i = line.find('#');
161    if (i<std::string::npos) 
162      line.resize(i);
163    if (line.empty())
164      return getline(is, gff);
165    gff.vec_.clear();
166    utility::split(gff.vec_, line, '\t');
167    gff.attributes_.clear();
168    return true;
169  }
170
171}}}
Note: See TracBrowser for help on using the repository browser.