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
42 if (m->control_pressed) { break; }
44 Sequence seq(in); m->gobble(in);
46 if (seq.getName() != "") {
48 string group = groupMap->getGroup(seq.getName());
49 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(); }
51 seqs[group].push_back(seq);
52 seqName[seq.getName()] = seq.getAligned();
58 if (error == 1) { m->control_pressed = true; }
62 m->openInputFile(nameFile, inName);
66 while(!inName.eof()) {
68 if (m->control_pressed) { break; }
70 inName >> first; m->gobble(inName);
71 inName >> second; m->gobble(inName);
74 m->splitAtChar(second, names, ',');
76 //get aligned string for these seqs from the fasta file
77 string alignedString = "";
78 map<string, string>::iterator itAligned = seqName.find(names[0]);
79 if (itAligned == seqName.end()) {
80 error = 1; m->mothurOut("[ERROR]: " + names[0] + " is in your name file and not in your fasta file, please correct."); m->mothurOutEndLine();
82 alignedString = itAligned->second;
85 //separate by group - parse one line in name file
86 map<string, string> splitMap; //group -> name1,name2,...
87 map<string, string>::iterator it;
88 for (int i = 0; i < names.size(); i++) {
90 string group = groupMap->getGroup(names[i]);
91 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(); }
94 it = splitMap.find(group);
95 if (it != splitMap.end()) { //adding seqs to this group
96 (it->second) += "," + names[i];
98 }else { //first sighting of this group
99 splitMap[group] = names[i];
102 //is this seq in the fasta file?
103 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
104 Sequence tempSeq(names[i], alignedString); //get the first guys sequence string since he's in the fasta file.
105 seqs[group].push_back(tempSeq);
110 allSeqsMap[names[i]] = names[0];
114 //fill nameMapPerGroup - holds all lines in namefile separated by group
115 for (it = splitMap.begin(); it != splitMap.end(); it++) {
117 string firstName = "";
118 for(int i = 0; i < (it->second).length(); i++) {
119 if (((it->second)[i]) != ',') {
120 firstName += ((it->second)[i]);
124 //group1 -> seq1 -> seq1,seq2,seq3
125 nameMapPerGroup[it->first][firstName] = it->second;
131 if (error == 1) { m->control_pressed = true; }
133 if (countName != (groupMap->getNumSeqs())) {
134 m->mothurOutEndLine();
135 m->mothurOut("[ERROR]: Your name file contains " + toString(countName) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
136 m->mothurOutEndLine();
137 m->control_pressed = true;
141 catch(exception& e) {
142 m->errorOut(e, "SequenceParser", "SequenceParser");
146 /************************************************************/
147 SequenceParser::SequenceParser(string groupFile, string fastaFile) {
150 m = MothurOut::getInstance();
154 groupMap = new GroupMap(groupFile);
155 error = groupMap->readMap();
157 if (error == 1) { m->control_pressed = true; }
160 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
161 for (int i = 0; i < namesOfGroups.size(); i++) {
162 vector<Sequence> temp;
163 seqs[namesOfGroups[i]] = temp;
166 //read fasta file making sure each sequence is in the group file
168 m->openInputFile(fastaFile, in);
173 if (m->control_pressed) { break; }
175 Sequence seq(in); m->gobble(in);
177 if (seq.getName() != "") {
179 string group = groupMap->getGroup(seq.getName());
180 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(); }
181 else { seqs[group].push_back(seq); count++; }
186 if (error == 1) { m->control_pressed = true; }
188 if (count != (groupMap->getNumSeqs())) {
189 m->mothurOutEndLine();
190 m->mothurOut("[ERROR]: Your fasta file contains " + toString(count) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
191 if (count < (groupMap->getNumSeqs())) { m->mothurOut(" Did you forget to include the name file?"); }
192 m->mothurOutEndLine();
193 m->control_pressed = true;
197 catch(exception& e) {
198 m->errorOut(e, "SequenceParser", "SequenceParser");
202 /************************************************************/
203 SequenceParser::~SequenceParser(){ delete groupMap; }
204 /************************************************************/
205 int SequenceParser::getNumGroups(){ return groupMap->getNumGroups(); }
206 /************************************************************/
207 vector<string> SequenceParser::getNamesOfGroups(){ return groupMap->getNamesOfGroups(); }
208 /************************************************************/
209 bool SequenceParser::isValidGroup(string g){ return groupMap->isValidGroup(g); }
210 /************************************************************/
211 string SequenceParser::getGroup(string g){ return groupMap->getGroup(g); }
212 /************************************************************/
213 int SequenceParser::getNumSeqs(string g){
215 map<string, vector<Sequence> >::iterator it;
219 if(it == seqs.end()) {
220 m->mothurOut("[ERROR]: " + g + " is not a valid group, please correct."); m->mothurOutEndLine();
222 num = (it->second).size();
227 catch(exception& e) {
228 m->errorOut(e, "SequenceParser", "getNumSeqs");
232 /************************************************************/
233 vector<Sequence> SequenceParser::getSeqs(string g){
235 map<string, vector<Sequence> >::iterator it;
236 vector<Sequence> seqForThisGroup;
239 if(it == seqs.end()) {
240 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
242 seqForThisGroup = it->second;
245 return seqForThisGroup;
247 catch(exception& e) {
248 m->errorOut(e, "SequenceParser", "getSeqs");
252 /************************************************************/
253 int SequenceParser::getSeqs(string g, string filename, bool uchimeFormat=false){
255 map<string, vector<Sequence> >::iterator it;
256 vector<Sequence> seqForThisGroup;
257 vector<seqPriorityNode> nameVector;
260 if(it == seqs.end()) {
261 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
265 m->openOutputFile(filename, out);
267 seqForThisGroup = it->second;
270 // format should look like
271 //>seqName /ab=numRedundantSeqs/
274 map<string, string> nameMapForThisGroup = getNameMap(g);
275 map<string, string>::iterator itNameMap;
278 for (int i = 0; i < seqForThisGroup.size(); i++) {
279 itNameMap = nameMapForThisGroup.find(seqForThisGroup[i].getName());
281 if (itNameMap == nameMapForThisGroup.end()){
283 m->mothurOut("[ERROR]: " + seqForThisGroup[i].getName() + " is in your fastafile, but is not in your namesfile, please correct."); m->mothurOutEndLine();
285 int num = m->getNumNames(itNameMap->second);
287 seqPriorityNode temp(num, seqForThisGroup[i].getAligned(), seqForThisGroup[i].getName());
288 nameVector.push_back(temp);
292 if (error == 1) { out.close(); m->mothurRemove(filename); return 1; }
294 //sort by num represented
295 sort(nameVector.begin(), nameVector.end(), compareSeqPriorityNodes);
297 //print new file in order of
298 for (int i = 0; i < nameVector.size(); i++) {
300 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
302 out << ">" << nameVector[i].name << "/ab=" << nameVector[i].numIdentical << "/" << endl << nameVector[i].seq << endl;
306 for (int i = 0; i < seqForThisGroup.size(); i++) {
308 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
310 seqForThisGroup[i].printSequence(out);
318 catch(exception& e) {
319 m->errorOut(e, "SequenceParser", "getSeqs");
324 /************************************************************/
325 map<string, string> SequenceParser::getNameMap(string g){
327 map<string, map<string, string> >::iterator it;
328 map<string, string> nameMapForThisGroup;
330 it = nameMapPerGroup.find(g);
331 if(it == nameMapPerGroup.end()) {
332 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
334 nameMapForThisGroup = it->second;
337 return nameMapForThisGroup;
339 catch(exception& e) {
340 m->errorOut(e, "SequenceParser", "getNameMap");
344 /************************************************************/
345 int SequenceParser::getNameMap(string g, string filename){
347 map<string, map<string, string> >::iterator it;
348 map<string, string> nameMapForThisGroup;
350 it = nameMapPerGroup.find(g);
351 if(it == nameMapPerGroup.end()) {
352 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
354 nameMapForThisGroup = it->second;
357 m->openOutputFile(filename, out);
359 for (map<string, string>::iterator itFile = nameMapForThisGroup.begin(); itFile != nameMapForThisGroup.end(); itFile++) {
361 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
363 out << itFile->first << '\t' << itFile->second << endl;
371 catch(exception& e) {
372 m->errorOut(e, "SequenceParser", "getNameMap");
376 /************************************************************/