Debian Codegraph @

seagen/encodedQuery.C

raw symbols SCIP
1 #include <new>
2
3 #include "encodedQuery.H"
4
5 encodedQuery::encodedQuery(seqInCore *S,
6 uint32 k) {
7
8 _iid = S->getIID();
9 _sequenceLength = S->sequenceLength();
10 _merSize = k;
11 _mersTotal = 0;
12 _mersAvail = 0;
13 _mers = 0L;
14 _skip = 0L;
15 _numberOfResults = 0;
16 _output = 0L;
17 _outputLen = 0;
18 _outputMax = 0;
19
20 if (k > _sequenceLength)
21 return;
22
23 char const *seq = S->sequence();
24 uint32 seqLen = S->sequenceLength();
25
26 _mersTotal = seqLen - k + 1;
27 _mersAvail = 0;
28 _mers = new uint64 [_mersTotal];
29 _skip = new uint8 [_mersTotal];
30
31 uint64 substring = uint64ZERO;
32 uint64 mermask = uint64MASK(2 * k);
33 int32 timeUntilValid = k;
34
35 for (uint32 i=0; i<seqLen; ) {
36 substring <<= 2;
37 substring &= mermask;
38
39 if (letterToBits[seq[i]] != 0xff) {
40 substring |= letterToBits[ seq[i] ];
41 timeUntilValid--;
42 } else {
43 timeUntilValid = k;
44 }
45
46 i++;
47
48 if (i >= k) {
49 _mers[i-k] = substring;
50 _skip[i-k] = timeUntilValid > 0;
51 _mersAvail += 1 - _skip[i-k];
52 }
53 }
54 }
55
56 encodedQuery::~encodedQuery() {
57 delete [] _mers;
58 delete [] _skip;
59 delete [] _output;
60 }
61
62
63
64
65 void
66 encodedQuery::test(seqInCore *S) {
67
68 // We assume we've been initialized with the forward version!
69
70 uint32 k = _merSize;
71
72 char const *seq = S->sequence();
73 uint32 seqLen = S->sequenceLength();
74
75 uint64 substring = uint64ZERO;
76 uint64 mermask = uint64MASK(2 * k);
77 int32 timeUntilValid = k;
78
79 // Compute the complement version; we'll iterate through all data
80 // in us, comparing against what the original method would say.
81
82 uint32 _r_mersAvail = 0;
83 uint64 *_r_mers = new uint64 [_mersTotal];
84 uint8 *_r_skip = new uint8 [_mersTotal];
85
86 substring = uint64ZERO;
87 mermask = uint64MASK(2 * k);
88 timeUntilValid = k;
89
90 for (uint32 i=0; i<seqLen; ) {
91 substring <<= 2;
92 substring &= mermask;
93
94 if (letterToBits[seq[seqLen - 1 - i]] != 0xff) {
95 substring |= letterToBits[ complementSymbol[ seq[seqLen - 1 - i] ]];
96 timeUntilValid--;
97 } else {
98 timeUntilValid = k;
99 }
100
101 i++;
102
103 if (i >= k) {
104 _r_mers[i-k] = substring;
105 _r_skip[i-k] = timeUntilValid > 0;
106 _r_mersAvail += 1 - _r_skip[i-k];
107 }
108 }
109
110 #if 0
111 // For comparison, this is the original code used to compute the
112 // reverse complement mers.
113
114 for (uint32 i=0; i<seqLen; ) {
115 substring <<= 2;
116 substring &= mermask;
117
118 if (validSymbol[seq[seqLen - 1 - i]]) {
119 substring |= compressSymbol[ complementSymbol[ seq[seqLen - 1 - i] ]];
120 timeUntilValid--;
121 } else {
122 timeUntilValid = k;
123 }
124
125 i++;
126
127 if (i >= k) {
128 _mers[i-k] = substring;
129 _skip[i-k] = timeUntilValid > 0;
130 _mersAvail += 1 - _skip[i-k];
131 }
132 }
133 #endif
134
135
136
137
138
139 // CHECK!
140 //
141 if (_r_mersAvail != _mersAvail) {
142 fprintf(stderr, "encodedQuery::test()-- mersAvail incorrect: Recomputed:"uint32FMT" Real:"uint32FMT"\n", _mersAvail, _r_mersAvail);
143 }
144
145 char mer1[65];
146 char mer2[65];
147 bool fail = false;
148
149 for (uint32 i=0; i<_mersTotal; i++) {
150
151 if (getSkip(i, true) != _r_skip[i]) {
152 fprintf(stderr, "encodedQuery::test()-- skip["uint32FMTW(4)"] incorrect: Acc:%d Real:%d\n", i, getSkip(i, true), _r_skip[i]);
153 fail = true;
154 }
155
156 if (getSkip(i, true) == false) {
157 if (getMer(i, true) != _r_mers[i]) {
158 uint64ToMerString(_merSize, getMer(i, true), mer1);
159 uint64ToMerString(_merSize, _r_mers[i], mer2);
160 fprintf(stderr, "encodedQuery::test()-- mers["uint32FMTW(4)"] incorrect: Acc:"uint64HEX" %s Real:"uint64HEX" %s\n",
161 i,
162 getMer(i, true), mer1,
163 _r_mers[i], mer2);
164 fail = true;
165 }
166 }
167
168 if (fail) {
169 char rev[2048];
170 strcpy(rev, seq);
171 fprintf(stderr, "seq='%s'\n", seq);
172 fprintf(stderr, "rev='%s'\n", reverseComplementSequence(rev, seqLen));
173 exit(1);
174 }
175 }
176
177 //fprintf(stderr, "encodedQuery::test()-- %s\n", seq);
178 //fprintf(stderr, "encodedQuery::test()-- tested avail:"uint32FMT" total:"uint32FMT"\n", _mersAvail, _mersTotal);
179
180 delete [] _r_mers;
181 delete [] _r_skip;
182 }
183
184
185 void
186 encodedQuery::addOutput(void *newout, uint32 size) {
187
188 // Allocate space for the output -- 1MB should be enough for about
189 // 29000 signals. Make it 32K -> 900 signals.
190 //
191 // You probably do not want to move this into the query
192 // constructor, as that will just waste a lot of memory with
193 // thousands of these in the input queue.
194 //
195 if (_output == 0L) {
196 _outputLen = 0;
197 _outputMax = 32 * 1024;
198 _output = new char [_outputMax];
199 }
200
201 if (_outputLen + 128 >= _outputMax) {
202 _outputMax <<= 1;
203 char *o = 0L;
204
205 try {
206 o = new char [_outputMax];
207 } catch (std::bad_alloc) {
208 fprintf(stderr, "encodedQuery::addOutput()-- out of memory, tried to extend output string\n");
209 fprintf(stderr, "encodedQuery::addOutput()-- from "uint32FMT" to "uint32FMT" bytes.\n",
210 _outputLen, _outputMax);
211 exit(1);
212 }
213
214 memcpy(o, _output, _outputLen);
215 delete [] _output;
216 _output = o;
217 }
218
219 if (size > 0) {
220 memcpy(_output + _outputLen, newout, size);
221 _outputLen += size;
222 } else {
223 char *n = (char *)newout;
224
225 while (*n)
226 _output[_outputLen++] = *n++;
227 _output[_outputLen] = 0;
228 }
229
230 _numberOfResults++;
231 };