libStatGen Software 1
Loading...
Searching...
No Matches
glfHandler Class Reference
Collaboration diagram for glfHandler:

Public Member Functions

bool Open (const String &filename)
 
void OpenStub ()
 
bool Create (const String &filename)
 
bool isOpen ()
 
void Close ()
 
void Rewind ()
 
bool NextSection ()
 
bool NextEntry ()
 
bool NextBaseEntry ()
 
void BeginSection (const String &sectionLabel, int sectionLength)
 
void EndSection ()
 
void WriteEntry (int outputPosition)
 
char GetReference (int position, char defaultBase)
 
int GetDepth (int position)
 
const double * GetLikelihoods (int position)
 
const unsigned char * GetLogLikelihoods (int position)
 
int GetMapQuality (int position)
 

Static Public Member Functions

static const double * GetDefaultLikelihoods ()
 
static const unsigned char * GetDefaultLogLikelihoods ()
 
static int GenotypeIndex (int base1, int base2)
 

Public Attributes

bool isStub
 
IFILE handle
 
String header
 
String label
 
int sections
 
int currentSection
 
int maxPosition
 
bool endOfSection
 
glfEntry data
 
int position
 
double likelihoods [10]
 
String indelSequence [2]
 
const char * errorMsg
 

Detailed Description

Definition at line 72 of file glfHandler.h.

Constructor & Destructor Documentation

◆ glfHandler()

glfHandler::glfHandler ( )

Definition at line 26 of file glfHandler.cpp.

27{
28 isStub = true;
29 sections = 0;
30 currentSection = 0;
31 maxPosition = position = endOfSection = 0;
32}

◆ ~glfHandler()

glfHandler::~glfHandler ( )

Definition at line 34 of file glfHandler.cpp.

35{
36 // Not safe to close the file here in case a copy of the file was generated
37 // if (isOpen())
38 // Close();
39}

Member Function Documentation

◆ BeginSection()

void glfHandler::BeginSection ( const String sectionLabel,
int  sectionLength 
)

Definition at line 355 of file glfHandler.cpp.

356{
357 int labelLength = sectionLabel.Length() + 1;
358
359 ifwrite(handle, &labelLength, sizeof(int));
360 ifwrite(handle, (void *)(const char *) sectionLabel, labelLength);
361 ifwrite(handle, &sectionLength, sizeof(int));
362
363 label = sectionLabel;
364 maxPosition = sectionLength;
365}
unsigned int ifwrite(IFILE file, const void *buffer, unsigned int size)
Write the specified number of bytes from the specified buffer into the file.
Definition InputFile.h:669

◆ Close()

void glfHandler::Close ( )

Definition at line 147 of file glfHandler.cpp.

148{
149 if (isOpen())
150 ifclose(handle);
151}
int ifclose(IFILE &file)
Close the file.
Definition InputFile.h:580

◆ Create()

bool glfHandler::Create ( const String filename)

Definition at line 71 of file glfHandler.cpp.

72{
73 isStub = false;
74 // glf is in BGZF format.
75 handle = ifopen(filename, "wb", InputFile::BGZF);
76
77 if (handle == NULL)
78 {
79 isStub = true;
80 return false;
81 }
82
83 WriteHeader();
84
85 return handle != NULL;
86}
IFILE ifopen(const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT)
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
Definition InputFile.h:562
@ BGZF
bgzf file.
Definition InputFile.h:48

◆ EndSection()

void glfHandler::EndSection ( )

Definition at line 367 of file glfHandler.cpp.

368{
369 char marker = 0;
370
371 ifwrite(handle, &marker, sizeof(char));
372}

◆ GenotypeIndex()

static int glfHandler::GenotypeIndex ( int  base1,
int  base2 
)
inlinestatic

Definition at line 132 of file glfHandler.h.

133 {
134 return base1 < base2 ? (base1 - 1) *(10 - base1) / 2 + (base2 - base1) :
135 (base2 - 1) *(10 - base2) / 2 + (base1 - base2);
136 }

◆ GetDefaultLikelihoods()

static const double * glfHandler::GetDefaultLikelihoods ( )
inlinestatic

Definition at line 123 of file glfHandler.h.

124 {
125 return nullLikelihoods;
126 }

◆ GetDefaultLogLikelihoods()

static const unsigned char * glfHandler::GetDefaultLogLikelihoods ( )
inlinestatic

Definition at line 127 of file glfHandler.h.

128 {
129 return nullLogLikelihoods;
130 }

◆ GetDepth()

int glfHandler::GetDepth ( int  position)

