libStatGen Software  1
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:
Collaboration diagram for SamHeaderRecord:

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 SamHeaderRecordcreateCopy () const =0
 Return a pointer to a newly created header record of the appropriate type that is a copy of this record. More...
 
bool setFields (const StringArray &tokens)
 Set the fields from the passed in line. More...
 
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. More...
 
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. More...
 
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

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.

31  {
32  HD, ///< Header
33  SQ, ///< Sequence Dictionary
34  RG, ///< Read Group
35  PG ///< Program
36  };
@ SQ
Sequence Dictionary.
@ RG
Read Group.

Member Function Documentation

◆ 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 SamHeaderSQ, SamHeaderRG, SamHeaderPG, and SamHeaderHD.

Referenced by SamFileHeader::addRecordCopy().

◆ 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.

101 {
102  // Look up the tag in myTags.
103  int index = myTagHash.Integer(tag);
104  if(index < 0)
105  {
106  // The tag was not found in the hash, so return "".
107  return("");
108  }
109 
110  // The tag was found in the hash, so return the tag value found at the
111  // index associated with the tag.
112  return(myTags[index]->getValue());
113 }

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

◆ 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.

39 {
40  bool status = true;
41 
42  // Loop through the tags for this type.
43  // The tags start in column 1 since column 0 contains the type.
44  for(int columnIndex = 1; columnIndex < tokens.Length(); columnIndex++)
45  {
46  // Validate that the tag is at least 3 characters. Two for the token,
47  // one for the ':'.
48  if((tokens[columnIndex].Length() < 3) ||
49  (tokens[columnIndex][2] != ':'))
50  {
51  // Continue to the next tag, this one is too small/invalid.
52  status = false;
53  std::cerr << "ERROR: Poorly formatted tag in header: "
54  << tokens[columnIndex] << std::endl;
55  continue;
56  }
57 
58  // Get the tag from the token.
59  char tag[3];
60  tag[0] = tokens[columnIndex][0];
61  tag[1] = tokens[columnIndex][1];
62  tag[2] = 0;
63 
64  // The tag value is the rest of the substring.
65  String tagValue = (tokens[columnIndex]).SubStr(3);
66 
67  // Set the tag.
68  status &= setTag(tag, tagValue.c_str());
69  }
70 
71  status &= isValid();
72 
73  return(status);
74 }
bool isValid()
Check to see if the record is valid.
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.

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.

120 {
121  // Lookup the tag in the hash.
122  int vectorIndex = myTagHash.Integer(tag);
123  if(vectorIndex < 0)
124  {
125  // The tag was not found in the hash, so create a new one.
126  SamHeaderTag* tagPtr = new SamHeaderTag(tag, value);
127 
128  if(tagPtr == NULL)
129  {
130  // Failed to allocate the tag, return false.
131  std::cerr << "Failed to allocate space (new) for a SamHeaderTag.\n";
132  return(false);
133  }
134 
135  // Add the new tag to the back of the tag values.
136  vectorIndex = myTags.size();
137  myTags.push_back(tagPtr);
138 
139  // If the value is not null, increment the number of active tags.
140  if(value[0] != 0)
141  {
142  ++myNumActiveTags;
143  }
144 
145  // Add the tag to the hash.
146  int hashIndex = myTagHash.Add(tag, vectorIndex);
147 
148  if((myTagHash.Integer(hashIndex) != vectorIndex) ||
149  (myTagHash[hashIndex] != tag))
150  {
151  // Failed to add the tag, so return false.
152  std::cerr << "Failed to add tag, " << tag
153  << ", to the hash." << std::endl;
154  return(false);
155  }
156  return(true);
157  }
158  else if((unsigned int)vectorIndex < myTags.size())
159  {
160  // Found the tag in the hash. So, update the tag if it
161  // is not the key.
162  if(myKeyTag != tag)
163  {
164  // Not the key, so update the tag.
165  // If the new value is null and the old one is not, decrement the
166  // number of active tags.
167  if((value[0] == 0) && ((myTags[vectorIndex]->getValue())[0] != 0))
168  {
169  // Tag was deleted since the new value is blank but the old
170  // value was not.
171  --myNumActiveTags;
172  }
173  else if((value[0] != 0) &&
174  ((myTags[vectorIndex]->getValue())[0] == 0))
175  {
176  // Tag was added since the old value was blank and the new value
177  // is not.
178  ++myNumActiveTags;
179  }
180 
181  // Just modifying a tag, so this does not affect the number
182  // of active tags.
183  return(myTags[vectorIndex]->setValue(value));
184  }
185  else if(strcmp(value, myTags[vectorIndex]->getValue()) == 0)
186  {
187  // The new key value is the same as the previous value, so
188  // it is not a change, return true.
189  return(true);
190  }
191  else
192  {
193  // Can't modify the key tag's value since that will
194  // screw up the hash.
195  std::cerr << "Can't modify the key tag, " << tag << " from "
196  << myTags[vectorIndex]->getValue() << " to "
197  << value << std::endl;
198  return(false);
199  }
200  }
201 
202  // Got an invalid index from the hash. This is not supposed to happen.
203  // so return false.
204  std::cerr << "Invalid tag index found: " << vectorIndex
205  << ", but max index is " << myTags.size() << " for tag: "
206  << tag << std::endl;
207  return(false);
208 }

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


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