-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPEDIndividualsExtractor.cpp
224 lines (194 loc) · 5.79 KB
/
PEDIndividualsExtractor.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
// KOSIndividualsExtractor.cpp: Manages input from Plink files
#include "PEDIndividualsExtractor.h"
#include <cctype>
#include <iostream>
using namespace std;
// PEDIndividualsExtractor(): default constructor
PEDIndividualsExtractor::PEDIndividualsExtractor()
{}
void PEDIndividualsExtractor::stripWhitespace()
{
if (stream.is_open())
{
char c;
while((c=stream.peek())!=EOF && isspace(c))
stream.get();
}
else
{
cerr << "WARNING:PolymorphicIndividualsExtractor::stripWhiteSpace():stream is not open" << endl;
valid_flag = false;
}
}
void PEDIndividualsExtractor::loadInput()
{
individualsP = &ALL_SAMPLES;
ALL_SNPS.processMAPFile();
ALL_SNPS.beginChromosome();
numberOfMarkers = ALL_SNPS.size();
while (!stream.eof() )
{
loadIndividuals();
stream.seekg(4*(numberOfMarkers-ALL_SNPS.getROIEnd().getMarkerNumber() -1)+1,ios::cur);
}
stream.clear();
}
// getInput(): gets individuals from .ped file
void PEDIndividualsExtractor::getInput(string map, string ped)
{
if (map == "")
{
cout << "Please enter the MAP file name" << endl;
cin >> map_file;
}
else map_file = map;
if (ped == "")
{
cout << "Please enter the PED file name" << endl;
cin >> ped_file;
}
else ped_file = ped;
if ( !ALL_SNPS.setFile( map_file ) )
{
cerr << "WARNING:PEDIndividualsExtractor::getInput():cannot open map file" << endl;
valid_flag = false;
return;
}
stream.open( ped_file.c_str() );
if ( !stream )
{
cerr << "WARNING:PEDIndividualsExtractor::getInput():cannot open ped file" << endl;
valid_flag = false;
return;
}
}
// getIndividuals(): gets the next nuclear family from stream
void PEDIndividualsExtractor::loadIndividuals()
{
string discard, ID, famID;
stream >> famID >> ID >> discard >> discard >> discard >> discard;
if(!stream.good()) return;
if ( HAPLOID )
{
Individual * new_ind[2];
new_ind[0] = new Individual();
new_ind[1] = new Individual();
new_ind[0]->setOffset( stream.tellg() );
new_ind[1]->setOffset( stream.tellg() );
new_ind[0]->setID(famID + " " + ID + ".0" );
new_ind[1]->setID(famID + " " + ID + ".1" );
//////////////////////////////////////////////////
loadCompleteMarkerSet(new_ind);
//////////////////////////////////////////////////
individualsP->addIndividual( new_ind[0] );
individualsP->addIndividual( new_ind[1] );
} else
{
Individual* new_ind = new Individual();
new_ind->setOffset(stream.tellg());
new_ind->setID(famID + " " + ID);
//////////////////////////////////////////////////
loadCompleteMarkerSet(&new_ind);
//////////////////////////////////////////////////
individualsP->addIndividual(new_ind);
}
}
void PEDIndividualsExtractor::loadCompleteMarkerSet(Individual ** p)
{
stream.seekg(p[0]->getOffset() + 4*ALL_SNPS.getROIStart().getMarkerNumber());
unsigned int maxsize = ALL_SNPS.currentSize();
vector<bool>* buffer[2];
buffer[0] = new vector<bool>(maxsize,false);
buffer[1] = new vector<bool>(maxsize,false);
for (unsigned int position = 0; position < maxsize; position++)
{
for(int al=0;al<2;al++){
stripWhitespace();
char marker = stream.peek();
if ( ALL_SNPS.mapNucleotideToBinary(marker, position ) == 1 )
buffer[al]->at(position)=true;
else
buffer[al]->at(position)=false;
stream.get();
}
}
if(HAPLOID)
{
p[0]->addMarkers(TRANS,buffer[0]);
p[1]->addMarkers(TRANS,buffer[1]);
}
else
{
p[0]->addMarkers(UNTRANS,buffer[0]);
p[0]->addMarkers(TRANS, buffer[1]);
}
delete buffer[0];
delete buffer[1];
}
void PEDIndividualsExtractor::updateMarkerSet(Individual * p,unsigned int start,unsigned int end)
{
p->updateMarkerSet(start,end);
}
void PEDIndividualsExtractor::appendMarkerSet(Individual * p,unsigned int end, int num_markers)
{
p->appendMarkerSet(end,num_markers);
}
/////////////////////////////////////////////////////////////////////////////////////
//:TO BE OMITTED
void PEDIndividualsExtractor::getCompleteMarkerSet(Individual * p)
{
stream.seekg(p->getOffset() + 4*ALL_SNPS.getROIStart().getMarkerNumber() + 4*position_ms*MARKER_SET_SIZE + 1);
MarkerSet * ms[2];
ms[0] = new MarkerSet();
ms[1] = new MarkerSet();
//Memorytally
mem_markers += (2*(sizeof(MarkerSet) + (ms[0]->getMarkerBits().num_blocks() * sizeof(unsigned long))));
readMarkerSet( ms );
p->addMarkerSet(UNTRANS,ms[0]);
p->addMarkerSet(TRANS,ms[1]);
}
//:TO BE OMITTED
void PEDIndividualsExtractor::readMarkerSet( MarkerSet ** ms )
{
unsigned int maxsize = ALL_SNPS.currentSize();
for (int position = 0; position < MARKER_SET_SIZE; position++)
{
if(position_ms*MARKER_SET_SIZE+position >= maxsize) break;
for(int al=0;al<2;al++){
stripWhitespace();
char marker = stream.peek();
if ( ALL_SNPS.mapNucleotideToBinary(marker,position_ms*MARKER_SET_SIZE+position) == 1 )
ms[al]->set(position , true );
stream.get();
}
}
}
//:TO BE OMITTED
void PEDIndividualsExtractor::getCompleteMarkerSet(Individual * p0 , Individual * p1 )
{
stream.seekg(p0->getOffset() + 4*ALL_SNPS.getROIStart().getMarkerNumber() + 4*position_ms*MARKER_SET_SIZE + 1);
MarkerSet * ms[2];
ms[0] = new MarkerSet();
ms[1] = new MarkerSet();
readMarkerSet( ms );
p0->addMarkerSet(TRANS,ms[0]);
p1->addMarkerSet(TRANS,ms[1]);
}
//:TOBE OMITTED
void PEDIndividualsExtractor::loadMarkerSet( MarkerSet ** ms )
{
unsigned int maxsize = ALL_SNPS.currentSize();
for (unsigned int position = 0; position < maxsize; position++)
{
for(int al=0;al<2;al++){
stripWhitespace();
char marker = stream.peek();
if ( ALL_SNPS.mapNucleotideToBinary(marker, position ) == 1 )
ms[al]->pushback(true );
else
ms[al]->pushback(false );
stream.get();
}
}
}
// end PEDIndividualsExtractor.cpp