]> git.donarmstrong.com Git - bamtools.git/blob - src/api/SamSequenceDictionary.cpp
Added SAM header-handling classes for read/write/validate.
[bamtools.git] / src / api / SamSequenceDictionary.cpp
1 #include <api/SamSequenceDictionary.h>
2 using namespace BamTools;
3
4 #include <iostream>
5 using namespace std;
6
7 // ctor
8 SamSequenceDictionary::SamSequenceDictionary(void) { }
9
10 // dtor
11 SamSequenceDictionary::~SamSequenceDictionary(void) {
12     m_data.clear();
13 }
14
15 // adds sequence if not already in container
16 void SamSequenceDictionary::Add(const SamSequence& sequence) {
17     if ( IsEmpty() || !Contains(sequence) )
18         m_data.push_back(sequence);
19 }
20
21 // overload to support std::string
22 void SamSequenceDictionary::Add(const string& sequenceName) {
23     Add( SamSequence(sequenceName) );
24 }
25
26 // add multiple sequences
27 void SamSequenceDictionary::Add(const vector<SamSequence>& sequences) {
28     vector<SamSequence>::const_iterator rgIter = sequences.begin();
29     vector<SamSequence>::const_iterator rgEnd  = sequences.end();
30     for ( ; rgIter!= rgEnd; ++rgIter )
31         Add(*rgIter);
32 }
33
34 // overload to support std::string
35 void SamSequenceDictionary::Add(const vector<string>& sequenceNames) {
36     vector<string>::const_iterator rgIter = sequenceNames.begin();
37     vector<string>::const_iterator rgEnd  = sequenceNames.end();
38     for ( ; rgIter!= rgEnd; ++rgIter )
39         Add(*rgIter);
40 }
41
42 // returns iterator to container begin
43 SamSequenceIterator SamSequenceDictionary::Begin(void) {
44     return m_data.begin();
45 }
46
47 // returns const_iterator to container begin
48 SamSequenceConstIterator SamSequenceDictionary::Begin(void) const {
49     return m_data.begin();
50 }
51
52 // clear sequence container
53 void SamSequenceDictionary::Clear(void) {
54     m_data.clear();
55 }
56
57 // explicit request for const_iterator to container begin
58 SamSequenceConstIterator SamSequenceDictionary::ConstBegin(void) const {
59     return m_data.begin();
60 }
61
62 // explicit request for const_iterator to container end
63 SamSequenceConstIterator SamSequenceDictionary::ConstEnd(void) const {
64     return m_data.end();
65 }
66
67 // returns true if container contains a sequence with this ID tag
68 bool SamSequenceDictionary::Contains(const string& sequenceName) const {
69     return ( IndexOf(sequenceName) != (int)m_data.size() );
70 }
71
72 bool SamSequenceDictionary::Contains(const SamSequence& seq) const {
73     return ( IndexOf(seq) != (int)m_data.size() );
74 }
75
76 // returns iterator to container end
77 SamSequenceIterator SamSequenceDictionary::End(void) {
78     return m_data.end();
79 }
80
81 // returns const_iterator to container begin
82 SamSequenceConstIterator SamSequenceDictionary::End(void) const {
83     return m_data.end();
84 }
85
86 // returns vector index of sequence if found
87 // returns vector::size() (invalid index) if not found
88 int SamSequenceDictionary::IndexOf(const SamSequence& sequence) const {
89     SamSequenceConstIterator begin = ConstBegin();
90     SamSequenceConstIterator iter  = begin;
91     SamSequenceConstIterator end   = ConstEnd();
92     for ( ; iter != end; ++iter )
93         if ( *iter == sequence ) break;
94     return distance( begin, iter );
95 }
96
97 // overload to support std::string
98 int SamSequenceDictionary::IndexOf(const string& sequenceName) const {
99     return IndexOf( SamSequence(sequenceName) );
100 }
101
102 // returns true if container is empty
103 bool SamSequenceDictionary::IsEmpty(void) const {
104     return m_data.empty();
105 }
106
107 // removes sequence (if it exists)
108 void SamSequenceDictionary::Remove(const SamSequence& sequence) {
109     if ( Contains(sequence) )
110         m_data.erase( m_data.begin() + IndexOf(sequence) );
111 }
112
113 // overlaod to support std::string
114 void SamSequenceDictionary::Remove(const string& sequenceName) {
115     Remove( SamSequence(sequenceName) );
116 }
117
118 // remove multiple sequences
119 void SamSequenceDictionary::Remove(const vector<SamSequence>& sequences) {
120     vector<SamSequence>::const_iterator rgIter = sequences.begin();
121     vector<SamSequence>::const_iterator rgEnd  = sequences.end();
122     for ( ; rgIter!= rgEnd; ++rgIter )
123         Remove(*rgIter);
124 }
125
126 // overload to support std::string
127 void SamSequenceDictionary::Remove(const vector<string>& sequenceNames) {
128     vector<string>::const_iterator rgIter = sequenceNames.begin();
129     vector<string>::const_iterator rgEnd  = sequenceNames.end();
130     for ( ; rgIter!= rgEnd; ++rgIter )
131         Remove(*rgIter);
132 }
133
134 // returns size of container (number of current sequences)
135 int SamSequenceDictionary::Size(void) const {
136     return m_data.size();
137 }
138
139 // retrieves the SamSequence object associated with this name
140 // if sequenceName is unknown, a new SamSequence is created with this name (and invalid length 0)
141 // and a reference to this new sequence entry is returned (like std::map)
142 SamSequence& SamSequenceDictionary::operator[](const std::string& sequenceName) {
143
144     // look up sequence ID
145     int index = IndexOf(sequenceName);
146
147     // if found, return sequence at index
148     if ( index != (int)m_data.size() )
149         return m_data[index];
150
151     // otherwise, append new sequence and return reference
152     else {
153         SamSequence seq(sequenceName);
154         seq.Length = "0";
155         m_data.push_back(seq);
156         return m_data.back();
157     }
158 }
159