]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/SamFormatPrinter_p.cpp
69c78df26399fbe2daa4fa96f1e4cc39806f7e96
[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: 21 March 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         // UR:<URI>
92         if ( seq.HasURI() )
93             out << FormatTag(Constants::SAM_SQ_URI_TAG, seq.URI);
94
95         // SP:<Species>
96         if ( seq.HasSpecies() )
97             out << FormatTag(Constants::SAM_SQ_SPECIES_TAG, seq.Species);
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> 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);
116
117         // LB:<Library>
118         if ( rg.HasLibrary() )
119             out << FormatTag(Constants::SAM_RG_LIBRARY_TAG, rg.Library);
120
121         // DS:<Description>
122         if ( rg.HasDescription() )
123             out << FormatTag(Constants::SAM_RG_DESCRIPTION_TAG, rg.Description);
124
125         // PU:<PlatformUnit>
126         if ( rg.HasPlatformUnit() )
127             out << FormatTag(Constants::SAM_RG_PLATFORMUNIT_TAG, rg.PlatformUnit);
128
129         // PI:<PredictedInsertSize>
130         if ( rg.HasPredictedInsertSize() )
131             out << FormatTag(Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG, rg.PredictedInsertSize);
132
133         // CN:<SequencingCenter>
134         if ( rg.HasSequencingCenter() )
135             out << FormatTag(Constants::SAM_RG_SEQCENTER_TAG, rg.SequencingCenter);
136
137         // DT:<ProductionDate>
138         if ( rg.HasProductionDate() )
139             out << FormatTag(Constants::SAM_RG_PRODUCTIONDATE_TAG, rg.ProductionDate);
140
141         // PL:<SequencingTechnology>
142         if ( rg.HasSequencingTechnology() )
143             out << FormatTag(Constants::SAM_RG_SEQTECHNOLOGY_TAG, rg.SequencingTechnology);
144
145         // newline
146         out << endl;
147     }
148 }
149
150 void SamFormatPrinter::PrintPG(std::stringstream& out) const {
151
152     // if header has @PG data
153     if ( m_header.HasProgramName() ) {
154
155         // @PG ID:<ProgramName>
156         out << Constants::SAM_PG_BEGIN_TOKEN
157             << FormatTag(Constants::SAM_PG_NAME_TAG, m_header.ProgramName);
158
159         // VN:<ProgramVersion>
160         if ( m_header.HasProgramVersion() )
161             out << FormatTag(Constants::SAM_PG_VERSION_TAG, m_header.ProgramVersion);
162
163         // CL:<ProgramCommandLine>
164         if ( m_header.HasProgramCommandLine() )
165             out << FormatTag(Constants::SAM_PG_COMMANDLINE_TAG, m_header.ProgramCommandLine);
166
167         // newline
168         out << endl;
169     }
170 }
171
172 void SamFormatPrinter::PrintCO(std::stringstream& out) const {
173
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 ) {
178
179         // @CO <Comment>
180         out << Constants::SAM_CO_BEGIN_TOKEN
181             << Constants::SAM_TAB
182             << (*commentIter)
183             << endl;
184     }
185 }