libStatGen Software  1
SamFileHeader Class Reference

This class allows a user to get/set the fields in a SAM/BAM Header. More...

#include <SamFileHeader.h>

Collaboration diagram for SamFileHeader:

Public Member Functions

Copying a Header

These methods are ways of copying the contents of one header into another one.

 SamFileHeader (const SamFileHeader &header)
 Copy Constructor copies the specified header into this one.
 
SamFileHeaderoperator= (const SamFileHeader &header)
 Overload operator = to copy the passed in header into this header.
 
bool copy (const SamFileHeader &header)
 Copy method copies the passed in header into this header. More...
 
void resetHeader ()
 Initialize the header.
 
Get the Entire Header

Get the entire header as a single string.

bool getHeaderString (std::string &header) const
 Set the passed in string to the entire header string, clearing its current contents. More...
 
int getReferenceID (const String &referenceName, bool addID=false)
 Get the reference ID for the specified reference name (chromosome). More...
 
int getReferenceID (const char *referenceName, bool addID=false)
 Get the reference ID for the specified reference name (chromosome). More...
 
const StringgetReferenceLabel (int id) const
 Return the reference name (chromosome) for the specified reference id.
 
const SamReferenceInfogetReferenceInfo () const
 Get the Reference Information.
 
SamReferenceInfogetReferenceInfoForBamInterface ()
 
Adding an entire header/comment line.

These methods are ways of adding an entire header line at once.

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. More...
 
bool addHeaderLine (const char *headerLine)
 Add a header line that is already preformatted in a const char*. More...
 
bool addHeader (const char *header)
 Add a header that is already preformatted in a const char*. More...
 
bool addComment (const char *comment)
 Add the specified comment to the header (do not include "@CO" or "\n"). More...
 
Set/Add/Remove a Single Tag

The passed in tag should be the two character SAM tag as defined in the SAM spec.

A tag is removed from the header record by setting it to "". For the SQ and RG header types, the key tags (SN for SQ and ID for RG) may not be modified or removed once set. This is because these values are used as a lookup key for the header record, so the entire record must be removed.

bool setHDTag (const char *tag, const char *value)
 Set the specified tag to the specified value in the HD header, remove the tag by specifying value="". More...
 
bool setSQTag (const char *tag, const char *value, const char *name)
 Set the specified tag to the specified value in the SQ header with the specified name, remove the tag by specifying value="". More...
 
bool setRGTag (const char *tag, const char *value, const char *id)
 Set the specified tag to the specified value in the RG header with the specified id, remove the tag by specifying value="". More...
 
bool setPGTag (const char *tag, const char *value, const char *id)
 Set the specified tag to the specified value in the PG header with the specified id, remove the tag by specifying value="". More...
 
Add an Already Setup SamHeaderRecord

NOTE: These methods add a pointer to the passed in record.

The header record will be deleted when it's cleaned up from this header. NOTE: Do NOT delete the passed in record, the SamFileHeader class takes care of that itself.

bool addHD (SamHeaderHD *hd)
 Add the HD record to the header. More...
 
bool addSQ (SamHeaderSQ *sq)
 Add the SQ record to the header. More...
 
bool addRG (SamHeaderRG *rg)
 Add the RG record to the header. More...
 
bool addPG (SamHeaderPG *pg)
 Add the PG record to the header. More...
 
bool addRecordCopy (const SamHeaderRecord &hdrRec)
 Add a copy of the specified header record to the header. More...
 
Remove an Entire Header Record
bool removeHD ()
 Remove the HD record. More...
 
bool removeSQ (const char *name)
 Remove SQ record with the specified key. More...
 
bool removeRG (const char *id)
 Remove RG record with the specified key. More...
 
bool removePG (const char *id)
 Remove PG record with the specified key. More...
 
Get a Specific Tag

These methods return the value associated with the specified tag.

If the tag does not exist in the record "" is returned.

For SQ, RG, and PG the value returned is for the tag associated with the specified key (name/id). If a record with that key does not exist or if the tag does not exist for the record with that key, "" is returned.

const char * getHDTagValue (const char *tag)
 Returns the value associated with the specified HD tag, returning "" if the tag does not exist in the header.
 
const char * getSQTagValue (const char *tag, const char *name)
 Get the value associated with the specified tag on the SQ line with the specified sequence name, returning "" if the tag or key does not exist.
 
const char * getRGTagValue (const char *tag, const char *id)
 Get the value associated with the specified tag on the RG line with the specified read group identifier, returning "" if the tag or key does not exist.
 
const char * getPGTagValue (const char *tag, const char *id)
 Get the value associated with the specified tag on the RG line with the specified id, returning "" if the tag or key does not exist.
 
int getNumSQs ()
 Get the number of SQ objects.
 
int getNumRGs ()
 Get the number of RG objects.
 
int getNumPGs ()
 Get the number of PG objects.
 
Get a Specific Header Record

