]> git.donarmstrong.com Git - bamtools.git/blob - src/api/SamHeader.cpp
Minor update to API version 0.9.3 - addition of SamHeader::SetHeaderText().
[bamtools.git] / src / api / SamHeader.cpp
1 // ***************************************************************************
2 // SamHeader.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 23 December 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides functionality for querying/manipulating SAM header data
9 // **************************************************************************
10
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;
17 using namespace std;
18
19 // ctor
20 SamHeader::SamHeader(const string& headerText)
21     : Version("")
22     , SortOrder("")
23     , GroupOrder("")
24     , ProgramName("")
25     , ProgramVersion("")
26     , ProgramCommandLine("")
27 {
28     SamFormatParser parser(*this);
29     parser.Parse(headerText);
30 }
31
32 // copy ctor
33 SamHeader::SamHeader(const SamHeader& other)
34     : Version(other.Version)
35     , SortOrder(other.SortOrder)
36     , GroupOrder(other.GroupOrder)
37     , Sequences(other.Sequences)
38     , ReadGroups(other.ReadGroups)
39     , ProgramName(other.ProgramName)
40     , ProgramVersion(other.ProgramVersion)
41     , ProgramCommandLine(other.ProgramCommandLine)
42 { }
43
44 // dtor
45 SamHeader::~SamHeader(void) {
46     Clear();
47 }
48
49 void SamHeader::Clear(void) {
50     Version.clear();
51     SortOrder.clear();
52     GroupOrder.clear();
53     Sequences.Clear();
54     ReadGroups.Clear();
55     ProgramName.clear();
56     ProgramVersion.clear();
57     ProgramCommandLine.clear();
58     Comments.clear();
59 }
60
61 void SamHeader::SetHeaderText(const std::string& headerText) {
62
63     // clear prior data
64     Clear();
65
66     // parse header text into data
67     SamFormatParser parser(*this);
68     parser.Parse(headerText);
69 }
70
71 // retrieve the SAM header, with any local modifications
72 string SamHeader::ToString(void) const {
73     SamFormatPrinter printer(*this);
74     return printer.ToString();
75 }
76
77 // query if header contains @HD ID:<Version>
78 bool SamHeader::HasVersion(void) const {
79     return (!Version.empty());
80 }
81
82 // query if header contains @HD SO:<SortOrder>
83 bool SamHeader::HasSortOrder(void) const {
84     return (!SortOrder.empty());
85 }
86
87 // query if header contains @HD GO:<GroupOrder>
88 bool SamHeader::HasGroupOrder(void) const {
89     return (!GroupOrder.empty());
90 }
91
92 // query if header contains @SQ entries
93 bool SamHeader::HasSequences(void) const {
94     return (!Sequences.IsEmpty());
95 }
96
97 // query if header contains @RG entries
98 bool SamHeader::HasReadGroups(void) const {
99     return (!ReadGroups.IsEmpty());
100 }
101
102 // query if header contains @PG ID:<ProgramName>
103 bool SamHeader::HasProgramName(void) const {
104     return (!ProgramName.empty());
105 }
106
107 // query if header contains @HD VN:<ProgramVersion>
108 bool SamHeader::HasProgramVersion(void) const {
109     return (!ProgramVersion.empty());
110 }
111
112 // query if header contains @HD CL:<ProgramCommandLine>
113 bool SamHeader::HasProgramCommandLine(void) const {
114     return (!ProgramCommandLine.empty());
115 }
116
117 // query if header contains @CO entries
118 bool SamHeader::HasComments(void) const {
119     return (!Comments.empty());
120 }
121
122 // validation
123 bool SamHeader::IsValid(bool verbose) const {
124     SamHeaderValidator validator(*this);
125     return validator.Validate(verbose);
126 }