1 // ***************************************************************************
2 // SamSequenceDictionary.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 18 April 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides methods for operating on a collection of SamSequence entries.
9 // *************************************************************************
11 #include <api/SamSequenceDictionary.h>
12 using namespace BamTools;
17 /*! \class BamTools::SamSequenceDictionary
18 \brief Container of SamSequence entries.
20 Provides methods for operating on a collection of SamSequence entries.
23 /*! \fn SamSequenceDictionary::SamSequenceDictionary(void)
26 SamSequenceDictionary::SamSequenceDictionary(void) { }
28 /*! \fn SamSequenceDictionary::SamSequenceDictionary(const SamSequenceDictionary& other)
29 \brief copy constructor
31 SamSequenceDictionary::SamSequenceDictionary(const SamSequenceDictionary& other)
32 : m_data(other.m_data)
35 /*! \fn SamSequenceDictionary::~SamSequenceDictionary(void)
38 SamSequenceDictionary::~SamSequenceDictionary(void) { }
40 /*! \fn void SamSequenceDictionary::Add(const SamSequence& sequence)
41 \brief Adds a sequence to the dictionary.
43 Duplicate entries are silently discarded.
45 \param sequence entry to be added
47 void SamSequenceDictionary::Add(const SamSequence& sequence) {
49 // TODO: report error on attempted duplicate?
51 if ( IsEmpty() || !Contains(sequence) )
52 m_data.push_back(sequence);
55 /*! \fn void SamSequenceDictionary::Add(const std::string& name, const int& length)
56 \brief Adds a sequence to the dictionary.
58 This is an overloaded function.
60 \param name name of sequence entry to be added
61 \param length length of sequence entry to be added
64 void SamSequenceDictionary::Add(const std::string& name, const int& length) {
65 Add( SamSequence(name, length) );
68 /*! \fn void SamSequenceDictionary::Add(const std::vector<SamSequence>& sequences)
69 \brief Adds multiple sequences to the dictionary.
71 This is an overloaded function.
73 \param sequences entries to be added
76 void SamSequenceDictionary::Add(const std::vector<SamSequence>& sequences) {
77 vector<SamSequence>::const_iterator seqIter = sequences.begin();
78 vector<SamSequence>::const_iterator seqEnd = sequences.end();
79 for ( ; seqIter!= seqEnd; ++seqIter )
83 /*! \fn void SamSequenceDictionary::Add(const std::map<std::string, int>& sequenceMap)
84 \brief Adds multiple sequences to the dictionary.
86 This is an overloaded function.
88 \param sequenceMap map of sequence entries (name => length) to be added
91 void SamSequenceDictionary::Add(const std::map<std::string, int>& sequenceMap) {
92 map<string, int>::const_iterator seqIter = sequenceMap.begin();
93 map<string, int>::const_iterator seqEnd = sequenceMap.end();
94 for ( ; seqIter != seqEnd; ++seqIter ) {
95 const string& name = (*seqIter).first;
96 const int& length = (*seqIter).second;
97 Add( SamSequence(name, length) );
101 /*! \fn SamSequenceIterator SamSequenceDictionary::Begin(void)
102 \return an STL iterator pointing to the first sequence
103 \sa ConstBegin(), End()
105 SamSequenceIterator SamSequenceDictionary::Begin(void) {
106 return m_data.begin();
109 /*! \fn SamSequenceConstIterator SamSequenceDictionary::Begin(void) const
110 \return an STL const_iterator pointing to the first sequence
112 This is an overloaded function.
114 \sa ConstBegin(), End()
116 SamSequenceConstIterator SamSequenceDictionary::Begin(void) const {
117 return m_data.begin();
120 /*! \fn void SamSequenceDictionary::Clear(void)
121 \brief Clears all sequence entries.
123 void SamSequenceDictionary::Clear(void) {
127 /*! \fn SamSequenceConstIterator SamSequenceDictionary::ConstBegin(void) const
128 \return an STL const_iterator pointing to the first sequence
129 \sa Begin(), ConstEnd()
131 SamSequenceConstIterator SamSequenceDictionary::ConstBegin(void) const {
132 return m_data.begin();
135 /*! \fn SamSequenceConstIterator SamSequenceDictionary::ConstEnd(void) const
136 \return an STL const_iterator pointing to the imaginary entry after the last sequence
137 \sa End(), ConstBegin()
139 SamSequenceConstIterator SamSequenceDictionary::ConstEnd(void) const {
143 /*! \fn bool SamSequenceDictionary::Contains(const std::string& sequenceName) const
144 \brief Returns true if dictionary contains sequence.
145 \param sequenceName search for sequence matching this name
146 \return \c true if dictionary contains a sequence with this name
148 bool SamSequenceDictionary::Contains(const std::string& sequenceName) const {
149 return ( IndexOf(sequenceName) != (int)m_data.size() );
152 /*! \fn bool SamSequenceDictionary::Contains(const SamSequence& sequence) const
153 \brief Returns true if dictionary contains sequence (matches on name).
155 This is an overloaded function.
157 \param sequence search for this sequence
158 \return \c true if dictionary contains sequence (matching on name)
160 bool SamSequenceDictionary::Contains(const SamSequence& sequence) const {
161 return ( IndexOf(sequence.Name) != (int)m_data.size() );
164 /*! \fn SamSequenceIterator SamSequenceDictionary::End(void)
165 \return an STL iterator pointing to the imaginary entry after the last sequence
166 \sa Begin(), ConstEnd()
168 SamSequenceIterator SamSequenceDictionary::End(void) {
172 /*! \fn SamSequenceConstIterator SamSequenceDictionary::End(void) const
173 \return an STL const_iterator pointing to the imaginary entry after the last sequence
175 This is an overloaded function.
177 \sa Begin(), ConstEnd()
179 SamSequenceConstIterator SamSequenceDictionary::End(void) const {
183 /*! \fn int SamSequenceDictionary::IndexOf(const std::string& name) const
185 \return index of sequence if found (matching on name). Otherwise, returns vector::size() (invalid index).
187 int SamSequenceDictionary::IndexOf(const std::string& name) const {
188 SamSequenceConstIterator begin = ConstBegin();
189 SamSequenceConstIterator iter = begin;
190 SamSequenceConstIterator end = ConstEnd();
191 for ( ; iter != end; ++iter ) {
192 const SamSequence& currentSeq = (*iter);
193 if ( currentSeq.Name == name )
196 return distance( begin, iter );
199 /*! \fn bool SamSequenceDictionary::IsEmpty(void) const
200 \brief Returns \c true if dictionary contains no sequences
203 bool SamSequenceDictionary::IsEmpty(void) const {
204 return m_data.empty();
207 /*! \fn void SamSequenceDictionary::Remove(const SamSequence& sequence)
208 \brief Removes sequence from dictionary, if found (matches on name).
210 This is an overloaded function.
212 \param sequence SamSequence to remove (matching on name)
214 void SamSequenceDictionary::Remove(const SamSequence& sequence) {
215 Remove( sequence.Name );
218 /*! \fn void SamSequenceDictionary::Remove(const std::string& sequenceName)
219 \brief Removes sequence from dictionary, if found.
221 \param sequenceName name of sequence to remove
224 void SamSequenceDictionary::Remove(const std::string& sequenceName) {
225 if ( Contains(sequenceName) )
226 m_data.erase( m_data.begin() + IndexOf(sequenceName) );
229 /*! \fn void SamSequenceDictionary::Remove(const std::vector<SamSequence>& sequences)
230 \brief Removes multiple sequences from dictionary.
232 This is an overloaded function.
234 \param sequences sequences to remove
237 void SamSequenceDictionary::Remove(const std::vector<SamSequence>& sequences) {
238 vector<SamSequence>::const_iterator rgIter = sequences.begin();
239 vector<SamSequence>::const_iterator rgEnd = sequences.end();
240 for ( ; rgIter!= rgEnd; ++rgIter )
244 /*! \fn void SamSequenceDictionary::Remove(const std::vector<std::string>& sequenceNames)
245 \brief Removes multiple sequences from dictionary.
247 This is an overloaded function.
249 \param sequenceNames names of the sequences to remove
252 void SamSequenceDictionary::Remove(const std::vector<std::string>& sequenceNames) {
253 vector<string>::const_iterator rgIter = sequenceNames.begin();
254 vector<string>::const_iterator rgEnd = sequenceNames.end();
255 for ( ; rgIter!= rgEnd; ++rgIter )
259 /*! \fn int SamSequenceDictionary::Size(void) const
260 \brief Returns number of sequences in dictionary.
263 int SamSequenceDictionary::Size(void) const {
264 return m_data.size();
267 /*! \fn SamSequence& SamSequenceDictionary::operator[](const std::string& sequenceName)
268 \brief Retrieves the modifiable SamSequence that matches \a sequenceName.
270 NOTE - If the dictionary contains no sequence matching this name, this function inserts
271 a new one with this name (length:0), and returns a reference to it.
273 If you want to avoid this insertion behavior, check the result of Contains() before
276 \param sequenceName name of sequence to retrieve
277 \return a modifiable reference to the SamSequence associated with the name
279 SamSequence& SamSequenceDictionary::operator[](const std::string& sequenceName) {
281 // look up sequence ID
282 int index = IndexOf(sequenceName);
284 // if found, return sequence at index
285 if ( index != (int)m_data.size() )
286 return m_data[index];
288 // otherwise, append new sequence and return reference
290 m_data.push_back( SamSequence(sequenceName, 0) );
291 return m_data.back();