These methods return a reference to the specific record that was requested, returning NULL if that record does not exist in the header.

The returned record can be modified to add/remove some tags. Since a reference is returned, the SamHeaderFile automatically reflects these changes.

SamHeaderHDgetHD ()
 Get the HD object, returning NULL if there is no HD record.
 
SamHeaderSQgetSQ (const char *name)
 Get the SQ object with the specified sequence name, returning NULL if there is no SQ object with that key.
 
SamHeaderRGgetRG (const char *id)
 Get the RG object with the specified read group identifier, returning NULL if there is no RG object with that key. More...
 
SamHeaderPGgetPG (const char *id)
 Get the PG object with the specified id, returning NULL if there is no PG object with that key. More...
 
const char * getSortOrder ()
 Return the Sort Order value that is set in the Header, returning "" if this field does not exist.
 
const char * getTagSO ()
 DEPRECATED.
 

Get the Header Record/Comment/Line by Record/Comment/Line

These methods iterate through the header.

NOTE: both getNextHeaderRecord and getNextHeaderLine increment the same iterator. getNextHeaderRecord that takes a header type uses the same iterator as the getNextXXRecord with that type. Otherwise the iterators are independent.

static const std::string EMPTY_RETURN = ""
 
SamHeaderRecordgetNextSQRecord ()
 Get the next SQ header record. More...
 
SamHeaderRecordgetNextRGRecord ()
 Get the next RG header record. More...
 
SamHeaderRecordgetNextPGRecord ()
 Get the next PG header record. More...
 
void resetSQRecordIter ()
 Reset to the beginning of the header records so the next call to getNextSQRecord returns the first SQ header record.
 
void resetRGRecordIter ()
 Reset to the beginning of the header records so the next call to getNextRGRecord returns the first RG header record.
 
void resetPGRecordIter ()
 Reset to the beginning of the header records so the next call to getNextPGRecord returns the first PG header record.
 
SamHeaderRecordgetNextHeaderRecord (uint32_t &index, SamHeaderRecord::SamHeaderRecordType headerType)
 Get the next header record of the specified type starting from the specified index and update the index. More...
 
SamHeaderRecordgetNextHeaderRecord ()
 Get the next header record, but not comment line. More...
 
bool getNextHeaderLine (std::string &headerLine)
 Set the passed in string to the next header line, overwritting the passed in string. More...
 
void resetHeaderRecordIter ()
 Reset to the beginning of the header records so the next call to getNextHeaderRecord returns the first header line.
 
void appendCommentLines (std::string &commentLines)
 Append all of the comment lines to the specified string.
 
const char * getNextComment ()
 Returns the comment on the next comment line. More...
 
void resetCommentIter ()
 Resets to the beginning of the comments so getNextComment returns the first comment.
 
const char * getErrorMessage ()
 Get the failure message if a method returned failure.
 

Detailed Description

This class allows a user to get/set the fields in a SAM/BAM Header.

Sam/Bam headers contain comments and multiple SamHeaderRecords (HD, SQs, RGs, PGs) comprised of tag/value pairs with each tag only appearing once within a specific record.

Definition at line 34 of file SamFileHeader.h.

Member Function Documentation

◆ addComment()

bool SamFileHeader::addComment ( const char *  comment)

Add the specified comment to the header (do not include "@CO" or "\n").

Returns
true if successfully added, false if not.

Definition at line 213 of file SamFileHeader.cpp.

214 {
215  if((comment != NULL) && (strcmp(comment, EMPTY_RETURN.c_str()) != 0))
216  {
217  // Valid comment, so add it.
218  myComments.push_back(comment);
219  }
220  return(true);
221 }

◆ addHD()

bool SamFileHeader::addHD ( SamHeaderHD hd)

Add the HD record to the header.

Note: it adds a pointer to the passed in header record. The header record will be deleted when it is cleaned up from this header.

Returns
true if the record was successfully added, false otherwise.

Definition at line 393 of file SamFileHeader.cpp.

394 {
395  // If there is already an HD header or if null
396  // was passed in, return false.
397  if(myHD != NULL)
398  {
399  myErrorMessage = "Failed add an HD tag - there is already one";
400  return(false);
401  }
402  if(hd == NULL)
403  {
404  myErrorMessage = "Failed add an HD tag - no tag specified";
405  return(false);
406  }
407  myHD = hd;
408 
409  myHeaderRecords.push_back(myHD);
410  return(true);
411 }

Referenced by addRecordCopy().

◆ addHeader()

bool SamFileHeader::addHeader ( const char *  header)

Add a header that is already preformatted in a const char*.

Returns true if at least one header line was successfully added.

Definition at line 204 of file SamFileHeader.cpp.

205 {
206  // Parse the added header line.
207  String headerString = header;
208  return(parseHeader(headerString));
209 }

◆ addHeaderLine() [1/2]

bool SamFileHeader::addHeaderLine ( const char *  headerLine)

