5 * Created by westcott on 9/9/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "sequenceparser.h"
13 /************************************************************/
14 SequenceParser::SequenceParser(string groupFile, string fastaFile, string nameFile) {
17 m = MothurOut::getInstance();
21 groupMap = new GroupMap(groupFile);
22 error = groupMap->readMap();
24 if (error == 1) { m->control_pressed = true; }
27 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
28 for (int i = 0; i < namesOfGroups.size(); i++) {
29 vector<Sequence> temp;
30 map<string, string> tempMap;
31 seqs[namesOfGroups[i]] = temp;
32 nameMapPerGroup[namesOfGroups[i]] = tempMap;
35 //read fasta file making sure each sequence is in the group file
37 m->openInputFile(fastaFile, in);
39 map<string, string> seqName; //stores name -> sequence string so we can make new "unique" sequences when we parse the name file
43 if (m->control_pressed) { break; }
45 Sequence seq(in); m->gobble(in);
47 if (m->debug) { if((fastaCount) % 1000 == 0){ m->mothurOut("[DEBUG]: reading seq " + toString(fastaCount) + "\n."); } }
49 if (seq.getName() != "") {
51 string group = groupMap->getGroup(seq.getName());
52 if (group == "not found") { error = 1; m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file and not in your groupfile, please correct."); m->mothurOutEndLine(); }
54 seqs[group].push_back(seq);
55 seqName[seq.getName()] = seq.getAligned();
61 if (error == 1) { m->control_pressed = true; }
65 m->openInputFile(nameFile, inName);
69 set<string> thisnames1;
73 bool pairDone = false;
74 bool columnOne = true;
75 string firstCol, secondCol;
77 while (!inName.eof()) {
78 if (m->control_pressed) { break; }
80 inName.read(buffer, 4096);
81 vector<string> pieces = m->splitWhiteSpace(rest, buffer, inName.gcount());
83 for (int i = 0; i < pieces.size(); i++) {
84 if (columnOne) { firstCol = pieces[i]; columnOne=false; }
85 else { secondCol = pieces[i]; pairDone = true; columnOne=true; }
87 if (pairDone) { //save one line
89 m->splitAtChar(second, names, ',');
91 //get aligned string for these seqs from the fasta file
92 string alignedString = "";
93 map<string, string>::iterator itAligned = seqName.find(names[0]);
94 if (itAligned == seqName.end()) {
95 error = 1; m->mothurOut("[ERROR]: " + names[0] + " is in your name file and not in your fasta file, please correct."); m->mothurOutEndLine();
97 alignedString = itAligned->second;
100 //separate by group - parse one line in name file
101 map<string, string> splitMap; //group -> name1,name2,...
102 map<string, string>::iterator it;
103 for (int i = 0; i < names.size(); i++) {
105 string group = groupMap->getGroup(names[i]);
106 if (group == "not found") { error = 1; m->mothurOut("[ERROR]: " + names[i] + " is in your name file and not in your groupfile, please correct."); m->mothurOutEndLine(); }
109 it = splitMap.find(group);
110 if (it != splitMap.end()) { //adding seqs to this group
111 (it->second) += "," + names[i];
112 thisnames1.insert(names[i]);
114 }else { //first sighting of this group
115 splitMap[group] = names[i];
117 thisnames1.insert(names[i]);
119 //is this seq in the fasta file?
120 if (i != 0) { //if not then we need to add a duplicate sequence to the seqs for this group so the new "fasta" and "name" files will match
121 Sequence tempSeq(names[i], alignedString); //get the first guys sequence string since he's in the fasta file.
122 seqs[group].push_back(tempSeq);
127 allSeqsMap[names[i]] = names[0];
131 //fill nameMapPerGroup - holds all lines in namefile separated by group
132 for (it = splitMap.begin(); it != splitMap.end(); it++) {
134 string firstName = "";
135 for(int i = 0; i < (it->second).length(); i++) {
136 if (((it->second)[i]) != ',') {
137 firstName += ((it->second)[i]);
141 //group1 -> seq1 -> seq1,seq2,seq3
142 nameMapPerGroup[it->first][firstName] = it->second;
151 if (error == 1) { m->control_pressed = true; }
153 if (countName != (groupMap->getNumSeqs())) {
154 vector<string> groupseqsnames = groupMap->getNamesSeqs();
156 for (int i = 0; i < groupseqsnames.size(); i++) {
157 set<string>::iterator itnamesfile = thisnames1.find(groupseqsnames[i]);
158 if (itnamesfile == thisnames1.end()){
159 cout << "missing name " + groupseqsnames[i] << '\t' << allSeqsMap[groupseqsnames[i]] << endl;
163 m->mothurOutEndLine();
164 m->mothurOut("[ERROR]: Your name file contains " + toString(countName) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
165 m->mothurOutEndLine();
166 m->control_pressed = true;
170 catch(exception& e) {
171 m->errorOut(e, "SequenceParser", "SequenceParser");
175 /************************************************************/
176 SequenceParser::SequenceParser(string groupFile, string fastaFile) {
179 m = MothurOut::getInstance();
183 groupMap = new GroupMap(groupFile);
184 error = groupMap->readMap();
186 if (error == 1) { m->control_pressed = true; }
189 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
190 for (int i = 0; i < namesOfGroups.size(); i++) {
191 vector<Sequence> temp;
192 seqs[namesOfGroups[i]] = temp;
195 //read fasta file making sure each sequence is in the group file
197 m->openInputFile(fastaFile, in);
202 if (m->control_pressed) { break; }
204 Sequence seq(in); m->gobble(in);
206 if (seq.getName() != "") {
208 string group = groupMap->getGroup(seq.getName());
209 if (group == "not found") { error = 1; m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file and not in your groupfile, please correct."); m->mothurOutEndLine(); }
210 else { seqs[group].push_back(seq); count++; }
215 if (error == 1) { m->control_pressed = true; }
217 if (count != (groupMap->getNumSeqs())) {
218 m->mothurOutEndLine();
219 m->mothurOut("[ERROR]: Your fasta file contains " + toString(count) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
220 if (count < (groupMap->getNumSeqs())) { m->mothurOut(" Did you forget to include the name file?"); }
221 m->mothurOutEndLine();
222 m->control_pressed = true;
226 catch(exception& e) {
227 m->errorOut(e, "SequenceParser", "SequenceParser");
231 /************************************************************/
232 SequenceParser::~SequenceParser(){ delete groupMap; }
233 /************************************************************/
234 int SequenceParser::getNumGroups(){ return groupMap->getNumGroups(); }
235 /************************************************************/
236 vector<string> SequenceParser::getNamesOfGroups(){ return groupMap->getNamesOfGroups(); }
237 /************************************************************/
238 bool SequenceParser::isValidGroup(string g){ return groupMap->isValidGroup(g); }
239 /************************************************************/
240 string SequenceParser::getGroup(string g){ return groupMap->getGroup(g); }
241 /************************************************************/
242 int SequenceParser::getNumSeqs(string g){
244 map<string, vector<Sequence> >::iterator it;
248 if(it == seqs.end()) {
249 m->mothurOut("[ERROR]: " + g + " is not a valid group, please correct."); m->mothurOutEndLine();
251 num = (it->second).size();
256 catch(exception& e) {
257 m->errorOut(e, "SequenceParser", "getNumSeqs");
261 /************************************************************/
262 vector<Sequence> SequenceParser::getSeqs(string g){
264 map<string, vector<Sequence> >::iterator it;
265 vector<Sequence> seqForThisGroup;
268 if(it == seqs.end()) {
269 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
271 seqForThisGroup = it->second;
272 if (m->debug) { m->mothurOut("[DEBUG]: group " + g + " fasta file has " + toString(seqForThisGroup.size()) + " sequences."); }
275 return seqForThisGroup;
277 catch(exception& e) {
278 m->errorOut(e, "SequenceParser", "getSeqs");
282 /************************************************************/
283 int SequenceParser::getSeqs(string g, string filename, bool uchimeFormat=false){
285 map<string, vector<Sequence> >::iterator it;
286 vector<Sequence> seqForThisGroup;
287 vector<seqPriorityNode> nameVector;
290 if(it == seqs.end()) {
291 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
295 m->openOutputFile(filename, out);
297 seqForThisGroup = it->second;
300 // format should look like
301 //>seqName /ab=numRedundantSeqs/
304 map<string, string> nameMapForThisGroup = getNameMap(g);
305 map<string, string>::iterator itNameMap;
308 for (int i = 0; i < seqForThisGroup.size(); i++) {
309 itNameMap = nameMapForThisGroup.find(seqForThisGroup[i].getName());
311 if (itNameMap == nameMapForThisGroup.end()){
313 m->mothurOut("[ERROR]: " + seqForThisGroup[i].getName() + " is in your fastafile, but is not in your namesfile, please correct."); m->mothurOutEndLine();
315 int num = m->getNumNames(itNameMap->second);
317 seqPriorityNode temp(num, seqForThisGroup[i].getAligned(), seqForThisGroup[i].getName());
318 nameVector.push_back(temp);
322 if (error == 1) { out.close(); m->mothurRemove(filename); return 1; }
324 //sort by num represented
325 sort(nameVector.begin(), nameVector.end(), compareSeqPriorityNodes);
327 //print new file in order of
328 for (int i = 0; i < nameVector.size(); i++) {
330 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
332 out << ">" << nameVector[i].name << "/ab=" << nameVector[i].numIdentical << "/" << endl << nameVector[i].seq << endl;
336 //m->mothurOut("Group " + g + " contains " + toString(seqForThisGroup.size()) + " unique seqs.\n");
337 for (int i = 0; i < seqForThisGroup.size(); i++) {
339 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
341 seqForThisGroup[i].printSequence(out);
349 catch(exception& e) {
350 m->errorOut(e, "SequenceParser", "getSeqs");
355 /************************************************************/
356 map<string, string> SequenceParser::getNameMap(string g){
358 map<string, map<string, string> >::iterator it;
359 map<string, string> nameMapForThisGroup;
361 it = nameMapPerGroup.find(g);
362 if(it == nameMapPerGroup.end()) {
363 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
365 nameMapForThisGroup = it->second;
366 if (m->debug) { m->mothurOut("[DEBUG]: group " + g + " name file has " + toString(nameMapForThisGroup.size()) + " unique sequences."); }
369 return nameMapForThisGroup;
371 catch(exception& e) {
372 m->errorOut(e, "SequenceParser", "getNameMap");
376 /************************************************************/
377 int SequenceParser::getNameMap(string g, string filename){
379 map<string, map<string, string> >::iterator it;
380 map<string, string> nameMapForThisGroup;
382 it = nameMapPerGroup.find(g);
383 if(it == nameMapPerGroup.end()) {
384 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
386 nameMapForThisGroup = it->second;
389 m->openOutputFile(filename, out);
391 for (map<string, string>::iterator itFile = nameMapForThisGroup.begin(); itFile != nameMapForThisGroup.end(); itFile++) {
393 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
395 out << itFile->first << '\t' << itFile->second << endl;
403 catch(exception& e) {
404 m->errorOut(e, "SequenceParser", "getNameMap");
408 /************************************************************/