]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedcommand.cpp
fixed phylo.diversity
[mothur.git] / sharedcommand.cpp
index 2ce9bbcded2ba51409f9d130489953a3753ea438..fb3691e662a7ee2b378d8e1dd13e4221b698106d 100644 (file)
@@ -8,40 +8,57 @@
  */
 
 #include "sharedcommand.h"
-
+//********************************************************************************************************************
+//sorts lowest to highest
+inline bool compareSharedRabunds(SharedRAbundVector* left, SharedRAbundVector* right){
+       return (left->getGroup() < right->getGroup());  
+} 
 //**********************************************************************************************************************
 
-SharedCommand::SharedCommand(){
+SharedCommand::SharedCommand(string o) : outputDir(o) {
        try {
                globaldata = GlobalData::getInstance();
                
                //getting output filename
                filename = globaldata->inputFileName;
-               filename = getRootName(filename);
+               if (outputDir == "") { outputDir += m->hasPath(filename); }
+               
+               filename = outputDir + m->getRootName(m->getSimpleName(filename));
                filename = filename + "shared";
-               openOutputFile(filename, out);
+               
+               m->openOutputFile(filename, out);
+               pickedGroups = false;
                
                groupMap = globaldata->gGroupmap;
                
+               //if hte user has not specified any groups then use them all
+               if (globaldata->Groups.size() == 0) {
+                       groups = groupMap->namesOfGroups;
+               }else{ //they have specified groups
+                       groups = globaldata->Groups;
+                       pickedGroups = true;
+               }
+               
                //fill filehandles with neccessary ofstreams
                int i;
                ofstream* temp;
-               for (i=0; i<groupMap->getNumGroups(); i++) {
+               for (i=0; i<groups.size(); i++) {
                        temp = new ofstream;
-                       filehandles[groupMap->namesOfGroups[i]] = temp;
+                       filehandles[groups[i]] = temp;
                }
                
                //set fileroot
-               fileroot = getRootName(globaldata->getListFile());
+               fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->getListFile()));
                
                //clears file before we start to write to it below
-               for (int i=0; i<groupMap->getNumGroups(); i++) {
-                       remove((fileroot + groupMap->namesOfGroups[i] + ".rabund").c_str());
+               for (int i=0; i<groups.size(); i++) {
+                       remove((fileroot + groups[i] + ".rabund").c_str());
+                       outputNames.push_back((fileroot + groups[i] + ".rabund"));
                }
 
        }
        catch(exception& e) {
-               errorOut(e, "SharedCommand", "SharedCommand");
+               m->errorOut(e, "SharedCommand", "SharedCommand");
                exit(1);
        }
 }
@@ -52,7 +69,11 @@ int SharedCommand::execute(){
                
                //lookup.clear();
                string errorOff = "no error";
-                       
+               //errorOff = "";
+               
+               //if user provided an order file containing the order the shared file should be in read it
+               if (globaldata->getOrderGroupFile() != "") { readOrderFile(); }
+               
                //read in listfile
                read = new ReadOTUFile(globaldata->inputFileName);      
                read->read(&*globaldata); 
@@ -63,8 +84,16 @@ int SharedCommand::execute(){
                string lastLabel = SharedList->getLabel();
                vector<SharedRAbundVector*> lookup; 
                
-               if (SharedList->getNumSeqs() != groupMap->getNumSeqs()) {  
-                       mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine(); 
+               if (m->control_pressed) { 
+                       delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL; 
+                       for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
+                       out.close(); remove(filename.c_str()); 
+                       for (int i=0; i<groups.size(); i++) {  remove((fileroot + groups[i] + ".rabund").c_str());              }
+                       return 1; 
+               }
+                               
+               if ((globaldata->Groups.size() == 0) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) {  //if the user has not specified any groups and their files don't match exit with error
+                       m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine(); 
                        
                        out.close();
                        remove(filename.c_str()); //remove blank shared file you made
@@ -75,24 +104,66 @@ int SharedCommand::execute(){
                        for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
                                delete it3->second;
                        }
+                       delete input;
+                       globaldata->ginput = NULL;
                        delete SharedList;
                        globaldata->gSharedList = NULL;
                        
                        return 1; 
                }
                
-               //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
-               set<string> processedLabels;
-               set<string> userLabels = globaldata->labels;
+               //if user has specified groups make new groupfile for them
+               if (globaldata->Groups.size() != 0) { //make new group file
+                       string groups = "";
+                       for (int i = 0; i < globaldata->Groups.size(); i++) {
+                               groups += globaldata->Groups[i] + ".";
+                       }
                
+                       string newGroupFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + groups + "groups";
+                       ofstream outGroups;
+                       m->openOutputFile(newGroupFile, outGroups);
                
+                       vector<string> names = groupMap->getNamesSeqs();
+                       string groupName;
+                       for (int i = 0; i < names.size(); i++) {
+                               groupName = groupMap->getGroup(names[i]);
+                               if (isValidGroup(groupName, globaldata->Groups)) {
+                                       outGroups << names[i] << '\t' << groupName << endl;
+                               }
+                       }
+                       outGroups.close();
+               }
+               
+               //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
+               set<string> processedLabels;
+               set<string> userLabels = globaldata->labels;    
+       
                while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
-                       
-
+                       if (m->control_pressed) { 
+                               delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL; 
+                               for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
+                               out.close(); remove(filename.c_str()); 
+                               for (int i=0; i<groups.size(); i++) {  remove((fileroot + groups[i] + ".rabund").c_str());              }
+                               return 1; 
+                       }
+               
                        if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
                                        
                                        lookup = SharedList->getSharedRAbundVector();
-                                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                                       
+                                       m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+                                       if (pickedGroups) { //check for otus with no seqs in them
+                                               eliminateZeroOTUS(lookup);
+                                       }
+                                       
+                                       if (m->control_pressed) { 
+                                               delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL; 
+                                               for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
+                                               for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
+                                               out.close(); remove(filename.c_str()); 
+                                               for (int i=0; i<groups.size(); i++) {  remove((fileroot + groups[i] + ".rabund").c_str());              }
+                                               return 1; 
+                                       }
                                        
                                        printSharedData(lookup); //prints info to the .shared file
                                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
@@ -101,18 +172,36 @@ int SharedCommand::execute(){
                                        userLabels.erase(SharedList->getLabel());
                        }
                        
-                       if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
+                                       string saveLabel = SharedList->getLabel();
+                                       
                                        delete SharedList;
                                        SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
                                        
                                        lookup = SharedList->getSharedRAbundVector();
-                                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                                       m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+                                       if (pickedGroups) { //check for otus with no seqs in them
+                                               eliminateZeroOTUS(lookup);
+                                       }
+                                       
+                                       
+                                       if (m->control_pressed) { 
+                                               delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL; 
+                                               for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
+                                               for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;  }
+                                               out.close(); remove(filename.c_str()); 
+                                               for (int i=0; i<groups.size(); i++) {  remove((fileroot + groups[i] + ".rabund").c_str());              }
+                                               return 1; 
+                                       }
                                        
                                        printSharedData(lookup); //prints info to the .shared file
                                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
                                        
                                        processedLabels.insert(SharedList->getLabel());
                                        userLabels.erase(SharedList->getLabel());
+                                       
+                                       //restore real lastlabel to save below
+                                       SharedList->setLabel(saveLabel);
                        }
                        
                
@@ -137,7 +226,18 @@ int SharedCommand::execute(){
                        SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
                                        
                        lookup = SharedList->getSharedRAbundVector();
-                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
+                       m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+                       if (pickedGroups) { //check for otus with no seqs in them
+                               eliminateZeroOTUS(lookup);
+                       }
+                       
+                       if (m->control_pressed) { 
+                                       delete input;  globaldata->ginput = NULL; 
+                                       for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {  delete it3->second;   }
+                                       out.close(); remove(filename.c_str()); 
+                                       for (int i=0; i<groups.size(); i++) {  remove((fileroot + groups[i] + ".rabund").c_str());              }
+                                       return 1; 
+                       }
                        
                        printSharedData(lookup); //prints info to the .shared file
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
@@ -153,10 +253,30 @@ int SharedCommand::execute(){
                }
 
                
+               //change format to shared  to speed up commands
+               globaldata->setFormat("sharedfile");
+               globaldata->setListFile("");
+               globaldata->setGroupFile("");
+               globaldata->setSharedFile(filename);
+               
+               
+               if (m->control_pressed) { 
+                               delete input;  globaldata->ginput = NULL; 
+                               remove(filename.c_str()); 
+                               for (int i=0; i<groups.size(); i++) {  remove((fileroot + groups[i] + ".rabund").c_str());              }
+                               return 1; 
+               }
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOut(filename); m->mothurOutEndLine();
+               m->mothurOutEndLine();
+               
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "SharedCommand", "execute");
+               m->errorOut(e, "SharedCommand", "execute");
                exit(1);
        }
 }
