19 #include "GlfRecord.h"
20 #include "GlfException.h"
21 #include "StringBasics.h"
23 std::string GlfRecord::REF_BASE_CHAR =
"XACMGRSVTQYHKSVN";
41 myRec1Base.offset = 0;
42 myRec1Base.min_depth = 0;
43 myRec1Base.rmsMapQ = 0;
44 for(
int i = 0; i < 10; i++)
49 myRec2Base.offset = 0;
50 myRec2Base.min_depth = 0;
51 myRec2Base.rmsMapQ = 0;
52 myRec2Base.lkHom1 = 0;
53 myRec2Base.lkHom2 = 0;
55 myRec2Base.indelLen1 = 0;
56 myRec2Base.indelLen2 = 0;
69 int byteLen =
sizeof(uint8_t);
70 numRead =
ifread(filePtr, &myRecTypeRefBase, byteLen);
71 if(numRead != byteLen)
73 String errorMsg =
"Failed to read the record type & reference base (";
75 errorMsg +=
" bytes). Only read ";
77 errorMsg +=
" bytes.";
78 std::string errorString = errorMsg.c_str();
99 String errorMsg =
"Failed to read the record: unknown type: ";
101 std::string errorString = errorMsg.c_str();
119 writeRtypeRef(filePtr);
131 String errorMsg =
"Failed to write the record: unknown type: ";
133 std::string errorString = errorMsg.c_str();
157 std::cout <<
"\toffset: " << myRec1Base.offset
158 <<
"; min_lk: " << (myRec1Base.min_depth >> 24)
159 <<
"; read_depth: " << (myRec1Base.min_depth & 0xFFFFFF)
160 <<
"; rmsMapQ: " << (int)myRec1Base.rmsMapQ;
161 for(
int i = 0; i < 10; ++i)
163 std::cout <<
"; lk[" << i <<
"]: " << (int)myRec1Base.lk[i];
170 std::cout <<
"\toffset: " << myRec2Base.offset
171 <<
"; min_lk: " << (myRec2Base.min_depth >> 24)
172 <<
"; read_depth: " << (myRec2Base.min_depth & 0xFFFFF)
173 <<
"; rmsMapQ: " << (int)myRec2Base.rmsMapQ
174 <<
"; lkHom1: " << (
int)myRec2Base.lkHom1
175 <<
"; lkHom2: " << (int)myRec2Base.lkHom2
176 <<
"; lkHet: " << (
int)myRec2Base.lkHet
177 <<
"; indelLen1: " << myRec2Base.indelLen1
178 <<
"; indelLen2: " << myRec2Base.indelLen2
179 <<
"; myIndelSeq1: " << myIndelSeq1.c_str()
180 <<
"; myIndelSeq2: " << myIndelSeq2.c_str()
190 myRecTypeRefBase = rtypeRef;
197 (myRecTypeRefBase & REF_BASE_MASK) | (recType << REC_TYPE_SHIFT);
204 (myRecTypeRefBase & REC_TYPE_MASK) | (refBase & REF_BASE_MASK);
217 myRec1Base.offset = offset;
218 myRec2Base.offset = offset;
224 myRec1Base.min_depth = minDepth;
225 myRec2Base.min_depth = minDepth;
231 setMinDepth((myRec1Base.min_depth & READ_DEPTH_MASK) |
232 (minLk << MIN_LK_SHIFT));
239 (readDepth & READ_DEPTH_MASK));
245 myRec1Base.rmsMapQ = rmsMapQ;
246 myRec2Base.rmsMapQ = rmsMapQ;
253 int index = myRecTypeRefBase & REF_BASE_MASK;
254 if((index > REF_BASE_MAX) || (index < 0))
259 return(REF_BASE_CHAR[index]);
267 return(myRec1Base.offset);
271 return(myRec2Base.offset);
274 "Tried to call getOffset for Record not of type 1 or 2."));
282 return(myRec1Base.min_depth);
286 return(myRec2Base.min_depth);
289 "Tried to call getMinDepth for Record not of type 1 or 2."));
297 return(myRec1Base.min_depth >> MIN_LK_SHIFT);
301 return(myRec2Base.min_depth >> MIN_LK_SHIFT);
304 "Tried to call getMinLk for Record not of type 1 or 2."));
312 return(myRec1Base.min_depth & READ_DEPTH_MASK);
316 return(myRec2Base.min_depth & READ_DEPTH_MASK);
319 "Tried to call getReadDepth for Record not of type 1 or 2."));
327 return(myRec1Base.rmsMapQ);
331 return(myRec2Base.rmsMapQ);
334 "Tried to call getRmsMapQ for Record not of type 1 or 2."));
342 if((index < 0) || (index >= NUM_REC1_LIKELIHOOD))
346 "Trying to set Record Type 1 likelihood position< 0 or >= 10."));
351 myRec1Base.lk[index] = value;
360 "Tried to call getLk for Record not of type 1."));
363 if((index < 0) || (index >= NUM_REC1_LIKELIHOOD))
366 "Tried to call getLk for index < 0 or >= 10."));
369 return(myRec1Base.lk[index]);
376 myRec2Base.lkHom1 = lk;
382 myRec2Base.lkHom2 = lk;
388 myRec2Base.lkHet = lk;
394 myRec2Base.indelLen1 = indelSeq.length();
395 myIndelSeq1 = indelSeq;
401 myRec2Base.indelLen1 = 0 - (indelSeq.length());
402 myIndelSeq1 = indelSeq;
408 myRec2Base.indelLen2 = indelSeq.length();
409 myIndelSeq2 = indelSeq;
415 myRec2Base.indelLen2 = 0 - (indelSeq.length());
416 myIndelSeq2 = indelSeq;
425 "Tried to call getLkHom1 for Record not of type 2."));
428 return(myRec2Base.lkHom1);
436 "Tried to call getLkHom2 for Record not of type 2."));
439 return(myRec2Base.lkHom2);
447 "Tried to call getLkHet for Record not of type 2."));
450 return(myRec2Base.lkHet);
458 "Tried to call getIndel1 for Record not of type 2."));
461 indelSeq = myIndelSeq1.c_str();
462 return(myRec2Base.indelLen1);
470 "Tried to call getIndel2 for Record not of type 2."));
473 indelSeq = myIndelSeq2.c_str();
474 return(myRec2Base.indelLen2);
478 void GlfRecord::readType1(
IFILE filePtr)
482 numRead =
ifread(filePtr, &myRec1Base, REC1_BASE_SIZE);
483 if(numRead != REC1_BASE_SIZE)
485 String errorMsg =
"Failed to read record of type 1 (";
486 errorMsg += REC1_BASE_SIZE;
487 errorMsg +=
" bytes). Only read ";
489 errorMsg +=
" bytes.";
490 std::string errorString = errorMsg.c_str();
499 void GlfRecord::readType2(
IFILE filePtr)
503 numRead =
ifread(filePtr, &myRec2Base, REC2_BASE_SIZE);
504 if(numRead != REC2_BASE_SIZE)
506 String errorMsg =
"Failed to read record of type 2 base info (";
507 errorMsg += REC2_BASE_SIZE;
508 errorMsg +=
" bytes). Only read ";
510 errorMsg +=
" bytes.";
511 std::string errorString = errorMsg.c_str();
517 int16_t len = abs(myRec2Base.indelLen1);
518 numRead = myIndelSeq1.readFromFile(filePtr, len);
521 String errorMsg =
"Failed to read record of type 2, 1st indel sequence (";
523 errorMsg +=
" bytes). Only read ";
525 errorMsg +=
" bytes.";
526 std::string errorString = errorMsg.c_str();
529 len = abs(myRec2Base.indelLen2);
530 numRead = myIndelSeq2.readFromFile(filePtr, len);
533 String errorMsg =
"Failed to read record of type 2, 2nd indel sequence (";
535 errorMsg +=
" bytes). Only read ";
537 errorMsg +=
" bytes.";
538 std::string errorString = errorMsg.c_str();
544 void GlfRecord::writeRtypeRef(
IFILE filePtr)
const
546 int byteLen =
sizeof(myRecTypeRefBase);
548 ifwrite(filePtr, &myRecTypeRefBase, byteLen);
549 if(numWrite != byteLen)
552 "Failed to write the length of the record type and reference base (";
554 errorMsg +=
" bytes). Only wrote ";
555 errorMsg += numWrite;
556 errorMsg +=
" bytes.";
557 std::string errorString = errorMsg.c_str();
563 void GlfRecord::writeType1(
IFILE filePtr)
const
566 writeRtypeRef(filePtr);
570 int numWrite =
ifwrite(filePtr, &myRec1Base, REC1_BASE_SIZE);
571 if(numWrite != REC1_BASE_SIZE)
574 String errorMsg =
"Failed to write record of type 1 (";
575 errorMsg += REC1_BASE_SIZE;
576 errorMsg +=
" bytes). Only wrote ";
577 errorMsg += numWrite;
578 errorMsg +=
" bytes.";
579 std::string errorString = errorMsg.c_str();
588 void GlfRecord::writeType2(
IFILE filePtr)
const
591 writeRtypeRef(filePtr);
594 int numWrite =
ifwrite(filePtr, &myRec2Base, REC2_BASE_SIZE);
595 if(numWrite != REC2_BASE_SIZE)
598 String errorMsg =
"Failed to write record of type 2 base info (";
599 errorMsg += REC2_BASE_SIZE;
600 errorMsg +=
" bytes). Only wrote ";
601 errorMsg += numWrite;
602 errorMsg +=
" bytes.";
603 std::string errorString = errorMsg.c_str();
609 int len = myIndelSeq1.length();
610 numWrite =
ifwrite(filePtr, myIndelSeq1.c_str(), len);
614 String errorMsg =
"Failed to write record of type 2, 1st indel sequence (";
616 errorMsg +=
" bytes). Only wrote ";
617 errorMsg += numWrite;
618 errorMsg +=
" bytes.";
619 std::string errorString = errorMsg.c_str();
622 len = myIndelSeq2.length();
623 numWrite =
ifwrite(filePtr, myIndelSeq2.c_str(), len);
627 String errorMsg =
"Failed to write record of type 2, 2nd indel sequence (";
629 errorMsg +=
" bytes). Only wrote ";
630 errorMsg += numWrite;
631 errorMsg +=
" bytes.";
632 std::string errorString = errorMsg.c_str();
GlfException objects should be thrown by functions that operate on Glf files for exceptions.
bool setLk(int index, uint8_t value)
Set the likelihood for the specified genotype.
void print() const
Print the reference section in a readable format.
bool setDeletionIndel2(const std::string &indelSeq)
Set the sequence of the 2nd indel allele if the 2nd indel is an deletion.
int getRecordType() const
Return the record type.
uint8_t getLkHet()
Return the likelihood of a heterozygote.
bool write(IFILE filePtr) const
Write the record to the specified file.
uint8_t getLkHom1()
Return the likelihood of the 1st homozygous indel allele.
char getRefBaseChar() const
Return the reference base as a character.
bool setLkHom1(uint8_t lk)
Set the likelihood of the first homozygous indel allele.
int getRefBase() const
Return the reference base as an integer.
bool setDeletionIndel1(const std::string &indelSeq)
Set the sequence of the first indel allele if the first indel is an deletion.
bool setRmsMapQ(uint8_t rmsMapQ)
Set the RMS of mapping qualities of reads covering the site.
uint8_t getRmsMapQ() const
Return the RMS of mapping qualities of reads covering the site.
bool setInsertionIndel2(const std::string &indelSeq)
Set the sequence of the 2nd indel allele if the 2nd indel is an insertion.
uint32_t getOffset() const
Return the offset from the precedent record.
bool setRecordType(uint8_t recType)
Set the record type.
uint32_t getMinDepth() const
Return the minimum likelihood and read depth.
bool setRefBaseInt(uint8_t refBase)
Set the reference base from an integer value.
bool setReadDepth(uint32_t readDepth)
Set the the read depth.
bool setLkHet(uint8_t lk)
Set the likelihood of a heterozygote.
uint8_t getMinLk() const
Return the minimum likelihood.
bool setRtypeRef(uint8_t rtypeRef)
Set the record type and reference base.
bool setLkHom2(uint8_t lk)
Set the likelihood of the 2nd homozygous indel allele.
int16_t getIndel1(std::string &indelSeq)
Get the sequence and length (+:ins, -:del) of the 1st indel allele.
int16_t getIndel2(std::string &indelSeq)
Get the sequence and length (+:ins, -:del) of the 2nd indel allele.
bool read(IFILE filePtr)
Read the record from the specified file (file MUST be in the correct position for reading a record).
uint32_t getReadDepth() const
Return the read depth.
uint8_t getLk(int index)
Get the likelihood for the specified genotype index.
bool setMinLk(uint8_t minLk)
Set the minimum likelihood.
void reset()
Clear this record back to the default setting.
uint8_t getLkHom2()
Return the likelihood of the 2nd homozygous indel allele.
bool setOffset(uint32_t offset)
Set the offset from the precedent record.
bool setInsertionIndel1(const std::string &indelSeq)
Set the sequence of the first indel allele if the first indel is an insertion.
bool setMinDepth(uint32_t minDepth)
Set the minimum likelihood and the read depth.
@ UNKNOWN
unknown result (default value should never be used)
@ FAIL_IO
method failed due to an I/O issue.