1 // ***************************************************************************
2 // SamReadGroupDictionary.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 23 December 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides container operations for collection of read group entries
9 // *************************************************************************
11 #include <api/SamReadGroupDictionary.h>
12 using namespace BamTools;
19 SamReadGroupDictionary::SamReadGroupDictionary(void) { }
22 SamReadGroupDictionary::~SamReadGroupDictionary(void) {
26 // adds read group if not already in container
27 void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
28 if ( IsEmpty() || !Contains(readGroup) )
29 m_data.push_back(readGroup);
32 // overload to support std::string
33 void SamReadGroupDictionary::Add(const string& readGroupId) {
34 Add( SamReadGroup(readGroupId) );
37 // add multiple read groups
38 void SamReadGroupDictionary::Add(const vector<SamReadGroup>& readGroups) {
39 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
40 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
41 for ( ; rgIter!= rgEnd; ++rgIter )
45 // overload to support std::string
46 void SamReadGroupDictionary::Add(const vector<string>& readGroupIds) {
47 vector<string>::const_iterator rgIter = readGroupIds.begin();
48 vector<string>::const_iterator rgEnd = readGroupIds.end();
49 for ( ; rgIter!= rgEnd; ++rgIter )
53 // returns iterator to container begin
54 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
55 return m_data.begin();
58 // returns const_iterator to container begin
59 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
60 return m_data.begin();
63 // clear read group container
64 void SamReadGroupDictionary::Clear(void) {
68 // explicit request for const_iterator to container begin
69 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
70 return m_data.begin();
73 // explicit request for const_iterator to container end
74 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
78 // returns true if container contains a read group with this ID tag
79 bool SamReadGroupDictionary::Contains(const string& readGroupId) const {
80 return ( IndexOf(readGroupId) != (int)m_data.size() );
83 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
84 return ( IndexOf(readGroup) != (int)m_data.size() );
87 // returns iterator to container end
88 SamReadGroupIterator SamReadGroupDictionary::End(void) {
92 // returns const_iterator to container begin
93 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
97 // returns vector index of read group if found
98 // returns vector::size() (invalid index) if not found
99 int SamReadGroupDictionary::IndexOf(const SamReadGroup& readGroup) const {
100 SamReadGroupConstIterator begin = ConstBegin();
101 SamReadGroupConstIterator iter = begin;
102 SamReadGroupConstIterator end = ConstEnd();
103 for ( ; iter != end; ++iter )
104 if ( *iter == readGroup ) break;
105 return distance( begin, iter );
108 // overload to support std::string
109 int SamReadGroupDictionary::IndexOf(const string& readGroupId) const {
110 return IndexOf( SamReadGroup(readGroupId) );
113 // returns true if container is empty
114 bool SamReadGroupDictionary::IsEmpty(void) const {
115 return m_data.empty();
118 // removes read group (if it exists)
119 void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
120 if ( Contains(readGroup) )
121 m_data.erase( m_data.begin() + IndexOf(readGroup) );
124 // overlaod to support std::string
125 void SamReadGroupDictionary::Remove(const string& readGroupId) {
126 Remove( SamReadGroup(readGroupId) );
129 // remove multiple read groups
130 void SamReadGroupDictionary::Remove(const vector<SamReadGroup>& readGroups) {
131 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
132 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
133 for ( ; rgIter!= rgEnd; ++rgIter )
137 // overload to support std::string
138 void SamReadGroupDictionary::Remove(const vector<string>& readGroupIds) {
139 vector<string>::const_iterator rgIter = readGroupIds.begin();
140 vector<string>::const_iterator rgEnd = readGroupIds.end();
141 for ( ; rgIter!= rgEnd; ++rgIter )
145 // returns size of container (number of current read groups)
146 int SamReadGroupDictionary::Size(void) const {
147 return m_data.size();
150 // retrieves the SamReadGroup object associated with this ID
151 // if readGroupId is unknown, a new SamReadGroup is created with this ID
152 // and a reference to this new read group entry is returned (like std::map)
153 SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
155 // look up read group ID
156 int index = IndexOf(readGroupId);
158 // if found, return read group at index
159 if ( index != (int)m_data.size() )
160 return m_data[index];
162 // otherwise, append new read group and return reference
164 SamReadGroup rg(readGroupId);
165 m_data.push_back(rg);
166 return m_data.back();