forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacked_reads.h
178 lines (152 loc) · 5.57 KB
/
packed_reads.h
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
/*
* MEGAHIT
* Copyright (C) 2014 - 2015 The University of Hong Kong & L3 Bioinformatics Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* contact: Dinghua Li <[email protected]> */
/**
* Functions for packed reads or edges (ACGT -> 0123, packed by uint32_t)
*/
#ifndef PACKED_READS_H__
#define PACKED_READS_H__
#include <algorithm>
#include "definitions.h"
#include "kseq.h"
#include "mem_file_checker-inl.h"
#include "bit_operation.h"
// 'spacing' is the strip length for read-word "coalescing"
/**
* @brief copy src_read[offset...(offset+num_chars_to_copy-1)] to dest
*
* @param dest
* @param src_read
* @param offset
* @param num_chars_to_copy
*/
inline void CopySubstring(uint32_t *dest, uint32_t *src_read, int offset, int num_chars_to_copy,
int64_t spacing, int words_per_read, int words_per_substring) {
// copy words of the suffix to the suffix pool
int which_word = offset / kCharsPerEdgeWord;
int word_offset = offset % kCharsPerEdgeWord;
uint32_t *src_p = src_read + which_word;
uint32_t *dest_p = dest;
int num_words_copied = 0;
if (!word_offset) { // special case (word aligned), easy
while (which_word < words_per_read && num_words_copied < words_per_substring) {
*dest_p = *src_p; // write out
dest_p += spacing;
src_p++;
which_word++;
num_words_copied++;
}
}
else { // not word-aligned
int bit_shift = word_offset * kBitsPerEdgeChar;
uint32_t s = *src_p;
uint32_t d = s << bit_shift;
which_word++;
while (which_word < words_per_read) {
s = *(++src_p);
d |= s >> (kBitsPerEdgeWord - bit_shift);
*dest_p = d; // write out
if (++num_words_copied >= words_per_substring) goto here;
dest_p += spacing;
d = s << bit_shift;
which_word++;
}
*dest_p = d; // write last word
here:
;
}
{
// now mask the extra bits (TODO can be optimized)
int num_bits_to_copy = num_chars_to_copy * 2;
int which_word = num_bits_to_copy / kBitsPerEdgeWord;
uint32_t *p = dest + which_word * spacing;
int bits_to_clear = kBitsPerEdgeWord - num_bits_to_copy % kBitsPerEdgeWord;
if (bits_to_clear < kBitsPerEdgeWord) {
*p >>= bits_to_clear;
*p <<= bits_to_clear;
}
else if (which_word < words_per_substring) {
*p = 0;
}
which_word++;
while (which_word < words_per_substring) { // fill zero
*(p += spacing) = 0;
which_word++;
}
}
}
/**
* @brief copy the reverse complement of src_read[offset...(offset+num_chars_to_copy-1)] to dest
*
* @param dest [description]
* @param src_read [description]
* @param offset [description]
* @param num_chars_to_copy [description]
*/
inline void CopySubstringRC(uint32_t *dest, uint32_t *src_read, int offset, int num_chars_to_copy,
int64_t spacing, int words_per_read, int words_per_substring) {
int which_word = (offset + num_chars_to_copy - 1) / kCharsPerEdgeWord;
int word_offset = (offset + num_chars_to_copy - 1) % kCharsPerEdgeWord;
uint32_t *dest_p = dest;
if (word_offset == kCharsPerEdgeWord - 1) { // uint32_t aligned
for (int i = 0; i < words_per_substring && i <= which_word; ++i) {
*dest_p = src_read[which_word - i];
bit_operation::ReverseComplement(*dest_p);
dest_p += spacing;
}
}
else {
int bit_offset = (kCharsPerEdgeWord - 1 - word_offset) * kBitsPerEdgeChar;
int i;
uint32_t w;
for (i = 0; i < words_per_substring - 1 && i < which_word; ++i) {
w = (src_read[which_word - i] >> bit_offset) |
(src_read[which_word - i - 1] << (kBitsPerEdgeWord - bit_offset));
*dest_p = w;
bit_operation::ReverseComplement(*dest_p);
dest_p += spacing;
}
// last word
w = src_read[which_word - i] >> bit_offset;
if (which_word >= i + 1) {
w |= (src_read[which_word - i - 1] << (kBitsPerEdgeWord - bit_offset));
}
*dest_p = w;
bit_operation::ReverseComplement(*dest_p);
}
{
// now mask the extra bits (TODO can be optimized)
int num_bits_to_copy = num_chars_to_copy * 2;
int which_word = num_bits_to_copy / kBitsPerEdgeWord;
uint32_t *p = dest + which_word * spacing;
int bits_to_clear = kBitsPerEdgeWord - num_bits_to_copy % kBitsPerEdgeWord;
if (bits_to_clear < kBitsPerEdgeWord) {
*p >>= bits_to_clear;
*p <<= bits_to_clear;
}
else if (which_word < words_per_substring) {
*p = 0;
}
which_word++;
while (which_word < words_per_substring) { // fill zero
*(p += spacing) = 0;
which_word++;
}
}
}
#endif // PACKED_READS_H__