1 // ***************************************************************************
2 // SamFormatPrinter.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 19 April 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/SamFormatPrinter_p.h>
13 using namespace BamTools;
14 using namespace BamTools::Internal;
21 SamFormatPrinter::SamFormatPrinter(const SamHeader& header)
25 SamFormatPrinter::~SamFormatPrinter(void) { }
27 const string SamFormatPrinter::FormatTag(const string &tag, const string &value) const {
28 return string(Constants::SAM_TAB + tag + Constants::SAM_COLON + value);
31 const string SamFormatPrinter::ToString(void) const {
36 // generate formatted header text
47 void SamFormatPrinter::PrintHD(std::stringstream& out) const {
49 // if header has @HD data
50 if ( m_header.HasVersion() ) {
53 out << Constants::SAM_HD_BEGIN_TOKEN
54 << FormatTag(Constants::SAM_HD_VERSION_TAG, m_header.Version);
57 if ( m_header.HasSortOrder() )
58 out << FormatTag(Constants::SAM_HD_SORTORDER_TAG, m_header.SortOrder);
61 if ( m_header.HasGroupOrder() )
62 out << FormatTag(Constants::SAM_HD_GROUPORDER_TAG, m_header.GroupOrder);
69 void SamFormatPrinter::PrintSQ(std::stringstream& out) const {
71 // iterate over sequence entries
72 SamSequenceConstIterator seqIter = m_header.Sequences.ConstBegin();
73 SamSequenceConstIterator seqEnd = m_header.Sequences.ConstEnd();
74 for ( ; seqIter != seqEnd; ++seqIter ) {
75 const SamSequence& seq = (*seqIter);
77 // @SQ SN:<Name> LN:<Length>
78 out << Constants::SAM_SQ_BEGIN_TOKEN
79 << FormatTag(Constants::SAM_SQ_NAME_TAG, seq.Name)
80 << FormatTag(Constants::SAM_SQ_LENGTH_TAG, seq.Length);
83 if ( seq.HasAssemblyID() )
84 out << FormatTag(Constants::SAM_SQ_ASSEMBLYID_TAG, seq.AssemblyID);
87 if ( seq.HasChecksum() )
88 out << FormatTag(Constants::SAM_SQ_CHECKSUM_TAG, seq.Checksum);
91 if ( seq.HasSpecies() )
92 out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
96 out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
103 void SamFormatPrinter::PrintRG(std::stringstream& out) const {
105 // iterate over read group entries
106 SamReadGroupConstIterator rgIter = m_header.ReadGroups.ConstBegin();
107 SamReadGroupConstIterator rgEnd = m_header.ReadGroups.ConstEnd();
108 for ( ; rgIter != rgEnd; ++rgIter ) {
109 const SamReadGroup& rg = (*rgIter);
112 out << Constants::SAM_RG_BEGIN_TOKEN
113 << FormatTag(Constants::SAM_RG_ID_TAG, rg.ID);
115 // CN:<SequencingCenter>
116 if ( rg.HasSequencingCenter() )
117 out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
120 if ( rg.HasDescription() )
121 out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
123 // DT:<ProductionDate>
124 if ( rg.HasProductionDate() )
125 out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
128 if ( rg.HasFlowOrder() )
129 out << FormatTag(Constants::SAM_RG_FLOWORDER_TAG, rg.FlowOrder);
132 if ( rg.HasKeySequence() )
133 out << FormatTag(Constants::SAM_RG_KEYSEQUENCE_TAG, rg.KeySequence);
136 if ( rg.HasLibrary() )
137 out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
140 if ( rg.HasProgram() )
141 out << FormatTag(Constants::SAM_RG_PROGRAM_TAG, rg.Program);
143 // PI:<PredictedInsertSize>
144 if ( rg.HasPredictedInsertSize() )
145 out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
147 // PL:<SequencingTechnology>
148 if ( rg.HasSequencingTechnology() )
149 out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
152 if ( rg.HasPlatformUnit() )
153 out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
156 if ( rg.HasSample() )
157 out << FormatTag(Constants::SAM_RG_SAMPLE_TAG, rg.Sample);
164 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
166 // iterate over program record entries
167 SamProgramConstIterator pgIter = m_header.Programs.ConstBegin();
168 SamProgramConstIterator pgEnd = m_header.Programs.ConstEnd();
169 for ( ; pgIter != pgEnd; ++pgIter ) {
170 const SamProgram& pg = (*pgIter);
173 out << Constants::SAM_PG_BEGIN_TOKEN
174 << FormatTag(Constants::SAM_PG_ID_TAG, pg.ID);
178 out << FormatTag(Constants::SAM_PG_NAME_TAG, pg.Name);
181 if ( pg.HasCommandLine() )
182 out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, pg.CommandLine);
184 // PP:<PreviousProgramID>
185 if ( pg.HasPreviousProgramID() )
186 out << FormatTag(Constants::SAM_PG_PREVIOUSPROGRAM_TAG, pg.PreviousProgramID);
189 if ( pg.HasVersion() )
190 out << FormatTag(Constants::SAM_PG_VERSION_TAG, pg.Version);
197 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
199 // iterate over comments
200 vector<string>::const_iterator commentIter = m_header.Comments.begin();
201 vector<string>::const_iterator commentEnd = m_header.Comments.end();
202 for ( ; commentIter != commentEnd; ++commentIter ) {
205 out << Constants::SAM_CO_BEGIN_TOKEN
206 << Constants::SAM_TAB