Loading...
Searching...
No Matches
SamHeaderRecord Class Referenceabstract

This class encapsulates the tag value pairs contained with a SAM Header line with accessors for getting and setting the tags within this header. More...

`#include <SamHeaderRecord.h>`

Inheritance diagram for SamHeaderRecord:
[legend]
Collaboration diagram for SamHeaderRecord:
[legend]

## Public Types

enum | SamHeaderRecordType { HD , SQ , RG , PG }
| Specifies the Type for the sam header record (line). More...

## Public Member Functions

| SamHeaderRecord ()
| Constructor.
virtual | ~SamHeaderRecord ()
| Destructor.
virtual SamHeaderRecord * | createCopy () const =0
| Return a pointer to a newly created header record of the appropriate type that is a copy of this record.
bool | setFields (const StringArray &tokens)
| Set the fields from the passed in line.
bool | isValid ()
| Check to see if the record is valid.
const char * | getTagValue (const char *tag) const
| Return the value associated with the specified tag.
bool | setTag (const char *tag, const char *value)
| Set the value of the specified tag to the specified value, deletes the tag when value is NULL.
void | reset ()
| Reset this header record to an empty state with no tags.
bool | appendString (std::string &header)
| Appends the string representation of this header record to the passed in string.
bool | addKey (const char *value)
| Add the key tag with the specified value (not for HD headers).
const char * | getKeyValue () const
| Get the value associated with the key tag. Returns "" if it is not set.
bool | isActiveHeaderRecord ()
| This record is active (true) if there is at least one tag set.
const char * | getTypeString ()
| Return the type of this header record (HD, SQ, RG, or PG) as a string.
SamHeaderRecordType | getType ()
| Return the type of this header record (HD, SQ, RG, or PG) as an enum.

## Protected Member Functions

void | addRequiredTag (const char *requiredTag)
virtual void | internalCopy (SamHeaderRecord &newRec) const

## Protected Attributes

std::string | myTypeString
SamHeaderRecordType | myType
std::string | myKeyTag

## Detailed Description

This class encapsulates the tag value pairs contained with a SAM Header line with accessors for getting and setting the tags within this header.

Definition at line 27 of file SamHeaderRecord.h.

## Member Enumeration Documentation

## SamHeaderRecordType

enum SamHeaderRecord::SamHeaderRecordType

Specifies the Type for the sam header record (line).

Enumerator
HD | Header.
SQ | Sequence Dictionary.
RG | Read Group.
PG | Program.

Definition at line 31 of file SamHeaderRecord.h.

{
HD, ///< Header
SQ, ///< Sequence Dictionary
RG, ///< Read Group
PG ///< Program
};
SamHeaderRecord::HD
@ HD
Header.
Definition SamHeaderRecord.h:32
SamHeaderRecord::SQ
@ SQ
Sequence Dictionary.
Definition SamHeaderRecord.h:33
SamHeaderRecord::PG
@ PG
Program.
Definition SamHeaderRecord.h:35
SamHeaderRecord::RG
@ RG
Read Group.
Definition SamHeaderRecord.h:34

## Constructor & Destructor Documentation

## SamHeaderRecord()

SamHeaderRecord::SamHeaderRecord | ( |  | ) |

Constructor.

Definition at line 21 of file SamHeaderRecord.cpp.

: myTagHash(),
myTags(),
myNumActiveTags(0)
{
}

Referenced by SamHeaderHD::createCopy(), SamHeaderPG::createCopy(), createCopy(), SamHeaderRG::createCopy(), and SamHeaderSQ::createCopy().

## ~SamHeaderRecord()

SamHeaderRecord::~SamHeaderRecord | ( |  | ) |

virtual

Destructor.

Definition at line 30 of file SamHeaderRecord.cpp.

{
reset();
}
SamHeaderRecord::reset
void reset()
Reset this header record to an empty state with no tags.
Definition SamHeaderRecord.cpp:212

References reset().

## Member Function Documentation

## addKey()

