Loading...
Searching...
No Matches
SamInterface Class Reference
Inheritance diagram for SamInterface:
[legend]
Collaboration diagram for SamInterface:
[legend]

## Public Member Functions

virtual bool | readHeader (IFILE filePtr, SamFileHeader &header, SamStatus &status)
virtual bool | writeHeader (IFILE filePtr, SamFileHeader &header, SamStatus &status)
virtual void | readRecord (IFILE filePtr, SamFileHeader &header, SamRecord &record, SamStatus &samStatus)
virtual SamStatus::Status | writeRecord (IFILE filePtr, SamFileHeader &header, SamRecord &record, SamRecord::SequenceTranslation translation)
virtual bool | isEOF (IFILE filePtr)

## Detailed Description

Definition at line 23 of file SamInterface.h.

## Constructor & Destructor Documentation

## SamInterface()

SamInterface::SamInterface | ( |  | ) |

Definition at line 24 of file SamInterface.cpp.

: myFirstRecord("")
{
}

## ~SamInterface()

SamInterface::~SamInterface | ( |  | ) |

Definition at line 30 of file SamInterface.cpp.

{
}

## Member Function Documentation

## isEOF()

bool SamInterface::isEOF | ( | IFILE | filePtr | ) |

virtual

Reimplemented from GenericSamInterface.

Definition at line 419 of file SamInterface.cpp.

{
if(myFirstRecord.Length() != 0)
{
// First record is set, so return false, not EOF, since we
// know the record still needs to be processed.
return(false);
}
return(GenericSamInterface::isEOF(filePtr));
}

## readHeader()

bool SamInterface::readHeader | ( | IFILE | filePtr,
|  | SamFileHeader & | header,
|  | SamStatus & | status )

virtual

Implements GenericSamInterface.

Definition at line 36 of file SamInterface.cpp.

{
if(filePtr == NULL)
{
// File is not open.
status.setStatus(SamStatus::FAIL_ORDER,
"Cannot read header since the file pointer is null");
return(false);
}
// Clear the passed in header.
header.resetHeader();
int numValid = 0;
int numInvalid = 0;
std::string errorMessages = "";
do {
StringIntHash tags;
StringArray values;
buffer.ReadLine(filePtr);
// Stop reading header lines if at the end of the file or
// if the line is not blank and does not start with an @.
if ( ifeof(filePtr) ||
((buffer.Length() != 0) && (buffer[0] != '@')) )
{
break;
}
// This is a header line, so add it to header.
if(header.addHeaderLine(buffer.c_str()))
{
if(buffer.Length() != 0)
{
++numValid;
}
}
else
{
++numInvalid;
// Failed reading the header.
errorMessages += header.getErrorMessage();
// Skip further processing on this line since it was an error.
continue;
}
} while (1);
// Store the first record since it was read.
myFirstRecord = buffer;
if(numInvalid > 0)
{
if(numValid == 0)
{
std::cerr << "Failed to parse " << numInvalid << " header lines";
std::cerr << ". No valid header lines.\n";
status.setStatus(SamStatus::FAIL_PARSE, errorMessages.c_str());
return(false);
}
}
// Successfully read.
return(true);
}
ifeof
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition InputFile.h:654
SamFileHeader::getErrorMessage
const char * getErrorMessage()
Get the failure message if a method returned failure.
Definition SamFileHeader.h:423
SamFileHeader::addHeaderLine
bool addHeaderLine(const char *type, const char *tag, const char *value)
Add a header line that is just one tag with a const char* value.
Definition SamFileHeader.cpp:180
SamFileHeader::resetHeader
void resetHeader()
Initialize the header.
Definition SamFileHeader.cpp:90
StatGenStatus::FAIL_PARSE
@ FAIL_PARSE
failed to parse a record/header - invalid format.
Definition StatGenStatus.h:42
StatGenStatus::FAIL_ORDER
@ FAIL_ORDER
FAIL_ORDER: method failed because it was called out of order, like trying to read a file without open...
Definition StatGenStatus.h:41
StatGenStatus::setStatus
void setStatus(Status newStatus, const char *newMessage)
Set the status with the specified status enum and message.
Definition StatGenStatus.cpp:83

## readRecord()

