1 // ***************************************************************************
2 // SamSequenceDictionary.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides methods for operating on a collection of SamSequence entries.
8 // *************************************************************************
10 #include <api/SamSequenceDictionary.h>
11 using namespace BamTools;
16 /*! \class BamTools::SamSequenceDictionary
17 \brief Container of SamSequence entries.
19 Provides methods for operating on a collection of SamSequence entries.
22 /*! \fn SamSequenceDictionary::SamSequenceDictionary(void)
25 SamSequenceDictionary::SamSequenceDictionary(void) { }
27 /*! \fn SamSequenceDictionary::SamSequenceDictionary(const SamSequenceDictionary& other)
28 \brief copy constructor
30 SamSequenceDictionary::SamSequenceDictionary(const SamSequenceDictionary& other)
31 : m_data(other.m_data)
34 /*! \fn SamSequenceDictionary::~SamSequenceDictionary(void)
37 SamSequenceDictionary::~SamSequenceDictionary(void) { }
39 /*! \fn void SamSequenceDictionary::Add(const SamSequence& sequence)
40 \brief Appends a sequence to the dictionary.
42 Duplicate entries are silently discarded.
44 \param[in] sequence entry to be added
46 void SamSequenceDictionary::Add(const SamSequence& sequence) {
47 if ( IsEmpty() || !Contains(sequence) )
48 m_data.push_back(sequence);
51 /*! \fn void SamSequenceDictionary::Add(const std::string& name, const int& length)
52 \brief Appends a sequence to the dictionary.
54 This is an overloaded function.
56 \param[in] name name of sequence entry to be added
57 \param[in] length length of sequence entry to be added
60 void SamSequenceDictionary::Add(const std::string& name, const int& length) {
61 Add( SamSequence(name, length) );
64 /*! \fn void SamSequenceDictionary::Add(const SamSequenceDictionary& sequences)
65 \brief Appends another sequence dictionary to this one
67 This is an overloaded function.
69 \param[in] sequences sequence dictionary to be appended
72 void SamSequenceDictionary::Add(const SamSequenceDictionary& sequences) {
73 SamSequenceConstIterator seqIter = sequences.ConstBegin();
74 SamSequenceConstIterator seqEnd = sequences.ConstEnd();
75 for ( ; seqIter != seqEnd; ++seqIter )
79 /*! \fn void SamSequenceDictionary::Add(const std::vector<SamSequence>& sequences)
80 \brief Appends multiple sequences to the dictionary.
82 This is an overloaded function.
84 \param[in] sequences entries to be added
87 void SamSequenceDictionary::Add(const std::vector<SamSequence>& sequences) {
88 vector<SamSequence>::const_iterator seqIter = sequences.begin();
89 vector<SamSequence>::const_iterator seqEnd = sequences.end();
90 for ( ; seqIter!= seqEnd; ++seqIter )
94 /*! \fn void SamSequenceDictionary::Add(const std::map<std::string, int>& sequenceMap)
95 \brief Appends multiple sequences to the dictionary.
97 This is an overloaded function.
99 \param[in] sequenceMap map of sequence entries (name => length) to be added
102 void SamSequenceDictionary::Add(const std::map<std::string, int>& sequenceMap) {
103 map<string, int>::const_iterator seqIter = sequenceMap.begin();
104 map<string, int>::const_iterator seqEnd = sequenceMap.end();
105 for ( ; seqIter != seqEnd; ++seqIter ) {
106 const string& name = (*seqIter).first;
107 const int& length = (*seqIter).second;
108 Add( SamSequence(name, length) );
112 /*! \fn SamSequenceIterator SamSequenceDictionary::Begin(void)
113 \return an STL iterator pointing to the first sequence
114 \sa ConstBegin(), End()
116 SamSequenceIterator SamSequenceDictionary::Begin(void) {
117 return m_data.begin();
120 /*! \fn SamSequenceConstIterator SamSequenceDictionary::Begin(void) const
121 \return an STL const_iterator pointing to the first sequence
123 This is an overloaded function.
125 \sa ConstBegin(), End()
127 SamSequenceConstIterator SamSequenceDictionary::Begin(void) const {
128 return m_data.begin();
131 /*! \fn void SamSequenceDictionary::Clear(void)
132 \brief Clears all sequence entries.
134 void SamSequenceDictionary::Clear(void) {
138 /*! \fn SamSequenceConstIterator SamSequenceDictionary::ConstBegin(void) const
139 \return an STL const_iterator pointing to the first sequence
140 \sa Begin(), ConstEnd()
142 SamSequenceConstIterator SamSequenceDictionary::ConstBegin(void) const {
143 return m_data.begin();
146 /*! \fn SamSequenceConstIterator SamSequenceDictionary::ConstEnd(void) const
147 \return an STL const_iterator pointing to the imaginary entry after the last sequence
148 \sa End(), ConstBegin()
150 SamSequenceConstIterator SamSequenceDictionary::ConstEnd(void) const {
154 /*! \fn bool SamSequenceDictionary::Contains(const std::string& sequenceName) const
155 \brief Returns true if dictionary contains sequence.
157 \param[in] sequenceName search for sequence matching this name
158 \return \c true if dictionary contains a sequence with this name
160 bool SamSequenceDictionary::Contains(const std::string& sequenceName) const {
161 return ( IndexOf(sequenceName) != (int)m_data.size() );
164 /*! \fn bool SamSequenceDictionary::Contains(const SamSequence& sequence) const
165 \brief Returns true if dictionary contains sequence (matches on name).
167 This is an overloaded function.
169 \param[in] sequence search for this sequence
170 \return \c true if dictionary contains sequence (matching on name)
172 bool SamSequenceDictionary::Contains(const SamSequence& sequence) const {
173 return ( IndexOf(sequence.Name) != (int)m_data.size() );
176 /*! \fn SamSequenceIterator SamSequenceDictionary::End(void)
177 \return an STL iterator pointing to the imaginary entry after the last sequence
178 \sa Begin(), ConstEnd()
180 SamSequenceIterator SamSequenceDictionary::End(void) {
184 /*! \fn SamSequenceConstIterator SamSequenceDictionary::End(void) const
185 \return an STL const_iterator pointing to the imaginary entry after the last sequence
187 This is an overloaded function.
189 \sa Begin(), ConstEnd()
191 SamSequenceConstIterator SamSequenceDictionary::End(void) const {
195 /*! \fn int SamSequenceDictionary::IndexOf(const std::string& name) const
197 \return index of sequence if found (matching on name). Otherwise, returns vector::size() (invalid index).
199 int SamSequenceDictionary::IndexOf(const std::string& name) const {
200 SamSequenceConstIterator begin = ConstBegin();
201 SamSequenceConstIterator iter = begin;
202 SamSequenceConstIterator end = ConstEnd();
203 for ( ; iter != end; ++iter ) {
204 const SamSequence& currentSeq = (*iter);
205 if ( currentSeq.Name == name )
208 return distance( begin, iter );
211 /*! \fn bool SamSequenceDictionary::IsEmpty(void) const
212 \brief Returns \c true if dictionary contains no sequences
215 bool SamSequenceDictionary::IsEmpty(void) const {
216 return m_data.empty();
219 /*! \fn void SamSequenceDictionary::Remove(const SamSequence& sequence)
220 \brief Removes sequence from dictionary, if found (matches on name).
222 This is an overloaded function.
224 \param[in] sequence SamSequence to remove (matching on name)
226 void SamSequenceDictionary::Remove(const SamSequence& sequence) {
227 Remove( sequence.Name );
230 /*! \fn void SamSequenceDictionary::Remove(const std::string& sequenceName)
231 \brief Removes sequence from dictionary, if found.
233 \param[in] sequenceName name of sequence to remove
236 void SamSequenceDictionary::Remove(const std::string& sequenceName) {
237 if ( Contains(sequenceName) )
238 m_data.erase( m_data.begin() + IndexOf(sequenceName) );
241 /*! \fn void SamSequenceDictionary::Remove(const std::vector<SamSequence>& sequences)
242 \brief Removes multiple sequences from dictionary.
244 This is an overloaded function.
246 \param[in] sequences sequences to remove
249 void SamSequenceDictionary::Remove(const std::vector<SamSequence>& sequences) {
250 vector<SamSequence>::const_iterator rgIter = sequences.begin();
251 vector<SamSequence>::const_iterator rgEnd = sequences.end();
252 for ( ; rgIter!= rgEnd; ++rgIter )
256 /*! \fn void SamSequenceDictionary::Remove(const std::vector<std::string>& sequenceNames)
257 \brief Removes multiple sequences from dictionary.
259 This is an overloaded function.
261 \param[in] sequenceNames names of the sequences to remove
264 void SamSequenceDictionary::Remove(const std::vector<std::string>& sequenceNames) {
265 vector<string>::const_iterator rgIter = sequenceNames.begin();
266 vector<string>::const_iterator rgEnd = sequenceNames.end();
267 for ( ; rgIter!= rgEnd; ++rgIter )
271 /*! \fn int SamSequenceDictionary::Size(void) const
272 \brief Returns number of sequences in dictionary.
275 int SamSequenceDictionary::Size(void) const {
276 return m_data.size();
279 /*! \fn SamSequence& SamSequenceDictionary::operator[](const std::string& sequenceName)
280 \brief Retrieves the modifiable SamSequence that matches \a sequenceName.
282 \note If the dictionary contains no sequence matching this name, this function inserts
283 a new one with this name (length:0), and returns a reference to it. If you want to avoid
284 this insertion behavior, check the result of Contains() before using this operator.
286 \param[in] sequenceName name of sequence to retrieve
287 \return a modifiable reference to the SamSequence associated with the name
289 SamSequence& SamSequenceDictionary::operator[](const std::string& sequenceName) {
291 // look up sequence ID
292 int index = IndexOf(sequenceName);
294 // if found, return sequence at index
295 if ( index != (int)m_data.size() )
296 return m_data[index];
298 // otherwise, append new sequence and return reference
300 m_data.push_back( SamSequence(sequenceName, 0) );
301 return m_data.back();