bool SamHeaderRecord::addKey | ( | const char * | value | ) |

Add the key tag with the specified value (not for HD headers).

Definition at line 273 of file SamHeaderRecord.cpp.

{
if(myKeyTag.size() == 0)
{
return(false);
}
return(setTag(myKeyTag.data(), value));
}
SamHeaderRecord::setTag
bool setTag(const char *tag, const char *value)
Set the value of the specified tag to the specified value, deletes the tag when value is NULL.
Definition SamHeaderRecord.cpp:119

References setTag().

Referenced by SamFileHeader::setPGTag(), SamFileHeader::setRGTag(), and SamFileHeader::setSQTag().

## addRequiredTag()

void SamHeaderRecord::addRequiredTag | ( | const char * | requiredTag | ) |

protected

Definition at line 321 of file SamHeaderRecord.cpp.

{
myRequiredTags.push_back(requiredTag);
}

## appendString()

bool SamHeaderRecord::appendString | ( | std::string & | header | ) |

Appends the string representation of this header record to the passed in string.

Definition at line 234 of file SamHeaderRecord.cpp.

{
// Track whether or not the header type has been written.
// Only write the header type if at least one of the tags has
// an associated value.
bool writtenHeader = false;
if(isActiveHeaderRecord() && isValid())
{
// Loop through all the entries in the tag vector.
for(unsigned int vectorIndex = 0;
vectorIndex < myTags.size();
vectorIndex++)
{
if(!writtenHeader && (myTags[vectorIndex]->hasValue()))
{
// The tag has a value and the header type has not yet been written,
// so write it.
header += "@";
header += myTypeString;
writtenHeader = true;
}
myTags[vectorIndex]->getTagString(header);
}
// If a header has been written, add a new line character.
if(writtenHeader)
{
header += "\n";
return(true);
}
}
// Nothing was written, return false.
return(false);
}
SamHeaderRecord::isValid
bool isValid()
Check to see if the record is valid.
Definition SamHeaderRecord.cpp:78
SamHeaderRecord::isActiveHeaderRecord
bool isActiveHeaderRecord()
This record is active (true) if there is at least one tag set.
Definition SamHeaderRecord.cpp:301

References isActiveHeaderRecord(), and isValid().

## createCopy()

virtual SamHeaderRecord * SamHeaderRecord::createCopy | ( |  | ) | const

pure virtual

Return a pointer to a newly created header record of the appropriate type that is a copy of this record.

The newly created record will not be deleted by this class and it is the responsibility of the calling method to handle the deletion. Returns NULL on failure to copy.

Implemented in SamHeaderHD, SamHeaderPG, SamHeaderRG, and SamHeaderSQ.

References SamHeaderRecord().

Referenced by SamFileHeader::addRecordCopy().

## getKeyValue()

const char * SamHeaderRecord::getKeyValue | ( |  | ) | const

Get the value associated with the key tag. Returns "" if it is not set.

Definition at line 284 of file SamHeaderRecord.cpp.

{
// Look up the tag in myTags.
int index = myTagHash.Integer(myKeyTag.c_str());
if(index < 0)
{
// The tag was not found in the hash, so return "".
return("");
}
// The tag was found in the hash, so return the tag value found at the
// index associated with the tag.
return(myTags[index]->getValue());
}

## getTagValue()

const char * SamHeaderRecord::getTagValue | ( | const char * | tag | ) | const

Return the value associated with the specified tag.

Returns "" if it is not set.

Definition at line 100 of file SamHeaderRecord.cpp.

{
// Look up the tag in myTags.
int index = myTagHash.Integer(tag);
if(index < 0)
{
// The tag was not found in the hash, so return "".
return("");
}
// The tag was found in the hash, so return the tag value found at the
// index associated with the tag.
return(myTags[index]->getValue());
}

Referenced by SamFileHeader::addPG(), SamFileHeader::addRG(), SamFileHeader::addSQ(), SamFileHeader::getPGTagValue(), SamFileHeader::getRGTagValue(), and SamFileHeader::getSQTagValue().

