1 // ***************************************************************************
2 // SamFormatPrinter.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 25 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides functionality for printing formatted SAM header to string
8 // ***************************************************************************
10 #include "api/SamConstants.h"
11 #include "api/SamHeader.h"
12 #include "api/internal/sam/SamFormatPrinter_p.h"
13 using namespace BamTools;
14 using namespace BamTools::Internal;
21 // ------------------------
22 // static utility methods
23 // ------------------------
26 const string FormatTag(const string& tag, const string& value) {
27 return string(Constants::SAM_TAB + tag + Constants::SAM_COLON + value);
30 // ---------------------------------
31 // SamFormatPrinter implementation
32 // ---------------------------------
34 SamFormatPrinter::SamFormatPrinter(const SamHeader& header)
38 SamFormatPrinter::~SamFormatPrinter(void) { }
40 const string SamFormatPrinter::ToString(void) const {
45 // generate formatted header text
56 void SamFormatPrinter::PrintHD(std::stringstream& out) const {
58 // if header has @HD data
59 if ( m_header.HasVersion() ) {
62 out << Constants::SAM_HD_BEGIN_TOKEN
63 << FormatTag(Constants::SAM_HD_VERSION_TAG, m_header.Version);
66 if ( m_header.HasSortOrder() )
67 out << FormatTag(Constants::SAM_HD_SORTORDER_TAG, m_header.SortOrder);
70 if ( m_header.HasGroupOrder() )
71 out << FormatTag(Constants::SAM_HD_GROUPORDER_TAG, m_header.GroupOrder);
78 void SamFormatPrinter::PrintSQ(std::stringstream& out) const {
80 // iterate over sequence entries
81 SamSequenceConstIterator seqIter = m_header.Sequences.ConstBegin();
82 SamSequenceConstIterator seqEnd = m_header.Sequences.ConstEnd();
83 for ( ; seqIter != seqEnd; ++seqIter ) {
84 const SamSequence& seq = (*seqIter);
86 // @SQ SN:<Name> LN:<Length>
87 out << Constants::SAM_SQ_BEGIN_TOKEN
88 << FormatTag(Constants::SAM_SQ_NAME_TAG, seq.Name)
89 << FormatTag(Constants::SAM_SQ_LENGTH_TAG, seq.Length);
92 if ( seq.HasAssemblyID() )
93 out << FormatTag(Constants::SAM_SQ_ASSEMBLYID_TAG, seq.AssemblyID);
96 if ( seq.HasChecksum() )
97 out << FormatTag(Constants::SAM_SQ_CHECKSUM_TAG, seq.Checksum);
100 if ( seq.HasSpecies() )
101 out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
105 out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
112 void SamFormatPrinter::PrintRG(std::stringstream& out) const {
114 // iterate over read group entries
115 SamReadGroupConstIterator rgIter = m_header.ReadGroups.ConstBegin();
116 SamReadGroupConstIterator rgEnd = m_header.ReadGroups.ConstEnd();
117 for ( ; rgIter != rgEnd; ++rgIter ) {
118 const SamReadGroup& rg = (*rgIter);
121 out << Constants::SAM_RG_BEGIN_TOKEN
122 << FormatTag(Constants::SAM_RG_ID_TAG, rg.ID);
124 // CN:<SequencingCenter>
125 if ( rg.HasSequencingCenter() )
126 out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
129 if ( rg.HasDescription() )
130 out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
132 // DT:<ProductionDate>
133 if ( rg.HasProductionDate() )
134 out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
137 if ( rg.HasFlowOrder() )
138 out << FormatTag(Constants::SAM_RG_FLOWORDER_TAG, rg.FlowOrder);
141 if ( rg.HasKeySequence() )
142 out << FormatTag(Constants::SAM_RG_KEYSEQUENCE_TAG, rg.KeySequence);
145 if ( rg.HasLibrary() )
146 out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
149 if ( rg.HasProgram() )
150 out << FormatTag(Constants::SAM_RG_PROGRAM_TAG, rg.Program);
152 // PI:<PredictedInsertSize>
153 if ( rg.HasPredictedInsertSize() )
154 out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
156 // PL:<SequencingTechnology>
157 if ( rg.HasSequencingTechnology() )
158 out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
161 if ( rg.HasPlatformUnit() )
162 out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
165 if ( rg.HasSample() )
166 out << FormatTag(Constants::SAM_RG_SAMPLE_TAG, rg.Sample);
173 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
175 // iterate over program record entries
176 SamProgramConstIterator pgIter = m_header.Programs.ConstBegin();
177 SamProgramConstIterator pgEnd = m_header.Programs.ConstEnd();
178 for ( ; pgIter != pgEnd; ++pgIter ) {
179 const SamProgram& pg = (*pgIter);
182 out << Constants::SAM_PG_BEGIN_TOKEN
183 << FormatTag(Constants::SAM_PG_ID_TAG, pg.ID);
187 out << FormatTag(Constants::SAM_PG_NAME_TAG, pg.Name);
190 if ( pg.HasCommandLine() )
191 out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, pg.CommandLine);
193 // PP:<PreviousProgramID>
194 if ( pg.HasPreviousProgramID() )
195 out << FormatTag(Constants::SAM_PG_PREVIOUSPROGRAM_TAG, pg.PreviousProgramID);
198 if ( pg.HasVersion() )
199 out << FormatTag(Constants::SAM_PG_VERSION_TAG, pg.Version);
206 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
208 // iterate over comments
209 vector<string>::const_iterator commentIter = m_header.Comments.begin();
210 vector<string>::const_iterator commentEnd = m_header.Comments.end();
211 for ( ; commentIter != commentEnd; ++commentIter ) {
214 out << Constants::SAM_CO_BEGIN_TOKEN
215 << Constants::SAM_TAB