File: RollingHashIterator.h

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (235 lines) | stat: -rw-r--r-- 5,471 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef ROLLING_HASH_ITERATOR_H
#define ROLLING_HASH_ITERATOR_H 1

#include <cstring>
#include <vector>
#include <cassert>
#include <limits>
#include <string>
#include <algorithm>
#include <cctype>
#include <deque>
#include "BloomDBG/RollingHash.h"

/**
 * Permitted characters in k-mers. All k-mers containing
 * other characters will be skipped.
 */
#define ACGT_CHARS "ACGT"

/**
 * Iterate over hash values for k-mers in a
 * given DNA sequence.
 *
 * This implementation uses a rolling hash
 * function to efficiently calculate
 * hash values for successive k-mers.
 */
class RollingHashIterator
{
private:

	/**
	 * Advance iterator right to the next valid k-mer.
	 */
	void next()
	{
		if (m_seq.length() < m_k) {
			m_pos = std::numeric_limits<std::size_t>::max();
			return;
		}

		const std::string& spacedSeed = MaskedKmer::mask();

		while(m_pos < m_seq.length() - m_k + 1) {

			/* skip k-mers with non-ACGT chars in unmasked positions */

			while (!m_badCharPos.empty() && m_badCharPos.front() < m_pos)
				m_badCharPos.pop_front();

			if (!m_badCharPos.empty() && m_badCharPos.front() < m_pos + m_k) {
				/* empty spaced seed is equivalent to a string of '1's */
				if (spacedSeed.empty()) {
					m_rollNextHash = false;
					m_pos = m_badCharPos.front() + 1;
					continue;
				}
				bool goodKmer = true;
				assert(spacedSeed.length() == m_k);
				for (size_t i = 0; i < m_badCharPos.size() &&
					m_badCharPos.at(i) < m_pos + m_k; ++i) {
					size_t kmerPos = m_badCharPos.at(i) - m_pos;
					if (spacedSeed.at(kmerPos) == '1') {
						goodKmer = false;
						break;
					}
				}
				if (!goodKmer) {
					m_rollNextHash = false;
					++m_pos;
					continue;
				}
			}

			/* we are positioned at the next valid k-mer */

			if (!m_rollNextHash) {
				/* we don't have hash values for the
				 * preceding k-mer, so we must compute
				 * the hash values from scratch */
				m_rollingHash.reset(m_seq.substr(m_pos, m_k));
				m_rollNextHash = true;
			} else {
				/* compute new hash values based on
				 * hash values of preceding k-mer */
				assert(m_pos > 0);
				m_rollingHash.rollRight(m_seq.c_str() + m_pos - 1,
					m_seq[m_pos + m_k - 1]);
			}
			m_rollingHash.getHashes(m_hashes);
			return;

		}

		/* there are no more valid k-mers */
		m_pos = std::numeric_limits<std::size_t>::max();
	}

public:

	typedef uint64_t hash_t;
	/**
	 * Default constructor. Creates an iterator pointing to
	 * the end of the iterator range.
	 */
	RollingHashIterator() : m_numHashes(0), m_k(0),
		m_rollingHash(m_numHashes, m_k),
		m_pos(std::numeric_limits<std::size_t>::max()) {}

	/**
	 * Constructor.
	 * @param seq DNA sequence to be hashed
	 * @param k k-mer size
	 * for each k-mer
	 */
	RollingHashIterator(const std::string& seq, unsigned numHashes, unsigned k)
		: m_seq(seq), m_numHashes(numHashes), m_k(k),
		m_rollingHash(m_numHashes, m_k), m_rollNextHash(false), m_pos(0)
	{
		init();
	}

	/**
	 * Initialize internal state of iterator.
	 */
	void init()
	{
		/* note: empty spaced seed indicates no masking (string of '1's) */
		assert(MaskedKmer::mask().empty() || MaskedKmer::mask().length() == m_k);

		/* convert sequence to upper case */
		std::transform(m_seq.begin(), m_seq.end(), m_seq.begin(), ::toupper);

		/* record positions of non-ACGT chars */
		size_t i = m_seq.find_first_not_of(ACGT_CHARS);
		while (i != std::string::npos) {
			m_badCharPos.push_back(i);
			i = m_seq.find_first_not_of(ACGT_CHARS, i + 1);
		}

		/* find first "good" k-mer in sequence */
		next();
	}

	/** get reference to hash values for current k-mer */
	const hash_t* operator*() const
	{
		assert(m_pos + m_k <= m_seq.length());
		return m_hashes;
	}

	/** test equality with another iterator */
	bool operator==(const RollingHashIterator& it) const
	{
		return m_pos == it.m_pos;
	}

	/** test inequality with another iterator */
	bool operator!=(const RollingHashIterator& it) const
	{
		return !(*this == it);
	}

	/** pre-increment operator */
	RollingHashIterator& operator++()
	{
		++m_pos;
		next();
		return *this;
	}

	/** post-increment operator */
	RollingHashIterator operator++(int)
	{
		RollingHashIterator it = *this;
		++*this;
		return it;
	}

    /** iterator pointing to one past last element */
	static const RollingHashIterator end()
	{
		return RollingHashIterator();
	}

	/** return position of current k-mer */
	unsigned pos() const
	{
		return m_pos;
	}

	/** return k-mer at current position */
	std::string kmer(bool mask=false) const
	{
		std::string kmer(m_seq, m_pos, m_k);
		const std::string& spacedSeed = MaskedKmer::mask();
		if (mask && !spacedSeed.empty()) {
			assert(spacedSeed.length() == m_k);
			for(size_t i = 0; i < spacedSeed.length(); ++i) {
				if (spacedSeed.at(i) == '0')
					kmer.at(i) = 'N';
			}
		}
		return kmer;
	}

	/** return RollingHash object for current state */
	RollingHash rollingHash()
	{
		return m_rollingHash;
	}

private:

	/** DNA sequence being hashed */
	std::string m_seq;
	/** number of hash functions */
	unsigned m_numHashes;
	/** hash values */
	hash_t m_hashes[MAX_HASHES];
	/** k-mer size */
	unsigned m_k;
	/** internal state for rolling hash */
	RollingHash m_rollingHash;
	/** true whenever we can "roll" the hash values for
	 * the current k-mer to compute the hash values for the
	 * next k-mer */
	bool m_rollNextHash;
	/** position of current k-mer */
	size_t m_pos;
	/** positions of non-ACGT chars in sequence */
	std::deque<size_t> m_badCharPos;
};

#endif