]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/SamFormatParser_p.cpp
Added SAM header-handling classes for read/write/validate.
[bamtools.git] / src / api / internal / SamFormatParser_p.cpp
1 // ***************************************************************************
2 // SamFormatParser.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 23 December 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides functionality for parsing SAM header text into SamHeader object
9 // ***************************************************************************
10
11 #include <api/SamConstants.h>
12 #include <api/SamHeader.h>
13 #include <api/internal/SamFormatParser_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 SamFormatParser::SamFormatParser(SamHeader& header)
23     : m_header(header)
24 { }
25
26 SamFormatParser::~SamFormatParser(void) { }
27
28 void SamFormatParser::Parse(const string& headerText) {
29
30     // clear header's prior contents
31     m_header.Clear();
32
33     // empty header is OK, but skip processing
34     if ( headerText.empty() )
35         return;
36
37     // other wise parse SAM lines
38     istringstream headerStream(headerText);
39     string headerLine = "";
40     while ( getline(headerStream, headerLine) )
41          ParseSamLine(headerLine);
42     return;
43 }
44
45 void SamFormatParser::ParseSamLine(const string& line) {
46
47     // skip if line is not long enough to contain true values
48     if (line.length() < 5 ) return;
49
50     // determine token at beginning of line
51     const string firstToken = line.substr(0,3);
52     string restOfLine = line.substr(4);
53     if      ( firstToken == Constants::SAM_HD_BEGIN_TOKEN) ParseHDLine(restOfLine);
54     else if ( firstToken == Constants::SAM_SQ_BEGIN_TOKEN) ParseSQLine(restOfLine);
55     else if ( firstToken == Constants::SAM_RG_BEGIN_TOKEN) ParseRGLine(restOfLine);
56     else if ( firstToken == Constants::SAM_PG_BEGIN_TOKEN) ParsePGLine(restOfLine);
57     else if ( firstToken == Constants::SAM_CO_BEGIN_TOKEN) ParseCOLine(restOfLine);
58     else cerr << "SAM Format Error - unknown token: " << firstToken << endl;
59     return;
60 }
61
62 void SamFormatParser::ParseHDLine(const string& line) {
63
64     // split HD lines into tokens
65     vector<string> tokens = Split(line, Constants::SAM_TAB);
66
67     // iterate over tokens
68     vector<string>::const_iterator tokenIter = tokens.begin();
69     vector<string>::const_iterator tokenEnd  = tokens.end();
70     for ( ; tokenIter != tokenEnd; ++tokenIter ) {
71
72         // get tag/value
73         const string tokenTag = (*tokenIter).substr(0,2);
74         const string tokenValue = (*tokenIter).substr(3);
75
76         // set header contents
77         if      ( tokenTag == Constants::SAM_HD_VERSION_TAG    ) m_header.Version    = tokenValue;
78         else if ( tokenTag == Constants::SAM_HD_GROUPORDER_TAG ) m_header.GroupOrder = tokenValue;
79         else if ( tokenTag == Constants::SAM_HD_SORTORDER_TAG  ) m_header.SortOrder  = tokenValue;
80         else
81             cerr << "SAM Format Error - unknown HD tag: " << tokenTag << endl;
82     }
83
84     // if @HD line exists, VN must be provided
85     if ( !m_header.HasVersion() ) {
86         cerr << "SAM Format Error - @HD line is missing VN tag!" << endl;
87         return;
88     }
89 }
90
91 void SamFormatParser::ParseSQLine(const string& line) {
92
93     SamSequence seq;
94
95     // split SQ line into tokens
96     vector<string> tokens = Split(line, Constants::SAM_TAB);
97
98     // iterate over tokens
99     vector<string>::const_iterator tokenIter = tokens.begin();
100     vector<string>::const_iterator tokenEnd  = tokens.end();
101     for ( ; tokenIter != tokenEnd; ++tokenIter ) {
102
103         // get tag/value
104         const string tokenTag = (*tokenIter).substr(0,2);
105         const string tokenValue = (*tokenIter).substr(3);
106
107         // set sequence contents
108         if      ( tokenTag == Constants::SAM_SQ_NAME_TAG       ) seq.Name = tokenValue;
109         else if ( tokenTag == Constants::SAM_SQ_LENGTH_TAG     ) seq.Length = tokenValue;
110         else if ( tokenTag == Constants::SAM_SQ_ASSEMBLYID_TAG ) seq.AssemblyID = tokenValue;
111         else if ( tokenTag == Constants::SAM_SQ_URI_TAG        ) seq.URI = tokenValue;
112         else if ( tokenTag == Constants::SAM_SQ_CHECKSUM_TAG   ) seq.Checksum = tokenValue;
113         else if ( tokenTag == Constants::SAM_SQ_SPECIES_TAG    ) seq.Species = tokenValue;
114         else
115             cerr << "SAM Format Error - unknown SQ tag: " << tokenTag << endl;
116     }
117
118     // if @SQ line exists, SN must be provided
119     if ( !seq.HasName() ) {
120         cerr << "SAM Format Error - @SQ line is missing SN tag!" << endl;
121         return;
122     }
123
124     // if @SQ line exists, LN must be provided
125     if ( !seq.HasLength() ) {
126         cerr << "SAM Format Error - @SQ line is missing LN tag!" << endl;
127         return;
128     }
129
130     // store SAM sequence entry
131     m_header.Sequences.Add(seq);
132 }
133
134 void SamFormatParser::ParseRGLine(const string& line) {
135
136     SamReadGroup rg;
137
138     // split string into tokens
139     vector<string> tokens = Split(line, Constants::SAM_TAB);
140
141     // iterate over tokens
142     vector<string>::const_iterator tokenIter = tokens.begin();
143     vector<string>::const_iterator tokenEnd  = tokens.end();
144     for ( ; tokenIter != tokenEnd; ++tokenIter ) {
145
146         // get token tag/value
147         const string tokenTag = (*tokenIter).substr(0,2);
148         const string tokenValue = (*tokenIter).substr(3);
149
150         // set read group contents
151         if      ( tokenTag == Constants::SAM_RG_ID_TAG                  ) rg.ID = tokenValue;
152         else if ( tokenTag == Constants::SAM_RG_SAMPLE_TAG              ) rg.Sample = tokenValue;
153         else if ( tokenTag == Constants::SAM_RG_LIBRARY_TAG             ) rg.Library = tokenValue;
154         else if ( tokenTag == Constants::SAM_RG_DESCRIPTION_TAG         ) rg.Description = tokenValue;
155         else if ( tokenTag == Constants::SAM_RG_PLATFORMUNIT_TAG        ) rg.PlatformUnit = tokenValue;
156         else if ( tokenTag == Constants::SAM_RG_PREDICTEDINSERTSIZE_TAG ) rg.PredictedInsertSize = tokenValue;
157         else if ( tokenTag == Constants::SAM_RG_SEQCENTER_TAG           ) rg.SequencingCenter = tokenValue;
158         else if ( tokenTag == Constants::SAM_RG_PRODUCTIONDATE_TAG      ) rg.ProductionDate = tokenValue;
159         else if ( tokenTag == Constants::SAM_RG_SEQTECHNOLOGY_TAG       ) rg.SequencingTechnology = tokenValue;
160         else
161             cerr << "SAM Format Error - unknown RG tag: " << tokenTag << endl;
162     }
163
164     // if @RG line exists, ID must be provided
165     if ( !rg.HasID() ) {
166         cerr << "SAM Format Error - @RG line is missing ID tag!" << endl;
167         return;
168     }
169
170     // if @RG line exists, SM must be provided
171     if ( !rg.HasSample() ) {
172         cerr << "SAM Format Error - @RG line is missing SM tag!" << endl;
173         return;
174     }
175
176     // store SAM read group entry
177     m_header.ReadGroups.Add(rg);
178 }
179
180 void SamFormatParser::ParsePGLine(const string& line) {
181
182     // split string into tokens
183     vector<string> tokens = Split(line, Constants::SAM_TAB);
184
185     // iterate over tokens
186     vector<string>::const_iterator tokenIter = tokens.begin();
187     vector<string>::const_iterator tokenEnd  = tokens.end();
188     for ( ; tokenIter != tokenEnd; ++tokenIter ) {
189
190         // get token tag/value
191         const string tokenTag = (*tokenIter).substr(0,2);
192         const string tokenValue = (*tokenIter).substr(3);
193
194         // set header contents
195         if      ( tokenTag == Constants::SAM_PG_NAME_TAG        ) m_header.ProgramName = tokenValue;
196         else if ( tokenTag == Constants::SAM_PG_VERSION_TAG     ) m_header.ProgramVersion = tokenValue;
197         else if ( tokenTag == Constants::SAM_PG_COMMANDLINE_TAG ) m_header.ProgramCommandLine = tokenValue;
198         else
199             cerr << "SAM Format Error - unknown PG tag: " << tokenTag << endl;
200     }
201
202     // if @PG line exists, ID must be provided
203     if ( !m_header.HasProgramName() ) {
204         cerr << "SAM Format Error - @PG line is missing ID tag!" << endl;
205         return;
206     }
207 }
208
209 void SamFormatParser::ParseCOLine(const string& line) {
210     // simply add line to comments list
211     m_header.Comments.push_back(line);
212 }
213
214 const vector<string> SamFormatParser::Split(const string& line, const char delim) {
215     vector<string> tokens;
216     stringstream lineStream(line);
217     string token;
218     while ( getline(lineStream, token, delim) )
219         tokens.push_back(token);
220     return tokens;
221 }