1 // ***************************************************************************
2 // SamHeader.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 4 March 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides direct read/write access to the SAM header data fields.
9 // ***************************************************************************
11 #include <api/SamHeader.h>
12 #include <api/internal/SamFormatParser_p.h>
13 #include <api/internal/SamFormatPrinter_p.h>
14 #include <api/internal/SamHeaderValidator_p.h>
15 using namespace BamTools;
16 using namespace BamTools::Internal;
19 /*! \struct BamTools::SamHeader
20 \brief Represents the SAM-formatted text header that is part of the BAM file header.
22 Provides direct read/write access to the SAM header data fields.
24 \sa http://samtools.sourceforge.net/SAM-1.3.pdf
26 /*! \var SamHeader::Version
27 \brief corresponds to \@HD VN:\<Version\>
29 /*! \var SamHeader::SortOrder
30 \brief corresponds to \@HD SO:\<SortOrder\>
32 /*! \var SamHeader::GroupOrder
33 \brief corresponds to \@HD GO:\<GroupOrder\>
35 /*! \var SamHeader::Sequences
36 \brief corresponds to \@SQ entries
37 \sa SamSequence, SamSequenceDictionary
39 /*! \var SamHeader::ReadGroups
40 \brief corresponds to \@RG entries
41 \sa SamReadGroup, SamReadGroupDictionary
43 /*! \var SamHeader::ProgramName
44 \brief corresponds to \@PG ID:\<ProgramName\>
46 /*! \var SamHeader::ProgramVersion
47 \brief corresponds to \@PG VN:\<ProgramVersion\>
49 /*! \var SamHeader::ProgramCommandLine
50 \brief corresponds to \@PG CL:\<ProgramCommandLine\>
52 /*! \var SamHeader::Comments
53 \brief corresponds to \@CO entries
56 /*! \fn SamHeader::SamHeader(const std::string& headerText = "")
59 SamHeader::SamHeader(const std::string& headerText)
65 , ProgramCommandLine("")
67 SamFormatParser parser(*this);
68 parser.Parse(headerText);
71 /*! \fn SamHeader::SamHeader(const SamHeader& other)
72 \brief copy constructor
74 SamHeader::SamHeader(const SamHeader& other)
75 : Version(other.Version)
76 , SortOrder(other.SortOrder)
77 , GroupOrder(other.GroupOrder)
78 , Sequences(other.Sequences)
79 , ReadGroups(other.ReadGroups)
80 , ProgramName(other.ProgramName)
81 , ProgramVersion(other.ProgramVersion)
82 , ProgramCommandLine(other.ProgramCommandLine)
85 /*! \fn SamHeader::~SamHeader(void)
88 SamHeader::~SamHeader(void) { }
90 /*! \fn void SamHeader::Clear(void)
91 \brief Clears all header contents.
93 void SamHeader::Clear(void) {
100 ProgramVersion.clear();
101 ProgramCommandLine.clear();
105 /*! \fn bool SamHeader::HasVersion(void) const
106 \brief Returns \c true if header contains \@HD ID:\<Version\>
108 bool SamHeader::HasVersion(void) const {
109 return (!Version.empty());
112 /*! \fn bool SamHeader::HasSortOrder(void) const
113 \brief Returns \c true if header contains \@HD SO:\<SortOrder\>
115 bool SamHeader::HasSortOrder(void) const {
116 return (!SortOrder.empty());
119 /*! \fn bool SamHeader::HasGroupOrder(void) const
120 \brief Returns \c true if header contains \@HD GO:\<GroupOrder\>
122 bool SamHeader::HasGroupOrder(void) const {
123 return (!GroupOrder.empty());
126 /*! \fn bool SamHeader::HasSequences(void) const
127 \brief Returns \c true if header contains any \@SQ entries
129 bool SamHeader::HasSequences(void) const {
130 return (!Sequences.IsEmpty());
133 /*! \fn bool SamHeader::HasReadGroups(void) const
134 \brief Returns \c true if header contains any \@RG entries
136 bool SamHeader::HasReadGroups(void) const {
137 return (!ReadGroups.IsEmpty());
140 /*! \fn bool SamHeader::HasProgramName(void) const
141 \brief Returns \c true if header contains \@PG ID:\<ProgramName\>
143 bool SamHeader::HasProgramName(void) const {
144 return (!ProgramName.empty());
147 /*! \fn bool SamHeader::HasProgramVersion(void) const
148 \brief Returns \c true if header contains \@PG VN:\<ProgramVersion\>
150 bool SamHeader::HasProgramVersion(void) const {
151 return (!ProgramVersion.empty());
154 /*! \fn bool SamHeader::HasProgramCommandLine(void) const
155 \brief Returns \c true if header contains \@PG CL:\<ProgramCommandLine\>
157 bool SamHeader::HasProgramCommandLine(void) const {
158 return (!ProgramCommandLine.empty());
161 /*! \fn bool SamHeader::HasComments(void) const
162 \brief Returns \c true if header contains any \@CO entries
164 bool SamHeader::HasComments(void) const {
165 return (!Comments.empty());
168 /*! \fn bool SamHeader::IsValid(bool verbose = false) const
169 \brief Checks header contents for required data and proper formatting.
170 \param verbose If set to true, validation errors & warnings will be printed to stderr.
171 Otherwise, output is suppressed and only validation check occurs.
172 \return \c true if SAM header is well-formed
174 bool SamHeader::IsValid(bool verbose) const {
175 SamHeaderValidator validator(*this);
176 return validator.Validate(verbose);
179 /*! \fn void SamHeader::SetHeaderText(const std::string& headerText)
180 \brief Replaces header contents with \a headerText.
181 \param headerText SAM formatted-text that will be parsed into data fields
183 void SamHeader::SetHeaderText(const std::string& headerText) {
188 // parse header text into data
189 SamFormatParser parser(*this);
190 parser.Parse(headerText);
193 /*! \fn std::string SamHeader::ToString(void) const
194 \brief Converts data fields to SAM-formatted text.
196 Applies any local modifications made since creating this object or calling SetHeaderText().
198 \return SAM-formatted header text
200 string SamHeader::ToString(void) const {
201 SamFormatPrinter printer(*this);
202 return printer.ToString();