## getType()

SamHeaderRecord::SamHeaderRecordType SamHeaderRecord::getType | ( |  | ) |

Return the type of this header record (HD, SQ, RG, or PG) as an enum.

Definition at line 315 of file SamHeaderRecord.cpp.

{
return(myType);
}

Referenced by SamFileHeader::addRecordCopy(), and SamFileHeader::getNextHeaderRecord().

## getTypeString()

const char * SamHeaderRecord::getTypeString | ( |  | ) |

Return the type of this header record (HD, SQ, RG, or PG) as a string.

Definition at line 308 of file SamHeaderRecord.cpp.

{
return(myTypeString.c_str());
}

## internalCopy()

void SamHeaderRecord::internalCopy | ( | SamHeaderRecord & | newRec | ) | const

protected virtual

Definition at line 327 of file SamHeaderRecord.cpp.

{
newRec.myTagHash = myTagHash;
newRec.myTags.clear();
// Loop through copying the tags.
for(unsigned int vectorIndex = 0;
vectorIndex < myTags.size();
vectorIndex++)
{
if(myTags[vectorIndex] != NULL)
{
newRec.myTags.push_back(new SamHeaderTag(*(myTags[vectorIndex])));
}
}
newRec.myRequiredTags = myRequiredTags;
newRec.myNumActiveTags = myNumActiveTags;
}

## isActiveHeaderRecord()

bool SamHeaderRecord::isActiveHeaderRecord | ( |  | ) |

This record is active (true) if there is at least one tag set.

Definition at line 301 of file SamHeaderRecord.cpp.

{
return(myNumActiveTags != 0);
}

Referenced by appendString(), SamFileHeader::getNextHeaderRecord(), and SamFileHeader::getNextHeaderRecord().

## isValid()

bool SamHeaderRecord::isValid | ( |  | ) |

Check to see if the record is valid.

Definition at line 78 of file SamHeaderRecord.cpp.

{
bool status = true;
// Check that the required tags are set. If they aren't, return false.
for(unsigned int reqIndex = 0; reqIndex < myRequiredTags.size(); reqIndex++)
{
// Check to see if the required tag at this index exists and has
// a value.
int index = myTagHash.Integer(myRequiredTags[reqIndex].c_str());
if((index < 0) || !(myTags[index]->hasValue()))
{
// Did not find the tag, stet status to false.
std::cerr << "ERROR: Missing required tag: "
<< myRequiredTags[reqIndex] << "." << std::endl;
status = false;
}
}
return(status);
}

Referenced by appendString(), and setFields().

## reset()

void SamHeaderRecord::reset | ( |  | ) |

Reset this header record to an empty state with no tags.

Definition at line 212 of file SamHeaderRecord.cpp.

{
// Delete the tag hash.
myTagHash.Clear();
// Loop through deleting all the tags in the vector.
for(unsigned int vectorIndex = 0;
vectorIndex < myTags.size();
vectorIndex++)
{
delete myTags[vectorIndex];
myTags[vectorIndex] = NULL;
}
// Clear the tag vector.
myTags.clear();
myNumActiveTags = 0;
}

Referenced by ~SamHeaderRecord(), SamFileHeader::removePG(), SamFileHeader::removeRG(), and SamFileHeader::removeSQ().

## setFields()

bool SamHeaderRecord::setFields | ( | const StringArray & | tokens | ) |

Set the fields from the passed in line.

Return true if successfully set.

Definition at line 38 of file SamHeaderRecord.cpp.

{
bool status = true;
// Loop through the tags for this type.
// The tags start in column 1 since column 0 contains the type.
for(int columnIndex = 1; columnIndex < tokens.Length(); columnIndex++)
{
// Validate that the tag is at least 3 characters. Two for the token,
// one for the ':'.
if((tokens[columnIndex].Length() < 3) ||
(tokens[columnIndex][2] != ':'))
{
// Continue to the next tag, this one is too small/invalid.
status = false;
std::cerr << "ERROR: Poorly formatted tag in header: "
<< tokens[columnIndex] << std::endl;
continue;
}
// Get the tag from the token.
char tag[3];
tag[0] = tokens[columnIndex][0];
tag[1] = tokens[columnIndex][1];
tag[2] = 0;
// The tag value is the rest of the substring.
String tagValue = (tokens[columnIndex]).SubStr(3);
// Set the tag.
status &= setTag(tag, tagValue.c_str());
}
status &= isValid();
return(status);
}

