-
Notifications
You must be signed in to change notification settings - Fork 7
/
Match.cpp
202 lines (179 loc) · 6.83 KB
/
Match.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
#include "Match.h"
void Match::extendBack()
{
// save old position values
int SAVE_pms = position_ms;
// iterate backwards through genome
while(position_ms > 0)
{
position_ms--;
if( !approxEqual() )
{
position_ms++;
break;
}
}
start_ms = position_ms;
// restore saved values
position_ms = SAVE_pms;
}
bool Match::approxEqual()
{
// homozygosity check
if ( node[0] == node[1] )
{
if ( ALLOW_HOM )
{
if ( (int) ( node[0]->getChromosome( 0 )->getMarkerSet()->getMarkerBits() ^ node[1]->getChromosome( 1 )->getMarkerSet()->getMarkerBits() ).count()
<= ( WINDOWS_LIST.err_hom(position_ms) + WINDOWS_LIST.err_het(position_ms) ) ) return true; else return false;
}
else
{
return false;
}
}
else
{
// 1. Haplotype extension
if ( HAPLOID )
{
if ( (int)(node[0]->getChromosome( 0 )->getMarkerSet()->getMarkerBits() ^ node[1]->getChromosome( 0 )->getMarkerSet()->getMarkerBits()).count() <= WINDOWS_LIST.err_hom(position_ms) ) return true;
} else
{
for ( int a = 0 ; a < 2 ; a++ ) {
for ( int b = 0 ; b < 2 ; b++ ) {
if ( (int)(node[0]->getChromosome( a )->getMarkerSet()->getMarkerBits() ^ node[1]->getChromosome( b )->getMarkerSet()->getMarkerBits()).count() <= WINDOWS_LIST.err_hom(position_ms) )
{
return true;
}
}
}
}
if ( HAPLOID || HAP_EXT ) return false;
// 2. Genotype extension
// identify common homozygous SNPs
boost::dynamic_bitset<> mask
= ( node[0]->getChromosome( 0 )->getMarkerSet()->getMarkerBits() ^ node[0]->getChromosome( 1 )->getMarkerSet()->getMarkerBits() ).flip()
& ( node[1]->getChromosome( 0 )->getMarkerSet()->getMarkerBits() ^ node[1]->getChromosome( 1 )->getMarkerSet()->getMarkerBits() ).flip();
// assert that homozygous SNPs are identical
if ( (int) ((node[0]->getChromosome( 0 )->getMarkerSet()->getMarkerBits() ^ node[1]->getChromosome( 0 )->getMarkerSet()->getMarkerBits()) & mask).count() <= WINDOWS_LIST.err_het(position_ms) )
return true;
else return false;
}
}
int Match::scanLeft( unsigned int ms )
{
bool err = false;
int marker;
boost::dynamic_bitset<> mask
= ( node[0]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[0]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).flip()
& ( node[1]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[1]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).flip();
mask = ( node[0]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[1]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits()) & mask;
for( marker = WINDOWS_LIST.getWindowSize(ms) - 1 ; marker >= 0 && !err ; marker-- )
if ( mask[marker] ) err = true;
return marker;
}
int Match::scanRight( unsigned int ms )
{
bool err = false;
int marker;
boost::dynamic_bitset<> mask
= ( node[0]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[0]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).flip()
& ( node[1]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[1]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).flip();
mask = ( node[0]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[1]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits()) & mask;
for( marker = 0 ; marker < WINDOWS_LIST.getWindowSize(ms) && !err ; marker++ )
if ( mask[marker] ) err = true;
return marker;
}
int Match::diff( unsigned int ms )
{
boost::dynamic_bitset<> mask
= ( node[0]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[0]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).flip()
& ( node[1]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[1]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).flip();
mask = ( node[0]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[1]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits()) & mask;
return int(mask.count());
}
bool Match::isHom( int n , unsigned int ms )
{
return (int) ( node[n]->getChromosome( 0 )->getMarkerSet(ms)->getMarkerBits() ^ node[n]->getChromosome( 1 )->getMarkerSet(ms)->getMarkerBits() ).count() <= ( WINDOWS_LIST.err_hom(ms) + WINDOWS_LIST.err_het(ms));
}
void Match::print( ostream& fout )
{
// TODO: If match length + current window size < minimum match threshold, don't bother extending forward
// extend this match from both ends
unsigned int snp_start=0, snp_end=0;
snp_start = WINDOWS_LIST.getWindowStart(start_ms);
snp_end = WINDOWS_LIST.getWindowEnd(end_ms) -1;
int marker;
if ( WIN_EXT )
{
// backwards
if( start_ms > 0 )
{
marker = scanLeft( start_ms - 1 );
snp_start -= (WINDOWS_LIST.getWindowSize(start_ms) - marker - 2);
}
}
if (LAST_SET || WIN_EXT)
{
// forwards
if( end_ms < num_sets - 1 )
{
marker = scanRight( end_ms + 1 );
snp_end += marker;
}
}
bool genetic;
float distance;
if ( ( distance = ALL_SNPS.getDistance(ALL_SNPS.getROIStart().getMarkerNumber()+snp_start,ALL_SNPS.getROIStart().getMarkerNumber()+snp_end,genetic)) < MIN_MATCH_LEN ) return;
// get hamming distance & ignored bit count
int dif = 0;
for( unsigned int i = start_ms; i <= end_ms ; i++) { dif += diff( i ); }
// calculate if homozygous
bool hom[2];
if ( node[0] == node[1] ) fout << '\t' << 1 << '\t' << 1;
else
{
for ( int n = 0 ; n < 2 ; n++ )
{
hom[n] = true;
for ( unsigned int i = start_ms ; i<= end_ms && hom ; i++ )
{
hom[n] = isHom( n , i );
}
}
}
if ( BINARY_OUT )
{
unsigned int pid[2];
pid[0] = node[0]->getNumericID();
pid[1] = node[1]->getNumericID();
unsigned int sid[2];
sid[0] = ALL_SNPS.getSNP(snp_start).getMarkerNumber();
sid[1] = ALL_SNPS.getSNP(snp_end).getMarkerNumber();
fout.write( (char*) &pid[0] , sizeof( unsigned int ) );
fout.write( (char*) &pid[1] , sizeof( unsigned int ) );
fout.write( (char*) &sid[0] , sizeof( unsigned int ) );
fout.write( (char*) &sid[1] , sizeof( unsigned int ) );
fout.write( (char*) &dif , sizeof( int ) );
fout.write( (char*) &hom[0] , sizeof( bool ) );
fout.write( (char*) &hom[1] , sizeof( bool ) );
} else
{
fout << node[0]->getID() << '\t';
fout << node[1]->getID() << '\t';
fout << ALL_SNPS.getSNP(snp_start).getChr() << '\t';
fout << ALL_SNPS.getSNP(snp_start).getPhysPos() << ' ';
fout << ALL_SNPS.getSNP(snp_end).getPhysPos() << '\t';
fout << ALL_SNPS.getSNP(snp_start).getSNPID() << ' ';
fout << ALL_SNPS.getSNP(snp_end).getSNPID() << '\t';
fout << ( snp_end - snp_start + 1) << '\t';
fout << setiosflags(ios::fixed) << setprecision(2) << distance << '\t';
if ( genetic ) fout << "cM" << '\t'; else fout << "MB" << '\t';
fout << dif;
for ( int n = 0 ; n < 2 ; n++ )
if ( hom[n] ) fout << '\t' << 1; else fout << '\t' << 0;
fout << endl;
}
num_matches++;
}