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