@@ -164,38 +284,122 @@ int SharedCommand::execute(){
 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
        try {
                
-               //initialize bin values
+               if (order.size() == 0) { //user has not specified an order so do aplabetically
+                       sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
+                       
+                       globaldata->Groups.clear();
+                       
+                       //initialize bin values
+                       for (int i = 0; i < thislookup.size(); i++) {
+                               out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
+                               thislookup[i]->print(out);
+                               
+                               globaldata->Groups.push_back(thislookup[i]->getGroup());
+                               
+                               RAbundVector rav = thislookup[i]->getRAbundVector();
+                               m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
+                               rav.print(*(filehandles[thislookup[i]->getGroup()]));
+                               (*(filehandles[thislookup[i]->getGroup()])).close();
+                       }
+               }else{
+                       //create a map from groupName to each sharedrabund
+                       map<string, SharedRAbundVector*> myMap;
+                       map<string, SharedRAbundVector*>::iterator myIt;
+                       
+                       for (int i = 0; i < thislookup.size(); i++) {
+                               myMap[thislookup[i]->getGroup()] = thislookup[i];
+                       }
+                       
+                       globaldata->Groups.clear();
+                       
+                       //loop through ordered list and print the rabund
+                       for (int i = 0; i < order.size(); i++) {
+                               myIt = myMap.find(order[i]);
+                               
+                               if(myIt != myMap.end()) { //we found it
+                                       out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
+                                       (myIt->second)->print(out);
+                                       
+                                       globaldata->Groups.push_back((myIt->second)->getGroup());
+                               
+                                       RAbundVector rav = (myIt->second)->getRAbundVector();
+                                       m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
+                                       rav.print(*(filehandles[(myIt->second)->getGroup()]));
+                                       (*(filehandles[(myIt->second)->getGroup()])).close();
+                               }else{
+                                       m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
+                               }
+                       }
+               
+               }
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "printSharedData");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
+       try {
+               
+               vector<SharedRAbundVector*> newLookup;
                for (int i = 0; i < thislookup.size(); i++) {
-                       out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
-                       thislookup[i]->print(out);
+                       SharedRAbundVector* temp = new SharedRAbundVector();
+                       temp->setLabel(thislookup[i]->getLabel());
+                       temp->setGroup(thislookup[i]->getGroup());
+                       newLookup.push_back(temp);
+               }
+               
+               //for each bin
+               for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
+                       if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
+               
+                       //look at each sharedRabund and make sure they are not all zero
+                       bool allZero = true;
+                       for (int j = 0; j < thislookup.size(); j++) {
+                               if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
+                       }
                        
-                       RAbundVector rav = thislookup[i]->getRAbundVector();
-                       openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
-                       rav.print(*(filehandles[thislookup[i]->getGroup()]));
-                       (*(filehandles[thislookup[i]->getGroup()])).close();
+                       //if they are not all zero add this bin
+                       if (!allZero) {
+                               for (int j = 0; j < thislookup.size(); j++) {
+                                       newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
+                               }
+                       }
+                       //else{  cout << "bin # " << i << " is all zeros" << endl;  }
                }
+       
+               for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
+               thislookup = newLookup;
+               
+               return 0;
  
        }
        catch(exception& e) {
-               errorOut(e, "SharedCommand", "printSharedData");
+               m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
                exit(1);
        }
 }
 //**********************************************************************************************************************