Add a header line that is already preformatted in a const char*.

Returns true if at least one header line was successfully added.

Definition at line 195 of file SamFileHeader.cpp.

196 {
197  // Parse the added header line.
198  String headerString = headerLine;
199  return(parseHeader(headerString));
200 }

◆ addHeaderLine() [2/2]

bool SamFileHeader::addHeaderLine ( const char *  type,
const char *  tag,
const char *  value 
)

Add a header line that is just one tag with a const char* value.

Note: This method will only do one tag per type on a line, so if a type has multiple tags, the whole line needs to be added at once, and a different method should be used.

Definition at line 180 of file SamFileHeader.cpp.

182 {
183  String headerLine;
184  headerLine += "@";
185  headerLine += type;
186  headerLine += "\t";
187  headerLine += tag;
188  headerLine += ":";
189  headerLine += value;
190  return(addHeaderLine(headerLine.c_str()));
191 }
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.

◆ addPG()

bool SamFileHeader::addPG ( SamHeaderPG pg)

Add the PG record to the header.

Note: it adds a pointer to the passed in header record. The header record will be deleted when it is cleaned up from this header.

Returns
true if the record was successfully added, false otherwise.

Definition at line 492 of file SamFileHeader.cpp.

493 {
494  // If a null pointer was passed in, return false.
495  if(pg == NULL)
496  {
497  myErrorMessage = "SAM/BAM Header line failed to allocate PG.";
498  return(false);
499  }
500  const char* id = pg->getTagValue("ID");
501  if(strcmp(id, EMPTY_RETURN.c_str()) == 0)
502  {
503  // ID is not set, so can't add the header record.
504  myErrorMessage = "SAM/BAM Header line failure: Skipping PG line that is missing the ID field.";
505  return(false);
506  }
507 
508  // Determine whether or not a record with this
509  // key is already in the hash.
510  if(myPGs.Find(id) < 0)
511  {
512  // It is not already in the hash so
513  // add it.
514  myPGs.Add(id, pg);
515  myHeaderRecords.push_back(pg);
516  return(true);
517  }
518 
519  // It is already in the hash, so cannot be added.
520  myErrorMessage = "SAM/BAM Header line failure: Skipping PG line that has a repeated ID field.";
521  return(false);
522 }
const char * getTagValue(const char *tag) const
Return the value associated with the specified tag.

References SamHeaderRecord::getTagValue().

Referenced by addRecordCopy().

◆ addRecordCopy()

bool SamFileHeader::addRecordCopy ( const SamHeaderRecord hdrRec)

Add a copy of the specified header record to the header.

Note: it creates a new header record that is identical to the specified one and adds it to the header. The passed in pointer will not be deleted due to this.

Returns
true if the record was successfully added, false otherwise.

Definition at line 526 of file SamFileHeader.cpp.

527 {
528  SamHeaderRecord* newRec = hdrRec.createCopy();
529  bool returnVal = true;
530  switch(newRec->getType())
531  {
532  case SamHeaderRecord::HD:
533  returnVal = addHD((SamHeaderHD*)newRec);
534  break;
535  case SamHeaderRecord::PG:
536  returnVal = addPG((SamHeaderPG*)newRec);
537  break;
538  case SamHeaderRecord::RG:
539  returnVal = addRG((SamHeaderRG*)newRec);
540  break;
541  case SamHeaderRecord::SQ:
542  returnVal = addSQ((SamHeaderSQ*)newRec);
543  break;
544  default:
545  myErrorMessage = "Failed to copy a header record, unknown type.";
546  returnVal = false;
547  break;
548  }
549  return(returnVal);
550 }
bool addPG(SamHeaderPG *pg)
Add the PG record to the header.
bool addRG(SamHeaderRG *rg)
Add the RG record to the header.
bool addSQ(SamHeaderSQ *sq)
Add the SQ record to the header.
bool addHD(SamHeaderHD *hd)
Add the HD record to the header.
This class encapsulates the tag value pairs contained with a SAM Header line with accessors for getti...
SamHeaderRecordType getType()
Return the type of this header record (HD, SQ, RG, or PG) as an enum.
virtual SamHeaderRecord * createCopy() const =0
Return a pointer to a newly created header record of the appropriate type that is a copy of this reco...
@ SQ
Sequence Dictionary.
@ RG
Read Group.

References addHD(), addPG(), addRG(), addSQ(), SamHeaderRecord::createCopy(), SamHeaderRecord::getType(), SamHeaderRecord::HD, SamHeaderRecord::PG, SamHeaderRecord::RG, and SamHeaderRecord::SQ.

◆ addRG()

bool SamFileHeader::addRG ( SamHeaderRG rg)

Add the RG record to the header.

Note: it adds a pointer to the passed in header record. The header record will be deleted when it is cleaned up from this header.

Returns
true if the record was successfully added, false otherwise.

