]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedcommand.cpp
fixes while testing
[mothur.git] / sharedcommand.cpp
index 213fd49588bbf8a9278bc04c14d474bd11ccb278..c252b4fecc9cebad935e0970fa88aaf5d43e7ac0 100644 (file)
@@ -8,21 +8,78 @@
  */
 
 #include "sharedcommand.h"
+//********************************************************************************************************************
+//sorts lowest to highest
+inline bool compareSharedRabunds(SharedRAbundVector* left, SharedRAbundVector* right){
+       return (left->getGroup() < right->getGroup());  
+} 
+//**********************************************************************************************************************
+vector<string> SharedCommand::getValidParameters(){    
+       try {
+               vector<string> myArray; 
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+SharedCommand::SharedCommand(){        
+       try {           
 
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["shared"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "SharedCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> SharedCommand::getRequiredParameters(){ 
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> SharedCommand::getRequiredFiles(){      
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 
 SharedCommand::SharedCommand(string o) : outputDir(o) {
        try {
                globaldata = GlobalData::getInstance();
                
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["shared"] = tempOutNames;
+               
                //getting output filename
                filename = globaldata->inputFileName;
-               if (outputDir == "") { outputDir += hasPath(filename); }
+               if (outputDir == "") { outputDir += m->hasPath(filename); }
                
-               filename = outputDir + getRootName(getSimpleName(filename));
+               filename = outputDir + m->getRootName(m->getSimpleName(filename));
                filename = filename + "shared";
+               outputTypes["shared"].push_back(filename);
                
-               openOutputFile(filename, out);
+               m->openOutputFile(filename, out);
                pickedGroups = false;
                
                groupMap = globaldata->gGroupmap;
@@ -44,12 +101,13 @@ SharedCommand::SharedCommand(string o) : outputDir(o) {
                }
                
                //set fileroot
-               fileroot = outputDir + getRootName(getSimpleName(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<groups.size(); i++) {
                        remove((fileroot + groups[i] + ".rabund").c_str());
                        outputNames.push_back((fileroot + groups[i] + ".rabund"));
+                       outputTypes["rabund"].push_back((fileroot + groups[i] + ".rabund"));
                }
 
        }
@@ -67,6 +125,9 @@ int SharedCommand::execute(){
                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); 
@@ -112,9 +173,9 @@ int SharedCommand::execute(){
                                groups += globaldata->Groups[i] + ".";
                        }
                
-                       string newGroupFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + groups + "groups";
+                       string newGroupFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + groups + "groups";
                        ofstream outGroups;
-                       openOutputFile(newGroupFile, outGroups);
+                       m->openOutputFile(newGroupFile, outGroups);
                
                        vector<string> names = groupMap->getNamesSeqs();
                        string groupName;
@@ -143,6 +204,7 @@ int SharedCommand::execute(){
                        if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
                                        
                                        lookup = SharedList->getSharedRAbundVector();
+                                       
                                        m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
                                        if (pickedGroups) { //check for otus with no seqs in them
                                                eliminateZeroOTUS(lookup);
@@ -164,7 +226,7 @@ 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;
@@ -251,6 +313,7 @@ int SharedCommand::execute(){
                globaldata->setGroupFile("");
                globaldata->setSharedFile(filename);
                
+               
                if (m->control_pressed) { 
                                delete input;  globaldata->ginput = NULL; 
                                remove(filename.c_str()); 
@@ -275,16 +338,53 @@ int SharedCommand::execute(){
 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
        try {
                
-               //initialize bin values
-               for (int i = 0; i < thislookup.size(); i++) {
-//cout << "in printData " << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() <<  endl;
-                       out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
-                       thislookup[i]->print(out);
+               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;
                        
-                       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();
+                       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();
+                               }
+                       }
+               
                }
  
        }
@@ -339,14 +439,14 @@ int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
 int SharedCommand::createMisMatchFile() {
        try {
                ofstream outMisMatch;
-               string outputMisMatchName = outputDir + getRootName(getSimpleName(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";
                        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();
                        
-                       openOutputFile(outputMisMatchName, outMisMatch);
+                       m->openOutputFile(outputMisMatchName, outMisMatch);
                        
                        map<string, string> listNames;
                        map<string, string>::iterator itList;
@@ -420,7 +520,7 @@ int 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++) {
@@ -451,7 +551,32 @@ 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) {