1 // ***************************************************************************
2 // SamReadGroupDictionary.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 12 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides methods for operating on a collection of SamReadGroup entries.
8 // ***************************************************************************
10 #include "api/SamReadGroupDictionary.h"
11 using namespace BamTools;
16 /*! \class BamTools::SamReadGroupDictionary
17 \brief Container of SamReadGroup entries.
19 Provides methods for operating on a collection of SamReadGroup entries.
22 /*! \typedef BamTools::SamReadGroupIterator
23 \brief mutable iterator for SamReadGroupDictionary data
25 \note This iterator, dereferenced, actually points to a
26 std::pair<std::string, SamReadGroup>, NOT a "plain old" SamReadGroup.
27 To retrieve the read group object:
30 SamReadGroupIterator iter;
31 SamReadGroup& rg = (*iter).second // OR iter->second;
35 /*! \typedef BamTools::SamReadGroupConstIterator
36 \brief const iterator for SamReadGroupDictionary data
38 \note This iterator, dereferenced, actually points to a
39 std::pair<std::string, SamReadGroup>, NOT a "plain old" SamReadGroup.
40 To retrieve the read group object:
43 SamReadGroupConstIterator iter;
44 const SamReadGroup& sq = (*iter).second // OR iter->second;
48 /*! \fn SamReadGroupDictionary::SamReadGroupDictionary(void)
51 SamReadGroupDictionary::SamReadGroupDictionary(void) { }
53 /*! \fn SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
54 \brief copy constructor
56 SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
57 : m_data(other.m_data)
60 /*! \fn SamReadGroupDictionary::~SamReadGroupDictionary(void)
63 SamReadGroupDictionary::~SamReadGroupDictionary(void) { }
65 /*! \fn void SamReadGroupDictionary::Add(const SamReadGroup& readGroup)
66 \brief Appends a read group to the dictionary.
68 Duplicate entries are silently discarded.
70 \param[in] readGroup entry to be added
72 void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
73 if ( IsEmpty() || !Contains(readGroup) )
74 m_data[readGroup.ID] = readGroup;
77 /*! \fn void SamReadGroupDictionary::Add(const std::string& readGroupId)
78 \brief Appends a read group to the dictionary.
80 This is an overloaded function.
82 \param[in] readGroupId ID of read group to be added
85 void SamReadGroupDictionary::Add(const std::string& readGroupId) {
86 Add( SamReadGroup(readGroupId) );
89 /*! \fn void SamReadGroupDictionary::Add(const SamReadGroupDictionary& readGroups)
90 \brief Appends another read group dictionary to this one.
92 This is an overloaded function.
94 \param[in] readGroups entries to be added
97 void SamReadGroupDictionary::Add(const SamReadGroupDictionary& readGroups) {
98 SamReadGroupConstIterator rgIter = readGroups.ConstBegin();
99 SamReadGroupConstIterator rgEnd = readGroups.ConstEnd();
100 for ( ; rgIter != rgEnd; ++rgIter )
104 /*! \fn void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups)
105 \brief Appends multiple read groups to the dictionary.
107 This is an overloaded function.
109 \param[in] readGroups entries to be added
112 void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups) {
113 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
114 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
115 for ( ; rgIter!= rgEnd; ++rgIter )
119 /*! \fn void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds)
120 \brief Appends multiple read groups to the dictionary.
122 This is an overloaded function.
124 \param[in] readGroupIds IDs of read groups to be added
127 void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds) {
128 vector<string>::const_iterator rgIter = readGroupIds.begin();
129 vector<string>::const_iterator rgEnd = readGroupIds.end();
130 for ( ; rgIter!= rgEnd; ++rgIter )
134 /*! \fn SamReadGroupIterator SamReadGroupDictionary::Begin(void)
135 \return an STL iterator pointing to the first read group
136 \sa ConstBegin(), End()
138 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
139 return m_data.begin();
142 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const
143 \return an STL const_iterator pointing to the first read group
145 This is an overloaded function.
147 \sa ConstBegin(), End()
149 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
150 return m_data.begin();
153 /*! \fn void SamReadGroupDictionary::Clear(void)
154 \brief Clears all read group entries.
156 void SamReadGroupDictionary::Clear(void) {
160 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const
161 \return an STL const_iterator pointing to the first read group
162 \sa Begin(), ConstEnd()
164 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
165 return m_data.begin();
168 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const
169 \return an STL const_iterator pointing to the imaginary entry after the last read group
170 \sa ConstBegin(), End()
172 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
176 /*! \fn bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const
177 \brief Returns true if dictionary contains read group.
179 \param[in] readGroupId search for read group matching this ID
180 \return \c true if dictionary contains a read group with this ID
182 bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const {
183 return ( m_data.find(readGroupId) != m_data.end() );
186 /*! \fn bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const
187 \brief Returns true if dictionary contains read group (matching on ID).
189 This is an overloaded function.
191 \param[in] readGroup search for this read group
192 \return \c true if dictionary contains read group (matching on ID).
194 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
195 return Contains(readGroup.ID);
198 /*! \fn SamReadGroupIterator SamReadGroupDictionary::End(void)
199 \return an STL iterator pointing to the imaginary entry after the last read group
200 \sa Begin(), ConstEnd()
202 SamReadGroupIterator SamReadGroupDictionary::End(void) {
206 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::End(void) const
207 \return an STL const_iterator pointing to the imaginary entry after the last read group
209 This is an overloaded function.
211 \sa Begin(), ConstEnd()
213 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
217 /*! \fn bool SamReadGroupDictionary::IsEmpty(void) const
218 \brief Returns \c true if dictionary contains no read groups
221 bool SamReadGroupDictionary::IsEmpty(void) const {
222 return m_data.empty();
225 /*! \fn void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup)
226 \brief Removes read group from dictionary, if found (matching on ID).
228 This is an overloaded function.
230 \param[in] readGroup read group to remove (matches on ID)
232 void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
233 Remove(readGroup.ID);
236 /*! \fn void SamReadGroupDictionary::Remove(const std::string& readGroupId)
237 \brief Removes read group from dictionary, if found.
239 \param[in] readGroupId ID of read group to remove
242 void SamReadGroupDictionary::Remove(const std::string& readGroupId) {
243 m_data.erase(readGroupId);
246 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups)
247 \brief Removes multiple read groups from dictionary (matching on ID).
249 This is an overloaded function.
251 \param[in] readGroups read groups to remove
254 void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups) {
255 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
256 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
257 for ( ; rgIter!= rgEnd; ++rgIter )
261 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds)
262 \brief Removes multiple read groups from dictionary.
264 This is an overloaded function.
266 \param[in] readGroupIds IDs of the read groups to remove
269 void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds) {
270 vector<string>::const_iterator rgIter = readGroupIds.begin();
271 vector<string>::const_iterator rgEnd = readGroupIds.end();
272 for ( ; rgIter!= rgEnd; ++rgIter )
276 /*! \fn int SamReadGroupDictionary::Size(void) const
277 \brief Returns number of read groups in dictionary.
280 int SamReadGroupDictionary::Size(void) const {
281 return m_data.size();
284 /*! \fn SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId)
285 \brief Retrieves the modifiable SamReadGroup that matches \a readGroupId.
287 \note If the dictionary contains no read group matching this ID, this function inserts
288 a new one with this ID, and returns a reference to it. If you want to avoid this insertion
289 behavior, check the result of Contains() before using this operator.
291 \param[in] readGroupId ID of read group to retrieve
292 \return a modifiable reference to the SamReadGroup associated with the ID
294 SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
295 if ( !Contains(readGroupId) )
296 m_data[readGroupId] = SamReadGroup(readGroupId);
297 return m_data[readGroupId];