Definition at line 458 of file SamFileHeader.cpp.

459 {
460  if(rg == NULL)
461  {
462  // null pointer passed in, can't add it.
463  myErrorMessage = "SAM/BAM Header line failed to allocate RG.";
464  return(false);
465  }
466  const char* id = rg->getTagValue("ID");
467  if(strcmp(id, EMPTY_RETURN.c_str()) == 0)
468  {
469  // ID is not set, so can't add it.
470  myErrorMessage = "SAM/BAM Header line failure: Skipping RG line that is missing the ID field.";
471  return(false);
472  }
473 
474  // Determine whether or not a record with this
475  // key is already in the hash.
476  if(myRGs.Find(id) < 0)
477  {
478  // It is not already in the hash so
479  // add it.
480  myRGs.Add(id, rg);
481  myHeaderRecords.push_back(rg);
482  return(true);
483  }
484 
485  // It is already in the hash, so cannot be added.
486  myErrorMessage = "SAM/BAM Header line failure: Skipping RG line that has a repeated ID field.";
487  return(false);
488 }

References SamHeaderRecord::getTagValue().

Referenced by addRecordCopy().

◆ addSQ()

bool SamFileHeader::addSQ ( SamHeaderSQ sq)

Add the SQ record to the header.

Note: it adds a pointer to the passed in header record. The header record will be deleted when it is cleaned up from this header.

Returns
true if the record was successfully added, false otherwise.

Definition at line 415 of file SamFileHeader.cpp.

416 {
417  if(sq == NULL)
418  {
419  // null pointer passed in, can't add it.
420  myErrorMessage = "SAM/BAM Header line failed to allocate SQ.";
421  return(false);
422  }
423  const char* name = sq->getTagValue("SN");
424  const char* length = sq->getTagValue("LN");
425  if(strcmp(name, EMPTY_RETURN.c_str()) == 0)
426  {
427  // SN is not set, so can't add it.
428  myErrorMessage =
429  "SAM/BAM Header line failure: Skipping SQ line that is missing the SN field.";
430  return(false);
431  }
432  if(strcmp(length, EMPTY_RETURN.c_str()) == 0)
433  {
434  // LN is not set, so can't add it.
435  myErrorMessage =
436  "SAM/BAM Header line failure: Skipping SQ line that is missing the LN field.";
437  return(false);
438  }
439 
440  // Determine whether or not a record with this
441  // key is already in the hash.
442  if(mySQs.Find(name) < 0)
443  {
444  // It is not already in the hash so add it.
445  mySQs.Add(name, sq);
446  myHeaderRecords.push_back(sq);
447  myReferenceInfo.add(name, atoi(length));
448  return(true);
449  }
450 
451  // It is already in the hash, so cannot be added.
452  myErrorMessage = "SAM/BAM Header line failure: Skipping SQ line that has a repeated SN field.";
453  return(false);
454 }
void add(const char *referenceSequenceName, int32_t referenceSequenceLength)
Add reference sequence name and reference sequence length.

References SamReferenceInfo::add(), and SamHeaderRecord::getTagValue().

Referenced by addRecordCopy().

◆ copy()

bool SamFileHeader::copy ( const SamFileHeader header)

Copy method copies the passed in header into this header.

Returns true if at least one header line was successfully copied.

Definition at line 59 of file SamFileHeader.cpp.

60 {
61  // Check to see if the passed in value is the same as this.
62  if(this == &header)
63  {
64  return(true);
65  }
66 
67  resetHeader();
68 
69  // Copy the records by getting the other header's header string
70  // and parsing it.
71  std::string newString;
72  bool status = header.getHeaderString(newString);
73  String newHeaderString = newString.c_str();
74 
75  status &= parseHeader(newHeaderString);
76 
77  myCurrentHeaderIndex = header.myCurrentHeaderIndex;
78  myCurrentCommentIndex = header.myCurrentCommentIndex;
79 
80  // Clear the reference info and copy it to ensure it is the same.
81  myReferenceInfo.clear();
82  // Copy Reference contigs, hash, lengths.
83  myReferenceInfo = header.myReferenceInfo;
84 
85  return(status);
86 }
bool getHeaderString(std::string &header) const
Set the passed in string to the entire header string, clearing its current contents.
void resetHeader()
Initialize the header.
void clear()
Reset this reference info.

References SamReferenceInfo::clear(), getHeaderString(), and resetHeader().

Referenced by SamFileHeader(), and operator=().

◆ getHeaderString()

bool SamFileHeader::getHeaderString ( std::string &  header) const

Set the passed in string to the entire header string, clearing its current contents.

Returns
true if successfully set (even if set to "")

Definition at line 131 of file SamFileHeader.cpp.

132 {
133  header.clear();
134 
135  // Keep getting header lines until there are no more - false returned.
136  unsigned int index = 0;
137  while(getHeaderLine(index, header) != false)
138  {
139  ++index;
140  }
141 
142  return(true);
143 }

