]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/SamFormatPrinter_p.cpp
Brought API up to compliance with recent SAM Format Spec (v1.4-r962)
[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 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 19 April 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides functionality for printing formatted SAM header to string
9 // ***************************************************************************
10
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;
16
17 #include <iostream>
18 #include <sstream>
19 #include <vector>
20 using namespace std;
21
22 SamFormatPrinter::SamFormatPrinter(const SamHeader& header)
23     : m_header(header)
24 { }
25
26 SamFormatPrinter::~SamFormatPrinter(void) { }
27
28 const string SamFormatPrinter::FormatTag(const string &tag, const string &value) const {
29     return string(Constants::SAM_TAB + tag + Constants::SAM_COLON + value);
30 }
31
32 const string SamFormatPrinter::ToString(void) const {
33
34     // clear out stream
35     stringstream out("");
36
37     // generate formatted header text
38     PrintHD(out);
39     PrintSQ(out);
40     PrintRG(out);
41     PrintPG(out);
42     PrintCO(out);
43
44     // return result
45     return out.str();
46 }
47
48 void SamFormatPrinter::PrintHD(std::stringstream& out) const {
49
50     // if header has @HD data
51     if ( m_header.HasVersion() ) {
52
53         // @HD VN:<Version>
54         out << Constants::SAM_HD_BEGIN_TOKEN
55             << FormatTag(Constants::SAM_HD_VERSION_TAG, m_header.Version);
56
57         // SO:<SortOrder>
58         if ( m_header.HasSortOrder() )
59             out << FormatTag(Constants::SAM_HD_SORTORDER_TAG, m_header.SortOrder);
60
61         // GO:<GroupOrder>
62         if ( m_header.HasGroupOrder() )
63             out << FormatTag(Constants::SAM_HD_GROUPORDER_TAG, m_header.GroupOrder);
64
65         // newline
66         out << endl;
67     }
68 }
69
70 void SamFormatPrinter::PrintSQ(std::stringstream& out) const {
71
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);
77
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);
82
83         // AS:<AssemblyID>
84         if ( seq.HasAssemblyID() )
85             out << FormatTag(Constants::SAM_SQ_ASSEMBLYID_TAG, seq.AssemblyID);
86
87         // M5:<Checksum>
88         if ( seq.HasChecksum() )
89             out << FormatTag(Constants::SAM_SQ_CHECKSUM_TAG, seq.Checksum);
90
91         // SP:<Species>
92         if ( seq.HasSpecies() )
93             out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
94
95         // UR:<URI>
96         if ( seq.HasURI() )
97             out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
98
99         // newline
100         out << endl;
101     }
102 }
103
104 void SamFormatPrinter::PrintRG(std::stringstream& out) const {
105
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);
111
112         // @RG ID:<ID>
113         out << Constants::SAM_RG_BEGIN_TOKEN
114             << FormatTag(Constants::SAM_RG_ID_TAG, rg.ID);
115
116         // CN:<SequencingCenter>
117         if ( rg.HasSequencingCenter() )
118             out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
119
120         // DS:<Description>
121         if ( rg.HasDescription() )
122             out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
123
124         // DT:<ProductionDate>
125         if ( rg.HasProductionDate() )
126             out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
127
128         // FO:<FlowOrder>
129         if ( rg.HasFlowOrder() )
130             out << FormatTag(Constants::SAM_RG_FLOWORDER_TAG, rg.FlowOrder);
131
132         // KS:<KeySequence>
133         if ( rg.HasKeySequence() )
134             out << FormatTag(Constants::SAM_RG_KEYSEQUENCE_TAG, rg.KeySequence);
135
136         // LB:<Library>
137         if ( rg.HasLibrary() )
138             out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
139
140         // PG:<Program>
141         if ( rg.HasProgram() )
142             out << FormatTag(Constants::SAM_RG_PROGRAM_TAG, rg.Program);
143
144         // PI:<PredictedInsertSize>
145         if ( rg.HasPredictedInsertSize() )
146             out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
147
148         // PL:<SequencingTechnology>
149         if ( rg.HasSequencingTechnology() )
150             out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
151
152         // PU:<PlatformUnit>
153         if ( rg.HasPlatformUnit() )
154             out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
155
156         // SM:<Sample>
157         if ( rg.HasSample() )
158             out << FormatTag(Constants::SAM_RG_SAMPLE_TAG, rg.Sample);
159
160         // newline
161         out << endl;
162     }
163 }
164
165 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
166
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);
172
173         // @PG ID:<ID>
174         out << Constants::SAM_PG_BEGIN_TOKEN
175             << FormatTag(Constants::SAM_PG_ID_TAG, pg.ID);
176
177         // PN:<Name>
178         if ( pg.HasName() )
179             out << FormatTag(Constants::SAM_PG_NAME_TAG, pg.Name);
180
181         // CL:<CommandLine>
182         if ( pg.HasCommandLine() )
183             out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, pg.CommandLine);
184
185         // PP:<PreviousProgramID>
186         if ( pg.HasPreviousProgramID() )
187             out << FormatTag(Constants::SAM_PG_PREVIOUSPROGRAM_TAG, pg.PreviousProgramID);
188
189         // VN:<Version>
190         if ( pg.HasVersion() )
191             out << FormatTag(Constants::SAM_PG_VERSION_TAG, pg.Version);
192
193         // newline
194         out << endl;
195     }
196 }
197
198 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
199
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 ) {
204
205         // @CO <Comment>
206         out << Constants::SAM_CO_BEGIN_TOKEN
207             << Constants::SAM_TAB
208             << (*commentIter)
209             << endl;
210     }
211 }