forked from sgusev/GERMLINE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PEDIndividualsExtractor.cpp
155 lines (130 loc) · 3.74 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
// 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();
if (VERBOSE) {
cout << "Number of SNPs in the set are " << numberOfMarkers << endl;
}
int i = 0;
// The stream was getting failbit set and not eof bit
while (stream)
{
getIndividuals();
stream.seekg(numberOfMarkers*4 + 1,ios_base::cur);
if (VERBOSE) {
cout << "Added individual #" << ++i << endl;
cout << "Position in stream is " << stream.tellg() << endl;
}
}
individualsP->initialize();
stream.clear();
}
// getInput(): gets individuals from .ped file
void PEDIndividualsExtractor::getInput()
{
cout << "Please enter the MAP file name" << endl;
cin >> map_file;
cout << "Please enter the PED file name" << endl;
cin >> ped_file;
if ( !ALL_SNPS.setFile( map_file ) )
{
cerr << "WARNING:PEDIndividualsExtractor::getInput():cannot open map file" << endl;
valid_flag = false;
return;
}
if (VERBOSE) {
cout << "Opened map file " << map_file << " successfully" << endl;
}
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::getIndividuals()
{
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" );
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);
individualsP->addIndividual(new_ind);
}
}
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();
readMarkerSet( ms );
p->addMarkerSet(UNTRANS,ms[0]);
p->addMarkerSet(TRANS,ms[1]);
}
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();
}
}
}
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]);
}
// end PEDIndividualsExtractor.cpp