]> git.donarmstrong.com Git - mothur.git/blob - sequenceparser.cpp
changes while testing 1.26
[mothur.git] / sequenceparser.cpp
1 /*
2  *  sequenceParser.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/9/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "sequenceparser.h"
11
12
13 /************************************************************/
14 SequenceParser::SequenceParser(string groupFile, string fastaFile, string nameFile) {
15         try {
16                 
17                 m = MothurOut::getInstance();
18                 int error;
19                 
20                 //read group file
21                 groupMap = new GroupMap(groupFile);
22                 error = groupMap->readMap();
23                 
24                 if (error == 1) { m->control_pressed = true; }
25                 
26                 //initialize maps
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;
33                 }
34                 
35                 //read fasta file making sure each sequence is in the group file
36                 ifstream in;
37                 m->openInputFile(fastaFile, in);
38                 
39                 map<string, string> seqName; //stores name -> sequence string so we can make new "unique" sequences when we parse the name file
40         int fastaCount = 0;
41                 while (!in.eof()) {
42                         
43                         if (m->control_pressed) { break; }
44                         
45                         Sequence seq(in); m->gobble(in);
46             fastaCount++;
47             if (m->debug) { if((fastaCount) % 1000 == 0){       m->mothurOut("[DEBUG]: reading seq " + toString(fastaCount) + "\n.");   } }
48                         
49         if (seq.getName() != "") {
50                                 
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();  }
53                                  else { 
54                                          seqs[group].push_back(seq);    
55                                          seqName[seq.getName()] = seq.getAligned();
56                                  }
57                         }
58                 }
59                 in.close();
60                                  
61                 if (error == 1) { m->control_pressed = true; }
62                                  
63                 //read name file
64                 ifstream inName;
65                 m->openInputFile(nameFile, inName);
66                 
67                 //string first, second;
68                 int countName = 0;
69                 set<string> thisnames1;
70                 
71         string rest = "";
72         char buffer[4096];
73         bool pairDone = false;
74         bool columnOne = true;
75         string firstCol, secondCol;
76         
77                 while (!inName.eof()) {
78                         if (m->control_pressed) { break; }
79                         
80             inName.read(buffer, 4096);
81             vector<string> pieces = m->splitWhiteSpace(rest, buffer, inName.gcount());
82             
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; }
86                 
87                 if (pairDone) { //save one line
88                     if (m->debug) { m->mothurOut("[DEBUG]: reading names: " + firstCol + '\t' + secondCol + ".\n"); }
89                     vector<string> names;
90                     m->splitAtChar(secondCol, names, ',');
91                     
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();
97                     }else {
98                         alignedString = itAligned->second;
99                     }
100                     
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++) {
105                         
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();  }
108                         else {  
109                             
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]);
114                                 countName++;
115                             }else { //first sighting of this group
116                                 splitMap[group] = names[i];
117                                 countName++;
118                                 thisnames1.insert(names[i]);
119                                 
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);
124                                 }
125                             }
126                         }
127                         
128                         allSeqsMap[names[i]] = names[0];
129                     }
130                     
131                     
132                     //fill nameMapPerGroup - holds all lines in namefile separated by group
133                     for (it = splitMap.begin(); it != splitMap.end(); it++) {
134                         //grab first name
135                         string firstName = "";
136                         for(int i = 0; i < (it->second).length(); i++) {
137                             if (((it->second)[i]) != ',') {
138                                 firstName += ((it->second)[i]);
139                             }else { break; }
140                         }
141                         
142                         //group1 -> seq1 -> seq1,seq2,seq3
143                         nameMapPerGroup[it->first][firstName] = it->second;
144                     }
145
146                     pairDone = false; 
147                 }
148             }
149                 }
150                 inName.close();
151                 
152                 if (error == 1) { m->control_pressed = true; }
153                         
154                 if (countName != (groupMap->getNumSeqs())) {
155                         vector<string> groupseqsnames = groupMap->getNamesSeqs();
156                         
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;
161                                 }
162                         }
163                         
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;
168                 }
169                 
170         }
171         catch(exception& e) {
172                 m->errorOut(e, "SequenceParser", "SequenceParser");
173                 exit(1);
174         }
175 }
176 /************************************************************/
177 SequenceParser::SequenceParser(string groupFile, string fastaFile) {
178         try {
179                 
180                 m = MothurOut::getInstance();
181                 int error;
182                 
183                 //read group file
184                 groupMap = new GroupMap(groupFile);
185                 error = groupMap->readMap();
186                 
187                 if (error == 1) { m->control_pressed = true; }
188                 
189                 //initialize maps
190                 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
191                 for (int i = 0; i < namesOfGroups.size(); i++) {
192                         vector<Sequence> temp;
193                         seqs[namesOfGroups[i]] = temp;
194                 }
195                 
196                 //read fasta file making sure each sequence is in the group file
197                 ifstream in;
198                 m->openInputFile(fastaFile, in);
199                 int count = 0;
200                 
201                 while (!in.eof()) {
202                         
203                         if (m->control_pressed) { break; }
204                         
205                         Sequence seq(in); m->gobble(in);
206                         
207                         if (seq.getName() != "") {
208                                 
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++; }
212                         }
213                 }
214                 in.close();
215                 
216                 if (error == 1) { m->control_pressed = true; }
217                 
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;
224                 }
225                 
226         }
227         catch(exception& e) {
228                 m->errorOut(e, "SequenceParser", "SequenceParser");
229                 exit(1);
230         }
231 }
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){ 
244         try {
245                 map<string, vector<Sequence> >::iterator it;
246                 int num = 0;
247                 
248                 it = seqs.find(g);
249                 if(it == seqs.end()) {
250                         m->mothurOut("[ERROR]: " + g + " is not a valid group, please correct."); m->mothurOutEndLine();
251                 }else {
252                         num = (it->second).size();
253                 }
254                 
255                 return num; 
256         }
257         catch(exception& e) {
258                 m->errorOut(e, "SequenceParser", "getNumSeqs");
259                 exit(1);
260         }
261 }
262 /************************************************************/
263 vector<Sequence> SequenceParser::getSeqs(string g){ 
264         try {
265                 map<string, vector<Sequence> >::iterator it;
266                 vector<Sequence> seqForThisGroup;
267                 
268                 it = seqs.find(g);
269                 if(it == seqs.end()) {
270                         m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
271                 }else {
272                         seqForThisGroup = it->second;
273             if (m->debug) {  m->mothurOut("[DEBUG]: group " + g + " fasta file has " + toString(seqForThisGroup.size()) + " sequences.");  }
274                 }
275                 
276                 return seqForThisGroup; 
277         }
278         catch(exception& e) {
279                 m->errorOut(e, "SequenceParser", "getSeqs");
280                 exit(1);
281         }
282 }
283 /************************************************************/
284 int SequenceParser::getSeqs(string g, string filename, bool uchimeFormat=false){ 
285         try {
286                 map<string, vector<Sequence> >::iterator it;
287                 vector<Sequence> seqForThisGroup;
288                 vector<seqPriorityNode> nameVector;
289                 
290                 it = seqs.find(g);
291                 if(it == seqs.end()) {
292                         m->mothurOut("[ERROR]: No sequences available for group " + g + ", please correct."); m->mothurOutEndLine();
293                 }else {
294                         
295                         ofstream out;
296                         m->openOutputFile(filename, out);
297                         
298                         seqForThisGroup = it->second;
299                         
300                         if (uchimeFormat) {
301                                 // format should look like 
302                                 //>seqName /ab=numRedundantSeqs/
303                                 //sequence
304                                 
305                                 map<string, string> nameMapForThisGroup = getNameMap(g);
306                                 map<string, string>::iterator itNameMap;
307                                 int error = 0;
308                                 
309                                 for (int i = 0; i < seqForThisGroup.size(); i++) {
310                                         itNameMap = nameMapForThisGroup.find(seqForThisGroup[i].getName());
311                                         
312                                         if (itNameMap == nameMapForThisGroup.end()){
313                                                 error = 1;
314                                                 m->mothurOut("[ERROR]: " + seqForThisGroup[i].getName() + " is in your fastafile, but is not in your namesfile, please correct."); m->mothurOutEndLine();
315                                         }else {
316                                                 int num = m->getNumNames(itNameMap->second);
317                                                 
318                                                 seqPriorityNode temp(num, seqForThisGroup[i].getAligned(), seqForThisGroup[i].getName());
319                                                 nameVector.push_back(temp);
320                                         }
321                                 }
322                                 
323                                 if (error == 1) { out.close(); m->mothurRemove(filename); return 1; }
324                                 
325                                 //sort by num represented
326                                 sort(nameVector.begin(), nameVector.end(), compareSeqPriorityNodes);
327
328                                 //print new file in order of
329                                 for (int i = 0; i < nameVector.size(); i++) {
330                                         
331                                         if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
332                                         
333                                         out << ">" << nameVector[i].name  << "/ab=" << nameVector[i].numIdentical << "/" << endl << nameVector[i].seq << endl;
334                                 }
335                                 
336                         }else { 
337                 //m->mothurOut("Group " + g +  " contains " + toString(seqForThisGroup.size()) + " unique seqs.\n");
338                                 for (int i = 0; i < seqForThisGroup.size(); i++) {
339                                         
340                                         if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
341                                         
342                                         seqForThisGroup[i].printSequence(out);  
343                                 }
344                         }
345                         out.close();
346                 }
347                 
348                 return 0; 
349         }
350         catch(exception& e) {
351                 m->errorOut(e, "SequenceParser", "getSeqs");
352                 exit(1);
353         }
354 }
355
356 /************************************************************/
357 map<string, string> SequenceParser::getNameMap(string g){ 
358         try {
359                 map<string, map<string, string> >::iterator it;
360                 map<string, string> nameMapForThisGroup;
361                 
362                 it = nameMapPerGroup.find(g);
363                 if(it == nameMapPerGroup.end()) {
364                         m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
365                 }else {
366                         nameMapForThisGroup = it->second;
367             if (m->debug) {  m->mothurOut("[DEBUG]: group " + g + " name file has " + toString(nameMapForThisGroup.size()) + " unique sequences.");  }
368                 }
369                 
370                 return nameMapForThisGroup; 
371         }
372         catch(exception& e) {
373                 m->errorOut(e, "SequenceParser", "getNameMap");
374                 exit(1);
375         }
376 }
377 /************************************************************/
378 int SequenceParser::getNameMap(string g, string filename){ 
379         try {
380                 map<string, map<string, string> >::iterator it;
381                 map<string, string> nameMapForThisGroup;
382                 
383                 it = nameMapPerGroup.find(g);
384                 if(it == nameMapPerGroup.end()) {
385                         m->mothurOut("[ERROR]: No nameMap available for group " + g + ", please correct."); m->mothurOutEndLine();
386                 }else {
387                         nameMapForThisGroup = it->second;
388                         
389                         ofstream out;
390                         m->openOutputFile(filename, out);
391                         
392                         for (map<string, string>::iterator itFile = nameMapForThisGroup.begin(); itFile != nameMapForThisGroup.end(); itFile++) {
393                                 
394                                 if(m->control_pressed) { out.close(); m->mothurRemove(filename); return 1; }
395                                 
396                                 out << itFile->first << '\t' << itFile->second << endl;
397                         }
398                         
399                         out.close();
400                 }
401                 
402                 return 0; 
403         }
404         catch(exception& e) {
405                 m->errorOut(e, "SequenceParser", "getNameMap");
406                 exit(1);
407         }
408 }
409 /************************************************************/
410
411
412