1 // ***************************************************************************
2 // SamFormatPrinter.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 19 April 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides functionality for printing formatted SAM header to string
9 // ***************************************************************************
11 #include <api/SamConstants.h>
12 #include <api/SamHeader.h>
13 #include <api/internal/SamFormatPrinter_p.h>
14 using namespace BamTools;
15 using namespace BamTools::Internal;
22 SamFormatPrinter::SamFormatPrinter(const SamHeader& header)
26 SamFormatPrinter::~SamFormatPrinter(void) { }
28 const string SamFormatPrinter::FormatTag(const string &tag, const string &value) const {
29 return string(Constants::SAM_TAB + tag + Constants::SAM_COLON + value);
32 const string SamFormatPrinter::ToString(void) const {
37 // generate formatted header text
48 void SamFormatPrinter::PrintHD(std::stringstream& out) const {
50 // if header has @HD data
51 if ( m_header.HasVersion() ) {
54 out << Constants::SAM_HD_BEGIN_TOKEN
55 << FormatTag(Constants::SAM_HD_VERSION_TAG, m_header.Version);
58 if ( m_header.HasSortOrder() )
59 out << FormatTag(Constants::SAM_HD_SORTORDER_TAG, m_header.SortOrder);
62 if ( m_header.HasGroupOrder() )
63 out << FormatTag(Constants::SAM_HD_GROUPORDER_TAG, m_header.GroupOrder);
70 void SamFormatPrinter::PrintSQ(std::stringstream& out) const {
72 // iterate over sequence entries
73 SamSequenceConstIterator seqIter = m_header.Sequences.ConstBegin();
74 SamSequenceConstIterator seqEnd = m_header.Sequences.ConstEnd();
75 for ( ; seqIter != seqEnd; ++seqIter ) {
76 const SamSequence& seq = (*seqIter);
78 // @SQ SN:<Name> LN:<Length>
79 out << Constants::SAM_SQ_BEGIN_TOKEN
80 << FormatTag(Constants::SAM_SQ_NAME_TAG, seq.Name)
81 << FormatTag(Constants::SAM_SQ_LENGTH_TAG, seq.Length);
84 if ( seq.HasAssemblyID() )
85 out << FormatTag(Constants::SAM_SQ_ASSEMBLYID_TAG, seq.AssemblyID);
88 if ( seq.HasChecksum() )
89 out << FormatTag(Constants::SAM_SQ_CHECKSUM_TAG, seq.Checksum);
92 if ( seq.HasSpecies() )
93 out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
97 out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
104 void SamFormatPrinter::PrintRG(std::stringstream& out) const {
106 // iterate over read group entries
107 SamReadGroupConstIterator rgIter = m_header.ReadGroups.ConstBegin();
108 SamReadGroupConstIterator rgEnd = m_header.ReadGroups.ConstEnd();
109 for ( ; rgIter != rgEnd; ++rgIter ) {
110 const SamReadGroup& rg = (*rgIter);
113 out << Constants::SAM_RG_BEGIN_TOKEN
114 << FormatTag(Constants::SAM_RG_ID_TAG, rg.ID);
116 // CN:<SequencingCenter>
117 if ( rg.HasSequencingCenter() )
118 out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
121 if ( rg.HasDescription() )
122 out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
124 // DT:<ProductionDate>
125 if ( rg.HasProductionDate() )
126 out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
129 if ( rg.HasFlowOrder() )
130 out << FormatTag(Constants::SAM_RG_FLOWORDER_TAG, rg.FlowOrder);
133 if ( rg.HasKeySequence() )
134 out << FormatTag(Constants::SAM_RG_KEYSEQUENCE_TAG, rg.KeySequence);
137 if ( rg.HasLibrary() )
138 out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
141 if ( rg.HasProgram() )
142 out << FormatTag(Constants::SAM_RG_PROGRAM_TAG, rg.Program);
144 // PI:<PredictedInsertSize>
145 if ( rg.HasPredictedInsertSize() )
146 out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
148 // PL:<SequencingTechnology>
149 if ( rg.HasSequencingTechnology() )
150 out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
153 if ( rg.HasPlatformUnit() )
154 out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
157 if ( rg.HasSample() )
158 out << FormatTag(Constants::SAM_RG_SAMPLE_TAG, rg.Sample);
165 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
167 // iterate over program record entries
168 SamProgramConstIterator pgIter = m_header.Programs.ConstBegin();
169 SamProgramConstIterator pgEnd = m_header.Programs.ConstEnd();
170 for ( ; pgIter != pgEnd; ++pgIter ) {
171 const SamProgram& pg = (*pgIter);
174 out << Constants::SAM_PG_BEGIN_TOKEN
175 << FormatTag(Constants::SAM_PG_ID_TAG, pg.ID);
179 out << FormatTag(Constants::SAM_PG_NAME_TAG, pg.Name);
182 if ( pg.HasCommandLine() )
183 out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, pg.CommandLine);
185 // PP:<PreviousProgramID>
186 if ( pg.HasPreviousProgramID() )
187 out << FormatTag(Constants::SAM_PG_PREVIOUSPROGRAM_TAG, pg.PreviousProgramID);
190 if ( pg.HasVersion() )
191 out << FormatTag(Constants::SAM_PG_VERSION_TAG, pg.Version);
198 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
200 // iterate over comments
201 vector<string>::const_iterator commentIter = m_header.Comments.begin();
202 vector<string>::const_iterator commentEnd = m_header.Comments.end();
203 for ( ; commentIter != commentEnd; ++commentIter ) {
206 out << Constants::SAM_CO_BEGIN_TOKEN
207 << Constants::SAM_TAB