1 // ***************************************************************************
2 // SamReadGroupDictionary.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 4 March 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides methods for operating on a collection of SamReadGroup entries.
9 // ***************************************************************************
11 #include <api/SamReadGroupDictionary.h>
12 using namespace BamTools;
18 /*! \class BamTools::SamReadGroupDictionary
19 \brief Container of SamReadGroup entries.
21 Provides methods for operating on a collection of SamReadGroup entries.
24 /*! \fn SamReadGroupDictionary::SamReadGroupDictionary(void)
27 SamReadGroupDictionary::SamReadGroupDictionary(void) { }
29 /*! \fn SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
30 \brief copy constructor
32 SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
33 : m_data(other.m_data)
36 /*! \fn SamReadGroupDictionary::~SamReadGroupDictionary(void)
39 SamReadGroupDictionary::~SamReadGroupDictionary(void) { }
41 /*! \fn void SamReadGroupDictionary::Add(const SamReadGroup& readGroup)
42 \brief Adds a read group to the dictionary.
44 Duplicate entries are discarded.
46 \param readGroup entry to be added
48 void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
49 if ( IsEmpty() || !Contains(readGroup) )
50 m_data.push_back(readGroup);
53 /*! \fn void SamReadGroupDictionary::Add(const std::string& readGroupId)
54 \brief Adds a read group to the dictionary.
56 This is an overloaded function.
58 \param readGroupId ID of read group to be added
61 void SamReadGroupDictionary::Add(const std::string& readGroupId) {
62 Add( SamReadGroup(readGroupId) );
65 /*! \fn void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups)
66 \brief Adds multiple read groups to the dictionary.
68 This is an overloaded function.
70 \param readGroups entries to be added
73 void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups) {
74 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
75 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
76 for ( ; rgIter!= rgEnd; ++rgIter )
80 /*! \fn void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds)
81 \brief Adds multiple read groups to the dictionary.
83 This is an overloaded function.
85 \param readGroupIds IDs of read groups to be added
88 void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds) {
89 vector<string>::const_iterator rgIter = readGroupIds.begin();
90 vector<string>::const_iterator rgEnd = readGroupIds.end();
91 for ( ; rgIter!= rgEnd; ++rgIter )
95 /*! \fn SamReadGroupIterator SamReadGroupDictionary::Begin(void)
96 \return an STL iterator pointing to the first read group
97 \sa ConstBegin(), End()
99 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
100 return m_data.begin();
103 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const
105 This is an overloaded function.
107 \return a const STL iterator pointing to the first read group
108 \sa ConstBegin(), End()
110 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
111 return m_data.begin();
114 /*! \fn void SamReadGroupDictionary::Clear(void)
115 \brief Clears all read group entries.
117 void SamReadGroupDictionary::Clear(void) {
121 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const
122 \return a const STL iterator pointing to the first read group
123 \sa Begin(), ConstEnd()
125 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
126 return m_data.begin();
129 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const
130 \return a const STL iterator pointing to the imaginary entry after the last read group
131 \sa ConstBegin(), End()
133 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
137 /*! \fn bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const
138 \brief Returns true if dictionary contains read group.
140 This is an overloaded function.
142 \param readGroupId search for read group matching this ID
143 \return \c true if dictionary contains a read group with this ID
145 bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const {
146 return Contains( SamReadGroup(readGroupId) );
149 /*! \fn bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const
150 \brief Returns true if dictionary contains read group.
151 \param readGroup search for this read group
152 \return \c true if dictionary contains read group
154 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
155 return ( IndexOf(readGroup) != (int)m_data.size() );
158 /*! \fn SamReadGroupIterator SamReadGroupDictionary::End(void)
159 \return an STL iterator pointing to the imaginary entry after the last read group
160 \sa Begin(), ConstEnd()
162 SamReadGroupIterator SamReadGroupDictionary::End(void) {
166 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::End(void) const
168 This is an overloaded function.
170 \return a const STL iterator pointing to the imaginary entry after the last read group
171 \sa Begin(), ConstEnd()
173 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
177 /*! \fn int SamReadGroupDictionary::IndexOf(const SamReadGroup& readGroup) const
179 \return index of read group if found. Otherwise, returns vector::size() (invalid index).
181 int SamReadGroupDictionary::IndexOf(const SamReadGroup& readGroup) const {
182 SamReadGroupConstIterator begin = ConstBegin();
183 SamReadGroupConstIterator iter = begin;
184 SamReadGroupConstIterator end = ConstEnd();
185 for ( ; iter != end; ++iter ) {
186 if ( *iter == readGroup )
189 return distance( begin, iter );
192 /*! \fn bool SamReadGroupDictionary::IsEmpty(void) const
193 \brief Returns \c true if dictionary contains no read groups
196 bool SamReadGroupDictionary::IsEmpty(void) const {
197 return m_data.empty();
200 /*! \fn void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup)
201 \brief Removes read group from dictionary, if found.
202 \param readGroup read group to remove
204 void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
205 if ( Contains(readGroup) )
206 m_data.erase( m_data.begin() + IndexOf(readGroup) );
209 /*! \fn void SamReadGroupDictionary::Remove(const std::string& readGroupId)
210 \brief Removes read group from dictionary, if found.
212 This is an overloaded function.
214 \param readGroupId ID of read group to remove
217 void SamReadGroupDictionary::Remove(const std::string& readGroupId) {
218 Remove( SamReadGroup(readGroupId) );
221 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups)
222 \brief Removes multiple read groups from dictionary.
224 This is an overloaded function.
226 \param readGroups read groups to remove
229 void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups) {
230 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
231 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
232 for ( ; rgIter!= rgEnd; ++rgIter )
236 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds)
237 \brief Removes multiple read groups from dictionary.
239 This is an overloaded function.
241 \param readGroupIds IDs of the read groups to remove
244 void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds) {
245 vector<string>::const_iterator rgIter = readGroupIds.begin();
246 vector<string>::const_iterator rgEnd = readGroupIds.end();
247 for ( ; rgIter!= rgEnd; ++rgIter )
251 /*! \fn int SamReadGroupDictionary::Size(void) const
252 \brief Returns number of read groups in dictionary.
255 int SamReadGroupDictionary::Size(void) const {
256 return m_data.size();
259 /*! \fn SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId)
260 \brief Retrieves the modifiable SamReadGroup that matches \a readGroupId.
262 NOTE - If the dictionary contains no read group matching this ID, this function inserts
263 a new one with this ID, and returns a reference to it.
265 If you want to avoid this insertion behavior, check the result of Contains() before
268 \param readGroupId ID of read group to retrieve
269 \return a modifiable reference to the SamReadGroup associated with the ID
271 SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
273 // look up read group ID
274 int index = IndexOf( SamReadGroup(readGroupId) );
276 // if found, return read group at index
277 if ( index != (int)m_data.size() )
278 return m_data[index];
280 // otherwise, append new read group and return reference
282 SamReadGroup rg(readGroupId);
283 m_data.push_back(rg);
284 return m_data.back();