void SamInterface::readRecord | ( | IFILE | filePtr,
|  | SamFileHeader & | header,
|  | SamRecord & | record,
|  | SamStatus & | samStatus )

virtual

Implements GenericSamInterface.

Definition at line 136 of file SamInterface.cpp.

{
// Initialize the status to success - will be set to false on failure.
samStatus = SamStatus::SUCCESS;
if((filePtr == NULL) || (filePtr->isOpen() == false))
{
// File is not open.
samStatus.addError(SamStatus::FAIL_ORDER,
"filePtr does not point to an open file.");
return;
}
// If the first record has been set, use that and clear it,
// otherwise read the record from the file.
if(myFirstRecord.Length() != 0)
{
buffer = myFirstRecord;
myFirstRecord.Clear();
}
else
{
// Read the next record.
buffer.Clear();
buffer.ReadLine(filePtr);
// If the end of the file and nothing was read, return false.
if ((ifeof(filePtr)) && (buffer.Length() == 0))
{
// end of the file and nothing to process.
samStatus.addError(SamStatus::NO_MORE_RECS,
"No more records in the file.");
return;
}
}
tokens.ReplaceColumns(buffer, '\t');
// Error string for reporting a parsing failure.
String errorString = "";
if (tokens.Length() < 11)
{
errorString = "Too few columns (";
errorString += tokens.Length();
errorString += ") in the Record, expected at least 11.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
return;
}
// Reset the record before setting any fields.
record.resetRecord();
if(!record.setReadName(tokens[0]))
{
samStatus.addError(record.getStatus());
}
long flagInt = 0;
if(!tokens[1].AsInteger(flagInt))
{
errorString = "flag, ";
errorString += tokens[1].c_str();
errorString += ", is not an integer.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if((flagInt < 0) || (flagInt > UINT16_MAX))
{
errorString = "flag, ";
errorString += tokens[1].c_str();
errorString += ", is not between 0 and (2^16)-1 = 65535.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if(!record.setFlag(flagInt))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
if(!record.setReferenceName(header, tokens[2]))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
long posInt = 0;
if(!tokens[3].AsInteger(posInt))
{
errorString = "position, ";
errorString += tokens[3].c_str();
errorString += ", is not an integer.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if((posInt < INT32_MIN) || (posInt > INT32_MAX))
{
// If it is not in this range, it cannot fit into a 32 bit int.
errorString = "position, ";
errorString += tokens[3].c_str();
errorString += ", does not fit in a 32 bit signed int.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if(!record.set1BasedPosition(posInt))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
long mapInt = 0;
if(!tokens[4].AsInteger(mapInt))
{
errorString = "map quality, ";
errorString += tokens[4].c_str();
errorString += ", is not an integer.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if((mapInt < 0) || (mapInt > UINT8_MAX))
{
errorString = "map quality, ";
errorString += tokens[4].c_str();
errorString += ", is not between 0 and (2^8)-1 = 255.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if(!record.setMapQuality(mapInt))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
if(!record.setCigar(tokens[5]))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
if(!record.setMateReferenceName(header, tokens[6]))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
long matePosInt = 0;
if(!tokens[7].AsInteger(matePosInt))
{
errorString = "mate position, ";
errorString += tokens[7].c_str();
errorString += ", is not an integer.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if(!record.set1BasedMatePosition(matePosInt))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
long insertInt = 0;
if(!tokens[8].AsInteger(insertInt))
{
errorString = "insert size, ";
errorString += tokens[8].c_str();
errorString += ", is not an integer.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
}
else if(!record.setInsertSize(insertInt))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
if(!record.setSequence(tokens[9]))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
if(!record.setQuality(tokens[10]))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
// Clear the tag fields.
record.clearTags();
// Add the tags to the record.
for (int i = 11; i < tokens.Length(); i++)
{
String & nugget = tokens[i];
if (nugget.Length() < 6 || nugget[2] != ':' || nugget[4] != ':')
{
// invalid tag format.
errorString = "Invalid Tag Format: ";
errorString += nugget.c_str();
errorString += ", should be cc:c:x*.";
samStatus.addError(SamStatus::FAIL_PARSE,
errorString.c_str());
continue;
}
// Valid tag format.
// Add the tag.
if(!record.addTag((const char *)nugget, nugget[3],
(const char *)nugget + 5))
{
samStatus.addError(record.getStatus().getStatus(),
record.getStatus().getStatusMessage());
}
}
return;
}
InputFile::isOpen
bool isOpen() const
Returns whether or not the file was successfully opened.
Definition InputFile.h:423
SamRecord::setReadName
bool setReadName(const char *readName)
Set QNAME to the passed in name.
Definition SamRecord.cpp:193
SamRecord::clearTags
void clearTags()
Clear the tags in this record.
Definition SamRecord.cpp:977
SamRecord::setInsertSize
bool setInsertSize(int32_t insertSize)
Sets the inferred insert size (ISIZE)/observed template length (TLEN).
Definition SamRecord.cpp:336
SamRecord::setMateReferenceName
bool setMateReferenceName(SamFileHeader &header, const char *mateReferenceName)
Set the mate/next fragment's reference sequence name (RNEXT) to the specified name,...
Definition SamRecord.cpp:297
SamRecord::setMapQuality
bool setMapQuality(uint8_t mapQuality)
Set the mapping quality (MAPQ).
Definition SamRecord.cpp:251
SamRecord::addTag
bool addTag(const char *tag, char vtype, const char *value)
Add the specified tag,vtype,value to the record.
Definition SamRecord.cpp:791
SamRecord::resetRecord
void resetRecord()
Reset the fields of the record to a default value.
Definition SamRecord.cpp:91
SamRecord::setFlag
bool setFlag(uint16_t flag)
Set the bitwise FLAG to the specified value.
Definition SamRecord.cpp:215
SamRecord::set1BasedPosition
bool set1BasedPosition(int32_t position)
Set the leftmost position (POS) using the specified 1-based (SAM format) value.
Definition SamRecord.cpp:236
SamRecord::getStatus
const SamStatus & getStatus()
Returns the status associated with the last method that sets the status.
Definition SamRecord.cpp:2403
SamRecord::setCigar
bool setCigar(const char *cigar)
Set the CIGAR to the specified SAM formatted cigar string.
Definition SamRecord.cpp:259
SamRecord::setSequence
bool setSequence(const char *seq)
Sets the sequence (SEQ) to the specified SAM formatted sequence string.
Definition SamRecord.cpp:344
SamRecord::set1BasedMatePosition
bool set1BasedMatePosition(int32_t matePosition)
Set the mate/next fragment's leftmost position (PNEXT) using the specified 1-based (SAM format) value...
Definition SamRecord.cpp:322
SamRecord::setQuality
bool setQuality(const char *quality)
Sets the quality (QUAL) to the specified SAM formatted quality string.
Definition SamRecord.cpp:357
SamRecord::setReferenceName
bool setReferenceName(SamFileHeader &header, const char *referenceName)
Set the reference sequence name (RNAME) to the specified name, using the header to determine the refe...
Definition SamRecord.cpp:223
StatGenStatus::getStatusMessage
const char * getStatusMessage() const
Return the status message for this object.
Definition StatGenStatus.cpp:149
StatGenStatus::NO_MORE_RECS
@ NO_MORE_RECS
NO_MORE_RECS: failed to read a record since there are no more to read either in the file or section i...
Definition StatGenStatus.h:36
StatGenStatus::SUCCESS
@ SUCCESS
method completed successfully.
Definition StatGenStatus.h:32
StatGenStatus::getStatus
Status getStatus() const
Return the enum for this status object.
Definition StatGenStatus.cpp:142
StatGenStatus::addError
void addError(Status newStatus, const char *newMessage)
Add the specified error message to the status message, setting the status to newStatus if the current...
Definition StatGenStatus.cpp:99

## writeHeader()

bool SamInterface::writeHeader | ( | IFILE | filePtr,
|  | SamFileHeader & | header,
|  | SamStatus & | status )

virtual

Implements GenericSamInterface.

Definition at line 103 of file SamInterface.cpp.

{
if((filePtr == NULL) || (filePtr->isOpen() == false))
{
// File is not open, return failure.
status.setStatus(SamStatus::FAIL_ORDER,
"Cannot write header since the file pointer is null");
return(false);
}
////////////////////////////////
// Write the header to the file.
////////////////////////////////
// Construct a string containing the entire header.
std::string headerString = "";
header.getHeaderString(headerString);
int32_t headerLen = headerString.length();
int numWrite = 0;
// Write the header to the file.
numWrite = ifwrite(filePtr, headerString.c_str(), headerLen);
if(numWrite != headerLen)
{
status.setStatus(SamStatus::FAIL_IO,
"Failed to write the SAM header.");
return(false);
}
return(true);
}
ifwrite
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
SamFileHeader::getHeaderString
bool getHeaderString(std::string &header) const
Set the passed in string to the entire header string, clearing its current contents.
Definition SamFileHeader.cpp:131
StatGenStatus::FAIL_IO
@ FAIL_IO
method failed due to an I/O issue.
Definition StatGenStatus.h:37

## writeRecord()

SamStatus::Status SamInterface::writeRecord | ( | IFILE | filePtr,
|  | SamFileHeader & | header,
|  | SamRecord & | record,
|  | SamRecord::SequenceTranslation | translation )

virtual

Implements GenericSamInterface.

Definition at line 360 of file SamInterface.cpp.

{
// Store all the fields into a string, then write the string.
String recordString = record.getReadName();
recordString += "\t";
recordString += record.getFlag();
recordString += "\t";
recordString += record.getReferenceName();
recordString += "\t";
recordString += record.get1BasedPosition();
recordString += "\t";
recordString += record.getMapQuality();
recordString += "\t";
recordString += record.getCigar();
recordString += "\t";
recordString += record.getMateReferenceNameOrEqual();
recordString += "\t";
recordString += record.get1BasedMatePosition();
recordString += "\t";
recordString += record.getInsertSize();
recordString += "\t";
recordString += record.getSequence(translation);
recordString += "\t";
recordString += record.getQuality();
// If there are any tags, add a preceding tab.
if(record.getTagLength() != 0)
{
recordString += "\t";
SamRecordHelper::genSamTagsString(record, recordString);
}
recordString += "\n";
// Write the record.
ifwrite(filePtr, recordString.c_str(), recordString.Length());
return(SamStatus::SUCCESS);
}
SamRecordHelper::genSamTagsString
static bool genSamTagsString(SamRecord &record, String &returnString, char delim='\t')
Helper to append the SAM string representation of all the tags to the specified string.
Definition SamRecordHelper.cpp:56
SamRecord::getReferenceName
const char * getReferenceName()
Get the reference sequence name (RNAME) of the record.
Definition SamRecord.cpp:1298
SamRecord::getInsertSize
int32_t getInsertSize()
Get the inferred insert size of the read pair (ISIZE) or observed template length (TLEN).
Definition SamRecord.cpp:1459
SamRecord::get1BasedPosition
int32_t get1BasedPosition()
Get the 1-based(SAM) leftmost position (POS) of the record.
Definition SamRecord.cpp:1312
SamRecord::getTagLength
uint32_t getTagLength()
Returns the length of the BAM formatted tags.
Definition SamRecord.cpp:1929
SamRecord::getMateReferenceNameOrEqual
const char * getMateReferenceNameOrEqual()
Get the mate/next fragment's reference sequence name (RNEXT), returning "=" if it is the same as the ...
Definition SamRecord.cpp:1420
SamRecord::getFlag
uint16_t getFlag()
Get the flag (FLAG).
Definition SamRecord.cpp:1384
SamRecord::get1BasedMatePosition
int32_t get1BasedMatePosition()
Get the 1-based(SAM) leftmost mate/next fragment's position (PNEXT).
Definition SamRecord.cpp:1445
SamRecord::getCigar
const char * getCigar()
Returns the SAM formatted CIGAR string.
Definition SamRecord.cpp:1555
SamRecord::getMapQuality
uint8_t getMapQuality()
Get the mapping quality (MAPQ) of the record.
Definition SamRecord.cpp:1340
SamRecord::getReadName
const char * getReadName()
Returns the SAM formatted Read Name (QNAME).
Definition SamRecord.cpp:1542
SamRecord::getQuality
const char * getQuality()
Returns the SAM formatted quality string (QUAL).
Definition SamRecord.cpp:1638
SamRecord::getSequence
const char * getSequence()
Returns the SAM formatted sequence string (SEQ), translating the base as specified by setSequenceTran...
Definition SamRecord.cpp:1568

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

- bam/SamInterface.h
- bam/SamInterface.cpp