1 #include <api/SamSequenceDictionary.h>
2 using namespace BamTools;
8 SamSequenceDictionary::SamSequenceDictionary(void) { }
11 SamSequenceDictionary::~SamSequenceDictionary(void) {
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);
21 // overload to support std::string
22 void SamSequenceDictionary::Add(const string& sequenceName) {
23 Add( SamSequence(sequenceName) );
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 )
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 )
42 // returns iterator to container begin
43 SamSequenceIterator SamSequenceDictionary::Begin(void) {
44 return m_data.begin();
47 // returns const_iterator to container begin
48 SamSequenceConstIterator SamSequenceDictionary::Begin(void) const {
49 return m_data.begin();
52 // clear sequence container
53 void SamSequenceDictionary::Clear(void) {
57 // explicit request for const_iterator to container begin
58 SamSequenceConstIterator SamSequenceDictionary::ConstBegin(void) const {
59 return m_data.begin();
62 // explicit request for const_iterator to container end
63 SamSequenceConstIterator SamSequenceDictionary::ConstEnd(void) const {
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() );
72 bool SamSequenceDictionary::Contains(const SamSequence& seq) const {
73 return ( IndexOf(seq) != (int)m_data.size() );
76 // returns iterator to container end
77 SamSequenceIterator SamSequenceDictionary::End(void) {
81 // returns const_iterator to container begin
82 SamSequenceConstIterator SamSequenceDictionary::End(void) const {
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 );
97 // overload to support std::string
98 int SamSequenceDictionary::IndexOf(const string& sequenceName) const {
99 return IndexOf( SamSequence(sequenceName) );
102 // returns true if container is empty
103 bool SamSequenceDictionary::IsEmpty(void) const {
104 return m_data.empty();
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) );
113 // overlaod to support std::string
114 void SamSequenceDictionary::Remove(const string& sequenceName) {
115 Remove( SamSequence(sequenceName) );
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 )
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 )
134 // returns size of container (number of current sequences)
135 int SamSequenceDictionary::Size(void) const {
136 return m_data.size();
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) {
144 // look up sequence ID
145 int index = IndexOf(sequenceName);
147 // if found, return sequence at index
148 if ( index != (int)m_data.size() )
149 return m_data[index];
151 // otherwise, append new sequence and return reference
153 SamSequence seq(sequenceName);
155 m_data.push_back(seq);
156 return m_data.back();