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(const SamReadGroupDictionary& other)
23 : m_data(other.m_data)
27 SamReadGroupDictionary::~SamReadGroupDictionary(void) {
31 // adds read group if not already in container
32 void SamReadGroupDictionary::Add(const SamReadGroup& readGroup) {
33 if ( IsEmpty() || !Contains(readGroup) )
34 m_data.push_back(readGroup);
37 // overload to support std::string
38 void SamReadGroupDictionary::Add(const string& readGroupId) {
39 Add( SamReadGroup(readGroupId) );
42 // add multiple read groups
43 void SamReadGroupDictionary::Add(const vector<SamReadGroup>& readGroups) {
44 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
45 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
46 for ( ; rgIter!= rgEnd; ++rgIter )
50 // overload to support std::string
51 void SamReadGroupDictionary::Add(const vector<string>& readGroupIds) {
52 vector<string>::const_iterator rgIter = readGroupIds.begin();
53 vector<string>::const_iterator rgEnd = readGroupIds.end();
54 for ( ; rgIter!= rgEnd; ++rgIter )
58 // returns iterator to container begin
59 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
60 return m_data.begin();
63 // returns const_iterator to container begin
64 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
65 return m_data.begin();
68 // clear read group container
69 void SamReadGroupDictionary::Clear(void) {
73 // explicit request for const_iterator to container begin
74 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
75 return m_data.begin();
78 // explicit request for const_iterator to container end
79 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
83 // returns true if container contains a read group with this ID tag
84 bool SamReadGroupDictionary::Contains(const string& readGroupId) const {
85 return ( IndexOf(readGroupId) != (int)m_data.size() );
88 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
89 return ( IndexOf(readGroup) != (int)m_data.size() );
92 // returns iterator to container end
93 SamReadGroupIterator SamReadGroupDictionary::End(void) {
97 // returns const_iterator to container begin
98 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
102 // returns vector index of read group if found
103 // returns vector::size() (invalid index) if not found
104 int SamReadGroupDictionary::IndexOf(const SamReadGroup& readGroup) const {
105 SamReadGroupConstIterator begin = ConstBegin();
106 SamReadGroupConstIterator iter = begin;
107 SamReadGroupConstIterator end = ConstEnd();
108 for ( ; iter != end; ++iter )
109 if ( *iter == readGroup ) break;
110 return distance( begin, iter );
113 // overload to support std::string
114 int SamReadGroupDictionary::IndexOf(const string& readGroupId) const {
115 return IndexOf( SamReadGroup(readGroupId) );
118 // returns true if container is empty
119 bool SamReadGroupDictionary::IsEmpty(void) const {
120 return m_data.empty();
123 // removes read group (if it exists)
124 void SamReadGroupDictionary::Remove(const SamReadGroup& readGroup) {
125 if ( Contains(readGroup) )
126 m_data.erase( m_data.begin() + IndexOf(readGroup) );
129 // overlaod to support std::string
130 void SamReadGroupDictionary::Remove(const string& readGroupId) {
131 Remove( SamReadGroup(readGroupId) );
134 // remove multiple read groups
135 void SamReadGroupDictionary::Remove(const vector<SamReadGroup>& readGroups) {
136 vector<SamReadGroup>::const_iterator rgIter = readGroups.begin();
137 vector<SamReadGroup>::const_iterator rgEnd = readGroups.end();
138 for ( ; rgIter!= rgEnd; ++rgIter )
142 // overload to support std::string
143 void SamReadGroupDictionary::Remove(const vector<string>& readGroupIds) {
144 vector<string>::const_iterator rgIter = readGroupIds.begin();
145 vector<string>::const_iterator rgEnd = readGroupIds.end();
146 for ( ; rgIter!= rgEnd; ++rgIter )
150 // returns size of container (number of current read groups)
151 int SamReadGroupDictionary::Size(void) const {
152 return m_data.size();
155 // retrieves the SamReadGroup object associated with this ID
156 // if readGroupId is unknown, a new SamReadGroup is created with this ID
157 // and a reference to this new read group entry is returned (like std::map)
158 SamReadGroup& SamReadGroupDictionary::operator[](const std::string& readGroupId) {
160 // look up read group ID
161 int index = IndexOf(readGroupId);
163 // if found, return read group at index
164 if ( index != (int)m_data.size() )
165 return m_data[index];
167 // otherwise, append new read group and return reference
169 SamReadGroup rg(readGroupId);
170 m_data.push_back(rg);
171 return m_data.back();