1 // ***************************************************************************
2 // SamReadGroupDictionary.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 SamReadGroup entries.
8 // ***************************************************************************
10 #include <api/SamReadGroupDictionary.h>
11 using namespace BamTools;
17 /*! \class BamTools::SamReadGroupDictionary
18 \brief Container of SamReadGroup entries.
20 Provides methods for operating on a collection of SamReadGroup entries.
23 /*! \fn SamReadGroupDictionary::SamReadGroupDictionary(void)
26 SamReadGroupDictionary::SamReadGroupDictionary(void) { }
28 /*! \fn SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
29 \brief copy constructor
31 SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
32 : m_data(other.m_data)
35 /*! \fn SamReadGroupDictionary::~SamReadGroupDictionary(void)
38 SamReadGroupDictionary::~SamReadGroupDictionary(void) { }
40 /*! \fn void SamReadGroupDictionary::Add(const SamReadGroup& readGroup)
41 \brief Appends a read group to the dictionary.
43 Duplicate entries are silently discarded.
45 \param[in] readGroup entry to be added
47 void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
48 if ( IsEmpty() || !Contains(readGroup) )
49 m_data.push_back(readGroup);
52 /*! \fn void SamReadGroupDictionary::Add(const std::string& readGroupId)
53 \brief Appends a read group to the dictionary.
55 This is an overloaded function.
57 \param[in] readGroupId ID of read group to be added
60 void SamReadGroupDictionary::Add(const std::string& readGroupId) {
61 Add( SamReadGroup(readGroupId) );
64 /*! \fn void SamReadGroupDictionary::Add(const SamReadGroupDictionary& readGroups)
65 \brief Appends another read group dictionary to this one.
67 This is an overloaded function.
69 \param[in] readGroups entries to be added
72 void SamReadGroupDictionary::Add(const SamReadGroupDictionary& readGroups) {
73 SamReadGroupConstIterator rgIter = readGroups.ConstBegin();
74 SamReadGroupConstIterator rgEnd = readGroups.ConstEnd();
75 for ( ; rgIter != rgEnd; ++rgIter )
79 /*! \fn void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups)
80 \brief Appends multiple read groups to the dictionary.
82 This is an overloaded function.
84 \param[in] readGroups entries to be added
87 void SamReadGroupDictionary::Add(const std::vector<SamReadGroup>& readGroups) {
88 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
89 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
90 for ( ; rgIter!= rgEnd; ++rgIter )
94 /*! \fn void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds)
95 \brief Appends multiple read groups to the dictionary.
97 This is an overloaded function.
99 \param[in] readGroupIds IDs of read groups to be added
102 void SamReadGroupDictionary::Add(const std::vector<std::string>& readGroupIds) {
103 vector<string>::const_iterator rgIter = readGroupIds.begin();
104 vector<string>::const_iterator rgEnd = readGroupIds.end();
105 for ( ; rgIter!= rgEnd; ++rgIter )
109 /*! \fn SamReadGroupIterator SamReadGroupDictionary::Begin(void)
110 \return an STL iterator pointing to the first read group
111 \sa ConstBegin(), End()
113 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
114 return m_data.begin();
117 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const
118 \return an STL const_iterator pointing to the first read group
120 This is an overloaded function.
122 \sa ConstBegin(), End()
124 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
125 return m_data.begin();
128 /*! \fn void SamReadGroupDictionary::Clear(void)
129 \brief Clears all read group entries.
131 void SamReadGroupDictionary::Clear(void) {
135 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const
136 \return an STL const_iterator pointing to the first read group
137 \sa Begin(), ConstEnd()
139 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
140 return m_data.begin();
143 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const
144 \return an STL const_iterator pointing to the imaginary entry after the last read group
145 \sa ConstBegin(), End()
147 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
151 /*! \fn bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const
152 \brief Returns true if dictionary contains read group.
154 \param[in] readGroupId search for read group matching this ID
155 \return \c true if dictionary contains a read group with this ID
157 bool SamReadGroupDictionary::Contains(const std::string& readGroupId) const {
158 return ( IndexOf(readGroupId) != (int)m_data.size() );
161 /*! \fn bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const
162 \brief Returns true if dictionary contains read group (matching on ID).
164 This is an overloaded function.
166 \param[in] readGroup search for this read group
167 \return \c true if dictionary contains read group (matching on ID).
169 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
170 return Contains( readGroup.ID );
173 /*! \fn SamReadGroupIterator SamReadGroupDictionary::End(void)
174 \return an STL iterator pointing to the imaginary entry after the last read group
175 \sa Begin(), ConstEnd()
177 SamReadGroupIterator SamReadGroupDictionary::End(void) {
181 /*! \fn SamReadGroupConstIterator SamReadGroupDictionary::End(void) const
182 \return an STL const_iterator pointing to the imaginary entry after the last read group
184 This is an overloaded function.
186 \sa Begin(), ConstEnd()
188 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
192 /*! \fn int SamReadGroupDictionary::IndexOf(const std::string& readGroupId) const
194 \return index of read group if found. Otherwise, returns vector::size() (invalid index).
196 int SamReadGroupDictionary::IndexOf(const std::string& readGroupId) const {
197 SamReadGroupConstIterator begin = ConstBegin();
198 SamReadGroupConstIterator iter = begin;
199 SamReadGroupConstIterator end = ConstEnd();
200 for ( ; iter != end; ++iter ) {
201 const SamReadGroup& current = (*iter);
202 if ( current.ID == readGroupId )
205 return distance( begin, iter );
208 /*! \fn bool SamReadGroupDictionary::IsEmpty(void) const
209 \brief Returns \c true if dictionary contains no read groups
212 bool SamReadGroupDictionary::IsEmpty(void) const {
213 return m_data.empty();
216 /*! \fn void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup)
217 \brief Removes read group from dictionary, if found (matching on ID).
219 This is an overloaded function.
221 \param[in] readGroup read group to remove (matches on ID)
223 void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
224 Remove( readGroup.ID );
227 /*! \fn void SamReadGroupDictionary::Remove(const std::string& readGroupId)
228 \brief Removes read group from dictionary, if found.
230 \param[in] readGroupId ID of read group to remove
233 void SamReadGroupDictionary::Remove(const std::string& readGroupId) {
234 if ( Contains(readGroupId) )
235 m_data.erase( m_data.begin() + IndexOf(readGroupId) );
238 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups)
239 \brief Removes multiple read groups from dictionary (matching on ID).
241 This is an overloaded function.
243 \param[in] readGroups read groups to remove
246 void SamReadGroupDictionary::Remove(const std::vector<SamReadGroup>& readGroups) {
247 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
248 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
249 for ( ; rgIter!= rgEnd; ++rgIter )
253 /*! \fn void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds)
254 \brief Removes multiple read groups from dictionary.
256 This is an overloaded function.
258 \param[in] readGroupIds IDs of the read groups to remove
261 void SamReadGroupDictionary::Remove(const std::vector<std::string>& readGroupIds) {
262 vector<string>::const_iterator rgIter = readGroupIds.begin();
263 vector<string>::const_iterator rgEnd = readGroupIds.end();
264 for ( ; rgIter!= rgEnd; ++rgIter )
268 /*! \fn int SamReadGroupDictionary::Size(void) const
269 \brief Returns number of read groups in dictionary.
272 int SamReadGroupDictionary::Size(void) const {
273 return m_data.size();
276 /*! \fn SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId)
277 \brief Retrieves the modifiable SamReadGroup that matches \a readGroupId.
279 \note If the dictionary contains no read group matching this ID, this function inserts
280 a new one with this ID, and returns a reference to it. If you want to avoid this insertion
281 behavior, check the result of Contains() before using this operator.
283 \param[in] readGroupId ID of read group to retrieve
284 \return a modifiable reference to the SamReadGroup associated with the ID
286 SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
288 // look up read group ID
289 int index = IndexOf(readGroupId);
291 // if found, return read group at index
292 if ( index != (int)m_data.size() )
293 return m_data[index];
295 // otherwise, append new read group and return reference
297 SamReadGroup rg(readGroupId);
298 m_data.push_back(rg);
299 return m_data.back();