]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
added pcr.seqs command. fixed bug in rarefacftion.single that caused parsing error...
[mothur.git] / sharedcommand.cpp
1 /*
2  *  sharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedcommand.h"
11 //********************************************************************************************************************
12 //sorts lowest to highest
13 inline bool compareSharedRabunds(SharedRAbundVector* left, SharedRAbundVector* right){
14         return (left->getGroup() < right->getGroup());  
15 }
16 //**********************************************************************************************************************
17 vector<string> SharedCommand::setParameters(){  
18         try {
19                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
20                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
21                 //CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup);
22                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
23                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
24                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
25                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26                 
27                 vector<string> myArray;
28                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
29                 return myArray;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "SharedCommand", "setParameters");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 string SharedCommand::getHelpString(){  
38         try {
39                 string helpString = "";
40                 helpString += "The make.shared command reads a list and group file and creates a shared file, as well as a rabund file for each group.\n";
41                 helpString += "The make.shared command parameters are list, group, ordergroup, groups and label. list and group are required unless a current file is available.\n";
42                 helpString += "The groups parameter allows you to indicate which groups you want to include, group names should be separated by dashes. ex. groups=A-B-C. Default is all groups in your groupfile.\n";
43                 helpString += "The label parameter allows you to indicate which labels you want to include, label names should be separated by dashes. Default is all labels in your list file.\n";
44                 //helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n";
45                 return helpString;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "SharedCommand", "getHelpString");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 SharedCommand::SharedCommand(){ 
54         try {
55                 abort = true; calledHelp = true; 
56                 setParameters();
57                 //initialize outputTypes
58                 vector<string> tempOutNames;
59                 outputTypes["rabund"] = tempOutNames;
60                 outputTypes["shared"] = tempOutNames;
61                 outputTypes["group"] = tempOutNames;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "SharedCommand", "SharedCommand");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69 SharedCommand::SharedCommand(string option)  {
70         try {
71                 abort = false; calledHelp = false;   
72                 allLines = 1;
73                 
74                 //allow user to run help
75                 if(option == "help") { help(); abort = true; calledHelp = true; }
76                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
77                 
78                 else {
79                         
80                          vector<string> myArray = setParameters();
81                          
82                          OptionParser parser(option);
83                          map<string, string> parameters = parser.getParameters();
84                          
85                          ValidParameters validParameter;
86                          map<string, string>::iterator it;
87                          
88                          //check to make sure all parameters are valid for command
89                          for (it = parameters.begin(); it != parameters.end(); it++) { 
90                                  if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
91                          }
92                          
93                          //if the user changes the input directory command factory will send this info to us in the output parameter 
94                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
95                          if (inputDir == "not found"){  inputDir = "";          }
96                          else {
97                                  string path;
98                                  it = parameters.find("list");
99                                  //user has given a template file
100                                  if(it != parameters.end()){ 
101                                          path = m->hasPath(it->second);
102                                          //if the user has not given a path then, add inputdir. else leave path alone.
103                                          if (path == "") {      parameters["list"] = inputDir + it->second;             }
104                                  }
105                          
106                                  it = parameters.find("group");
107                                  //user has given a template file
108                                  if(it != parameters.end()){ 
109                                          path = m->hasPath(it->second);
110                                          //if the user has not given a path then, add inputdir. else leave path alone.
111                                          if (path == "") {      parameters["group"] = inputDir + it->second;            }
112                                  }
113                          
114                                  it = parameters.find("ordergroup");
115                                  //user has given a template file
116                                  if(it != parameters.end()){ 
117                                          path = m->hasPath(it->second);
118                                          //if the user has not given a path then, add inputdir. else leave path alone.
119                                          if (path == "") {      parameters["ordergroup"] = inputDir + it->second;               }
120                                  }
121                          }
122                          
123                          
124                          //if the user changes the output directory command factory will send this info to us in the output parameter 
125                          outputDir = validParameter.validFile(parameters, "outputdir", false);          if (outputDir == "not found"){  outputDir = ""; }
126                          
127                          //check for required parameters
128                          listfile = validParameter.validFile(parameters, "list", true);
129                          if (listfile == "not open") { listfile = ""; abort = true; }
130                          else if (listfile == "not found") { 
131                                  listfile = m->getListFile(); 
132                                  if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
133                                  else {         m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
134                          }else { m->setListFile(listfile); }    
135                                                         
136                          ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
137                          if (ordergroupfile == "not open") { abort = true; }    
138                          else if (ordergroupfile == "not found") { ordergroupfile = ""; }
139                                                  
140                          groupfile = validParameter.validFile(parameters, "group", true);
141                          if (groupfile == "not open") { groupfile = ""; abort = true; } 
142                          else if (groupfile == "not found") { 
143                                  groupfile = m->getGroupFile(); 
144                                  if (groupfile != "") { 
145                                          m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
146                                          groupMap = new GroupMap(groupfile);
147                                          
148                                          int error = groupMap->readMap();
149                                          if (error == 1) { abort = true; }
150                                          vector<string> allGroups = groupMap->getNamesOfGroups();
151                                          m->setAllGroups(allGroups);
152                                  }
153                                  else {         m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
154                          }else {  
155                                  groupMap = new GroupMap(groupfile);
156                          
157                                  int error = groupMap->readMap();
158                                  if (error == 1) { abort = true; }
159                                  vector<string> allGroups = groupMap->getNamesOfGroups();
160                                  m->setAllGroups(allGroups);
161                                  m->setGroupFile(groupfile);
162                          }
163                          
164                          string groups = validParameter.validFile(parameters, "groups", false);                 
165                          if (groups == "not found") { groups = ""; }
166                          else { 
167                                  m->splitAtDash(groups, Groups);
168                                  m->setGroups(Groups);
169                          }
170                          
171                          //check for optional parameter and set defaults
172                          // ...at some point should added some additional type checking...
173                          string label = validParameter.validFile(parameters, "label", false);                   
174                          if (label == "not found") { label = ""; }
175                          else { 
176                                  if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
177                                  else { allLines = 1;  }
178                          }
179                 }
180                 
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "SharedCommand", "SharedCommand");
184                 exit(1);
185         }
186 }
187 //**********************************************************************************************************************
188
189 int SharedCommand::execute(){
190         try {
191                 
192                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
193                 
194                 //getting output filename
195                 filename = listfile;
196                 
197                 if (outputDir == "") { outputDir += m->hasPath(filename); }
198                 
199                 filename = outputDir + m->getRootName(m->getSimpleName(filename));
200                 filename = filename + "shared";
201                 outputTypes["shared"].push_back(filename);
202                 
203                 m->openOutputFile(filename, out);
204                 pickedGroups = false;
205                 
206                 //if hte user has not specified any groups then use them all
207                 if (Groups.size() == 0) {
208                         Groups = groupMap->getNamesOfGroups(); m->setGroups(Groups);
209                 }else { pickedGroups = true; }
210                 
211                 //fill filehandles with neccessary ofstreams
212                 int i;
213                 ofstream* temp;
214                 for (i=0; i<Groups.size(); i++) {
215                         temp = new ofstream;
216                         filehandles[Groups[i]] = temp;
217                 }
218                 
219                 //set fileroot
220                 fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
221                 
222                 //clears file before we start to write to it below
223                 for (int i=0; i<Groups.size(); i++) {
224                         m->mothurRemove((fileroot + Groups[i] + ".rabund"));
225                         outputNames.push_back((fileroot + Groups[i] + ".rabund"));
226                         outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund"));
227                 }
228                 
229                 //lookup.clear();
230                 string errorOff = "no error";
231                 //errorOff = "";
232                 
233                 //if user provided an order file containing the order the shared file should be in read it
234                 if (ordergroupfile != "") { readOrderFile(); }
235                 
236                 input = new InputData(listfile, "shared");
237                 SharedList = input->getSharedListVector();
238                 string lastLabel = SharedList->getLabel();
239                 vector<SharedRAbundVector*> lookup; 
240                 
241                 if (m->control_pressed) { 
242                         delete input; delete SharedList; delete groupMap; 
243                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
244                         out.close(); m->mothurRemove(filename); 
245                         for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
246                         return 0; 
247                 }
248                 
249                 //sanity check
250                 int error = ListGroupSameSeqs();
251                 
252                 if ((!pickedGroups) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) {  //if the user has not specified any groups and their files don't match exit with error
253                         m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine(); 
254                         
255                         out.close();
256                         m->mothurRemove(filename); //remove blank shared file you made
257                         
258                         createMisMatchFile();
259                         
260                         //delete memory
261                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
262                                 delete it3->second;
263                         }
264                 
265                         delete input; delete SharedList; delete groupMap; 
266                         
267                         return 0; 
268                 }
269                 
270                 if (error == 1) { m->control_pressed = true; }
271                 
272                 //if user has specified groups make new groupfile for them
273                 if (pickedGroups) { //make new group file
274                         string groups = "";
275                         if (m->getNumGroups() < 4) {
276                                 for (int i = 0; i < m->getNumGroups(); i++) {
277                                         groups += (m->getGroups())[i] + ".";
278                                 }
279                         }else { groups = "merge"; }
280                 
281                         string newGroupFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + groups + "groups";
282                         outputTypes["group"].push_back(newGroupFile); 
283                         outputNames.push_back(newGroupFile);
284                         ofstream outGroups;
285                         m->openOutputFile(newGroupFile, outGroups);
286                 
287                         vector<string> names = groupMap->getNamesSeqs();
288                         string groupName;
289                         for (int i = 0; i < names.size(); i++) {
290                                 groupName = groupMap->getGroup(names[i]);
291                                 if (isValidGroup(groupName, m->getGroups())) {
292                                         outGroups << names[i] << '\t' << groupName << endl;
293                                 }
294                         }
295                         outGroups.close();
296                 }
297                 
298                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
299                 set<string> processedLabels;
300                 set<string> userLabels = labels;        
301         
302                 while((SharedList != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
303                         if (m->control_pressed) { 
304                                 delete input; delete SharedList; delete groupMap;
305                                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
306                                 out.close(); m->mothurRemove(filename); 
307                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
308                                 return 0; 
309                         }
310                 
311                         if(allLines == 1 || labels.count(SharedList->getLabel()) == 1){
312                                         
313                                         lookup = SharedList->getSharedRAbundVector();
314                                         
315                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
316                                         if (pickedGroups) { //check for otus with no seqs in them
317                                                 eliminateZeroOTUS(lookup);
318                                         }
319                                         
320                                         if (m->control_pressed) { 
321                                                 delete input; delete SharedList; delete groupMap; 
322                                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
323                                                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
324                                                 out.close(); m->mothurRemove(filename); 
325                                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
326                                                 return 0; 
327                                         }
328                                         
329                                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
330                                         printSharedData(lookup); //prints info to the .shared file
331                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
332                                 
333                                         processedLabels.insert(SharedList->getLabel());
334                                         userLabels.erase(SharedList->getLabel());
335                         }
336                         
337                         if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
338                                         string saveLabel = SharedList->getLabel();
339                                         
340                                         delete SharedList;
341                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
342                                         
343                                         lookup = SharedList->getSharedRAbundVector();
344                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
345                                         if (pickedGroups) { //check for otus with no seqs in them
346                                                 eliminateZeroOTUS(lookup);
347                                         }
348                                         
349                                         
350                                         if (m->control_pressed) { 
351                                                 delete input; delete SharedList; delete groupMap; 
352                                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
353                                                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
354                                                 out.close(); m->mothurRemove(filename); 
355                                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
356                                                 return 0; 
357                                         }
358                                         
359                                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
360                                         printSharedData(lookup); //prints info to the .shared file
361                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
362                                         
363                                         processedLabels.insert(SharedList->getLabel());
364                                         userLabels.erase(SharedList->getLabel());
365                                         
366                                         //restore real lastlabel to save below
367                                         SharedList->setLabel(saveLabel);
368                         }
369                         
370                 
371                         lastLabel = SharedList->getLabel();
372                                 
373                         delete SharedList;
374                         SharedList = input->getSharedListVector(); //get new list vector to process
375                 }
376                 
377                 //output error messages about any remaining user labels
378                 set<string>::iterator it;
379                 bool needToRun = false;
380                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
381                         if (processedLabels.count(lastLabel) != 1) {
382                                 needToRun = true;
383                         }
384                 }
385                 
386                 //run last label if you need to
387                 if (needToRun == true)  {
388                         if (SharedList != NULL) {       delete SharedList;      }
389                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
390                                         
391                         lookup = SharedList->getSharedRAbundVector();
392                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
393                         if (pickedGroups) { //check for otus with no seqs in them
394                                 eliminateZeroOTUS(lookup);
395                         }
396                         
397                         if (m->control_pressed) { 
398                                 delete input;  delete groupMap;
399                                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;   }
400                                         out.close(); m->mothurRemove(filename); 
401                                         for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
402                                         return 0; 
403                         }
404                         
405                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
406                         printSharedData(lookup); //prints info to the .shared file
407                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
408                         delete SharedList;
409                 }
410                 
411                 out.close();
412                 
413                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
414                         delete it3->second;
415                 }
416
417                 delete input; delete groupMap;
418                 
419                 if (m->control_pressed) { 
420                                 m->mothurRemove(filename); 
421                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
422                                 return 0; 
423                 }
424                 
425                 //set rabund file as new current rabundfile
426                 string current = "";
427                 itTypes = outputTypes.find("rabund");
428                 if (itTypes != outputTypes.end()) {
429                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
430                 }
431                 
432                 itTypes = outputTypes.find("shared");
433                 if (itTypes != outputTypes.end()) {
434                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
435                 }       
436                 
437                 itTypes = outputTypes.find("group");
438                 if (itTypes != outputTypes.end()) {
439                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
440                 }
441                 
442                 m->mothurOutEndLine();
443                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
444                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
445                 m->mothurOut(filename); m->mothurOutEndLine();
446                 m->mothurOutEndLine();
447                 
448                 return 0;
449         }
450         catch(exception& e) {
451                 m->errorOut(e, "SharedCommand", "execute");
452                 exit(1);
453         }
454 }
455 //**********************************************************************************************************************
456 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
457         try {
458                 
459                 if (order.size() == 0) { //user has not specified an order so do aplabetically
460                         sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
461                         
462                         m->clearGroups();
463                         vector<string> Groups;
464                         
465                         //initialize bin values
466                         for (int i = 0; i < thislookup.size(); i++) {
467                                 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
468                                 thislookup[i]->print(out);
469                                 
470                                 Groups.push_back(thislookup[i]->getGroup());
471                                 
472                                 RAbundVector rav = thislookup[i]->getRAbundVector();
473                                 m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
474                                 rav.print(*(filehandles[thislookup[i]->getGroup()]));
475                                 (*(filehandles[thislookup[i]->getGroup()])).close();
476                         }
477                         m->setGroups(Groups);
478                 }else{
479                         //create a map from groupName to each sharedrabund
480                         map<string, SharedRAbundVector*> myMap;
481                         map<string, SharedRAbundVector*>::iterator myIt;
482                         
483                         for (int i = 0; i < thislookup.size(); i++) {
484                                 myMap[thislookup[i]->getGroup()] = thislookup[i];
485                         }
486                         
487                         m->clearGroups();
488                         vector<string> Groups;
489                         
490                         //loop through ordered list and print the rabund
491                         for (int i = 0; i < order.size(); i++) {
492                                 myIt = myMap.find(order[i]);
493                                 
494                                 if(myIt != myMap.end()) { //we found it
495                                         out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
496                                         (myIt->second)->print(out);
497                                         
498                                         Groups.push_back((myIt->second)->getGroup());
499                                 
500                                         RAbundVector rav = (myIt->second)->getRAbundVector();
501                                         m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
502                                         rav.print(*(filehandles[(myIt->second)->getGroup()]));
503                                         (*(filehandles[(myIt->second)->getGroup()])).close();
504                                 }else{
505                                         m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
506                                 }
507                         }
508                         
509                         m->setGroups(Groups);
510                 
511                 }
512  
513         }
514         catch(exception& e) {
515                 m->errorOut(e, "SharedCommand", "printSharedData");
516                 exit(1);
517         }
518 }
519 //**********************************************************************************************************************
520 int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
521         try {
522                 
523                 vector<SharedRAbundVector*> newLookup;
524                 for (int i = 0; i < thislookup.size(); i++) {
525                         SharedRAbundVector* temp = new SharedRAbundVector();
526                         temp->setLabel(thislookup[i]->getLabel());
527                         temp->setGroup(thislookup[i]->getGroup());
528                         newLookup.push_back(temp);
529                 }
530                 
531                 //for each bin
532                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
533                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
534                 
535                         //look at each sharedRabund and make sure they are not all zero
536                         bool allZero = true;
537                         for (int j = 0; j < thislookup.size(); j++) {
538                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
539                         }
540                         
541                         //if they are not all zero add this bin
542                         if (!allZero) {
543                                 for (int j = 0; j < thislookup.size(); j++) {
544                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
545                                 }
546                                 //if there is a bin label use it otherwise make one
547                         }
548                         //else{  cout << "bin # " << i << " is all zeros" << endl;  }
549                 }
550         
551                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
552                 thislookup = newLookup;
553                 
554                 return 0;
555  
556         }
557         catch(exception& e) {
558                 m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
559                 exit(1);
560         }
561 }
562 //**********************************************************************************************************************
563 int SharedCommand::createMisMatchFile() {
564         try {
565                 ofstream outMisMatch;
566                 string outputMisMatchName = outputDir + m->getRootName(m->getSimpleName(listfile));
567                 
568                 //you have sequences in your list file that are not in your group file
569                 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
570                         outputMisMatchName += "missing.group";
571                         m->mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); m->mothurOutEndLine();
572                         
573                         m->openOutputFile(outputMisMatchName, outMisMatch);
574                         
575                         set<string> listNames;
576                         set<string>::iterator itList;
577                         
578                         //go through list and if group returns "not found" output it
579                         for (int i = 0; i < SharedList->getNumBins(); i++) {
580                                 if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } 
581                         
582                                 string names = SharedList->get(i); 
583                                 
584                 vector<string> binNames;
585                 m->splitAtComma(names, binNames);
586                 
587                                 for (int j = 0; j < binNames.size(); j++) { 
588                                         string name = binNames[j];
589                                         string group = groupMap->getGroup(name);
590                                         
591                                         if(group == "not found") {      outMisMatch << name << endl;  }
592                                         
593                                         itList = listNames.find(name);
594                                         if (itList != listNames.end()) {  m->mothurOut(name + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
595                                         else { listNames.insert(name); }
596                                 }
597                         }
598                         
599                         outMisMatch.close();
600                         
601                 
602                 }else {//you have sequences in your group file that are not in you list file
603                         
604                         outputMisMatchName += "missing.name";
605                         m->mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); m->mothurOutEndLine();
606                         
607                         map<string, string> namesInList;
608                         map<string, string>::iterator itList;
609                         
610                         //go through listfile and get names
611                         for (int i = 0; i < SharedList->getNumBins(); i++) {
612                                 if (m->control_pressed) {  return 0; } 
613
614                                 
615                                 string names = SharedList->get(i); 
616                 
617                                 vector<string> binNames;
618                 m->splitAtComma(names, binNames);
619                 
620                                 for (int j = 0; j < binNames.size(); j++) { 
621
622                                         string name = binNames[j];
623                                         
624                                         itList = namesInList.find(name);
625                                         if (itList != namesInList.end()) {  m->mothurOut(name + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
626
627                                         namesInList[name] = name;
628                                         
629                                 }
630                         }
631                         
632                         //get names of sequences in groupfile
633                         vector<string> seqNames = groupMap->getNamesSeqs();
634                 
635                         map<string, string>::iterator itMatch;
636                         
637                         m->openOutputFile(outputMisMatchName, outMisMatch);
638                         
639                         //loop through names in seqNames and if they aren't in namesIn list output them
640                         for (int i = 0; i < seqNames.size(); i++) {
641                                 if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } 
642                                 
643                                 itMatch = namesInList.find(seqNames[i]);
644                                 
645                                 if (itMatch == namesInList.end()) {
646                                 
647                                         outMisMatch << seqNames[i] << endl; 
648                                 }
649                         }               
650                         outMisMatch.close();
651                 }
652                 
653                 return 0;
654         }
655         catch(exception& e) {
656                 m->errorOut(e, "SharedCommand", "createMisMatchFile");
657                 exit(1);
658         }
659 }
660 //**********************************************************************************************************************
661 int SharedCommand::ListGroupSameSeqs() {
662         try {
663                 
664                 int error = 0; 
665                 
666                 vector<string> groupMapsSeqs = groupMap->getNamesSeqs();
667         
668                 set<string> groupNamesSeqs;
669                 for(int i = 0; i < groupMapsSeqs.size(); i++) {
670                         groupNamesSeqs.insert(groupMapsSeqs[i]);
671                 }
672                 
673                 //go through list and if group returns "not found" output it
674                 for (int i = 0; i < SharedList->getNumBins(); i++) {
675                         if (m->control_pressed) { return 0; } 
676                         
677                         string names = SharedList->get(i); 
678                         
679                         vector<string> listNames;
680                         m->splitAtComma(names, listNames);
681                         
682                         for (int j = 0; j < listNames.size(); j++) {
683                                 int num = groupNamesSeqs.count(listNames[j]);
684                                 
685                                 if (num == 0) { error = 1; m->mothurOut("[ERROR]: " + listNames[j] + " is in your listfile and not in your groupfile. Please correct."); m->mothurOutEndLine(); }
686                                 else { groupNamesSeqs.erase(listNames[j]); }
687                         }
688                 }
689                 
690                 for (set<string>::iterator itGroupSet = groupNamesSeqs.begin(); itGroupSet != groupNamesSeqs.end(); itGroupSet++) {
691                         error = 1; 
692                         m->mothurOut("[ERROR]: " + (*itGroupSet) + " is in your groupfile and not your listfile. Please correct."); m->mothurOutEndLine();
693                 }
694                                 
695                 return error;
696         }
697         catch(exception& e) {
698                 m->errorOut(e, "SharedCommand", "ListGroupSameSeqs");
699                 exit(1);
700         }
701 }
702 //**********************************************************************************************************************
703
704 SharedCommand::~SharedCommand(){
705         //delete list;
706         
707         
708 }
709 //**********************************************************************************************************************
710 int SharedCommand::readOrderFile() {
711         try {
712                 //remove old names
713                 order.clear();
714                 
715                 ifstream in;
716                 m->openInputFile(ordergroupfile, in);
717                 string thisGroup;
718                 
719                 while(!in.eof()){
720                         in >> thisGroup; m->gobble(in);
721                                                 
722                         order.push_back(thisGroup);
723                         
724                         if (m->control_pressed) { order.clear(); break; }
725                 }
726                 in.close();             
727                 
728                 return 0;
729         }
730         catch(exception& e) {
731                 m->errorOut(e, "SharedCommand", "readOrderFile");
732                 exit(1);
733         }
734 }
735 //**********************************************************************************************************************
736
737 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
738         try {
739                 for (int i = 0; i < groups.size(); i++) {
740                         if (groupname == groups[i]) { return true; }
741                 }
742                 
743                 return false;
744         }
745         catch(exception& e) {
746                 m->errorOut(e, "SharedCommand", "isValidGroup");
747                 exit(1);
748         }
749 }
750 /************************************************************/
751
752