bpp-seq-omics  2.4.1
MafSequence.h
Go to the documentation of this file.
1 //
2 // File: MafSequence.h
3 // Authors: Julien Dutheil
4 // Created: Tue Sep 07 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (2010)
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 _MAFSEQUENCE_H_
41 #define _MAFSEQUENCE_H_
42 
43 #include "../../Feature/SequenceFeature.h"
44 
47 #include <Bpp/Seq/SequenceTools.h>
48 
49 namespace bpp {
50 
64 {
65  private:
67  size_t begin_;
68  std::string species_;
69  std::string chromosome_;
70  char strand_;
71  size_t size_;
72  size_t srcSize_;
73 
74  public:
76  EdSymbolList<int>(alphabet),
77  EdIntSymbolList(alphabet),
78  SequenceWithAnnotation(alphabet), hasCoordinates_(false), begin_(0), species_(""), chromosome_(""), strand_(0), size_(0), srcSize_(0)
79  {}
80 
81  MafSequence(const std::string& name, const std::string& sequence, bool parseName = true, const Alphabet* alphabet = &AlphabetTools::DNA_ALPHABET):
82  EdSymbolList<int>(alphabet),
83  EdIntSymbolList(alphabet),
84  SequenceWithAnnotation(name, sequence, alphabet), hasCoordinates_(false), begin_(0), species_(""), chromosome_(""), strand_(0), size_(0), srcSize_(0)
85  {
87  if (parseName)
89  }
90 
91  MafSequence(const std::string& name, const std::string& sequence, size_t begin, char strand, size_t srcSize, bool parseName = true, const Alphabet* alphabet = &AlphabetTools::DNA_ALPHABET) :
92  EdSymbolList<int>(alphabet),
93  EdIntSymbolList(alphabet),
94  SequenceWithAnnotation(name, sequence, alphabet), hasCoordinates_(true), begin_(begin), species_(""), chromosome_(""), strand_(strand), size_(0), srcSize_(srcSize)
95  {
97  if (parseName)
99  }
100 
101  MafSequence* clone() const {
102  return new MafSequence(*this);
103  }
104 
105  MafSequence* cloneMeta() const {
106  return new MafSequence(getName(), std::string(), begin_, strand_, srcSize_, true);
107  }
108 
110 
111  public:
112  bool hasCoordinates() const { return hasCoordinates_; }
113 
114  void removeCoordinates() { hasCoordinates_ = false; begin_ = 0; }
115 
116  size_t start() const {
117  if (hasCoordinates_) return begin_;
118  else throw Exception("MafSequence::start(). Sequence " + getName() + " does not have coordinates.");
119  }
120 
121  size_t stop() const {
122  if (hasCoordinates_) return begin_ + size_;
123  else throw Exception("MafSequence::stop(). Sequence " + getName() + " does not have coordinates.");
124  }
125 
131  Range<size_t> getRange(bool origin = true) const {
132  if (hasCoordinates_) {
133  if (origin && strand_ == '-') {
134  return Range<size_t>(srcSize_ - stop(), srcSize_ - start());
135  } else {
136  return Range<size_t>(start(), stop());
137  }
138  }
139  else throw Exception("MafSequence::getRange(). Sequence " + getName() + " does not have coordinates.");
140  }
141 
142  void setName(const std::string& name) {
143  try {
145  } catch (Exception& e) {
146  species_ = "";
147  chromosome_ = "";
148  }
150  }
151 
152  static void splitNameIntoSpeciesAndChromosome(const std::string& name, std::string& species, std::string& chr) {
153  size_t pos = name.find(".");
154  if (pos != std::string::npos) {
155  chr = name.substr(pos + 1);
156  species = name.substr(0, pos);
157  } else {
158  throw Exception("MafSequence::splitNameIntospeciesAndChromosome(). Invalid sequence name: " + name);
159  }
160  }
161 
162  const std::string& getSpecies() const { return species_; }
163 
164  const std::string& getChromosome() const { return chromosome_; }
165 
166  char getStrand() const { return strand_; }
167 
168  size_t getGenomicSize() const { return size_; }
169 
170  size_t getSrcSize() const { return srcSize_; }
171 
172  void setStart(size_t begin) { begin_ = begin; hasCoordinates_ = true; }
173 
174  void setChromosome(const std::string& chr) {
175  chromosome_ = chr;
177  }
178 
179  void setSpecies(const std::string& species) {
180  species_ = species;
182  }
183 
184  void setStrand(char s) { strand_ = s; }
185 
186  void setSrcSize(size_t srcSize) { srcSize_ = srcSize; }
187 
188  std::string getDescription() const { return getName() + strand_ + ":" + (hasCoordinates_ ? TextTools::toString(start()) + "-" + TextTools::toString(stop()) : "?-?"); }
189 
197  MafSequence* subSequence(size_t startAt, size_t length) const;
198 
199  private:
208 };
209 
210 } // end of namespace bpp.
211 
212 #endif //_MAFSEQUENCE_H_
static const DNA DNA_ALPHABET
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:64
std::string getDescription() const
Definition: MafSequence.h:188
void setChromosome(const std::string &chr)
Definition: MafSequence.h:174
void setName(const std::string &name)
Definition: MafSequence.h:142
MafSequence * clone() const
Definition: MafSequence.h:101
void afterSequenceChanged(const IntSymbolListEditionEvent &event)
Definition: MafSequence.h:201
void afterSequenceDeleted(const IntSymbolListDeletionEvent &event)
Definition: MafSequence.h:205
const std::string & getChromosome() const
Definition: MafSequence.h:164
const std::string & getSpecies() const
Definition: MafSequence.h:162
void setSrcSize(size_t srcSize)
Definition: MafSequence.h:186
void setStart(size_t begin)
Definition: MafSequence.h:172
void removeCoordinates()
Definition: MafSequence.h:114
size_t start() const
Definition: MafSequence.h:116
void setStrand(char s)
Definition: MafSequence.h:184
size_t stop() const
Definition: MafSequence.h:121
void beforeSequenceDeleted(const IntSymbolListDeletionEvent &event)
Definition: MafSequence.h:204
bool hasCoordinates() const
Definition: MafSequence.h:112
static void splitNameIntoSpeciesAndChromosome(const std::string &name, std::string &species, std::string &chr)
Definition: MafSequence.h:152
char getStrand() const
Definition: MafSequence.h:166
size_t getSrcSize() const
Definition: MafSequence.h:170
std::string species_
Definition: MafSequence.h:68
MafSequence(const std::string &name, const std::string &sequence, size_t begin, char strand, size_t srcSize, bool parseName=true, const Alphabet *alphabet=&AlphabetTools::DNA_ALPHABET)
Definition: MafSequence.h:91
Range< size_t > getRange(bool origin=true) const
Definition: MafSequence.h:131
std::string chromosome_
Definition: MafSequence.h:69
MafSequence * subSequence(size_t startAt, size_t length) const
Extract a sub-sequence.
Definition: MafSequence.cpp:48
void afterSequenceInserted(const IntSymbolListInsertionEvent &event)
Definition: MafSequence.h:203
void beforeSequenceSubstituted(const IntSymbolListSubstitutionEvent &event)
Definition: MafSequence.h:206
MafSequence(const Alphabet *alphabet=&AlphabetTools::DNA_ALPHABET)
Definition: MafSequence.h:75
MafSequence * cloneMeta() const
Definition: MafSequence.h:105
void setSpecies(const std::string &species)
Definition: MafSequence.h:179
size_t getGenomicSize() const
Definition: MafSequence.h:168
MafSequence(const std::string &name, const std::string &sequence, bool parseName=true, const Alphabet *alphabet=&AlphabetTools::DNA_ALPHABET)
Definition: MafSequence.h:81
void beforeSequenceInserted(const IntSymbolListInsertionEvent &event)
Definition: MafSequence.h:202
void afterSequenceSubstituted(const IntSymbolListSubstitutionEvent &event)
Definition: MafSequence.h:207
void beforeSequenceChanged(const IntSymbolListEditionEvent &event)
Definition: MafSequence.h:200
static size_t getNumberOfSites(const Sequence &seq)
void setName(const std::string &name)
const std::string & getName() const
std::string toString(T t)