bpp-seq-omics  2.4.1
MafBlock.h
Go to the documentation of this file.
1 //
2 // File: MafBlock.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 _MAFBLOCK_H_
41 #define _MAFBLOCK_H_
42 
43 #include "MafSequence.h"
45 
46 #include <Bpp/Clonable.h>
47 
48 namespace bpp {
49 
55 class MafBlock:
56  public virtual Clonable
57 {
58  private:
59  double score_;
60  unsigned int pass_;
62  std::map<std::string, Clonable*> properties_;
63 
64  public:
66  score_(log(0)),
67  pass_(0),
68  alignment_(&AlphabetTools::DNA_ALPHABET),
69  properties_()
70  {}
71 
72  MafBlock(const MafBlock& block):
73  score_(block.score_),
74  pass_(block.pass_),
75  alignment_(block.alignment_),
76  properties_()
77  {
78  std::map<std::string, Clonable*>::const_iterator it;
79  for (it = block.properties_.begin(); it != block.properties_.end(); ++it) {
80  properties_[it->first] = it->second->clone();
81  }
82  }
83 
84  MafBlock& operator=(const MafBlock& block)
85  {
86  score_ = block.score_;
87  pass_ = block.pass_;
88  alignment_ = block.alignment_;
90  std::map<std::string, Clonable*>::const_iterator it;
91  for (it = block.properties_.begin(); it != block.properties_.end(); ++it) {
92  properties_[it->first] = it->second->clone();
93  }
94  return *this;
95  }
96 
97  MafBlock* clone() const { return new MafBlock(*this); }
98 
99  virtual ~MafBlock() { deleteProperties_(); }
100 
101  public:
102  void setScore(double score) { score_ = score; }
103  void setPass(unsigned int pass) { pass_ = pass; }
104 
105  double getScore() const { return score_; }
106  unsigned int getPass() const { return pass_; }
107 
110 
112 
113  size_t getNumberOfSites() const { return alignment_.getNumberOfSites(); }
114 
115  void addSequence(const MafSequence& sequence) { alignment_.addSequence(sequence, false); }
116 
117  bool hasSequence(const std::string& name) const {
118  return getAlignment().hasSequence(name);
119  }
120 
121  const MafSequence& getSequence(const std::string& name) const {
122  return dynamic_cast<const MafSequence&>(getAlignment().getSequence(name));
123  }
124 
125  const MafSequence& getSequence(size_t i) const {
126  return dynamic_cast<const MafSequence&>(getAlignment().getSequence(i));
127  }
128 
129  bool hasSequenceForSpecies(const std::string& species) const {
130  for (size_t i = 0; i < getNumberOfSequences(); ++i) {
131  const MafSequence& seq = getSequence(i);
132  if (seq.getSpecies() == species)
133  return true;
134  }
135  return false;
136  }
137 
138  //Return the first sequence with the species name.
139  const MafSequence& getSequenceForSpecies(const std::string& species) const {
140  for (size_t i = 0; i < getNumberOfSequences(); ++i) {
141  const MafSequence& seq = getSequence(i);
142  if (seq.getSpecies() == species)
143  return seq;
144  }
145  throw SequenceNotFoundException("MafBlock::getSequenceForSpecies. No sequence with the given species name in this block.", species);
146  }
147 
148  //Return all sequences with the species name.
149  std::vector<const MafSequence*> getSequencesForSpecies(const std::string& species) const {
150  std::vector<const MafSequence*> selection;
151  for (size_t i = 0; i < getNumberOfSequences(); ++i) {
152  const MafSequence* seq = &getSequence(i);
153  if (seq->getSpecies() == species)
154  selection.push_back(seq);
155  }
156  return selection;
157  }
158 
162  std::vector<std::string> getSpeciesList() const {
163  std::vector<std::string> lst;
164  for (size_t i = 0; i < getNumberOfSequences(); ++i) {
165  lst.push_back(getSequence(i).getSpecies());
166  }
167  return lst;
168  }
169 
171  //This is a bit of a trick, but avoid useless recopies.
172  //It is safe here because the AlignedSequenceContainer is fully encapsulated.
173  //It would not work if a VectorSiteContainer was used.
174  const_cast<MafSequence&>(getSequence(i)).removeCoordinates();
175  }
176 
177  std::string getDescription() const {
178  std::string desc;
180  return desc;
181  }
182 
187  bool hasProperty(const std::string& property) const
188  {
189  std::map<std::string, Clonable*>::const_iterator it = properties_.find(property);
190  return it != properties_.end();
191  }
192 
200  const Clonable& getProperty(const std::string& property) const
201  {
202  std::map<std::string, Clonable*>::const_iterator it = properties_.find(property);
203  if (it == properties_.end())
204  throw Exception("MafBlock::getProperty. No data for property: " + property + " in block.");
205  return *it->second;
206  }
207 
214  void deleteProperty(const std::string& property)
215  {
216  std::map<std::string, Clonable*>::iterator it = properties_.find(property);
217  if (it == properties_.end())
218  throw Exception("MafBlock::deleteProperty. No data for property: " + property + " in block.");
219  delete it->second;
220  properties_.erase(it);
221  }
222 
231  void setProperty(const std::string& property, Clonable* data)
232  {
233  if (!data)
234  throw Exception("MafBlock::setProperty. Pointer to data is NULL.");
235  if (hasProperty(property))
236  deleteProperty(property);
237  properties_[property] = data;
238  }
239 
240  private:
242  {
243  std::map<std::string, Clonable*>::iterator it;
244  for (it = properties_.begin(); it != properties_.end(); ++it) {
245  delete it->second;
246  }
247  properties_.clear();
248  }
249 };
250 
251 } // end of namespace bpp.
252 
253 #endif //_MAFBLOCK_H_
void addSequence(const Sequence &sequence, bool checkName=true)
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:57
unsigned int getPass() const
Definition: MafBlock.h:106
MafBlock & operator=(const MafBlock &block)
Definition: MafBlock.h:84
void setScore(double score)
Definition: MafBlock.h:102
void deleteProperties_()
Definition: MafBlock.h:241
MafBlock * clone() const
Definition: MafBlock.h:97
void setPass(unsigned int pass)
Definition: MafBlock.h:103
size_t getNumberOfSequences() const
Definition: MafBlock.h:111
std::vector< const MafSequence * > getSequencesForSpecies(const std::string &species) const
Definition: MafBlock.h:149
double getScore() const
Definition: MafBlock.h:105
bool hasProperty(const std::string &property) const
Definition: MafBlock.h:187
size_t getNumberOfSites() const
Definition: MafBlock.h:113
const Clonable & getProperty(const std::string &property) const
Get the data associated to a query property.
Definition: MafBlock.h:200
const MafSequence & getSequence(const std::string &name) const
Definition: MafBlock.h:121
double score_
Definition: MafBlock.h:59
std::map< std::string, Clonable * > properties_
Definition: MafBlock.h:62
virtual ~MafBlock()
Definition: MafBlock.h:99
std::string getDescription() const
Definition: MafBlock.h:177
AlignedSequenceContainer & getAlignment()
Definition: MafBlock.h:108
void addSequence(const MafSequence &sequence)
Definition: MafBlock.h:115
void removeCoordinatesFromSequence(size_t i)
Definition: MafBlock.h:170
bool hasSequence(const std::string &name) const
Definition: MafBlock.h:117
unsigned int pass_
Definition: MafBlock.h:60
bool hasSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:129
void deleteProperty(const std::string &property)
Delete the data associated to a query property.
Definition: MafBlock.h:214
std::vector< std::string > getSpeciesList() const
Definition: MafBlock.h:162
void setProperty(const std::string &property, Clonable *data)
Set the data associated to a query property.
Definition: MafBlock.h:231
const AlignedSequenceContainer & getAlignment() const
Definition: MafBlock.h:109
MafBlock(const MafBlock &block)
Definition: MafBlock.h:72
AlignedSequenceContainer alignment_
Definition: MafBlock.h:61
const MafSequence & getSequence(size_t i) const
Definition: MafBlock.h:125
const MafSequence & getSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:139
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:64
const std::string & getSpecies() const
Definition: MafSequence.h:162
size_t getNumberOfSequences() const
const Sequence & getSequence(const std::string &name) const
bool hasSequence(const std::string &name) const
std::string toString(T t)