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;
71 while(!inName.eof()) {
73 if (m->control_pressed) { break; }
75 inName >> first; m->gobble(inName);
76 inName >> second; m->gobble(inName);
79 m->splitAtChar(second, names, ',');
81 //get aligned string for these seqs from the fasta file
82 string alignedString = "";
83 map<string, string>::iterator itAligned = seqName.find(names[0]);
84 if (itAligned == seqName.end()) {
85 error = 1; m->mothurOut("[ERROR]: " + names[0] + " is in your name file and not in your fasta file, please correct."); m->mothurOutEndLine();
87 alignedString = itAligned->second;
90 //separate by group - parse one line in name file
91 map<string, string> splitMap; //group -> name1,name2,...
92 map<string, string>::iterator it;
93 for (int i = 0; i < names.size(); i++) {
95 string group = groupMap->getGroup(names[i]);
96 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(); }
99 it = splitMap.find(group);
100 if (it != splitMap.end()) { //adding seqs to this group
101 (it->second) += "," + names[i];
102 thisnames1.insert(names[i]);
104 }else { //first sighting of this group
105 splitMap[group] = names[i];
107 thisnames1.insert(names[i]);
109 //is this seq in the fasta file?
110 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
111 Sequence tempSeq(names[i], alignedString); //get the first guys sequence string since he's in the fasta file.
112 seqs[group].push_back(tempSeq);
117 allSeqsMap[names[i]] = names[0];
121 //fill nameMapPerGroup - holds all lines in namefile separated by group
122 for (it = splitMap.begin(); it != splitMap.end(); it++) {
124 string firstName = "";
125 for(int i = 0; i < (it->second).length(); i++) {
126 if (((it->second)[i]) != ',') {
127 firstName += ((it->second)[i]);
131 //group1 -> seq1 -> seq1,seq2,seq3
132 nameMapPerGroup[it->first][firstName] = it->second;
138 if (error == 1) { m->control_pressed = true; }
140 if (countName != (groupMap->getNumSeqs())) {
141 vector<string> groupseqsnames = groupMap->getNamesSeqs();
143 for (int i = 0; i < groupseqsnames.size(); i++) {
144 set<string>::iterator itnamesfile = thisnames1.find(groupseqsnames[i]);
145 if (itnamesfile == thisnames1.end()){
146 cout << "missing name " + groupseqsnames[i] << '\t' << allSeqsMap[groupseqsnames[i]] << endl;
150 m->mothurOutEndLine();
151 m->mothurOut("[ERROR]: Your name file contains " + toString(countName) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
152 m->mothurOutEndLine();
153 m->control_pressed = true;
157 catch(exception& e) {
158 m->errorOut(e, "SequenceParser", "SequenceParser");
162 /************************************************************/
163 SequenceParser::SequenceParser(string groupFile, string fastaFile) {
166 m = MothurOut::getInstance();
170 groupMap = new GroupMap(groupFile);
171 error = groupMap->readMap();
173 if (error == 1) { m->control_pressed = true; }
176 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
177 for (int i = 0; i < namesOfGroups.size(); i++) {
178 vector<Sequence> temp;
179 seqs[namesOfGroups[i]] = temp;
182 //read fasta file making sure each sequence is in the group file
184 m->openInputFile(fastaFile, in);
189 if (m->control_pressed) { break; }
191 Sequence seq(in); m->gobble(in);
193 if (seq.getName() != "") {
195 string group = groupMap->getGroup(seq.getName());
196 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(); }
197 else { seqs[group].push_back(seq); count++; }
202 if (error == 1) { m->control_pressed = true; }
204 if (count != (groupMap->getNumSeqs())) {
205 m->mothurOutEndLine();
206 m->mothurOut("[ERROR]: Your fasta file contains " + toString(count) + " valid sequences, and your groupfile contains " + toString(groupMap->getNumSeqs()) + ", please correct.");
207 if (count < (groupMap->getNumSeqs())) { m->mothurOut(" Did you forget to include the name file?"); }
208 m->mothurOutEndLine();
209 m->control_pressed = true;
213 catch(exception& e) {
214 m->errorOut(e, "SequenceParser", "SequenceParser");
218 /************************************************************/
219 SequenceParser::~SequenceParser(){ delete groupMap; }
220 /************************************************************/
221 int SequenceParser::getNumGroups(){ return groupMap->getNumGroups(); }
222 /************************************************************/
223 vector<string> SequenceParser::getNamesOfGroups(){ return groupMap->getNamesOfGroups(); }
224 /************************************************************/
225 bool SequenceParser::isValidGroup(string g){ return groupMap->isValidGroup(g); }
226 /************************************************************/
227 string SequenceParser::getGroup(string g){ return groupMap->getGroup(g); }
228 /************************************************************/
229 int SequenceParser::getNumSeqs(string g){
231 map<string, vector<Sequence> >::iterator it;
235 if(it == seqs.end()) {
236 m->mothurOut("[ERROR]: " + g + " is not a valid group, please correct."); m->mothurOutEndLine();
238 num = (it->second).size();
243 catch(exception& e) {
244 m->errorOut(e, "SequenceParser", "getNumSeqs");
248 /************************************************************/
249 vector<Sequence> SequenceParser::getSeqs(string g){
251 map<string, vector<Sequence> >::iterator it;
252 vector<Sequence> seqForThisGroup;
255 if(it == seqs.end()) {
256 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
258 seqForThisGroup = it->second;
259 if (m->debug) { m->mothurOut("[DEBUG]: group " + g + " fasta file has " + toString(seqForThisGroup.size()) + " sequences."); }
262 return seqForThisGroup;
264 catch(exception& e) {
265 m->errorOut(e, "SequenceParser", "getSeqs");
269 /************************************************************/
270 int SequenceParser::getSeqs(string g, string filename, bool uchimeFormat=false){
272 map<string, vector<Sequence> >::iterator it;
273 vector<Sequence> seqForThisGroup;
274 vector<seqPriorityNode> nameVector;
277 if(it == seqs.end()) {
278 m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
282 m->openOutputFile(filename, out);
284 seqForThisGroup = it->second;
287 // format should look like
288 //>seqName /ab=numRedundantSeqs/
291 map<string, string> nameMapForThisGroup = getNameMap(g);
292 map<string, string>::iterator itNameMap;
295 for (int i = 0; i < seqForThisGroup.size(); i++) {
296 itNameMap = nameMapForThisGroup.find(seqForThisGroup[i].getName());
298 if (itNameMap == nameMapForThisGroup.end()){
300 m->mothurOut("[ERROR]: " + seqForThisGroup[i].getName() + " is in your fastafile, but is not in your namesfile, please correct."); m->mothurOutEndLine();
302 int num = m->getNumNames(itNameMap->second);
304 seqPriorityNode temp(num, seqForThisGroup[i].getAligned(), seqForThisGroup[i].getName());
305 nameVector.push_back(temp);
309 if (error == 1) { out.close(); m->mothurRemove(filename); return 1; }
311 //sort by num represented
312 sort(nameVector.begin(), nameVector.end(), compareSeqPriorityNodes);
314 //print new file in order of
315 for (int i = 0; i < nameVector.size(); i++) {
317 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
319 out << ">" << nameVector[i].name << "/ab=" << nameVector[i].numIdentical << "/" << endl << nameVector[i].seq << endl;
323 //m->mothurOut("Group " + g + " contains " + toString(seqForThisGroup.size()) + " unique seqs.\n");
324 for (int i = 0; i < seqForThisGroup.size(); i++) {
326 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
328 seqForThisGroup[i].printSequence(out);
336 catch(exception& e) {
337 m->errorOut(e, "SequenceParser", "getSeqs");
342 /************************************************************/
343 map<string, string> SequenceParser::getNameMap(string g){
345 map<string, map<string, string> >::iterator it;
346 map<string, string> nameMapForThisGroup;
348 it = nameMapPerGroup.find(g);
349 if(it == nameMapPerGroup.end()) {
350 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
352 nameMapForThisGroup = it->second;
353 if (m->debug) { m->mothurOut("[DEBUG]: group " + g + " name file has " + toString(nameMapForThisGroup.size()) + " unique sequences."); }
356 return nameMapForThisGroup;
358 catch(exception& e) {
359 m->errorOut(e, "SequenceParser", "getNameMap");
363 /************************************************************/
364 int SequenceParser::getNameMap(string g, string filename){
366 map<string, map<string, string> >::iterator it;
367 map<string, string> nameMapForThisGroup;
369 it = nameMapPerGroup.find(g);
370 if(it == nameMapPerGroup.end()) {
371 m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
373 nameMapForThisGroup = it->second;
376 m->openOutputFile(filename, out);
378 for (map<string, string>::iterator itFile = nameMapForThisGroup.begin(); itFile != nameMapForThisGroup.end(); itFile++) {
380 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
382 out << itFile->first << '\t' << itFile->second << endl;
390 catch(exception& e) {
391 m->errorOut(e, "SequenceParser", "getNameMap");
395 /************************************************************/