Referenced by copy().

◆ getNextComment()

const char * SamFileHeader::getNextComment ( )

Returns the comment on the next comment line.

Returns "" if all comment lines have been returned, until resetCommentIter is called.

Definition at line 985 of file SamFileHeader.cpp.

986 {
987  if(myCurrentCommentIndex < myComments.size())
988  {
989  return(myComments[myCurrentCommentIndex++].c_str());
990  }
991  // Already gone through all the comments, return EMPTY_RETURN.c_str().
992  return(EMPTY_RETURN.c_str());
993 }

◆ getNextHeaderLine()

bool SamFileHeader::getNextHeaderLine ( std::string &  headerLine)

Set the passed in string to the next header line, overwritting the passed in string.

If there are no more header lines or there is an error, false is returned and the passed in string is set to "" until a rest is called. NOTE: both getNextHeaderRecord and getNextHeaderLine increment the same iterator.

Definition at line 941 of file SamFileHeader.cpp.

942 {
943  headerLine = EMPTY_RETURN.c_str();
944 
945  // Until the header is set, keep reading.
946  // Header could return EMPTY_RETURN.c_str() if the header line is blank.
947  while(headerLine == EMPTY_RETURN.c_str())
948  {
949  if(getHeaderLine(myCurrentHeaderIndex, headerLine) == false)
950  {
951  // getHeaderLine failed, so stop processing, and return false.
952  return(false);
953  }
954  else
955  {
956  // In range, increment the index.
957  ++myCurrentHeaderIndex;
958  }
959  }
960  return(true);
961 }

◆ getNextHeaderRecord() [1/2]

SamHeaderRecord * SamFileHeader::getNextHeaderRecord ( )

Get the next header record, but not comment line.

After all headers have been retrieved, NULL is returned until a reset is called. NOTE: both getNextHeaderRecord and getNextHeaderLine increment the same iterator.

Definition at line 908 of file SamFileHeader.cpp.

909 {
910  // Get the next header record
911  SamHeaderRecord* foundRecord = NULL;
912  // Loop until a record is found or until out of range of the
913  // headerRecord vector.
914  while((myCurrentHeaderIndex < myHeaderRecords.size())
915  && (foundRecord == NULL))
916  {
917  // Get the next record.
918  foundRecord = myHeaderRecords[myCurrentHeaderIndex];
919  // Either way, increment the index.
920  ++myCurrentHeaderIndex;
921  // Check to see if the next record is active.
922  if(!foundRecord->isActiveHeaderRecord())
923  {
924  // Not active, so clear the pointer.
925  foundRecord = NULL;
926  }
927  }
928 
929  // Return the record if it was found. Will be null if none were found.
930  return(foundRecord);
931 }
bool isActiveHeaderRecord()
This record is active (true) if there is at least one tag set.

References SamHeaderRecord::isActiveHeaderRecord().

Referenced by getNextPGRecord(), getNextRGRecord(), and getNextSQRecord().

◆ getNextHeaderRecord() [2/2]

SamHeaderRecord * SamFileHeader::getNextHeaderRecord ( uint32_t &  index,
SamHeaderRecord::SamHeaderRecordType  headerType 
)

Get the next header record of the specified type starting from the specified index and update the index.

After all headers of that type have been retrieved, NULL is returned until a reset is called for that type.

Definition at line 871 of file SamFileHeader.cpp.

873 {
874  SamHeaderRecord* foundRecord = NULL;
875  // Loop until a record is found or until out of range of the
876  // headerRecord vector.
877  while((index < myHeaderRecords.size())
878  && (foundRecord == NULL))
879  {
880  // Get the next record.
881  foundRecord = myHeaderRecords[index];
882  // Either way, increment the index.
883  ++index;
884  // Check to see if the next record is active.
885  if(!foundRecord->isActiveHeaderRecord())
886  {
887  // Not active, so clear the pointer.
888  foundRecord = NULL;
889  }
890  // Check to see if the record is the right type.
891  else if(foundRecord->getType() != headerType)
892  {
893  // Not the right type, so clear the pointer.
894  foundRecord = NULL;
895  }
896  }
897 
898  // Return the record if it was found. Will be null if none were found.
899  return(foundRecord);
900 }

References SamHeaderRecord::getType(), and SamHeaderRecord::isActiveHeaderRecord().

◆ getNextPGRecord()

SamHeaderRecord * SamFileHeader::getNextPGRecord ( )

Get the next PG header record.

After all PG headers have been retrieved, NULL is returned until a reset is called. Independent from getNextHeaderRecord, getNextHeaderLine and the other getNextXXRecord methods and the associated reset methods.

Definition at line 835 of file SamFileHeader.cpp.

