forked from sgusev/GERMLINE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMIndividualsExtractor.cpp
166 lines (135 loc) · 3.81 KB
/
HMIndividualsExtractor.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
// HAPSIndividualsExtractor.cpp: Manages input from .haps files
#include "HMIndividualsExtractor.h"
#include <cctype>
#include <cmath>
#include <iostream>
using namespace std;
// HMIndividualsExtractor(): default constructor
HMIndividualsExtractor::HMIndividualsExtractor()
{}
void HMIndividualsExtractor::stripWhitespace()
{
if (stream_phased.is_open())
{
char c;
while((c=stream_phased.peek())!=EOF && isspace(c))
stream_phased.get();
}
else
{
cerr << "WARNING:PolymorphicIndividualsExtractor::stripWhiteSpace():stream is not open" << endl;
valid_flag = false;
}
}
void HMIndividualsExtractor::loadInput()
{
individualsP = &ALL_SAMPLES;
ALL_SNPS.processLegendFile();
ALL_SNPS.beginChromosome();
numberOfMarkers = ALL_SNPS.size();
while (!stream_sample && !stream_phased) getIndividuals();
stream_sample.close();
stream_phased.clear();
}
// getInput(): gets individuals from .haps file.
void HMIndividualsExtractor::getInput()
{
cout << "Please enter the _legend file name" << endl;
cin >> fileLegend;
cout << "Please enter the _phased file name" << endl;
cin >> filePhased;
cout << "Please enter the _sample file name" << endl;
cin >> fileSample;
if ( !ALL_SNPS.setFile( fileLegend ) )
{
cerr << "WARNING:HMIndividualsExtractor::getInput():cannot open legend file" << endl;
valid_flag = false;
return;
}
stream_phased.open(filePhased.c_str());
if (!stream_phased)
{
valid_flag = false;
cerr << "WARNING:HMIndividualsExtractor::getInput():cannot open phased file" << endl;
return;
}
stream_sample.open(fileSample.c_str());
if (!stream_sample)
{
valid_flag = false;
cerr << "WARNING: HMIndividualsExtractor::openFileStream(): sample stream could not be opened" << endl;
return;
}
}
// getIndividuals(): gets the next nuclear family from stream
void HMIndividualsExtractor::getIndividuals()
{
string ID ,discard;
streamoff offset = stream_phased.tellg(); if ( offset > 0 ) offset--;
getline ( stream_phased , discard );
if ( stream_phased ) return;
offset_buffer = ( stream_phased.tellg() - offset);
getline ( stream_phased , discard );
stream_sample >> ID >> discard;
if(ID == "") return;
if ( HAPLOID )
{
Individual * new_ind[2];
new_ind[0] = new Individual();
new_ind[1] = new Individual();
new_ind[0]->setOffset( offset );
new_ind[1]->setOffset( offset );
new_ind[0]->setID("0 " + ID + ".0" );
new_ind[1]->setID("0 " + ID + ".1" );
individualsP->addIndividual( new_ind[0] );
individualsP->addIndividual( new_ind[1] );
} else
{
Individual * new_ind = new Individual;
new_ind->setID(ID);
new_ind->setOffset( offset );
individualsP->addIndividual(new_ind);
}
}
void HMIndividualsExtractor::getCompleteMarkerSet(Individual * p0 , Individual * p1 )
{
MarkerSet * ms[2];
ms[0] = new MarkerSet();
ms[1] = new MarkerSet();
loadMarkerSet( p0->getOffset() , ms );
p0->addMarkerSet(TRANS,ms[0]);
p1->addMarkerSet(TRANS,ms[1]);
stream_phased.clear();
}
void HMIndividualsExtractor::loadMarkerSet( streamoff offset , MarkerSet ** ms )
{
unsigned int maxsize = ALL_SNPS.currentSize();
for(int al=0;al<2;al++)
{
stream_phased.seekg(
offset
+ 2 * ALL_SNPS.getROIStart().getMarkerNumber()
+ 2 * position_ms * MARKER_SET_SIZE
+ al * ( offset_buffer - 1 )
);
for (int position = 0; position < MARKER_SET_SIZE; position++)
{
if(position_ms*MARKER_SET_SIZE+position >= maxsize) break;
stripWhitespace();
char marker = stream_phased.peek();
if ( marker == '1' ) ms[al]->set(position , true );
stream_phased.get();
}
}
}
void HMIndividualsExtractor::getCompleteMarkerSet(Individual * p)
{
MarkerSet * ms[2];
ms[0] = new MarkerSet();
ms[1] = new MarkerSet();
loadMarkerSet( p->getOffset() , ms );
p->addMarkerSet(UNTRANS,ms[0]);
p->addMarkerSet(TRANS,ms[1]);
stream_phased.clear();
}
// end HMIndividualsExtractor.cpp