1 // ***************************************************************************
2 // SamReadGroupDictionary.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 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 silently discarded.
46 \param readGroup entry to be added
48 void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
50 // TODO: report error on attempted duplicate?
52 if ( IsEmpty() || !Contains(readGroup) )
53 m_data.push_back(readGroup);
56 /*! \fn void SamReadGroupDictionary::Add(const std::string& readGroupId)
57 \brief Adds a read group to the dictionary.
59 This is an overloaded function.
61 \param readGroupId ID of read group to be added
64 void SamReadGroupDictionary::Add(const std::string& readGroupId) {
65 Add( SamReadGroup(readGroupId) );
68 /*! \fn void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups)
69 \brief Adds multiple read groups to the dictionary.
71 This is an overloaded function.
73 \param readGroups entries to be added
76 void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups) {
77 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
78 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
79 for ( ; rgIter!= rgEnd; ++rgIter )
83 /*! \fn void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds)
84 \brief Adds multiple read groups to the dictionary.
86 This is an overloaded function.
88 \param readGroupIds IDs of read groups to be added
91 void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds) {
92 vector<string>::const_iterator rgIter = readGroupIds.begin();
93 vector<string>::const_iterator rgEnd = readGroupIds.end();
94 for ( ; rgIter!= rgEnd; ++rgIter )
98 /*! \fn SamReadGroupIterator SamReadGroupDictionary::Begin(void)
99 \return an STL iterator pointing to the first read group
100 \sa ConstBegin(), End()
102 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
103 return m_data.begin();
106 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const
107 \return an STL const_iterator pointing to the first read group
109 This is an overloaded function.
111 \sa ConstBegin(), End()
113 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
114 return m_data.begin();
117 /*! \fn void SamReadGroupDictionary::Clear(void)
118 \brief Clears all read group entries.
120 void SamReadGroupDictionary::Clear(void) {
124 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const
125 \return an STL const_iterator pointing to the first read group
126 \sa Begin(), ConstEnd()
128 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
129 return m_data.begin();
132 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const
133 \return an STL const_iterator pointing to the imaginary entry after the last read group
134 \sa ConstBegin(), End()
136 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
140 /*! \fn bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const
141 \brief Returns true if dictionary contains read group.
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 ( IndexOf(readGroupId) != (int)m_data.size() );
149 /*! \fn bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const
150 \brief Returns true if dictionary contains read group (matching on ID).
152 This is an overloaded function.
154 \param readGroup search for this read group
155 \return \c true if dictionary contains read group (matching on ID).
157 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
158 return Contains( readGroup.ID );
161 /*! \fn SamReadGroupIterator SamReadGroupDictionary::End(void)
162 \return an STL iterator pointing to the imaginary entry after the last read group
163 \sa Begin(), ConstEnd()
165 SamReadGroupIterator SamReadGroupDictionary::End(void) {
169 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::End(void) const
170 \return an STL const_iterator pointing to the imaginary entry after the last read group
172 This is an overloaded function.
174 \sa Begin(), ConstEnd()
176 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
180 /*! \fn int SamReadGroupDictionary::IndexOf(const std::string& readGroupId) const
182 \return index of read group if found. Otherwise, returns vector::size() (invalid index).
184 int SamReadGroupDictionary::IndexOf(const std::string& readGroupId) const {
185 SamReadGroupConstIterator begin = ConstBegin();
186 SamReadGroupConstIterator iter = begin;
187 SamReadGroupConstIterator end = ConstEnd();
188 for ( ; iter != end; ++iter ) {
189 const SamReadGroup& current = (*iter);
190 if ( current.ID == readGroupId )
193 return distance( begin, iter );
196 /*! \fn bool SamReadGroupDictionary::IsEmpty(void) const
197 \brief Returns \c true if dictionary contains no read groups
200 bool SamReadGroupDictionary::IsEmpty(void) const {
201 return m_data.empty();
204 /*! \fn void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup)
205 \brief Removes read group from dictionary, if found (matching on ID).
207 This is an overloaded function.
209 \param readGroup read group to remove (matches on ID)
211 void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
212 Remove( readGroup.ID );
215 /*! \fn void SamReadGroupDictionary::Remove(const std::string& readGroupId)
216 \brief Removes read group from dictionary, if found.
217 \param readGroupId ID of read group to remove
220 void SamReadGroupDictionary::Remove(const std::string& readGroupId) {
221 if ( Contains(readGroupId) )
222 m_data.erase( m_data.begin() + IndexOf(readGroupId) );
225 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups)
226 \brief Removes multiple read groups from dictionary (matching on ID).
228 This is an overloaded function.
230 \param readGroups read groups to remove
233 void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups) {
234 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
235 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
236 for ( ; rgIter!= rgEnd; ++rgIter )
240 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds)
241 \brief Removes multiple read groups from dictionary.
243 This is an overloaded function.
245 \param readGroupIds IDs of the read groups to remove
248 void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds) {
249 vector<string>::const_iterator rgIter = readGroupIds.begin();
250 vector<string>::const_iterator rgEnd = readGroupIds.end();
251 for ( ; rgIter!= rgEnd; ++rgIter )
255 /*! \fn int SamReadGroupDictionary::Size(void) const
256 \brief Returns number of read groups in dictionary.
259 int SamReadGroupDictionary::Size(void) const {
260 return m_data.size();
263 /*! \fn SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId)
264 \brief Retrieves the modifiable SamReadGroup that matches \a readGroupId.
266 NOTE - If the dictionary contains no read group matching this ID, this function inserts
267 a new one with this ID, and returns a reference to it.
269 If you want to avoid this insertion behavior, check the result of Contains() before
272 \param readGroupId ID of read group to retrieve
273 \return a modifiable reference to the SamReadGroup associated with the ID
275 SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
277 // look up read group ID
278 int index = IndexOf(readGroupId);
280 // if found, return read group at index
281 if ( index != (int)m_data.size() )
282 return m_data[index];
284 // otherwise, append new read group and return reference
286 SamReadGroup rg(readGroupId);
287 m_data.push_back(rg);
288 return m_data.back();