-
Notifications
You must be signed in to change notification settings - Fork 7
/
MatchesBuilder.cpp
190 lines (153 loc) · 5.22 KB
/
MatchesBuilder.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
// MatchesBuilder.cpp: builds matches from individuals
#include "MatchesBuilder.h"
#include <math.h>
using namespace std;
unsigned int position_marker=0;
int position_ms;
unsigned int num_sets=0;
WindowsList WINDOWS_LIST;
int ALLOWED_MASKED;
// MatchesBuilder(): default constructor
MatchesBuilder::MatchesBuilder( PolymorphicIndividualsExtractor * pie )
{
individualsP = & ALL_SAMPLES;
pieP = pie;
}
unsigned long long MatchesBuilder::calculateMemData()
{
return matchFactory.calculateMemData();
}
// buildMatches(): builds matches from individuals
void MatchesBuilder::buildMatches()
{
unsigned long long init_mem = (unsigned long long)(mem_all_matches+mem_bufferchr+mem_chromosome+mem_ind+mem_inds+mem_matchfactory+mem_markers+mem_snps+mem_window);
mem_expected_data= matchFactory.calculateMemData();
if(init_mem+mem_expected_data >= MEM_BOUND)
{
cerr<<"\n\n..NOT ENOUGH MEMORY TO BEGIN ANALYSIS" << endl;
return;
}
WINDOWS_LIST.initialize(ALL_SNPS_CURRENT_SIZE);
position_ms = -1;
if ( !SILENT ) cout << "Read-Match Markers" << endl;
do {
if ( !SILENT ) cerr << "\rReading-Matching Markers - " << (WINDOWS_LIST.getWindowEnd()*100) / ALL_SNPS_CURRENT_SIZE << "%" << flush;
readMatchMarkerSet();
}while(WINDOWS_LIST.getWindowEnd() != ALL_SNPS_CURRENT_SIZE);
if ( !SILENT ) cerr << '\r' << "Reading-Matching Markers Complete" << endl;
}
void MatchesBuilder::printHaplotypes(string fout_name)
{
ofstream fout( fout_name.c_str() );
for(individualsP->begin();individualsP->more();)
{
individualsP->next()->print(fout,0,num_sets);
}
fout.close();
}
void MatchesBuilder::readMarkerSet(unsigned int start, unsigned int end)
{
Individual * i;
for(individualsP->begin();individualsP->more();)
{
i = individualsP->next();
pieP->updateMarkerSet(i,start,end);
}
}
// matchMarkerSet(): builds matches for individuals considering markers in marker set.
void MatchesBuilder::matchMarkerSet()
{
// Match:
for(individualsP->begin();individualsP->more();)
matchFactory.hash( individualsP->next() );
unsigned long long total_mem = matchFactory.calculateMem();
// Verify:
matchFactory.assertShares();
// Extend:
for(individualsP->begin();individualsP->more();)
individualsP->next()->assertShares();
matchFactory.initialize();
}
void MatchesBuilder::readMatchMarkerSet()
{
unsigned long long total_mem=0;
WINDOWS_LIST.getNewWindowSize(ALL_SNPS_CURRENT_SIZE);
//Read:
position_ms++; position_marker=WINDOWS_LIST.getWindowEnd();
readMarkerSet(WINDOWS_LIST.getWindowStart(), WINDOWS_LIST.getWindowEnd());
//Match:
bool flag_updateWindow = true;
while(flag_updateWindow)
{
for(individualsP->begin();individualsP->more();)
matchFactory.hash( individualsP->next() );
if (WINDOWS_LIST.getWindowEnd() == ALL_SNPS_CURRENT_SIZE) LAST_SET=true;
//Check Memory Bound:
total_mem = matchFactory.calculateMem();
mem_expected_data = matchFactory.calculateMemData();
if ( (total_mem+ mem_expected_data) >= MEM_BOUND)
{//If Germline has run out of memory
matchFactory.initialize();
if ( LAST_SET ) {
cerr<<"\nNOT ENOUGH MEMORY TO HASH REMAINING DATA: ";
position_ms--;
return; }
else{ // Update WindowSize
int num_markers= WINDOWS_LIST.updateWindowSize(ALL_SNPS_CURRENT_SIZE);
appendMarkerSet(WINDOWS_LIST.getWindowEnd(),num_markers);
position_marker=WINDOWS_LIST.getWindowEnd();
}
}
else flag_updateWindow=false;
}
//Verify:
matchFactory.assertShares();
// Extend:
for(individualsP->begin();individualsP->more();)
individualsP->next()->assertShares();
matchFactory.initialize();
}
void MatchesBuilder::appendMarkerSet(unsigned int end,int num_markers)
{
Individual * i;
for(individualsP->begin();individualsP->more();)
{
i = individualsP->next();
pieP->appendMarkerSet(i,end,num_markers);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//:TO BE OMITTED
// matchAllMarkers(): builds matches for individuals considering all markers
void MatchesBuilder::matchAllMarkers()
{
for (position_ms = ms_start; position_ms < ms_end ; position_ms++)
{
if ( !SILENT ) cerr << "\rMatching Markers - " << (position_ms*100) / (ms_end ) << "%" << flush;
matchMarkerSet();
}
if ( !SILENT ) cerr << '\r' << "Matching Markers Complete" << endl;
}
void MatchesBuilder::readAllMarkers()
{
for (position_ms = ms_start; position_ms < ms_end; position_ms++)
{
if ( !SILENT ) cerr << "\rReading Markers - " << position_ms*100/ms_end << "%" << flush;
if ( HAPLOID && ROI) readHaploidMarkerSet(); //HAPLOID && ROI , :TO BE OMITTED
else
readMarkerSet(WINDOWS_LIST.getWindowStart(position_ms), WINDOWS_LIST.getWindowEnd(position_ms));
}
if ( !SILENT ) cerr << '\r' << "Reading Markers Complete" << endl;
}
void MatchesBuilder::readHaploidMarkerSet()
{
// Read the individuals two at a time
Individual * i[2];
for(individualsP->begin();individualsP->more();)
{
i[0] = individualsP->next();
i[1] = individualsP->next();
pieP->getCompleteMarkerSet( i[0] , i[1] );
}
}
// end MatchesBuilder.cpp