References isValid(), and setTag().

## setTag()

bool SamHeaderRecord::setTag | ( | const char * | tag,
|  | const char * | value )

Set the value of the specified tag to the specified value, deletes the tag when value is NULL.

Returns whether or not it was successful, fails if tag is the key tag and the key tag already exists.

Definition at line 119 of file SamHeaderRecord.cpp.

{
// Lookup the tag in the hash.
int vectorIndex = myTagHash.Integer(tag);
if(vectorIndex < 0)
{
// The tag was not found in the hash, so create a new one.
SamHeaderTag* tagPtr = new SamHeaderTag(tag, value);
if(tagPtr == NULL)
{
// Failed to allocate the tag, return false.
std::cerr << "Failed to allocate space (new) for a SamHeaderTag.\n";
return(false);
}
// Add the new tag to the back of the tag values.
vectorIndex = myTags.size();
myTags.push_back(tagPtr);
// If the value is not null, increment the number of active tags.
if(value[0] != 0)
{
++myNumActiveTags;
}
// Add the tag to the hash.
int hashIndex = myTagHash.Add(tag, vectorIndex);
if((myTagHash.Integer(hashIndex) != vectorIndex) ||
(myTagHash[hashIndex] != tag))
{
// Failed to add the tag, so return false.
std::cerr << "Failed to add tag, " << tag
<< ", to the hash." << std::endl;
return(false);
}
return(true);
}
else if((unsigned int)vectorIndex < myTags.size())
{
// Found the tag in the hash. So, update the tag if it
// is not the key.
if(myKeyTag != tag)
{
// Not the key, so update the tag.
// If the new value is null and the old one is not, decrement the
// number of active tags.
if((value[0] == 0) && ((myTags[vectorIndex]->getValue())[0] != 0))
{
// Tag was deleted since the new value is blank but the old
// value was not.
--myNumActiveTags;
}
else if((value[0] != 0) &&
((myTags[vectorIndex]->getValue())[0] == 0))
{
// Tag was added since the old value was blank and the new value
// is not.
++myNumActiveTags;
}
// Just modifying a tag, so this does not affect the number
// of active tags.
return(myTags[vectorIndex]->setValue(value));
}
else if(strcmp(value, myTags[vectorIndex]->getValue()) == 0)
{
// The new key value is the same as the previous value, so
// it is not a change, return true.
return(true);
}
else
{
// Can't modify the key tag's value since that will
// screw up the hash.
std::cerr << "Can't modify the key tag, " << tag << " from "
<< myTags[vectorIndex]->getValue() << " to "
<< value << std::endl;
return(false);
}
}
// Got an invalid index from the hash. This is not supposed to happen.
// so return false.
std::cerr << "Invalid tag index found: " << vectorIndex
<< ", but max index is " << myTags.size() << " for tag: "
<< tag << std::endl;
return(false);
}

Referenced by addKey(), setFields(), SamFileHeader::setPGTag(), SamFileHeader::setRGTag(), and SamFileHeader::setSQTag().

## Member Data Documentation

## myKeyTag

std::string SamHeaderRecord::myKeyTag

protected

Definition at line 105 of file SamHeaderRecord.h.

## myType

SamHeaderRecordType SamHeaderRecord::myType

protected

Definition at line 100 of file SamHeaderRecord.h.

## myTypeString

std::string SamHeaderRecord::myTypeString

protected

Definition at line 97 of file SamHeaderRecord.h.

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

- bam/SamHeaderRecord.h
- bam/SamHeaderRecord.cpp