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