836 {
837  return(getNextHeaderRecord(myCurrentPGIndex,
839 }
SamHeaderRecord * getNextHeaderRecord()
Get the next header record, but not comment line.

References getNextHeaderRecord(), and SamHeaderRecord::PG.

◆ getNextRGRecord()

SamHeaderRecord * SamFileHeader::getNextRGRecord ( )

Get the next RG header record.

After all RG headers have been retrieved, NULL is returned until a reset is called. Independent from getNextHeaderRecord, getNextHeaderLine and the other getNextXXRecord methods and the associated reset methods.

Definition at line 826 of file SamFileHeader.cpp.

827 {
828  return(getNextHeaderRecord(myCurrentRGIndex,
830 }

References getNextHeaderRecord(), and SamHeaderRecord::RG.

◆ getNextSQRecord()

SamHeaderRecord * SamFileHeader::getNextSQRecord ( )

Get the next SQ header record.

After all SQ headers have been retrieved, NULL is returned until a reset is called. Independent from getNextHeaderRecord, getNextHeaderLine and the other getNextXXRecord methods and the associated reset methods.

Definition at line 817 of file SamFileHeader.cpp.

818 {
819  return(getNextHeaderRecord(myCurrentSQIndex,
821 }

References getNextHeaderRecord(), and SamHeaderRecord::SQ.

◆ getPG()

SamHeaderPG * SamFileHeader::getPG ( const char *  id)

Get the PG object with the specified id, returning NULL if there is no PG object with that key.

Definition at line 789 of file SamFileHeader.cpp.

790 {
791  return((SamHeaderPG*)(myPGs.Object(id)));
792 }

Referenced by setPGTag().

◆ getReferenceID() [1/2]

int SamFileHeader::getReferenceID ( const char *  referenceName,
bool  addID = false 
)

Get the reference ID for the specified reference name (chromosome).

If addID is set to true, a reference id will be created for the referenceName if one does not already exist. If addID is set to false (default), it will return SamReferenceInfo::NO_REF_ID.

Definition at line 152 of file SamFileHeader.cpp.

153 {
154  return(myReferenceInfo.getReferenceID(referenceName, addID));
155 }
int getReferenceID(const String &referenceName, bool addID=false)
Get the reference ID for the specified name, if addID is set to true, a reference id will be created ...

References SamReferenceInfo::getReferenceID().

◆ getReferenceID() [2/2]

int SamFileHeader::getReferenceID ( const String referenceName,
bool  addID = false 
)

Get the reference ID for the specified reference name (chromosome).

If addID is set to true, a reference id will be created for the referenceName if one does not already exist. If addID is set to false (default), it will return SamReferenceInfo::NO_REF_ID.

Definition at line 146 of file SamFileHeader.cpp.

147 {
148  return(myReferenceInfo.getReferenceID(referenceName, addID));
149 }

References SamReferenceInfo::getReferenceID().

Referenced by SamFile::getNumMappedReadsFromIndex(), SamFile::getNumUnMappedReadsFromIndex(), SamRecord::setMateReferenceName(), and SamRecord::setReferenceName().

◆ getRG()

SamHeaderRG * SamFileHeader::getRG ( const char *  id)

Get the RG object with the specified read group identifier, returning NULL if there is no RG object with that key.

Definition at line 782 of file SamFileHeader.cpp.

783 {
784  return((SamHeaderRG*)(myRGs.Object(id)));
785 }

Referenced by setRGTag().

◆ removeHD()

bool SamFileHeader::removeHD ( )

Remove the HD record.

Returns
true if successfully removed or did not exist, false if the record still exists.

Definition at line 554 of file SamFileHeader.cpp.

555 {
556  if(myHD != NULL)
557  {
558  // Reset the record. Do not delete it since it is in the headerRecords
559  // vector and it is not worth the time to remove it from the middle of
560  // that vector since this is the header and the space does not need
561  // to be conserved.
562  myHD->reset();
563 
564  // Set myHD to null so a new HD could be added.
565  myHD = NULL;
566  }
567 
568  return(true);
569 }
void reset()
Reset this header record to an empty state with no tags.

References SamHeaderRecord::reset().

◆ removePG()

bool SamFileHeader::removePG ( const char *  id)

Remove PG record with the specified key.

Returns
true if successfully removed or did not exist, false if the record still exists.

Definition at line 647 of file SamFileHeader.cpp.

648 {
649  // Look up the id in the hash.
650  int hashIndex = myPGs.Find(id);
651  if(hashIndex < 0)
652  {
653  // Not found in the hash, so nothing to
654  // delete, return true it does not exist
655  // in the hash.
656  return(true);
657  }
658 
659  // Get the PG.
660  SamHeaderPG* pg = (SamHeaderPG*)(myPGs.Object(hashIndex));
661 
662  if(pg == NULL)
663  {
664  // pg is null, this is an error since hashIndex was greater than 0,
665  // so it should have been found.
666  myErrorMessage = "SAM/BAM Header line failed to get PG object.";
667  return(false);
668  }
669 
670  // Reset the record. Do not delete it since it is in the headerRecords
671  // vector and it is not worth the time to remove it from the middle of
672  // that vector since this is the header and the space does not need
673  // to be conserved.
674  pg->reset();
675 
676  // Delete the entry from the hash.
677  myPGs.Delete(hashIndex);
678 
679  return(true);
680 }

References SamHeaderRecord::reset().

◆ removeRG()

bool SamFileHeader::removeRG ( const char *  id)

Remove RG record with the specified key.

Returns
true if successfully removed or did not exist, false if the record still exists.

Definition at line 610 of file SamFileHeader.cpp.

611 {
612  // Look up the id in the hash.
613  int hashIndex = myRGs.Find(id);
614  if(hashIndex < 0)
615  {
616  // Not found in the hash, so nothing to
617  // delete, return true it does not exist
618  // in the hash.
619  return(true);
620  }
621 
622  // Get the RG.
623  SamHeaderRG* rg = (SamHeaderRG*)(myRGs.Object(hashIndex));
624 
625  if(rg == NULL)
626  {
627  // rg is null, this is an error since hashIndex was greater than 0,
628  // so it should have been found.
629  myErrorMessage = "SAM/BAM Header line failed to get RG object.";
630  return(false);
631  }
632 
633  // Reset the record. Do not delete it since it is in the headerRecords
634  // vector and it is not worth the time to remove it from the middle of
635  // that vector since this is the header and the space does not need
636  // to be conserved.
637  rg->reset();
638 
639  // Delete the entry from the hash.
640  myRGs.Delete(hashIndex);
641 
642  return(true);
643 }

References SamHeaderRecord::reset().

◆ removeSQ()

bool SamFileHeader::removeSQ ( const char *  name)

Remove SQ record with the specified key.

NOTE: Does not remove it from the BAM index.

Returns
true if successfully removed or did not exist, false if the record still exists.

Definition at line 573 of file SamFileHeader.cpp.

574 {
575  // Look up the name in the hash.
576  int hashIndex = mySQs.Find(name);
577  if(hashIndex < 0)
578  {
579  // Not found in the hash, so nothing to
580  // delete, return true it does not exist
581  // in the hash.
582  return(true);
583  }
584 
585  // Get the SQ.
586  SamHeaderSQ* sq = (SamHeaderSQ*)(mySQs.Object(hashIndex));
587 
588  if(sq == NULL)
589  {
590  // sq is null, this is an error since hashIndex was greater than 0,
591  // so it should have been found.
592  myErrorMessage = "SAM/BAM Header line failed to get SQ object.";
593  return(false);
594  }
595 
596  // Reset the record. Do not delete it since it is in the headerRecords
597  // vector and it is not worth the time to remove it from the middle of
598  // that vector since this is the header and the space does not need
599  // to be conserved.
600  sq->reset();
601 
602  // Delete the entry from the hash.
603  mySQs.Delete(hashIndex);
604 
605  return(true);
606 }

References SamHeaderRecord::reset().

◆ setHDTag()

bool SamFileHeader::setHDTag ( const char *  tag,
const char *  value 
)

Set the specified tag to the specified value in the HD header, remove the tag by specifying value="".

Returns
true if the tag was successfully set, false if not.

Definition at line 225 of file SamFileHeader.cpp.

226 {
227  if(myHD == NULL)
228  {
229  // Need to create the HD line.
230  myHD = new SamHeaderHD();
231  if(myHD == NULL)
232  {
233  // New failed, return false.
234  myErrorMessage = "SamFileHeader: Failed to allocate a new HD tag";
235  return(false);
236  }
237  // Succeeded to create the line, add it to the
238  // list.
239  myHeaderRecords.push_back(myHD);
240  }
241  if(!myHD->setTag(tag, value))
242  {
243  myErrorMessage = "SamFileHeader: Failed to set the specified HD tag";
244  return(false);
245  }
246  return(true);
247 }
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 SamHeaderRecord::setTag().

◆ setPGTag()

bool SamFileHeader::setPGTag ( const char *  tag,
const char *  value,
const char *  id 
)

Set the specified tag to the specified value in the PG header with the specified id, remove the tag by specifying value="".

If the header does not yet exist, the header is added and so is the ID tag with the value set to the passed in id. The ID tag may not be modified or removed after it is set unless the entire record is deleted.

Returns
true if the tag was successfully set, false if not.

Definition at line 353 of file SamFileHeader.cpp.

354 {
355  // Get the PG record for the specified name.
356  SamHeaderPG* pg = getPG(id);
357  if(pg == NULL)
358  {
359  // The PG does not yet exist.
360  // Add it.
361  pg = new SamHeaderPG();
362 
363  if(pg == NULL)
364  {
365  // Could not create the header record.
366  myErrorMessage = "Failed to allocate a new PG tag";
367  return(false);
368  }
369 
370  // Created the header record, so add it to the list of PG lines.
371  myPGs.Add(id, pg);
372  myHeaderRecords.push_back(pg);
373 
374  // Add the key tag
375  if(!pg->addKey(id))
376  {
377  // Failed to add the key tag, return false.
378  myErrorMessage = "Failed to add the specified PG key";
379  return(false);
380  }
381  }
382 
383  if(!pg->setTag(tag, value))
384  {
385  myErrorMessage = "Failed to set the specified PG tag";
386  return(false);
387  }
388  return(true);
389 }
SamHeaderPG * getPG(const char *id)
Get the PG object with the specified id, returning NULL if there is no PG object with that key.
bool addKey(const char *value)
Add the key tag with the specified value (not for HD headers).

References SamHeaderRecord::addKey(), getPG(), and SamHeaderRecord::setTag().

◆ setRGTag()

bool SamFileHeader::setRGTag ( const char *  tag,
const char *  value,
const char *  id 
)

Set the specified tag to the specified value in the RG header with the specified id, remove the tag by specifying value="".

If the header does not yet exist, the header is added and so is the ID tag with the value set to the passed in id. The ID tag may not be modified or removed after it is set unless the entire record is deleted.

Returns
true if the tag was successfully set, false if not.

Definition at line 311 of file SamFileHeader.cpp.

312 {
313  // Get the RG record for the specified name.
314  SamHeaderRG* rg = getRG(id);
315  if(rg == NULL)
316  {
317  // The RG does not yet exist.
318  // Add it.
319  rg = new SamHeaderRG();
320 
321  if(rg == NULL)
322  {
323  // Could not create the header record.
324  myErrorMessage = "Failed to allocate a new RG tag";
325  return(false);
326  }
327 
328  // Created the header record, so add it to the list of RG lines.
329  myRGs.Add(id, rg);
330  myHeaderRecords.push_back(rg);
331 
332  // Add the key tag
333  if(!rg->addKey(id))
334  {
335  // Failed to add the key tag, return false.
336  myErrorMessage = "Failed to add the specified RG key";
337  return(false);
338  }
339  }
340 
341  if(!rg->setTag(tag, value))
342  {
343  myErrorMessage = "Failed to set the specified RG tag";
344  return(false);
345  }
346  return(true);
347 }
SamHeaderRG * getRG(const char *id)
Get the RG object with the specified read group identifier, returning NULL if there is no RG object w...

References SamHeaderRecord::addKey(), getRG(), and SamHeaderRecord::setTag().

◆ setSQTag()

bool SamFileHeader::setSQTag ( const char *  tag,
const char *  value,
const char *  name 
)

Set the specified tag to the specified value in the SQ header with the specified name, remove the tag by specifying value="".

If the header does not yet exist, the tag must be "LN" and the header is added with the specified LN value and the SN value passed in name.
The SN & LN tags may not be modified or removed after they are set unless the entire record is deleted.

Returns
true if the tag was successfully set, false if not.

Definition at line 252 of file SamFileHeader.cpp.

254 {
255  // Get the SQ record for the specified name.
256  SamHeaderSQ* sq = getSQ(name);
257  if(sq == NULL)
258  {
259  // The SQ does not yet exist.
260  // Make sure the tag is LN.
261  if(strcmp(tag, "LN") != 0)
262  {
263  // LN is required so must be the first tag added
264  myErrorMessage =
265  "SamFileHeader:Failed to add the specified SQ key, LN not specified.";
266  return(false);
267  }
268 
269  // Add it.
270  sq = new SamHeaderSQ();
271 
272  if(sq == NULL)
273  {
274  // Could not create the header record.
275  myErrorMessage = "SamFileHeader: Failed to allocate a new SQ tag";
276  return(false);
277  }
278 
279  // Created the header record, so add it to the list of SQ lines.
280  mySQs.Add(name, sq);
281  myHeaderRecords.push_back(sq);
282  // value is the length, so update the reference info.
283  myReferenceInfo.add(name, atoi(value));
284 
285  // Add the key tag
286  if(!sq->addKey(name))
287  {
288  // Failed to add the key tag, return false.
289  myErrorMessage = "SamFileHeader:Failed to add the specified SQ key";
290  return(false);
291  }
292  }
293  else if(strcmp(tag, "LN") == 0)
294  {
295  // Cannot modify/remove the LN tag.
296  myErrorMessage = "SamFileHeader:Cannot modify/remove the SQ's LN tag";
297  return(false);
298  }
299 
300  if(!sq->setTag(tag, value))
301  {
302  myErrorMessage = "Failed to set the specified SQ tag";
303  return(false);
304  }
305  return(true);
306 }
SamHeaderSQ * getSQ(const char *name)
Get the SQ object with the specified sequence name, returning NULL if there is no SQ object with that...

References SamReferenceInfo::add(), SamHeaderRecord::addKey(), getSQ(), and SamHeaderRecord::setTag().


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