]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/SamFormatPrinter_p.cpp
d81db5d450918e4ca97ed04f5e8144b899b6ac4b
[bamtools.git] / src / api / internal / SamFormatPrinter_p.cpp
1 // ***************************************************************************
2 // SamFormatPrinter.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 6 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides functionality for printing formatted SAM header to string
8 // ***************************************************************************
9
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;
15
16 #include <iostream>
17 #include <sstream>
18 #include <vector>
19 using namespace std;
20
21 // ------------------------
22 // static utility methods
23 // ------------------------
24
25 static inline
26 const string FormatTag(const string& tag, const string& value) {
27     return string(Constants::SAM_TAB + tag + Constants::SAM_COLON + value);
28 }
29
30 // ---------------------------------
31 // SamFormatPrinter implementation
32 // ---------------------------------
33
34 SamFormatPrinter::SamFormatPrinter(const SamHeader& header)
35     : m_header(header)
36 { }
37
38 SamFormatPrinter::~SamFormatPrinter(void) { }
39
40 const string SamFormatPrinter::ToString(void) const {
41
42     // clear out stream
43     stringstream out("");
44
45     // generate formatted header text
46     PrintHD(out);
47     PrintSQ(out);
48     PrintRG(out);
49     PrintPG(out);
50     PrintCO(out);
51
52     // return result
53     return out.str();
54 }
55
56 void SamFormatPrinter::PrintHD(std::stringstream& out) const {
57
58     // if header has @HD data
59     if ( m_header.HasVersion() ) {
60
61         // @HD VN:<Version>
62         out << Constants::SAM_HD_BEGIN_TOKEN
63             << FormatTag(Constants::SAM_HD_VERSION_TAG, m_header.Version);
64
65         // SO:<SortOrder>
66         if ( m_header.HasSortOrder() )
67             out << FormatTag(Constants::SAM_HD_SORTORDER_TAG, m_header.SortOrder);
68
69         // GO:<GroupOrder>
70         if ( m_header.HasGroupOrder() )
71             out << FormatTag(Constants::SAM_HD_GROUPORDER_TAG, m_header.GroupOrder);
72
73         // newline
74         out << endl;
75     }
76 }
77
78 void SamFormatPrinter::PrintSQ(std::stringstream& out) const {
79
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);
85
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);
90
91         // AS:<AssemblyID>
92         if ( seq.HasAssemblyID() )
93             out << FormatTag(Constants::SAM_SQ_ASSEMBLYID_TAG, seq.AssemblyID);
94
95         // M5:<Checksum>
96         if ( seq.HasChecksum() )
97             out << FormatTag(Constants::SAM_SQ_CHECKSUM_TAG, seq.Checksum);
98
99         // SP:<Species>
100         if ( seq.HasSpecies() )
101             out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
102
103         // UR:<URI>
104         if ( seq.HasURI() )
105             out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
106
107         // newline
108         out << endl;
109     }
110 }
111
112 void SamFormatPrinter::PrintRG(std::stringstream& out) const {
113
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);
119
120         // @RG ID:<ID>
121         out << Constants::SAM_RG_BEGIN_TOKEN
122             << FormatTag(Constants::SAM_RG_ID_TAG, rg.ID);
123
124         // CN:<SequencingCenter>
125         if ( rg.HasSequencingCenter() )
126             out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
127
128         // DS:<Description>
129         if ( rg.HasDescription() )
130             out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
131
132         // DT:<ProductionDate>
133         if ( rg.HasProductionDate() )
134             out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
135
136         // FO:<FlowOrder>
137         if ( rg.HasFlowOrder() )
138             out << FormatTag(Constants::SAM_RG_FLOWORDER_TAG, rg.FlowOrder);
139
140         // KS:<KeySequence>
141         if ( rg.HasKeySequence() )
142             out << FormatTag(Constants::SAM_RG_KEYSEQUENCE_TAG, rg.KeySequence);
143
144         // LB:<Library>
145         if ( rg.HasLibrary() )
146             out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
147
148         // PG:<Program>
149         if ( rg.HasProgram() )
150             out << FormatTag(Constants::SAM_RG_PROGRAM_TAG, rg.Program);
151
152         // PI:<PredictedInsertSize>
153         if ( rg.HasPredictedInsertSize() )
154             out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
155
156         // PL:<SequencingTechnology>
157         if ( rg.HasSequencingTechnology() )
158             out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
159
160         // PU:<PlatformUnit>
161         if ( rg.HasPlatformUnit() )
162             out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
163
164         // SM:<Sample>
165         if ( rg.HasSample() )
166             out << FormatTag(Constants::SAM_RG_SAMPLE_TAG, rg.Sample);
167
168         // newline
169         out << endl;
170     }
171 }
172
173 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
174
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);
180
181         // @PG ID:<ID>
182         out << Constants::SAM_PG_BEGIN_TOKEN
183             << FormatTag(Constants::SAM_PG_ID_TAG, pg.ID);
184
185         // PN:<Name>
186         if ( pg.HasName() )
187             out << FormatTag(Constants::SAM_PG_NAME_TAG, pg.Name);
188
189         // CL:<CommandLine>
190         if ( pg.HasCommandLine() )
191             out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, pg.CommandLine);
192
193         // PP:<PreviousProgramID>
194         if ( pg.HasPreviousProgramID() )
195             out << FormatTag(Constants::SAM_PG_PREVIOUSPROGRAM_TAG, pg.PreviousProgramID);
196
197         // VN:<Version>
198         if ( pg.HasVersion() )
199             out << FormatTag(Constants::SAM_PG_VERSION_TAG, pg.Version);
200
201         // newline
202         out << endl;
203     }
204 }
205
206 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
207
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 ) {
212
213         // @CO <Comment>
214         out << Constants::SAM_CO_BEGIN_TOKEN
215             << Constants::SAM_TAB
216             << (*commentIter)
217             << endl;
218     }
219 }