]> git.donarmstrong.com Git - bamtools.git/blob - src/api/SamReadGroupDictionary.cpp
Added SAM header-handling classes for read/write/validate.
[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 // dtor
22 SamReadGroupDictionary::~SamReadGroupDictionary(void) {
23     m_data.clear();
24 }
25
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);
30 }
31
32 // overload to support std::string
33 void SamReadGroupDictionary::Add(const string& readGroupId) {
34     Add( SamReadGroup(readGroupId) );
35 }
36
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 )
42         Add(*rgIter);
43 }
44
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 )
50         Add(*rgIter);
51 }
52
53 // returns iterator to container begin
54 SamReadGroupIterator SamReadGroupDictionary::Begin(void) {
55     return m_data.begin();
56 }
57
58 // returns const_iterator to container begin
59 SamReadGroupConstIterator SamReadGroupDictionary::Begin(void) const {
60     return m_data.begin();
61 }
62
63 // clear read group container
64 void SamReadGroupDictionary::Clear(void) {
65     m_data.clear();
66 }
67
68 // explicit request for const_iterator to container begin
69 SamReadGroupConstIterator SamReadGroupDictionary::ConstBegin(void) const {
70     return m_data.begin();
71 }
72
73 // explicit request for const_iterator to container end
74 SamReadGroupConstIterator SamReadGroupDictionary::ConstEnd(void) const {
75     return m_data.end();
76 }
77
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() );
81 }
82
83 bool SamReadGroupDictionary::Contains(const SamReadGroup& readGroup) const {
84     return ( IndexOf(readGroup) != (int)m_data.size() );
85 }
86
87 // returns iterator to container end
88 SamReadGroupIterator SamReadGroupDictionary::End(void) {
89     return m_data.end();
90 }
91
92 // returns const_iterator to container begin
93 SamReadGroupConstIterator SamReadGroupDictionary::End(void) const {
94     return m_data.end();
95 }
96
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 );
106 }
107
108 // overload to support std::string
109 int SamReadGroupDictionary::IndexOf(const string& readGroupId) const {
110     return IndexOf( SamReadGroup(readGroupId) );
111 }
112
113 // returns true if container is empty
114 bool SamReadGroupDictionary::IsEmpty(void) const {
115     return m_data.empty();
116 }
117
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) );
122 }
123
124 // overlaod to support std::string
125 void SamReadGroupDictionary::Remove(const string& readGroupId) {
126     Remove( SamReadGroup(readGroupId) );
127 }
128
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 )
134         Remove(*rgIter);
135 }
136
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 )
142         Remove(*rgIter);
143 }
144
145 // returns size of container (number of current read groups)
146 int SamReadGroupDictionary::Size(void) const {
147     return m_data.size();
148 }
149
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) {
154
155     // look up read group ID
156     int index = IndexOf(readGroupId);
157
158     // if found, return read group at index
159     if ( index != (int)m_data.size() )
160         return m_data[index];
161
162     // otherwise, append new read group and return reference
163     else {
164         SamReadGroup rg(readGroupId);
165         m_data.push_back(rg);
166         return m_data.back();
167     }
168 }