-
Notifications
You must be signed in to change notification settings - Fork 0
/
align.h
356 lines (333 loc) · 15.7 KB
/
align.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifndef _ALIGN_H_
#define _ALIGN_H_
#include<vector>
#include<string>
#include<cstdio>
#include<set>
#include<algorithm>
#include<cmath>
#include "param.h"
#include "reads.h"
#include "refbase.h"
using namespace std;
const unsigned int FIXSIZE=SEGLEN*FIXELEMENT;
typedef gHit HitArray[MAXHITS+1];
typedef HitArray HitMatrix[MAXSNPS+1];
//typedef bit64_t SegArray[FIXELEMENT*2];
typedef bit64_t SegArray[FIXELEMENT*3];
typedef bit32_t SeedArray[16];
extern Param param;
extern char rev_char[];
extern char chain_flag[];
class SingleAlign {
public:
SingleAlign();
~SingleAlign();
void ImportBatchReads(bit32_t n, vector<ReadInf> &a);
bit32_t CountNs();
void set_RRBS_start();
int TrimLowQual();
void ConvertBinaySeq();
void ConvertBinarySeq();
int CountSeeds(RefSeq &ref, int n, bit32_t start);
inline void CountMismatch(bit64_t *q, bit32_t offset, bit64_t *s);
inline void CountMismatch_new(bit64_t *q, bit32_t offset, bit64_t *s);
void ClearHits();
int RunAlign(RefSeq &ref);
int FilterReads();
void Do_Batch(RefSeq &ref);
void StringAlign(RefSeq &ref, string &os);
void Reverse_Seq();
void s_OutHit(int chain, int n, bit8_t nspsn, gHit *hit, int insert_size, RefSeq &ref, string &os);
void SnpAlign(RefSeq &ref, bit32_t mode);
int TrimAdapter();
void SortHits4PE(int n);
void ReorderSeed(RefSeq &ref);
bit32_t GetTotalSeedLoc(RefSeq &ref, bit32_t start);
void AdjustSeedStartArray(RefSeq &ref);
inline bit32_t MismatchPattern0(bit64_t *q, bit64_t *s, bit32_t seglen_offset);
inline bit32_t MismatchPattern0_new(bit64_t *q, bit64_t *s, bit32_t seglen_offset);
inline bit32_t MismatchPattern1(bit64_t *q, bit64_t *s, bit32_t gap_index, bit32_t seglen_offset);
inline bit32_t MismatchPattern1_new(bit64_t *q, bit64_t *s, bit32_t gap_index, bit32_t seglen_offset);
bit32_t GapAlign(RefSeq &ref, bit32_t mode, bit32_t seed_pos);
//int MatchGap(bit32_t mmi1[], bit32_t mmi2[], bit32_t shift);
inline bit32_t AddHit(RefSeq &ref, bit32_t w, bit32_t mode);
gHit int2hit(RefSeq &ref, gHit gh, int gap_size, bit32_t gap_pos);
public:
//Hot data section
//cache line 1
bit32_t tmp_snp __attribute__((aligned(64))); //keep in the same cache line, 4 byte
bit32_t snp_thres, map_readlen, raw_readlen; // 16 byte
bit32_t read_chain_index, ref_chain_index, _seed, cseed_offset; //32 byte
bit32_t rand_rSeed, n_aligned, read_max_snp_num, seedseg_num; // 48 byte
bit32_t xflag_chain[2], xseed_start_offset[2]; // 64 byte
//cache line 2
gHit _hit, _ghit; // 16 byte
bit32_t *_pro, *_refloc2; // 32 byte
Hit *_refloc; //40 byte
HitMatrix *xhits; // 48 byte
HitArray *hits, *chits; //64 byte
//cache line 3
bit32_t N_count, tmp_snp0, n_unique, n_multiple;
//SegArray xseq[2] __attribute__((aligned(64)));
SegArray xseq[2] __attribute__((aligned(128)));
bit64_t tmp_seq, tmp_reg;
bit32_t end_element, end_offset;
SeedArray xseeds[2][MAXSNPS+1];
bit32_t xseed_array[2][FIXSIZE-SEGLEN], xseedreg_array[2][FIXSIZE-SEGLEN];
bit32_t x_cur_n_hit[2][MAXSNPS+1], *_cur_n_hit, *_cur_n_chit;
bit32_t xseed_start_array[2][MAXSNPS+1];
bit64_t M2, M3, M4;
set<ref_loc_t> *hitset, *ghitset; //, *chitset;
pair<int,int> xseedindex[2][MAXSNPS+1];
bit32_t mm_index[2*MAXGAPS+1][MAXSNPS+1];//position of each mismatch
char cigar[16];
bit64_t total_candidates, total_reads, total_seeds;
//cold data section
vector<ReadInf>::iterator _pread;
string _outseq[2], _outqual[2];
bit32_t num_reads;
vector<ReadInf> mreads;
string _str_align; //align results, prepare for output
vector<string>::iterator read_motif_iter;
//local variables
string::iterator _sp;
string::reverse_iterator _sq;
char _ch[1024];
pair<ref_loc_t,bit32_t> seg_info;
char _mapseq[256];
string::iterator _readnt, _adapternt;
};
inline void SingleAlign::CountMismatch(bit64_t *q, bit32_t offset, bit64_t *s) {
if(param.nt3) {
if((tmp_snp=N_count+param.XM64((((*q)>>offset)^param.XT64(*s))&((q[FIXELEMENT])>>offset)))>snp_thres) return;
for(bit32_t i=1;i<FIXELEMENT;i++) {
if((tmp_snp+=param.XM64(((((q[i-1]<<1)<<(63-offset))|q[i]>>offset)^param.XT64(s[i]))&(((q[FIXELEMENT-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT]>>offset)))>snp_thres) return;
}
}
else {
if((tmp_snp=N_count+param.XM64((((*q)>>offset)¶m.XC64(*s)^(*s))&((q[FIXELEMENT])>>offset)))>snp_thres) return;
for(bit32_t i=1;i<FIXELEMENT;i++) {
if((tmp_snp+=param.XM64(((((q[i-1]<<1)<<(63-offset))|q[i]>>offset)¶m.XC64(s[i])^s[i])&(((q[FIXELEMENT-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT]>>offset)))>snp_thres) return;
}
}
}
inline bit32_t SingleAlign::MismatchPattern0(bit64_t *q, bit64_t *s, bit32_t offset) {
register bit64_t tmp;
bit32_t *mm_array; int i, j,jj, ss=0;
//cout<<"in MMpattern() gap_index:"<<gap_index<<endl;
//cout<<" offset="<<seglen_offset<<" end_element="<<end_element<<" end_offset="<<end_offset<<endl;
mm_array=mm_index[0];
//cout<<"ss="<<ss<<" tmp_snp="<<tmp_snp<<" snp_thres="<<snp_thres<<endl;
for(i=0;i<end_element;i++) {
tmp=(s[i]<<offset)|((s[i+1]>>(63-offset))>>1);
if(param.nt3) tmp=(q[i]^param.XT64(tmp));
else tmp^=q[i]¶m.XC64(tmp);
j=(int)(i*SEGLEN)-1;
while(tmp) {
jj=__builtin_clzll(tmp)>>1; j+=jj+1;
//param.disp_bin64(tmp);
//cout<<"\ti="<<i<<" j="<<j<<" jj="<<jj<< "ss="<<ss+1<<endl;
mm_array[ss++]=j;
if(ss>(int)snp_thres-2) return j;
tmp<<=2; tmp<<=(jj<<1);
}
//cout<<"ss="<<ss<<" tmp_snp="<<tmp_snp<<" snp_thres="<<snp_thres<<endl;
}
tmp=(s[i]<<offset)|((s[i+1]>>(63-offset))>>1);
if(param.nt3) tmp=(q[i]^param.XT64(tmp));
else tmp^=q[i]¶m.XC64(tmp);
tmp>>=end_offset; tmp<<=end_offset;
j=(int)(i*SEGLEN)-1;
while(tmp) {
jj=__builtin_clzll(tmp)>>1; j+=jj+1;
mm_array[ss++]=j;
if(ss>(int)snp_thres-2) return j;
tmp<<=2; tmp<<=(jj<<1);
}
for(;ss<=(int)snp_thres-2;ss++) mm_array[ss]=map_readlen;
return map_readlen;
}
inline bit32_t SingleAlign::MismatchPattern1(bit64_t *q, bit64_t *s, bit32_t gap_index, bit32_t offset) {
register bit64_t tmp; register bit32_t shift;
bit32_t *mm_array; int i,ii, j,jj, ss=0;
//cout<<"in MMpattern() gap_index:"<<gap_index<<endl;
//cout<<"shift="<<(1-((int)gap_index%2)*2)*((int)gap_index+1)/2<<" offset="<<seglen_offset<<" end_element="<<end_element;
//cout<<" end_offset="<<end_offset<<endl;
//for(i=0;i<5;i++) disp_bfa64(param.swap_endian64(q[i])); cout<<endl;
//for(i=0;i<5;i++) disp_bfa64(param.swap_endian64(s[i])); cout<<endl;
mm_array=mm_index[gap_index];
for(i=end_element,ii=0;i>=0;i--,ii+=SEGLEN) {
tmp=(s[i]<<offset)|((s[i+1]>>(63-offset))>>1);
if(param.nt3) tmp=q[i]^param.XT64(tmp);
else tmp^=q[i]¶m.XC64(tmp);
shift=end_offset*(i==end_element); tmp=(tmp>>shift)<<shift;
j=ii-(int) (end_offset>>1)-1;
while(tmp) {
jj=__builtin_ctzll(tmp)>>1; j+=jj+1;
//cout<<"jj="<<jj<<" j="<<j<<" "; param.disp_bin64(tmp);
//cout<<" mm_array["<<ss<<"]="<<j<<endl;
mm_array[ss++]=j;
if(ss>(int)snp_thres-2) return 1;
tmp>>=2; tmp>>=(jj<<1);
}
}
for(;ss<=(int)snp_thres-2;ss++) mm_array[ss]=map_readlen;
return map_readlen;
}
//new matching rule for multiple convert_to_base
inline void SingleAlign::CountMismatch_new(bit64_t *q, bit32_t offset, bit64_t *s) {
if(param.nt3) {
if((tmp_snp=N_count+param.XM64((((*q)>>offset)^param.XT64(*s))&((q[FIXELEMENT])>>offset)))>snp_thres) return;
for(bit32_t i=1;i<FIXELEMENT;i++) {
if((tmp_snp+=param.XM64(((((q[i-1]<<1)<<(63-offset))|q[i]>>offset)^param.XT64(s[i]))&(((q[FIXELEMENT-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT]>>offset)))>snp_thres) return;
}
}
else {//4-nucleotide mapping, new rule
//M2 = param.XC64(*s) | ((q[FIXELEMENT*2])>>offset);
//M3 = param.M2_judge(M2);
//M4 = (((~M3) & M2) | (M3 & ((*q)>>offset))) ^ (*s);
M4 = (((~param.M2_judge(param.XC64(*s) | ((q[FIXELEMENT*2])>>offset))) & (param.XC64(*s) | ((q[FIXELEMENT*2])>>offset))) | ((param.M2_judge(param.XC64(*s) | ((q[FIXELEMENT*2])>>offset))) & ((*q)>>offset))) ^ (*s);
//param.disp_bin64(*q);param.disp_bin64(*s);param.disp_bin64(param.XC64(*s));param.disp_bin64(q[FIXELEMENT*2]);
//param.disp_bin64(M2);param.disp_bin64(M3);param.disp_bin64(M4);param.disp_bin64(q[FIXELEMENT]);
//cout<<param.XM64(M4&((q[FIXELEMENT])>>offset))<<endl;
if((tmp_snp=N_count+param.XM64(M4 & ((q[FIXELEMENT])>>offset)))>snp_thres) return;
for(bit32_t i=1;i<FIXELEMENT;i++) {
//M2 = param.XC64(s[i]) | (((q[FIXELEMENT*2-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT*2]>>offset);
//M3 = param.M2_judge(M2);
//M4 = (((~M3) & M2) | (M3 & (((q[i-1]<<1)<<(63-offset))|q[i]>>offset))) ^ s[i];
M4 = (((~param.M2_judge(param.XC64(s[i]) | (((q[FIXELEMENT*2-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT*2]>>offset))) & (param.XC64(s[i]) | (((q[FIXELEMENT*2-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT*2]>>offset))) | (param.M2_judge(param.XC64(s[i]) | (((q[FIXELEMENT*2-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT*2]>>offset)) & (((q[i-1]<<1)<<(63-offset))|q[i]>>offset))) ^ s[i];
/*
cout<<"q[i-1]\t";param.disp_bin64(q[i-1]);
cout<<"q[i]\t";param.disp_bin64(q[i]);
cout<<"q[FE*2+i-1]\t";param.disp_bin64(q[FIXELEMENT*2-1+i]);
cout<<"q[FE*2+i]\t";param.disp_bin64(q[FIXELEMENT*2+i]);
cout<<"XC64(s[i])\t";param.disp_bin64(param.XC64(s[i]));
cout<<"M2 right\t";param.disp_bin64(((q[FIXELEMENT*2-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT*2]>>offset);
cout<<"M2\t";param.disp_bin64(M2);
cout<<"M3\t";param.disp_bin64(M3);
cout<<"~M3\t";param.disp_bin64(~M3);
cout<<"M4 mid\t";param.disp_bin64(((q[i-1]<<1)<<(63-offset))|q[i]>>offset);
cout<<"M4 left\t";param.disp_bin64(((~M3) & M2) | (M3 & (((q[i-1]<<1)<<(63-offset))|q[i]>>offset)));
cout<<"s[i]\t";param.disp_bin64(s[i]);
cout<<"M4\t";param.disp_bin64(M4);
cout<<param.XM64(M4&(((q[FIXELEMENT-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT]>>offset))<<endl;
*/
if((tmp_snp+=param.XM64(M4&(((q[FIXELEMENT-1+i]<<1)<<(63-offset))|q[i+FIXELEMENT]>>offset)))>snp_thres) return;
}
}
}
//which base in each read is mismatched
inline bit32_t SingleAlign::MismatchPattern0_new(bit64_t *q, bit64_t *s, bit32_t offset) {
register bit64_t tmp;
bit32_t *mm_array; int i, j,jj, ss=0;
//cout<<"in MMpattern() gap_index:"<<gap_index<<endl;
//cout<<" offset="<<seglen_offset<<" end_element="<<end_element<<" end_offset="<<end_offset<<endl;
mm_array=mm_index[0];
//cout<<"ss="<<ss<<" tmp_snp="<<tmp_snp<<" snp_thres="<<snp_thres<<endl;
for(i=0;i<end_element;i++) {
tmp=(s[i]<<offset)|((s[i+1]>>(63-offset))>>1);
if(param.nt3){
tmp=(q[i]^param.XT64(tmp));
}else{
//M2=param.XC64(tmp)|q[i+FIXELEMENT*2];
//M3=param.M2_judge(M2);
//tmp^=(((~M3) & M2) | (M3 & q[i]));
tmp^=(((~param.M2_judge(param.XC64(tmp)|q[i+FIXELEMENT*2])) & (param.XC64(tmp)|q[i+FIXELEMENT*2])) | (param.M2_judge(param.XC64(tmp)|q[i+FIXELEMENT*2]) & q[i]));
}
j=(int)(i*SEGLEN)-1;
while(tmp) {
jj=__builtin_clzll(tmp)>>1; j+=jj+1;
//param.disp_bin64(tmp);
//cout<<"\ti="<<i<<" j="<<j<<" jj="<<jj<< "ss="<<ss+1<<endl;
mm_array[ss++]=j;
if(ss>(int)snp_thres-2) return j;
tmp<<=2; tmp<<=(jj<<1);
}
//cout<<"ss="<<ss<<" tmp_snp="<<tmp_snp<<" snp_thres="<<snp_thres<<endl;
}
tmp=(s[i]<<offset)|((s[i+1]>>(63-offset))>>1);
if(param.nt3){
tmp=(q[i]^param.XT64(tmp));
}else{
//M2=param.XC64(tmp)|q[i+FIXELEMENT*2];
//M3=param.M2_judge(M2);
//tmp^=(((~M3) & M2) | (M3 & q[i]));
tmp^=(((~param.M2_judge(param.XC64(tmp)|q[i+FIXELEMENT*2])) & (param.XC64(tmp)|q[i+FIXELEMENT*2])) | (param.M2_judge(param.XC64(tmp)|q[i+FIXELEMENT*2]) & q[i]));
}
tmp>>=end_offset; tmp<<=end_offset;
j=(int)(i*SEGLEN)-1;
while(tmp) {
jj=__builtin_clzll(tmp)>>1; j+=jj+1;
mm_array[ss++]=j;
if(ss>(int)snp_thres-2) return j;
tmp<<=2; tmp<<=(jj<<1);
}
for(;ss<=(int)snp_thres-2;ss++) mm_array[ss]=map_readlen;
return map_readlen;
}
inline bit32_t SingleAlign::MismatchPattern1_new(bit64_t *q, bit64_t *s, bit32_t gap_index, bit32_t offset) {
register bit64_t tmp; register bit32_t shift;
bit32_t *mm_array; int i,ii, j,jj, ss=0;
//cout<<"in MMpattern() gap_index:"<<gap_index<<endl;
//cout<<"shift="<<(1-((int)gap_index%2)*2)*((int)gap_index+1)/2<<" offset="<<seglen_offset<<" end_element="<<end_element;
//cout<<" end_offset="<<end_offset<<endl;
//for(i=0;i<5;i++) disp_bfa64(param.swap_endian64(q[i])); cout<<endl;
//for(i=0;i<5;i++) disp_bfa64(param.swap_endian64(s[i])); cout<<endl;
//param.disp_bin64(q[i]);
//param.disp_bin64(s[i]);
mm_array=mm_index[gap_index];
for(i=end_element,ii=0;i>=0;i--,ii+=SEGLEN) {
tmp=(s[i]<<offset)|((s[i+1]>>(63-offset))>>1);
if(param.nt3){
tmp=q[i]^param.XT64(tmp);
}else{
//M2=param.XC64(tmp)|q[i+FIXELEMENT*2];
//M3=param.M2_judge(M2);
//tmp^=(((~M3) & M2) | (M3 & q[i]));
tmp^=(((~param.M2_judge(param.XC64(tmp)|q[i+FIXELEMENT*2])) & (param.XC64(tmp)|q[i+FIXELEMENT*2])) | (param.M2_judge(param.XC64(tmp)|q[i+FIXELEMENT*2]) & q[i]));
};
//param.disp_bin64(tmp);
shift=end_offset*(i==end_element); tmp=(tmp>>shift)<<shift;
//cout<<"shift:"<<shift<<endl;
//param.disp_bin64(tmp);
j=ii-(int) (end_offset>>1)-1;
while(tmp) {
jj=__builtin_ctzll(tmp)>>1; j+=jj+1;
//cout<<"jj="<<jj<<" j="<<j<<" "; param.disp_bin64(tmp);
//cout<<" mm_array["<<ss<<"]="<<j<<endl;
mm_array[ss++]=j;
if(ss>(int)snp_thres-2) return 1;
tmp>>=2; tmp>>=(jj<<1);
//param.disp_bin64(tmp);
}
}
for(;ss<=(int)snp_thres-2;ss++) mm_array[ss]=map_readlen;
return map_readlen;
}
inline bit32_t SingleAlign::AddHit(RefSeq &ref, bit32_t w, bit32_t mode) {
if((int)_ghit.loc<0) return 0; //underflow the start of refseq
if(_ghit.loc+map_readlen>ref.title[_ghit.chr].size) return 0; //overflow the end of refseq
if(_ghit.gap_size) {
if(!ghitset[_ghit.chr>>1].insert(_ghit.loc).second) return 0;
}
else {
if(!hitset[_ghit.chr>>1].insert(_ghit.loc).second) return 0; //hit already exist
}
//cout<<"###HIT### "<<ref.title[_ghit.chr].name<<":"<<_ghit.loc<<" mis:"<<tmp_snp<<" snp_thres:"<<snp_thres<<endl;
xhits[read_chain_index][w][x_cur_n_hit[read_chain_index][w]++]=_ghit;
//if(w==mode&&!param.pairend&¶m.report_repeat_hits==0) if(_cur_n_hit[w]+_cur_n_chit[w]>1) return 1;
if(_cur_n_hit[w]+_cur_n_chit[w]>=param.max_num_hits) {
if(w==0) return 1;
else snp_thres=w-1;
}
return 0;
}
inline void SingleAlign::Reverse_Seq() {
_outseq[0]=_pread->seq; _outseq[1]=_pread->seq;
reverse(_outseq[1].begin(), _outseq[1].end());
for(string::iterator p=_outseq[1].begin(); p!=_outseq[1].end(); ++p) *p=rev_char[(unsigned char)*p];
_outqual[0]=_pread->qual; _outqual[1]=_pread->qual;
reverse(_outqual[1].begin(), _outqual[1].end());
}
#endif //_ALIGN_H_