]> git.donarmstrong.com Git - bamtools.git/blob - src/api/SamReadGroupDictionary.cpp
Added explicit copy ctors to SamHeader data structures
[bamtools.git] / src / api / SamReadGroupDictionary.cpp
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 // *************************************************************************
10
11 #include <api/SamReadGroupDictionary.h>
12 using namespace BamTools;
13
14 #include <algorithm>
15 #include <iostream>
16 using namespace std;
17
18 // ctor
19 SamReadGroupDictionary::SamReadGroupDictionary(void) { }
20
21 // copy ctor
22 SamReadGroupDictionary::SamReadGroupDictionary(const SamReadGroupDictionary& other)
23     : m_data(other.m_data)
24 { }
25
26 // dtor
27 SamReadGroupDictionary::~SamReadGroupDictionary(void) {
28     m_data.clear();
29 }
30
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);
35 }
36
37 // overload to support std::string
38 void SamReadGroupDictionary::Add(const string& readGroupId) {
39     Add( SamReadGroup(readGroupId) );
40 }
41
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 )
47         Add(*rgIter);
48 }
49
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 )
55         Add(*rgIter);
56 }
57
58 // returns iterator to container begin
59 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
60     return m_data.begin();
61 }
62
63 // returns const_iterator to container begin
64 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
65     return m_data.begin();
66 }
67
68 // clear read group container
69 void SamReadGroupDictionary::Clear(void) {
70     m_data.clear();
71 }
72
73 // explicit request for const_iterator to container begin
74 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
75     return m_data.begin();
76 }
77
78 // explicit request for const_iterator to container end
79 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
80     return m_data.end();
81 }
82
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() );
86 }
87
88 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
89     return ( IndexOf(readGroup) != (int)m_data.size() );
90 }
91
92 // returns iterator to container end
93 SamReadGroupIterator SamReadGroupDictionary::End(void) {
94     return m_data.end();
95 }
96
97 // returns const_iterator to container begin
98 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
99     return m_data.end();
100 }
101
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 );
111 }
112
113 // overload to support std::string
114 int SamReadGroupDictionary::IndexOf(const string& readGroupId) const {
115     return IndexOf( SamReadGroup(readGroupId) );
116 }
117
118 // returns true if container is empty
119 bool SamReadGroupDictionary::IsEmpty(void) const {
120     return m_data.empty();
121 }
122
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) );
127 }
128
129 // overlaod to support std::string
130 void SamReadGroupDictionary::Remove(const string& readGroupId) {
131     Remove( SamReadGroup(readGroupId) );
132 }
133
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 )
139         Remove(*rgIter);
140 }
141
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 )
147         Remove(*rgIter);
148 }
149
150 // returns size of container (number of current read groups)
151 int SamReadGroupDictionary::Size(void) const {
152     return m_data.size();
153 }
154
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) {
159
160     // look up read group ID
161     int index = IndexOf(readGroupId);
162
163     // if found, return read group at index
164     if ( index != (int)m_data.size() )
165         return m_data[index];
166
167     // otherwise, append new read group and return reference
168     else {
169         SamReadGroup rg(readGroupId);
170         m_data.push_back(rg);
171         return m_data.back();
172     }
173 }