Definition at line 327 of file glfHandler.cpp.

328{
329 if (pos == position)
330 return data.depth;
331
332 return 0;
333}
unsigned depth
log10 minimum likelihood * 10 and the number of mapped reads
Definition glfHandler.h:51

◆ GetLikelihoods()

const double * glfHandler::GetLikelihoods ( int  position)

Definition at line 303 of file glfHandler.cpp.

304{
305 if (pos == position)
306 return likelihoods;
307
308 return nullLikelihoods;
309}

◆ GetLogLikelihoods()

const unsigned char * glfHandler::GetLogLikelihoods ( int  position)

Definition at line 311 of file glfHandler.cpp.

312{
313 if (pos == position)
314 return data.lk;
315
316 return nullLogLikelihoods;
317}
unsigned char lk[10]
log10 likelihood ratio * 10 for genotypes AA, AC, AG, AT, CC, CG, CT, GG, GT, TT
Definition glfHandler.h:59

◆ GetMapQuality()

int glfHandler::GetMapQuality ( int  position)

Definition at line 335 of file glfHandler.cpp.

336{
337 if (pos == position)
338 return data.mapQuality;
339
340 return 0;
341}
unsigned char mapQuality
root mean squared maximum mapping quality for overlapping reads
Definition glfHandler.h:54

◆ GetReference()

char glfHandler::GetReference ( int  position,
char  defaultBase 
)

Definition at line 319 of file glfHandler.cpp.

320{
321 if (pos == position)
322 return data.refBase;
323
324 return defaultBase;
325}
unsigned char refBase
"XACMGRSVTWYHKDBN"[ref_base] gives the reference base
Definition glfHandler.h:45

◆ isOpen()

bool glfHandler::isOpen ( )

Definition at line 88 of file glfHandler.cpp.

89{
90 return handle != NULL;
91}

◆ NextBaseEntry()

bool glfHandler::NextBaseEntry ( )

Definition at line 200 of file glfHandler.cpp.

201{
202 bool result = true;
203
204 do
205 {
206 result = NextEntry();
207 }
208 while (result && data.recordType == 2);
209
210 return result;
211}

◆ NextEntry()

bool glfHandler::NextEntry ( )

Definition at line 214 of file glfHandler.cpp.

215{
216 if (isStub)
217 return false;
218
219 // Read record type
220 if (endOfSection || (ifread(handle, &data, 1) != 1))
221 {
222 endOfSection = true;
223 data.recordType = 0;
224 position = maxPosition + 1;
225 return false;
226 }
227
228 // printf("%d/%d\n", data.recordType, data.refBase);
229
230 if (position > maxPosition)
231 return true;
232
233 switch (data.recordType)
234 {
235 case 0 :
236 endOfSection = true;
237 position = maxPosition + 1;
238 return true;
239 case 1 :
240 if (ifread(handle,((char *) &data) + 1, sizeof(data) - 1) == sizeof(data) - 1)
241 {
242 data.refBase = translateBase[data.refBase];
243
244 for (int i = 0; i < 10; i++)
245 likelihoods[i] = bQualityConvertor.toDouble(data.lk[i]);
246
247 position = position + data.offset;
248 return true;
249 }
250
251 // Premature end of file
252 data.recordType = 0;
253 position = maxPosition + 1;
254 return false;
255 case 2 :
256 while (ifread(handle, ((char *) &data) + 1, sizeof(data) - 4) == sizeof(data) - 4)
257 {
258 data.refBase = translateBase[data.refBase];
259
260 for (int i = 0; i < 3; i++)
261 likelihoods[i] = bQualityConvertor.toDouble(data.indel.lk[i]);
262
263 position = position + data.offset;
264
265 indelSequence[0].SetLength(abs(data.indel.length[0]) + 1);
266 indelSequence[0][abs(data.indel.length[0])] = 0;
267 if (ifread(handle, indelSequence[0].LockBuffer(), abs(data.indel.length[0])) != (unsigned int) abs(data.indel.length[0]))
268 break;
269
270 indelSequence[1].SetLength(abs(data.indel.length[1]) + 1);
271 indelSequence[1][abs(data.indel.length[1])] = 0;
272 if (ifread(handle, indelSequence[1].LockBuffer(), abs(data.indel.length[1])) != (unsigned int) abs(data.indel.length[1]))
273 break;
274
275 return true;
276 }
277
278 // Premature end of file
279 data.recordType = 0;
280 position = maxPosition + 1;
281 return false;
282 }
283
284 return false;
285}
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition InputFile.h:600

