forked from ding-lab/msisensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refseq.cpp
334 lines (313 loc) · 10.2 KB
/
refseq.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* refseq.cpp for MSIsensor
* Copyright (c) 2013 Beifang Niu && Kai Ye WUGSC All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <sstream>
#include <bitset>
#include <omp.h>
#include "utilities.h"
#include "refseq.h"
extern Param param;
extern bit8_t alphabet[];
extern bit8_t rev_alphabet[];
extern char uhomo_code[];
extern char homo_code[];
extern std::ofstream fout;
RefSeq::RefSeq()
: totalHomosites(0)
, totalMicrosates(0)
, totalSites(0)
{
// buffer resize
homosBuffer.reserve(param.bufSize);
};
RefSeq::~RefSeq() {
//***
};
// output and clear buffer
bool RefSeq::PouroutHeader(std::ofstream &fout) {
fout << "chromosome" <<"\t"
<< "location" <<"\t"
<< "repeat_unit_length" <<"\t"
<< "repeat_unit_binary" <<"\t"
<< "repeat_times" <<"\t"
<< "left_flank_binary" <<"\t"
<< "right_flank_binary" <<"\t"
<< "repeat_unit_bases" <<"\t"
<< "left_flank_bases" <<"\t"
<< "right_flank_bases" <<"\n";
}
// output and clear buffer
bool RefSeq::PouroutBuffer(std::ofstream &fout) {
for (int i=0; i<homosBuffer.size(); i++) {
fout << homosBuffer[i].chr <<"\t"
<< homosBuffer[i].location <<"\t"
<< int(homosBuffer[i].typeLen) <<"\t"
<< homosBuffer[i].homoType <<"\t"
<< homosBuffer[i].length <<"\t"
<< homosBuffer[i].frontKmer <<"\t"
<< homosBuffer[i].endKmer <<"\t";
// append readable output
homosBuffer[i].TransferString();
fout << homosBuffer[i].transfer << "\n";
}
homosBuffer.clear();
}
// Load next chromosome
ref_loc_t RefSeq::LoadNextSeq(std::ifstream &fin) {
char c;
char ch[1000];
std::string s;
fin>>c;
if (fin.eof()) return 0;
_length = 0;
// get name
fin>>_name;
fin.getline(ch, 1000);
// get seq
while (!fin.eof()) {
fin>>c;
if (fin.eof()) break;
fin.unget();
if (c == '>') break;
fin>>s;
if (_length + s.size() >= param.max_dbseq_size) {
if (s.size() > param.append_dbseq_size) {
param.max_dbseq_size += (s.size() + 10);
} else {
param.max_dbseq_size += param.append_dbseq_size;
}
_seq.resize(param.max_dbseq_size);
}
copy(s.begin(), s.end(), _seq.begin() + _length);
_length += s.size();
}
return _length;
}
// load one homo/microsate
bit8_t RefSeq::LoadOneSite( const std::string & chr,
const std::string & seq,
int loc,
bit8_t hLen,
bit16_t type,
bit16_t len,
HomoSite & oneSite ) {
std::string::iterator h = _seq.begin();
std::string::iterator p0 = _seq.begin();
std::string::iterator p1 = _seq.begin();
// flank region
bit16_t flankH = 0;
bit16_t flankT = 0;
int contextStart = loc - param.ContextLength;
int contextStop = loc + len*hLen + param.ContextLength;
if ((contextStart < 0) || (contextStop >= _length)) return 0;
for (unsigned char s=0; s<param.ContextLength; s++) {
p0 = h+contextStart+s;
p1 = h+loc+len*hLen+s;
if ((alphabet[*p0] == 4) || (alphabet[*p1] == 4)) { return 0; }
flankH<<=2;
flankH|=alphabet[*p0];
flankT<<=2;
flankT|=alphabet[*p1];
}
oneSite.chr = chr;
oneSite.location = loc;
oneSite.typeLen = hLen;
oneSite.homoType = type;
oneSite.length = len;
oneSite.frontKmer = flankH;
oneSite.endKmer = flankT;
return 1;
}
// test sites binary
void RefSeq::TestSitesBinary() {
std::cout << "chromosome" <<"\t"
<< "location" <<"\t"
<< "sitelength" <<"\t"
<< "sitecontent" <<"\t"
<< "repeattimes" <<"\t"
<< "front_flank" <<"\t"
<< "tail_flank" << std::endl;
for (int i=0; i<homosBuffer.size(); i++) {
std::cout << homosBuffer[i].chr <<"\t"
<< homosBuffer[i].location <<"\t"
<< int(homosBuffer[i].typeLen) <<"\t"
<< homosBuffer[i].homoType <<"\t"
<< homosBuffer[i].length <<"\t"
<< homosBuffer[i].frontKmer <<"\t"
<< homosBuffer[i].endKmer << std::endl;
}
}
// redable test
void RefSeq::TestSites() {
for (int i=0; i<homosBuffer.size(); i++) {
homosBuffer[i].TransferString();
std::cout << homosBuffer[i].transfer << std::endl;
}
}
// Scanning ref seqs
void RefSeq::DoScan( int length,
const std::string &name,
unsigned int index ) {
std::string::iterator h = _seq.begin();
std::string::iterator p = _seq.begin();
std::string::iterator p0 = _seq.begin();
int location;
int endLocation;
int frontLocation;
unsigned int endLength;
unsigned int frontLength;
unsigned int tempChar = 0;
unsigned int hitLength = 0;
int i = 0;
int i0 = 0;
while (i < _length) {
tempChar = alphabet[*p];
// filter 'N'
if ( tempChar == 4 ) {
++i;
++p;
continue;
}
// record loc
p0 = p;
i0 = i;
hitLength = 0;
// homopolymer
while (alphabet[*p] == tempChar) {
++hitLength;
++i;
++p;
}
if (hitLength >= param.MininalHomoSize) {
// record homopolymer
HomoSite toneSite;
if (LoadOneSite(name, _seq, i0, 1, tempChar, hitLength, toneSite)) {
homosBuffer.push_back(toneSite);
totalHomosites++;
totalSites++;
// pour out from buffer
if (totalSites == param.bufSize){
PouroutBuffer(fout);
}
}
} else {
bool ifHit = false;
bool ifN = false;
unsigned short k = 2;
for (k=2; k<=param.MaxMicrosate; k++) {
// traceback
p = p0;
i = i0;
bit16_t s0 = 0;
bit16_t s1 = 0;
int m = 0;
while ((m<k) && (i < _length)) {
if (alphabet[*p] == 4) {
ifN = true;
break;
}
s0<<=2;
s0|=alphabet[*p];
m++;
i++;
p++;
}
if ((ifN) || (i >= _length)) break;
s1 = s0;
hitLength = 0;
while (s0 == s1) {
hitLength++;
if ((ifN) || (i >= _length)) break;
m = 0;
s0 = 0;
while ((m<k) && (i < _length)) {
if (alphabet[*p] == 4) {
ifN = true;
break;
}
s0<<=2;
s0|=alphabet[*p];
m++;
i++;
p++;
}
}
// record one
if (hitLength >= param.Repeats) {
// homo only ? no
if (param.HomoOnly == 0) {
// load one microsate
HomoSite toneSite;
if (LoadOneSite(name, _seq, i0, k, s1, hitLength, toneSite)) {
homosBuffer.push_back(toneSite);
totalMicrosates++;
totalSites++;
// pour out from buffer
if (totalSites == param.bufSize) {
PouroutBuffer(fout);
}
}
}
// load one microsate
ifHit = true;
break;
}
} // micro searching end
// return to head
if (ifHit) {
p = p0 + k*hitLength;
i = i0 + k*hitLength;
} else {
p = p0 + 1;
i = i0 + 1;
}
} // end if
}
}
// Scan homosites and windows
void RefSeq::ScanHomoAndMicrosate(std::ifstream &fin) {
_seq.resize(param.max_dbseq_size);
total_num = sum_length = 0;
unsigned int index = 0;
_count = 0;
while (LoadNextSeq(fin)) {
// filtering little reference sequences
if (_length < 20 ) continue;
RefTitle r;
r._name =_name;
r._size = _length;
title.push_back(r);
// scan window and homopolymer site
DoScan(_length, _name, index);
std::cout << "scanning chomosome "
<< _name << " done. "
<< Cal_AllTime() << " secs passed" << std::endl;
index++;
total_num++;
sum_length += _length;
}
_seq.clear();
}