1 // ***************************************************************************
2 // SamFormatPrinter.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 21 March 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);
93 out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
96 if ( seq.HasSpecies() )
97 out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
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);
112 // @RG ID:<ID> SM:<Sample>
113 out << Constants::SAM_RG_BEGIN_TOKEN
114 << FormatTag(Constants::SAM_RG_ID_TAG, rg.ID)
115 << FormatTag(Constants::SAM_RG_SAMPLE_TAG, rg.Sample);
118 if ( rg.HasLibrary() )
119 out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
122 if ( rg.HasDescription() )
123 out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
126 if ( rg.HasPlatformUnit() )
127 out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
129 // PI:<PredictedInsertSize>
130 if ( rg.HasPredictedInsertSize() )
131 out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
133 // CN:<SequencingCenter>
134 if ( rg.HasSequencingCenter() )
135 out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
137 // DT:<ProductionDate>
138 if ( rg.HasProductionDate() )
139 out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
141 // PL:<SequencingTechnology>
142 if ( rg.HasSequencingTechnology() )
143 out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
150 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
152 // if header has @PG data
153 if ( m_header.HasProgramName() ) {
155 // @PG ID:<ProgramName>
156 out << Constants::SAM_PG_BEGIN_TOKEN
157 << FormatTag(Constants::SAM_PG_NAME_TAG, m_header.ProgramName);
159 // VN:<ProgramVersion>
160 if ( m_header.HasProgramVersion() )
161 out << FormatTag(Constants::SAM_PG_VERSION_TAG, m_header.ProgramVersion);
163 // CL:<ProgramCommandLine>
164 if ( m_header.HasProgramCommandLine() )
165 out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, m_header.ProgramCommandLine);
172 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
174 // iterate over comments
175 vector<string>::const_iterator commentIter = m_header.Comments.begin();
176 vector<string>::const_iterator commentEnd = m_header.Comments.end();
177 for ( ; commentIter != commentEnd; ++commentIter ) {
180 out << Constants::SAM_CO_BEGIN_TOKEN
181 << Constants::SAM_TAB