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);
67 //string first, second;
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
88 if (m->debug) { m->mothurOut("[DEBUG]: reading names: " + firstCol + '\t' + secondCol + ".\n"); }
90 m->splitAtChar(secondCol, names, ',');
92 //get aligned string for these seqs from the fasta file
93 string alignedString = "";
94 map<string, string>::iterator itAligned = seqName.find(names[0]);
95 if (itAligned == seqName.end()) {
96 error = 1; m->mothurOut("[ERROR]: " + names[0] + " is in your name file and not in your fasta file, please correct."); m->mothurOutEndLine();
98 alignedString = itAligned->second;
101 //separate by group - parse one line in name file
102 map<string, string> splitMap; //group -> name1,name2,...
103 map<string, string>::iterator it;
104 for (int i = 0; i < names.size(); i++) {
106 string group = groupMap->getGroup(names[i]);
107 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(); }
110 it = splitMap.find(group);
111 if (it != splitMap.end()) { //adding seqs to this group
112 (it->second) += "," + names[i];
113 thisnames1.insert(names[i]);
115 }else { //first sighting of this group
116 splitMap[group] = names[i];
118 thisnames1.insert(names[i]);
120 //is this seq in the fasta file?
121 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
122 Sequence tempSeq(names[i], alignedString); //get the first guys sequence string since he's in the fasta file.
123 seqs[group].push_back(tempSeq);
128 allSeqsMap[names[i]] = names[0];
132 //fill nameMapPerGroup - holds all lines in namefile separated by group
133 for (it = splitMap.begin(); it != splitMap.end(); it++) {
135 string firstName = "";
136 for(int i = 0; i < (it->second).length(); i++) {
137 if (((it->second)[i]) != ',') {
138 firstName += ((it->second)[i]);
142 //group1 -> seq1 -> seq1,seq2,seq3
143 nameMapPerGroup[it->first][firstName] = it->second;
152 if (error == 1) { m->control_pressed = true; }
154 if (countName != (groupMap->getNumSeqs())) {
155 vector<string> groupseqsnames = groupMap->getNamesSeqs();
157 for (int i = 0; i < groupseqsnames.size(); i++) {
158 set<string>::iterator itnamesfile = thisnames1.find(groupseqsnames[i]);
159 if (itnamesfile == thisnames1.end()){
160 cout << "missing name " + groupseqsnames[i] << '\t' << allSeqsMap[groupseqsnames[i]] << endl;
164 m->mothurOutEndLine();
165 m->mothurOut("[ERROR]: Your name file contains " + toString(countName) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
166 m->mothurOutEndLine();
167 m->control_pressed = true;
171 catch(exception& e) {
172 m->errorOut(e, "SequenceParser", "SequenceParser");
176 /************************************************************/
177 SequenceParser::SequenceParser(string groupFile, string fastaFile) {
180 m = MothurOut::getInstance();
184 groupMap = new GroupMap(groupFile);
185 error = groupMap->readMap();
187 if (error == 1) { m->control_pressed = true; }
190 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
191 for (int i = 0; i < namesOfGroups.size(); i++) {
192 vector<Sequence> temp;
193 seqs[namesOfGroups[i]] = temp;
196 //read fasta file making sure each sequence is in the group file
198 m->openInputFile(fastaFile, in);
203 if (m->control_pressed) { break; }
205 Sequence seq(in); m->gobble(in);
207 if (seq.getName() != "") {
209 string group = groupMap->getGroup(seq.getName());
210 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(); }
211 else { seqs[group].push_back(seq); count++; }
216 if (error == 1) { m->control_pressed = true; }
218 if (count != (groupMap->getNumSeqs())) {
219 m->mothurOutEndLine();
220 m->mothurOut("[ERROR]: Your fasta file contains " + toString(count) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
221 if (count < (groupMap->getNumSeqs())) { m->mothurOut(" Did you forget to include the name file?"); }
222 m->mothurOutEndLine();
223 m->control_pressed = true;
227 catch(exception& e) {
228 m->errorOut(e, "SequenceParser", "SequenceParser");
232 /************************************************************/
233 SequenceParser::~SequenceParser(){ delete groupMap; }
234 /************************************************************/
235 int SequenceParser::getNumGroups(){ return groupMap->getNumGroups(); }
236 /************************************************************/
237 vector<string> SequenceParser::getNamesOfGroups(){ return groupMap->getNamesOfGroups(); }
238 /************************************************************/
239 bool SequenceParser::isValidGroup(string g){ return groupMap->isValidGroup(g); }
240 /************************************************************/
241 string SequenceParser::getGroup(string g){ return groupMap->getGroup(g); }
242 /************************************************************/
243 int SequenceParser::getNumSeqs(string g){
245 map<string, vector<Sequence> >::iterator it;
249 if(it == seqs.end()) {
250 m->mothurOut("[ERROR]: " + g + " is not a valid group, please correct."); m->mothurOutEndLine();
252 num = (it->second).size();
257 catch(exception& e) {
258 m->errorOut(e, "SequenceParser", "getNumSeqs");
262 /************************************************************/
263 vector<Sequence> SequenceParser::getSeqs(string g){
265 map<string, vector<Sequence> >::iterator it;
266 vector<Sequence> seqForThisGroup;
269 if(it == seqs.end()) {
270 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
272 seqForThisGroup = it->second;
273 if (m->debug) { m->mothurOut("[DEBUG]: group " + g + " fasta file has " + toString(seqForThisGroup.size()) + " sequences."); }
276 return seqForThisGroup;
278 catch(exception& e) {
279 m->errorOut(e, "SequenceParser", "getSeqs");
283 /************************************************************/
284 int SequenceParser::getSeqs(string g, string filename, bool uchimeFormat=false){
286 map<string, vector<Sequence> >::iterator it;
287 vector<Sequence> seqForThisGroup;
288 vector<seqPriorityNode> nameVector;
291 if(it == seqs.end()) {
292 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
296 m->openOutputFile(filename, out);
298 seqForThisGroup = it->second;
301 // format should look like
302 //>seqName /ab=numRedundantSeqs/
305 map<string, string> nameMapForThisGroup = getNameMap(g);
306 map<string, string>::iterator itNameMap;
309 for (int i = 0; i < seqForThisGroup.size(); i++) {
310 itNameMap = nameMapForThisGroup.find(seqForThisGroup[i].getName());
312 if (itNameMap == nameMapForThisGroup.end()){
314 m->mothurOut("[ERROR]: " + seqForThisGroup[i].getName() + " is in your fastafile, but is not in your namesfile, please correct."); m->mothurOutEndLine();
316 int num = m->getNumNames(itNameMap->second);
318 seqPriorityNode temp(num, seqForThisGroup[i].getAligned(), seqForThisGroup[i].getName());
319 nameVector.push_back(temp);
323 if (error == 1) { out.close(); m->mothurRemove(filename); return 1; }
325 //sort by num represented
326 sort(nameVector.begin(), nameVector.end(), compareSeqPriorityNodes);
328 //print new file in order of
329 for (int i = 0; i < nameVector.size(); i++) {
331 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
333 out << ">" << nameVector[i].name << "/ab=" << nameVector[i].numIdentical << "/" << endl << nameVector[i].seq << endl;
337 //m->mothurOut("Group " + g + " contains " + toString(seqForThisGroup.size()) + " unique seqs.\n");
338 for (int i = 0; i < seqForThisGroup.size(); i++) {
340 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
342 seqForThisGroup[i].printSequence(out);
350 catch(exception& e) {
351 m->errorOut(e, "SequenceParser", "getSeqs");
356 /************************************************************/
357 map<string, string> SequenceParser::getNameMap(string g){
359 map<string, map<string, string> >::iterator it;
360 map<string, string> nameMapForThisGroup;
362 it = nameMapPerGroup.find(g);
363 if(it == nameMapPerGroup.end()) {
364 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
366 nameMapForThisGroup = it->second;
367 if (m->debug) { m->mothurOut("[DEBUG]: group " + g + " name file has " + toString(nameMapForThisGroup.size()) + " unique sequences."); }
370 return nameMapForThisGroup;
372 catch(exception& e) {
373 m->errorOut(e, "SequenceParser", "getNameMap");
377 /************************************************************/
378 int SequenceParser::getNameMap(string g, string filename){
380 map<string, map<string, string> >::iterator it;
381 map<string, string> nameMapForThisGroup;
383 it = nameMapPerGroup.find(g);
384 if(it == nameMapPerGroup.end()) {
385 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
387 nameMapForThisGroup = it->second;
390 m->openOutputFile(filename, out);
392 for (map<string, string>::iterator itFile = nameMapForThisGroup.begin(); itFile != nameMapForThisGroup.end(); itFile++) {
394 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
396 out << itFile->first << '\t' << itFile->second << endl;
404 catch(exception& e) {
405 m->errorOut(e, "SequenceParser", "getNameMap");
409 /************************************************************/