bpp-seq  2.1.0
Bpp/Seq/SymbolList.cpp
Go to the documentation of this file.
00001 //
00002 // File: SymbolList.cpp
00003 // Created by: Julien Dutheil
00004 // Created on: Fri Apr 9 2005
00005 //
00006 
00007 /*
00008 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
00009 
00010 This software is a computer program whose purpose is to provide classes
00011 for sequences analysis.
00012 
00013 This software is governed by the CeCILL  license under French law and
00014 abiding by the rules of distribution of free software.  You can  use, 
00015 modify and/ or redistribute the software under the terms of the CeCILL
00016 license as circulated by CEA, CNRS and INRIA at the following URL
00017 "http://www.cecill.info". 
00018 
00019 As a counterpart to the access to the source code and  rights to copy,
00020 modify and redistribute granted by the license, users are provided only
00021 with a limited warranty  and the software's author,  the holder of the
00022 economic rights,  and the successive licensors  have only  limited
00023 liability. 
00024 
00025 In this respect, the user's attention is drawn to the risks associated
00026 with loading,  using,  modifying and/or developing or reproducing the
00027 software by the user in light of its specific status of free software,
00028 that may mean  that it is complicated to manipulate,  and  that  also
00029 therefore means  that it is reserved for developers  and  experienced
00030 professionals having in-depth computer knowledge. Users are therefore
00031 encouraged to load and test the software's suitability as regards their
00032 requirements in conditions enabling the security of their systems and/or 
00033 data to be ensured and,  more generally, to use and operate it in the 
00034 same conditions as regards security. 
00035 
00036 The fact that you are presently reading this means that you have had
00037 knowledge of the CeCILL license and that you accept its terms.
00038 */
00039 
00040 #include "SymbolList.h"
00041 #include "StringSequenceTools.h"
00042 
00043 using namespace bpp;
00044 
00045 using namespace std;
00046 
00047 /****************************************************************************************/
00048 
00049 BasicSymbolList::BasicSymbolList(const std::vector<string>& list, const Alphabet* alpha) throw (BadCharException) :
00050   alphabet_(alpha), content_()
00051 {
00052   setContent(list);
00053 }
00054 
00055 BasicSymbolList::BasicSymbolList(const std::vector<int>& list, const Alphabet* alpha) throw (BadIntException) :
00056   alphabet_(alpha), content_()
00057 {
00058   setContent(list);
00059 }
00060 
00061 /****************************************************************************************/
00062 
00063 BasicSymbolList::BasicSymbolList(const SymbolList& list):
00064   alphabet_(list.getAlphabet()), content_(list.getContent()) {}
00065 
00066 BasicSymbolList::BasicSymbolList(const BasicSymbolList& list):
00067   alphabet_(list.getAlphabet()), content_(list.getContent()) {}
00068 
00069 BasicSymbolList& BasicSymbolList::operator=(const SymbolList& list)
00070 {
00071   content_  = list.getContent();
00072   alphabet_ = list.getAlphabet();
00073   return *this;
00074 }
00075 
00076 BasicSymbolList& BasicSymbolList::operator=(const BasicSymbolList& list)
00077 {
00078   content_  = list.getContent();
00079   alphabet_ = list.getAlphabet();
00080   return *this;
00081 }
00082 
00083 /****************************************************************************************/
00084 
00085 void BasicSymbolList::setContent(const vector<string>& list) throw (BadCharException)
00086 {
00087   // Check list for incorrect characters
00088   vector<int> coded(list.size());
00089   for (size_t i = 0; i < list.size(); i++)
00090     if(!alphabet_->isCharInAlphabet(list[i])) throw BadCharException(list[i], "BasicSymbolList::setContent", alphabet_);
00091 
00092   for (size_t i = 0; i < list.size(); i++) 
00093     coded[i] = alphabet_->charToInt(list[i]);
00094   
00095   //BasicSymbolList is valid:
00096   content_ = coded;
00097 };
00098 
00099 /****************************************************************************************/
00100 
00101 void BasicSymbolList::setContent(const vector<int>& list) throw (BadIntException)
00102 {
00103   // Check list for incorrect characters
00104   for (size_t i = 0; i < list.size(); i++)
00105     if(!alphabet_->isIntInAlphabet(list[i]))
00106       throw BadIntException(list[i], "BasicSymbolList::setContent", alphabet_);
00107   
00108   //Sequence is valid:
00109   content_ = list;
00110 };
00111 
00112 /****************************************************************************************/
00113 
00114 string BasicSymbolList::toString() const
00115 {
00116   return StringSequenceTools::decodeSequence(content_, alphabet_);
00117 };
00118 
00119 /****************************************************************************************/
00120 
00121 void BasicSymbolList::addElement(const string& c) throw (BadCharException)
00122 {
00123   content_.push_back(alphabet_->charToInt(c));
00124 }
00125 
00126 /****************************************************************************************/
00127 
00128 void BasicSymbolList::addElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
00129 {
00130   if(pos >= content_.size()) throw IndexOutOfBoundsException("BasicSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
00131   content_.insert(content_.begin() + pos, alphabet_->charToInt(c));
00132 }
00133 
00134 /****************************************************************************************/
00135 
00136 void BasicSymbolList::setElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
00137 {
00138   if(pos >= content_.size())
00139     throw IndexOutOfBoundsException("BasicSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
00140   content_[pos] = alphabet_->charToInt(c);
00141 }
00142 
00143 /****************************************************************************************/
00144 
00145 string BasicSymbolList::getChar(size_t pos) const throw (IndexOutOfBoundsException)
00146 {
00147   if(pos >= content_.size())
00148     throw IndexOutOfBoundsException("BasicSymbolList::getChar. Invalid position.", pos, 0, size() - 1);
00149   string c = "";
00150   try
00151   {
00152     c = alphabet_->intToChar(content_[pos]);
00153   }
00154   catch(BadIntException bie)
00155   {
00156     //This should never happen!
00157   }
00158   return c;
00159 }
00160 
00161 /****************************************************************************************/
00162 
00163 void BasicSymbolList::deleteElement(size_t pos) throw (IndexOutOfBoundsException)
00164 {
00165   if(pos >= content_.size())
00166     throw IndexOutOfBoundsException("BasicSymbolList::deleteElement. Invalid position.", pos, 0, size() - 1);
00167   content_.erase(content_.begin() + pos);
00168 }
00169 
00170 /****************************************************************************************/
00171 
00172 void BasicSymbolList::deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException)
00173 {
00174   if (pos + len > content_.size())
00175     throw IndexOutOfBoundsException("BasicSymbolList::deleteElements. Invalid position.", pos + len, 0, size() - 1);
00176    content_.erase(content_.begin() + pos, content_.begin() + pos + len);
00177 }
00178 
00179 /****************************************************************************************/
00180 
00181 void BasicSymbolList::addElement(int v) throw (BadIntException)
00182 {
00183   //test:
00184   alphabet_->intToChar(v);
00185   content_.push_back(v);
00186 }
00187 
00188 /****************************************************************************************/
00189 
00190 void BasicSymbolList::addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
00191 {
00192   //test:
00193   if(pos >= content_.size())
00194     throw IndexOutOfBoundsException("BasicSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
00195   alphabet_->intToChar(v);
00196   content_.insert(content_.begin() + pos, v);
00197 }
00198 
00199 /****************************************************************************************/
00200 
00201 void BasicSymbolList::setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
00202 {
00203   //test:
00204   if(pos >= content_.size())
00205     throw IndexOutOfBoundsException("BasicSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
00206   alphabet_->intToChar(v);
00207   content_[pos] = v;
00208 }
00209 
00210 /****************************************************************************************/
00211 
00212 int BasicSymbolList::getValue(size_t pos) const throw (IndexOutOfBoundsException)
00213 {
00214   if(pos >= content_.size())
00215     throw IndexOutOfBoundsException("BasicSymbolList::getValue. Invalid position.", pos, 0, size() - 1);
00216   return content_[pos];
00217 }
00218 
00219 /****************************************************************************************/
00220 
00221 
00222 /****************************************************************************************/
00223 
00224 EdSymbolList::EdSymbolList(const std::vector<string>& list, const Alphabet* alpha) throw (BadCharException) :
00225   alphabet_(alpha), propagateEvents_(true), content_(), listeners_()
00226 {
00227   setContent(list);
00228 }
00229 
00230 EdSymbolList::EdSymbolList(const std::vector<int>& list, const Alphabet* alpha) throw (BadIntException) :
00231   alphabet_(alpha), propagateEvents_(true), content_(), listeners_()
00232 {
00233   setContent(list);
00234 }
00235 
00236 /****************************************************************************************/
00237 
00238 EdSymbolList::EdSymbolList(const SymbolList& list):
00239   alphabet_(list.getAlphabet()), propagateEvents_(true), content_(list.getContent()), listeners_() {}
00240 
00241 EdSymbolList::EdSymbolList(const EdSymbolList& list):
00242   alphabet_(list.getAlphabet()), propagateEvents_(list.propagateEvents_), content_(list.getContent()), listeners_(list.listeners_)
00243 {
00244   for (size_t i = 0; i < listeners_.size(); ++i)
00245     if (!list.listeners_[i]->isShared())
00246       listeners_[i] = dynamic_cast<SymbolListListener*>(list.listeners_[i]->clone());
00247 }
00248 
00249 EdSymbolList& EdSymbolList::operator=(const SymbolList& list)
00250 {
00251   content_         = list.getContent();
00252   alphabet_        = list.getAlphabet();
00253   propagateEvents_ = true;
00254   for (size_t i = 0; i < listeners_.size(); ++i)
00255     if (!listeners_[i]->isShared())
00256      delete listeners_[i];
00257   listeners_.clear();
00258   return *this;
00259 }
00260 
00261 EdSymbolList& EdSymbolList::operator=(const EdSymbolList& list)
00262 {
00263   content_         = list.getContent();
00264   alphabet_        = list.getAlphabet();
00265   propagateEvents_ = list.propagateEvents_;
00266   for (size_t i = 0; i < listeners_.size(); ++i)
00267     delete listeners_[i];
00268   listeners_ = list.listeners_;
00269   for (size_t i = 0; i < listeners_.size(); ++i)
00270     if (!list.listeners_[i]->isShared())
00271       listeners_[i] = dynamic_cast<SymbolListListener*>(list.listeners_[i]->clone());
00272   return *this;
00273 }
00274 
00275 /****************************************************************************************/
00276 
00277 void EdSymbolList::setContent(const vector<string>& list) throw (BadCharException)
00278 {
00279   SymbolListEditionEvent event(this);
00280   fireBeforeSequenceChanged(event);
00281 
00282   // Check list for incorrect characters
00283   vector<int> coded(list.size());
00284   for (size_t i = 0; i < list.size(); i++)
00285     if (!alphabet_->isCharInAlphabet(list[i])) throw BadCharException(list[i], "EdSymbolList::setContent", alphabet_);
00286   
00287   for (size_t i = 0; i < list.size(); i++) 
00288     coded[i] = alphabet_->charToInt(list[i]);
00289   
00290   //SymbolList is valid:
00291   content_ = coded;
00292   fireAfterSequenceChanged(event);
00293 };
00294 
00295 /****************************************************************************************/
00296 
00297 void EdSymbolList::setContent(const vector<int>& list) throw (BadIntException)
00298 {
00299   SymbolListEditionEvent event(this);
00300   fireBeforeSequenceChanged(event);
00301 
00302   // Check list for incorrect characters
00303   for (size_t i = 0; i < list.size(); i++)
00304     if(!alphabet_->isIntInAlphabet(list[i]))
00305       throw BadIntException(list[i], "EdSymbolList::setContent", alphabet_);
00306   
00307   //Sequence is valid:
00308   content_ = list;
00309   fireAfterSequenceChanged(event);
00310 };
00311 
00312 /****************************************************************************************/
00313 
00314 string EdSymbolList::toString() const
00315 {
00316   return StringSequenceTools::decodeSequence(content_, alphabet_);
00317 };
00318 
00319 /****************************************************************************************/
00320 
00321 void EdSymbolList::addElement(const string& c) throw (BadCharException)
00322 {
00323   SymbolListInsertionEvent event(this, size(), 1);
00324   fireBeforeSequenceInserted(event);
00325   content_.push_back(alphabet_->charToInt(c));
00326   fireAfterSequenceInserted(event);
00327 }
00328 
00329 /****************************************************************************************/
00330 
00331 void EdSymbolList::addElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
00332 {
00333   if (pos >= content_.size()) throw IndexOutOfBoundsException("EdSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
00334   SymbolListInsertionEvent event(this, pos, 1);
00335   fireBeforeSequenceInserted(event);
00336   content_.insert(content_.begin() + pos, alphabet_->charToInt(c));
00337   fireAfterSequenceInserted(event);
00338 }
00339 
00340 /****************************************************************************************/
00341 
00342 void EdSymbolList::setElement(size_t pos, const string& c) throw (BadCharException, IndexOutOfBoundsException)
00343 {
00344   if (pos >= content_.size())
00345     throw IndexOutOfBoundsException("EdSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
00346   SymbolListSubstitutionEvent event(this, pos, pos);
00347   fireBeforeSequenceSubstituted(event);
00348   content_[pos] = alphabet_->charToInt(c);
00349   fireAfterSequenceSubstituted(event);
00350 }
00351 
00352 /****************************************************************************************/
00353 
00354 string EdSymbolList::getChar(size_t pos) const throw (IndexOutOfBoundsException)
00355 {
00356   if (pos >= content_.size())
00357     throw IndexOutOfBoundsException("EdSymbolList::getChar. Invalid position.", pos, 0, size() - 1);
00358   string c = "";
00359   try {
00360     c = alphabet_->intToChar(content_[pos]);
00361   } catch(BadIntException bie) {
00362     //This should never happen!
00363   }
00364   return c;
00365 }
00366 
00367 /****************************************************************************************/
00368 
00369 void EdSymbolList::deleteElement(size_t pos) throw (IndexOutOfBoundsException)
00370 {
00371   if (pos >= content_.size())
00372     throw IndexOutOfBoundsException("EdSymbolList::deleteElement. Invalid position.", pos, 0, size() - 1);
00373   SymbolListDeletionEvent event(this, pos, 1);
00374   fireBeforeSequenceDeleted(event);
00375   content_.erase(content_.begin() + pos);
00376   fireAfterSequenceDeleted(event);
00377 }
00378 
00379 /****************************************************************************************/
00380 
00381 void EdSymbolList::deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException)
00382 {
00383   if(pos + len > content_.size())
00384     throw IndexOutOfBoundsException("EdSymbolList::deleteElements. Invalid position.", pos + len, 0, size() - 1);
00385   SymbolListDeletionEvent event(this, pos, len);
00386   fireBeforeSequenceDeleted(event);
00387   content_.erase(content_.begin() + pos, content_.begin() + pos + len);
00388   fireAfterSequenceDeleted(event);
00389 }
00390 
00391 
00392 /****************************************************************************************/
00393 
00394 void EdSymbolList::addElement(int v) throw (BadIntException)
00395 {
00396   SymbolListInsertionEvent event(this, size(), 1);
00397   fireBeforeSequenceInserted(event);
00398   //test:
00399   alphabet_->intToChar(v);
00400   content_.push_back(v);
00401   fireAfterSequenceInserted(event);
00402 }
00403 
00404 /****************************************************************************************/
00405 
00406 void EdSymbolList::addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
00407 {
00408   //test:
00409   if (pos >= content_.size())
00410     throw IndexOutOfBoundsException("EdSymbolList::addElement. Invalid position.", pos, 0, size() - 1);
00411   SymbolListInsertionEvent event(this, pos, 1);
00412   fireBeforeSequenceInserted(event);
00413   alphabet_->intToChar(v);
00414   content_.insert(content_.begin() + pos, v);
00415   fireAfterSequenceInserted(event);
00416 }
00417 
00418 /****************************************************************************************/
00419 
00420 void EdSymbolList::setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException)
00421 {
00422   //test:
00423   if (pos >= content_.size())
00424     throw IndexOutOfBoundsException("EdSymbolList::setElement. Invalid position.", pos, 0, size() - 1);
00425   SymbolListSubstitutionEvent event(this, pos, pos);
00426   fireBeforeSequenceSubstituted(event);
00427   alphabet_->intToChar(v);
00428   content_[pos] = v;
00429   fireAfterSequenceSubstituted(event);
00430 }
00431 
00432 /****************************************************************************************/
00433 
00434 int EdSymbolList::getValue(size_t pos) const throw (IndexOutOfBoundsException)
00435 {
00436   if (pos >= content_.size())
00437     throw IndexOutOfBoundsException("EdSymbolList::getValue. Invalid position.", pos, 0, size() - 1);
00438   return content_[pos];
00439 }
00440 
00441 /****************************************************************************************/
00442 
 All Classes Namespaces Files Functions Variables Typedefs Friends