-void SharedCommand::createMisMatchFile() {
+int SharedCommand::createMisMatchFile() {
        try {
                ofstream outMisMatch;
-               string outputMisMatchName = getRootName(globaldata->inputFileName);
+               string outputMisMatchName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
                
                //you have sequences in your list file that are not in your group file
                if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
                        outputMisMatchName += "missing.group";
-                       mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
+                       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();
+                       
+                       m->openOutputFile(outputMisMatchName, outMisMatch);
                        
-                       openOutputFile(outputMisMatchName, outMisMatch);
+                       map<string, string> listNames;
+                       map<string, string>::iterator itList;
                        
                        //go through list and if group returns "not found" output it
                        for (int i = 0; i < SharedList->getNumBins(); i++) {
+                               if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; } 
                        
                                string names = SharedList->get(i); 
                                
@@ -205,11 +409,20 @@ void SharedCommand::createMisMatchFile() {
                                        string group = groupMap->getGroup(name);
                                        
                                        if(group == "not found") {      outMisMatch << name << endl;  }
+                                       
+                                       itList = listNames.find(name);
+                                       if (itList != listNames.end()) {  m->mothurOut(name + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
+                                       else { listNames[name] = name; }
                                }
-                               
+                       
                                //get last name
                                string group = groupMap->getGroup(names);
-                               if(group == "not found") {      outMisMatch << names << endl;  }                                
+                               if(group == "not found") {      outMisMatch << names << endl;  }        
+                               
+                               itList = listNames.find(names);
+                               if (itList != listNames.end()) {  m->mothurOut(names + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
+                               else { listNames[names] = names; }
+
                        }
                        
                        outMisMatch.close();
@@ -218,22 +431,32 @@ void SharedCommand::createMisMatchFile() {
                }else {//you have sequences in your group file that are not in you list file
                        
                        outputMisMatchName += "missing.name";
-                       mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
+                       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();
                        
                        map<string, string> namesInList;
+                       map<string, string>::iterator itList;
                        
                        //go through listfile and get names
-                       for (int i = 0; i < SharedList->getNumSeqs(); i++) {
+                       for (int i = 0; i < SharedList->getNumBins(); i++) {
+                               if (m->control_pressed) {  return 0; } 
+
                                
                                string names = SharedList->get(i); 
-                               
+               
                                while (names.find_first_of(',') != -1) { 
                                        string name = names.substr(0,names.find_first_of(','));
                                        names = names.substr(names.find_first_of(',')+1, names.length());
                                        
+                                       itList = namesInList.find(name);
+                                       if (itList != namesInList.end()) {  m->mothurOut(name + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
+
                                        namesInList[name] = name;
+                                       
                                }
                                
+                               itList = namesInList.find(names);
+                               if (itList != namesInList.end()) {  m->mothurOut(names + " is in your list file more than once.  Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
+
                                //get last name
                                namesInList[names] = names;                             
                        }
@@ -243,10 +466,11 @@ void SharedCommand::createMisMatchFile() {
                
                        map<string, string>::iterator itMatch;
                        
-                       openOutputFile(outputMisMatchName, outMisMatch);
+                       m->openOutputFile(outputMisMatchName, outMisMatch);
                        
                        //loop through names in seqNames and if they aren't in namesIn list output them
                        for (int i = 0; i < seqNames.size(); i++) {
+                               if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; } 
                                
                                itMatch = namesInList.find(seqNames[i]);
                                
@@ -257,10 +481,11 @@ void SharedCommand::createMisMatchFile() {
                        }               
                        outMisMatch.close();
                }
+               
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "SharedCommand", "createMisMatchFile");
+               m->errorOut(e, "SharedCommand", "createMisMatchFile");
                exit(1);
        }
 }
@@ -272,5 +497,47 @@ SharedCommand::~SharedCommand(){
        
        
 }
-
 //**********************************************************************************************************************
+int SharedCommand::readOrderFile() {
+       try {
+               //remove old names
+               order.clear();
+               
+               ifstream in;
+               m->openInputFile(globaldata->getOrderGroupFile(), in);
+               string thisGroup;
+               
+               while(!in.eof()){
+                       in >> thisGroup; m->gobble(in);
+                                               
+                       order.push_back(thisGroup);
+                       
+                       if (m->control_pressed) { order.clear(); break; }
+               }
+               in.close();             
+               
+               return 0;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "readOrderFile");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+
+bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
+       try {
+               for (int i = 0; i < groups.size(); i++) {
+                       if (groupname == groups[i]) { return true; }
+               }
+               
+               return false;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "isValidGroup");
+               exit(1);
+       }
+}
+/************************************************************/
+
+