]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
changed normalize.shared and sub.sample commands to fix bug in normalize.shared and...
[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                                          m->namesOfGroups = groupMap->namesOfGroups;
151                                  }
152                                  else {         m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
153                          }else {  
154                                  groupMap = new GroupMap(groupfile);
155                          
156                                  int error = groupMap->readMap();
157                                  if (error == 1) { abort = true; }
158                                  m->namesOfGroups = groupMap->namesOfGroups;
159                                  m->setGroupFile(groupfile);
160                          }
161                          
162                          string groups = validParameter.validFile(parameters, "groups", false);                 
163                          if (groups == "not found") { groups = ""; }
164                          else { 
165                                  m->splitAtDash(groups, Groups);
166                                  m->Groups = Groups;
167                          }
168                          
169                          //check for optional parameter and set defaults
170                          // ...at some point should added some additional type checking...
171                          string label = validParameter.validFile(parameters, "label", false);                   
172                          if (label == "not found") { label = ""; }
173                          else { 
174                                  if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
175                                  else { allLines = 1;  }
176                          }
177                 }
178                 
179         }
180         catch(exception& e) {
181                 m->errorOut(e, "SharedCommand", "SharedCommand");
182                 exit(1);
183         }
184 }
185 //**********************************************************************************************************************
186
187 int SharedCommand::execute(){
188         try {
189                 
190                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
191                 
192                 //getting output filename
193                 filename = listfile;
194                 
195                 if (outputDir == "") { outputDir += m->hasPath(filename); }
196                 
197                 filename = outputDir + m->getRootName(m->getSimpleName(filename));
198                 filename = filename + "shared";
199                 outputTypes["shared"].push_back(filename);
200                 
201                 m->openOutputFile(filename, out);
202                 pickedGroups = false;
203                 
204                 //if hte user has not specified any groups then use them all
205                 if (Groups.size() == 0) {
206                         Groups = groupMap->namesOfGroups; m->Groups = Groups;
207                 }else { pickedGroups = true; }
208                 
209                 //fill filehandles with neccessary ofstreams
210                 int i;
211                 ofstream* temp;
212                 for (i=0; i<Groups.size(); i++) {
213                         temp = new ofstream;
214                         filehandles[Groups[i]] = temp;
215                 }
216                 
217                 //set fileroot
218                 fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
219                 
220                 //clears file before we start to write to it below
221                 for (int i=0; i<Groups.size(); i++) {
222                         m->mothurRemove((fileroot + Groups[i] + ".rabund"));
223                         outputNames.push_back((fileroot + Groups[i] + ".rabund"));
224                         outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund"));
225                 }
226                 
227                 //lookup.clear();
228                 string errorOff = "no error";
229                 //errorOff = "";
230                 
231                 //if user provided an order file containing the order the shared file should be in read it
232                 if (ordergroupfile != "") { readOrderFile(); }
233                 
234                 input = new InputData(listfile, "shared");
235                 SharedList = input->getSharedListVector();
236                 string lastLabel = SharedList->getLabel();
237                 vector<SharedRAbundVector*> lookup; 
238                 
239                 if (m->control_pressed) { 
240                         delete input; delete SharedList; delete groupMap; 
241                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
242                         out.close(); m->mothurRemove(filename); 
243                         for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
244                         return 0; 
245                 }
246                                 
247                 if ((m->Groups.size() == 0) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) {  //if the user has not specified any groups and their files don't match exit with error
248                         m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine(); 
249                         
250                         out.close();
251                         m->mothurRemove(filename); //remove blank shared file you made
252                         
253                         createMisMatchFile();
254                         
255                         //delete memory
256                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
257                                 delete it3->second;
258                         }
259                 
260                         delete input; delete SharedList; delete groupMap; 
261                         
262                         return 0; 
263                 }
264                 
265                 //if user has specified groups make new groupfile for them
266                 if (pickedGroups) { //make new group file
267                         string groups = "";
268                         if (m->Groups.size() < 4) {
269                                 for (int i = 0; i < m->Groups.size(); i++) {
270                                         groups += m->Groups[i] + ".";
271                                 }
272                         }else { groups = "merge"; }
273                 
274                         string newGroupFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + groups + "groups";
275                         outputTypes["group"].push_back(newGroupFile); 
276                         outputNames.push_back(newGroupFile);
277                         ofstream outGroups;
278                         m->openOutputFile(newGroupFile, outGroups);
279                 
280                         vector<string> names = groupMap->getNamesSeqs();
281                         string groupName;
282                         for (int i = 0; i < names.size(); i++) {
283                                 groupName = groupMap->getGroup(names[i]);
284                                 if (isValidGroup(groupName, m->Groups)) {
285                                         outGroups << names[i] << '\t' << groupName << endl;
286                                 }
287                         }
288                         outGroups.close();
289                 }
290                 
291                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
292                 set<string> processedLabels;
293                 set<string> userLabels = labels;        
294         
295                 while((SharedList != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
296                         if (m->control_pressed) { 
297                                 delete input; delete SharedList; delete groupMap;
298                                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
299                                 out.close(); m->mothurRemove(filename); 
300                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
301                                 return 0; 
302                         }
303                 
304                         if(allLines == 1 || labels.count(SharedList->getLabel()) == 1){
305                                         
306                                         lookup = SharedList->getSharedRAbundVector();
307                                         
308                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
309                                         if (pickedGroups) { //check for otus with no seqs in them
310                                                 eliminateZeroOTUS(lookup);
311                                         }
312                                         
313                                         if (m->control_pressed) { 
314                                                 delete input; delete SharedList; delete groupMap; 
315                                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
316                                                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
317                                                 out.close(); m->mothurRemove(filename); 
318                                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
319                                                 return 0; 
320                                         }
321                                         
322                                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
323                                         printSharedData(lookup); //prints info to the .shared file
324                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
325                                 
326                                         processedLabels.insert(SharedList->getLabel());
327                                         userLabels.erase(SharedList->getLabel());
328                         }
329                         
330                         if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
331                                         string saveLabel = SharedList->getLabel();
332                                         
333                                         delete SharedList;
334                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
335                                         
336                                         lookup = SharedList->getSharedRAbundVector();
337                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
338                                         if (pickedGroups) { //check for otus with no seqs in them
339                                                 eliminateZeroOTUS(lookup);
340                                         }
341                                         
342                                         
343                                         if (m->control_pressed) { 
344                                                 delete input; delete SharedList; delete groupMap; 
345                                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
346                                                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
347                                                 out.close(); m->mothurRemove(filename); 
348                                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
349                                                 return 0; 
350                                         }
351                                         
352                                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
353                                         printSharedData(lookup); //prints info to the .shared file
354                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
355                                         
356                                         processedLabels.insert(SharedList->getLabel());
357                                         userLabels.erase(SharedList->getLabel());
358                                         
359                                         //restore real lastlabel to save below
360                                         SharedList->setLabel(saveLabel);
361                         }
362                         
363                 
364                         lastLabel = SharedList->getLabel();
365                                 
366                         delete SharedList;
367                         SharedList = input->getSharedListVector(); //get new list vector to process
368                 }
369                 
370                 //output error messages about any remaining user labels
371                 set<string>::iterator it;
372                 bool needToRun = false;
373                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
374                         if (processedLabels.count(lastLabel) != 1) {
375                                 needToRun = true;
376                         }
377                 }
378                 
379                 //run last label if you need to
380                 if (needToRun == true)  {
381                         if (SharedList != NULL) {       delete SharedList;      }
382                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
383                                         
384                         lookup = SharedList->getSharedRAbundVector();
385                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
386                         if (pickedGroups) { //check for otus with no seqs in them
387                                 eliminateZeroOTUS(lookup);
388                         }
389                         
390                         if (m->control_pressed) { 
391                                 delete input;  delete groupMap;
392                                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;   }
393                                         out.close(); m->mothurRemove(filename); 
394                                         for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
395                                         return 0; 
396                         }
397                         
398                         if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
399                         printSharedData(lookup); //prints info to the .shared file
400                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
401                         delete SharedList;
402                 }
403                 
404                 out.close();
405                 
406                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
407                         delete it3->second;
408                 }
409
410                 delete input; delete groupMap;
411                 
412                 if (m->control_pressed) { 
413                                 m->mothurRemove(filename); 
414                                 for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
415                                 return 0; 
416                 }
417                 
418                 //set rabund file as new current rabundfile
419                 string current = "";
420                 itTypes = outputTypes.find("rabund");
421                 if (itTypes != outputTypes.end()) {
422                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
423                 }
424                 
425                 itTypes = outputTypes.find("shared");
426                 if (itTypes != outputTypes.end()) {
427                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
428                 }       
429                 
430                 itTypes = outputTypes.find("group");
431                 if (itTypes != outputTypes.end()) {
432                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
433                 }
434                 
435                 m->mothurOutEndLine();
436                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
437                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
438                 m->mothurOut(filename); m->mothurOutEndLine();
439                 m->mothurOutEndLine();
440                 
441                 return 0;
442         }
443         catch(exception& e) {
444                 m->errorOut(e, "SharedCommand", "execute");
445                 exit(1);
446         }
447 }
448 //**********************************************************************************************************************
449 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
450         try {
451                 
452                 if (order.size() == 0) { //user has not specified an order so do aplabetically
453                         sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
454                         
455                         m->Groups.clear();
456                         
457                         //initialize bin values
458                         for (int i = 0; i < thislookup.size(); i++) {
459                                 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
460                                 thislookup[i]->print(out);
461                                 
462                                 m->Groups.push_back(thislookup[i]->getGroup());
463                                 
464                                 RAbundVector rav = thislookup[i]->getRAbundVector();
465                                 m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
466                                 rav.print(*(filehandles[thislookup[i]->getGroup()]));
467                                 (*(filehandles[thislookup[i]->getGroup()])).close();
468                         }
469                 }else{
470                         //create a map from groupName to each sharedrabund
471                         map<string, SharedRAbundVector*> myMap;
472                         map<string, SharedRAbundVector*>::iterator myIt;
473                         
474                         for (int i = 0; i < thislookup.size(); i++) {
475                                 myMap[thislookup[i]->getGroup()] = thislookup[i];
476                         }
477                         
478                         m->Groups.clear();
479                         
480                         //loop through ordered list and print the rabund
481                         for (int i = 0; i < order.size(); i++) {
482                                 myIt = myMap.find(order[i]);
483                                 
484                                 if(myIt != myMap.end()) { //we found it
485                                         out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
486                                         (myIt->second)->print(out);
487                                         
488                                         m->Groups.push_back((myIt->second)->getGroup());
489                                 
490                                         RAbundVector rav = (myIt->second)->getRAbundVector();
491                                         m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
492                                         rav.print(*(filehandles[(myIt->second)->getGroup()]));
493                                         (*(filehandles[(myIt->second)->getGroup()])).close();
494                                 }else{
495                                         m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
496                                 }
497                         }
498                 
499                 }
500  
501         }
502         catch(exception& e) {
503                 m->errorOut(e, "SharedCommand", "printSharedData");
504                 exit(1);
505         }
506 }
507 //**********************************************************************************************************************
508 int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
509         try {
510                 
511                 vector<SharedRAbundVector*> newLookup;
512                 for (int i = 0; i < thislookup.size(); i++) {
513                         SharedRAbundVector* temp = new SharedRAbundVector();
514                         temp->setLabel(thislookup[i]->getLabel());
515                         temp->setGroup(thislookup[i]->getGroup());
516                         newLookup.push_back(temp);
517                 }
518                 
519                 //for each bin
520                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
521                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
522                 
523                         //look at each sharedRabund and make sure they are not all zero
524                         bool allZero = true;
525                         for (int j = 0; j < thislookup.size(); j++) {
526                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
527                         }
528                         
529                         //if they are not all zero add this bin
530                         if (!allZero) {
531                                 for (int j = 0; j < thislookup.size(); j++) {
532                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
533                                 }
534                                 //if there is a bin label use it otherwise make one
535                         }
536                         //else{  cout << "bin # " << i << " is all zeros" << endl;  }
537                 }
538         
539                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
540                 thislookup = newLookup;
541                 
542                 return 0;
543  
544         }
545         catch(exception& e) {
546                 m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
547                 exit(1);
548         }
549 }
550 //**********************************************************************************************************************
551 int SharedCommand::createMisMatchFile() {
552         try {
553                 ofstream outMisMatch;
554                 string outputMisMatchName = outputDir + m->getRootName(m->getSimpleName(listfile));
555                 
556                 //you have sequences in your list file that are not in your group file
557                 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
558                         outputMisMatchName += "missing.group";
559                         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();
560                         
561                         m->openOutputFile(outputMisMatchName, outMisMatch);
562                         
563                         map<string, string> listNames;
564                         map<string, string>::iterator itList;
565                         
566                         //go through list and if group returns "not found" output it
567                         for (int i = 0; i < SharedList->getNumBins(); i++) {
568                                 if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } 
569                         
570                                 string names = SharedList->get(i); 
571                                 
572                                 while (names.find_first_of(',') != -1) { 
573                                         string name = names.substr(0,names.find_first_of(','));
574                                         names = names.substr(names.find_first_of(',')+1, names.length());
575                                         string group = groupMap->getGroup(name);
576                                         
577                                         if(group == "not found") {      outMisMatch << name << endl;  }
578                                         
579                                         itList = listNames.find(name);
580                                         if (itList != listNames.end()) {  m->mothurOut(name + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
581                                         else { listNames[name] = name; }
582                                 }
583                         
584                                 //get last name
585                                 string group = groupMap->getGroup(names);
586                                 if(group == "not found") {      outMisMatch << names << endl;  }        
587                                 
588                                 itList = listNames.find(names);
589                                 if (itList != listNames.end()) {  m->mothurOut(names + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
590                                 else { listNames[names] = names; }
591
592                         }
593                         
594                         outMisMatch.close();
595                         
596                 
597                 }else {//you have sequences in your group file that are not in you list file
598                         
599                         outputMisMatchName += "missing.name";
600                         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();
601                         
602                         map<string, string> namesInList;
603                         map<string, string>::iterator itList;
604                         
605                         //go through listfile and get names
606                         for (int i = 0; i < SharedList->getNumBins(); i++) {
607                                 if (m->control_pressed) {  return 0; } 
608
609                                 
610                                 string names = SharedList->get(i); 
611                 
612                                 while (names.find_first_of(',') != -1) { 
613                                         string name = names.substr(0,names.find_first_of(','));
614                                         names = names.substr(names.find_first_of(',')+1, names.length());
615                                         
616                                         itList = namesInList.find(name);
617                                         if (itList != namesInList.end()) {  m->mothurOut(name + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
618
619                                         namesInList[name] = name;
620                                         
621                                 }
622                                 
623                                 itList = namesInList.find(names);
624                                 if (itList != namesInList.end()) {  m->mothurOut(names + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
625
626                                 //get last name
627                                 namesInList[names] = names;                             
628                         }
629                         
630                         //get names of sequences in groupfile
631                         vector<string> seqNames = groupMap->getNamesSeqs();
632                 
633                         map<string, string>::iterator itMatch;
634                         
635                         m->openOutputFile(outputMisMatchName, outMisMatch);
636                         
637                         //loop through names in seqNames and if they aren't in namesIn list output them
638                         for (int i = 0; i < seqNames.size(); i++) {
639                                 if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } 
640                                 
641                                 itMatch = namesInList.find(seqNames[i]);
642                                 
643                                 if (itMatch == namesInList.end()) {
644                                 
645                                         outMisMatch << seqNames[i] << endl; 
646                                 }
647                         }               
648                         outMisMatch.close();
649                 }
650                 
651                 return 0;
652         }
653         catch(exception& e) {
654                 m->errorOut(e, "SharedCommand", "createMisMatchFile");
655                 exit(1);
656         }
657 }
658
659 //**********************************************************************************************************************
660
661 SharedCommand::~SharedCommand(){
662         //delete list;
663         
664         
665 }
666 //**********************************************************************************************************************
667 int SharedCommand::readOrderFile() {
668         try {
669                 //remove old names
670                 order.clear();
671                 
672                 ifstream in;
673                 m->openInputFile(ordergroupfile, in);
674                 string thisGroup;
675                 
676                 while(!in.eof()){
677                         in >> thisGroup; m->gobble(in);
678                                                 
679                         order.push_back(thisGroup);
680                         
681                         if (m->control_pressed) { order.clear(); break; }
682                 }
683                 in.close();             
684                 
685                 return 0;
686         }
687         catch(exception& e) {
688                 m->errorOut(e, "SharedCommand", "readOrderFile");
689                 exit(1);
690         }
691 }
692 //**********************************************************************************************************************
693
694 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
695         try {
696                 for (int i = 0; i < groups.size(); i++) {
697                         if (groupname == groups[i]) { return true; }
698                 }
699                 
700                 return false;
701         }
702         catch(exception& e) {
703                 m->errorOut(e, "SharedCommand", "isValidGroup");
704                 exit(1);
705         }
706 }
707 /************************************************************/
708
709