◆ NextSection()

bool glfHandler::NextSection ( )

Definition at line 166 of file glfHandler.cpp.

167{
168 if (isStub)
169 {
170 endOfSection = true;
171 data.recordType = 0;
172 maxPosition = 1999999999;
173 position = maxPosition + 1;
174 return true;
175 }
176
177 while (!endOfSection && !ifeof(handle))
178 NextEntry();
179
180 endOfSection = false;
181
182 int labelLength = 0;
183
184 currentSection++;
185 position = 0;
186 if (ifread(handle, &labelLength, sizeof(int)) == sizeof(int))
187 {
188 ifread(handle, label.LockBuffer(labelLength+1), labelLength * sizeof(char));
189 label.UnlockBuffer();
190
191 maxPosition = 0;
192 ifread(handle, &maxPosition, sizeof(int));
193
194 return ((maxPosition > 0) && !ifeof(handle));
195 }
196
197 return false;
198}
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition InputFile.h:654

◆ Open()

bool glfHandler::Open ( const String filename)

Definition at line 41 of file glfHandler.cpp.

42{
43 isStub = false;
44 handle = ifopen(filename, "rb");
45
46 if (handle == NULL)
47 {
48 isStub = true;
49 return false;
50 }
51
52 if (!ReadHeader())
53 ifclose(handle);
54
55 endOfSection = true;
56
57 return handle != NULL;
58}

◆ OpenStub()

void glfHandler::OpenStub ( )

Definition at line 60 of file glfHandler.cpp.

61{
62 isStub = true;
63 handle = NULL;
64
65 endOfSection = true;
66 data.recordType = 0;
67 maxPosition = 1999999999;
68 position = maxPosition + 1;
69}

◆ Rewind()

void glfHandler::Rewind ( )

Definition at line 153 of file glfHandler.cpp.

154{
155 if (isOpen())
156 {
157 ifrewind(handle);
158
159 if (!ReadHeader())
160 ifclose(handle);
161
162 endOfSection = true;
163 }
164}
void ifrewind(IFILE file)
Reset to the beginning of the file (cannot be done for stdin/stdout).
Definition InputFile.h:642

◆ WriteEntry()

void glfHandler::WriteEntry ( int  outputPosition)

Definition at line 374 of file glfHandler.cpp.

375{
376 data.offset = outputPosition - position;
377 position = outputPosition;
378
379 switch (data.recordType)
380 {
381 case 0 :
382 EndSection();
383 return;
384 case 1 :
385 data.refBase = backTranslateBase[data.refBase];
386 ifwrite(handle, &data, sizeof(data));
387 data.refBase = translateBase[data.refBase];
388 return;
389 case 2 :
390 data.refBase = backTranslateBase[data.refBase];
391 ifwrite(handle, &data, sizeof(data) - 3);
392 data.refBase = translateBase[data.refBase];
393
394 ifwrite(handle, (void *)(const char *) indelSequence[0], abs(data.indel.length[0]));
395 ifwrite(handle, (void *)(const char *) indelSequence[1], abs(data.indel.length[1]));
396 return;
397 }
398}
unsigned int offset
offset of this record from the previous one, in bases
Definition glfHandler.h:48

Member Data Documentation

◆ currentSection

int glfHandler::currentSection

Definition at line 83 of file glfHandler.h.

◆ data

glfEntry glfHandler::data

Definition at line 90 of file glfHandler.h.

◆ endOfSection

bool glfHandler::endOfSection

Definition at line 87 of file glfHandler.h.

◆ errorMsg

const char* glfHandler::errorMsg

Definition at line 96 of file glfHandler.h.

◆ handle

IFILE glfHandler::handle

Definition at line 77 of file glfHandler.h.

◆ header

String glfHandler::header

Definition at line 78 of file glfHandler.h.

◆ indelSequence

String glfHandler::indelSequence[2]

Definition at line 93 of file glfHandler.h.

◆ isStub

bool glfHandler::isStub

Definition at line 76 of file glfHandler.h.

◆ label

String glfHandler::label

Definition at line 81 of file glfHandler.h.

◆ likelihoods

double glfHandler::likelihoods[10]

Definition at line 92 of file glfHandler.h.

◆ maxPosition

int glfHandler::maxPosition

Definition at line 84 of file glfHandler.h.

◆ position

int glfHandler::position

Definition at line 91 of file glfHandler.h.

◆ sections

int glfHandler::sections

Definition at line 82 of file glfHandler.h.


The documentation for this class was generated from the following files: