]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedcommand.cpp
changed normalize.shared and sub.sample commands to fix bug in normalize.shared and...
[mothur.git] / sharedcommand.cpp
index d15354377efe906b7a6b10f45d6ca8dcd454a233..2928214c2e671e366553439504c4e78d572a1f1b 100644 (file)
@@ -58,6 +58,7 @@ SharedCommand::SharedCommand(){
                vector<string> tempOutNames;
                outputTypes["rabund"] = tempOutNames;
                outputTypes["shared"] = tempOutNames;
+               outputTypes["group"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "SharedCommand", "SharedCommand");
@@ -72,6 +73,7 @@ SharedCommand::SharedCommand(string option)  {
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
                        
@@ -124,19 +126,19 @@ SharedCommand::SharedCommand(string option)  {
                         
                         //check for required parameters
                         listfile = validParameter.validFile(parameters, "list", true);
-                        if (listfile == "not open") { abort = true; }
+                        if (listfile == "not open") { listfile = ""; abort = true; }
                         else if (listfile == "not found") { 
                                 listfile = m->getListFile(); 
                                 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
                                 else {         m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
-                        }      
+                        }else { m->setListFile(listfile); }    
                                                        
                         ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
                         if (ordergroupfile == "not open") { abort = true; }    
                         else if (ordergroupfile == "not found") { ordergroupfile = ""; }
                                                 
                         groupfile = validParameter.validFile(parameters, "group", true);
-                        if (groupfile == "not open") { abort = true; } 
+                        if (groupfile == "not open") { groupfile = ""; abort = true; } 
                         else if (groupfile == "not found") { 
                                 groupfile = m->getGroupFile(); 
                                 if (groupfile != "") { 
@@ -172,41 +174,6 @@ SharedCommand::SharedCommand(string option)  {
                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                 else { allLines = 1;  }
                         }
-                       
-                       //getting output filename
-                       filename = listfile;
-                       if (outputDir == "") { outputDir += m->hasPath(filename); }
-                       
-                       filename = outputDir + m->getRootName(m->getSimpleName(filename));
-                       filename = filename + "shared";
-                       outputTypes["shared"].push_back(filename);
-                       
-                       m->openOutputFile(filename, out);
-                       pickedGroups = false;
-                                               
-                       //if hte user has not specified any groups then use them all
-                       if (Groups.size() == 0) {
-                               Groups = groupMap->namesOfGroups; m->Groups = Groups;
-                       }
-                       
-                       //fill filehandles with neccessary ofstreams
-                       int i;
-                       ofstream* temp;
-                       for (i=0; i<Groups.size(); i++) {
-                               temp = new ofstream;
-                               filehandles[Groups[i]] = temp;
-                       }
-                       
-                       //set fileroot
-                       fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
-                       
-                       //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"));
-                       }
-                       
                }
                
        }
@@ -220,6 +187,43 @@ SharedCommand::SharedCommand(string option)  {
 int SharedCommand::execute(){
        try {
                
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
+               
+               //getting output filename
+               filename = listfile;
+               
+               if (outputDir == "") { outputDir += m->hasPath(filename); }
+               
+               filename = outputDir + m->getRootName(m->getSimpleName(filename));
+               filename = filename + "shared";
+               outputTypes["shared"].push_back(filename);
+               
+               m->openOutputFile(filename, out);
+               pickedGroups = false;
+               
+               //if hte user has not specified any groups then use them all
+               if (Groups.size() == 0) {
+                       Groups = groupMap->namesOfGroups; m->Groups = Groups;
+               }else { pickedGroups = true; }
+               
+               //fill filehandles with neccessary ofstreams
+               int i;
+               ofstream* temp;
+               for (i=0; i<Groups.size(); i++) {
+                       temp = new ofstream;
+                       filehandles[Groups[i]] = temp;
+               }
+               
+               //set fileroot
+               fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
+               
+               //clears file before we start to write to it below
+               for (int i=0; i<Groups.size(); i++) {
+                       m->mothurRemove((fileroot + Groups[i] + ".rabund"));
+                       outputNames.push_back((fileroot + Groups[i] + ".rabund"));
+                       outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund"));
+               }
+               
                //lookup.clear();
                string errorOff = "no error";
                //errorOff = "";
@@ -235,8 +239,8 @@ int SharedCommand::execute(){
                if (m->control_pressed) { 
                        delete input; delete SharedList; delete groupMap; 
                        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());              }
+                       out.close(); m->mothurRemove(filename); 
+                       for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
                        return 0; 
                }
                                
@@ -244,7 +248,7 @@ int SharedCommand::execute(){
                        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
+                       m->mothurRemove(filename); //remove blank shared file you made
                        
                        createMisMatchFile();
                        
@@ -259,7 +263,7 @@ int SharedCommand::execute(){
                }
                
                //if user has specified groups make new groupfile for them
-               if (m->Groups.size() != 0) { //make new group file
+               if (pickedGroups) { //make new group file
                        string groups = "";
                        if (m->Groups.size() < 4) {
                                for (int i = 0; i < m->Groups.size(); i++) {
@@ -268,6 +272,8 @@ int SharedCommand::execute(){
                        }else { groups = "merge"; }
                
                        string newGroupFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + groups + "groups";
+                       outputTypes["group"].push_back(newGroupFile); 
+                       outputNames.push_back(newGroupFile);
                        ofstream outGroups;
                        m->openOutputFile(newGroupFile, outGroups);
                
@@ -290,8 +296,8 @@ int SharedCommand::execute(){
                        if (m->control_pressed) { 
                                delete input; delete SharedList; delete groupMap;
                                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());              }
+                               out.close(); m->mothurRemove(filename); 
+                               for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
                                return 0; 
                        }
                
@@ -308,11 +314,12 @@ int SharedCommand::execute(){
                                                delete input; delete SharedList; delete groupMap; 
                                                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());              }
+                                               out.close(); m->mothurRemove(filename); 
+                                               for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
                                                return 0; 
                                        }
                                        
+                                       if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
                                        printSharedData(lookup); //prints info to the .shared file
                                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
                                
@@ -337,11 +344,12 @@ int SharedCommand::execute(){
                                                delete input; delete SharedList; delete groupMap; 
                                                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());              }
+                                               out.close(); m->mothurRemove(filename); 
+                                               for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
                                                return 0; 
                                        }
                                        
+                                       if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
                                        printSharedData(lookup); //prints info to the .shared file
                                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
                                        
@@ -382,11 +390,12 @@ int SharedCommand::execute(){
                        if (m->control_pressed) { 
                                delete input;  delete groupMap;
                                        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());              }
+                                       out.close(); m->mothurRemove(filename); 
+                                       for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
                                        return 0; 
                        }
                        
+                       if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
                        printSharedData(lookup); //prints info to the .shared file
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
                        delete SharedList;
@@ -401,8 +410,8 @@ int SharedCommand::execute(){
                delete input; delete groupMap;
                
                if (m->control_pressed) { 
-                               remove(filename.c_str()); 
-                               for (int i=0; i<Groups.size(); i++) {  remove((fileroot + Groups[i] + ".rabund").c_str());              }
+                               m->mothurRemove(filename); 
+                               for (int i=0; i<Groups.size(); i++) {  m->mothurRemove((fileroot + Groups[i] + ".rabund"));             }
                                return 0; 
                }
                
@@ -418,6 +427,11 @@ int SharedCommand::execute(){
                        if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
                }       
                
+               itTypes = outputTypes.find("group");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
+               }
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -517,6 +531,7 @@ int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
                                for (int j = 0; j < thislookup.size(); j++) {
                                        newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
                                }
+                               //if there is a bin label use it otherwise make one
                        }
                        //else{  cout << "bin # " << i << " is all zeros" << endl;  }
                }
@@ -550,7 +565,7 @@ int SharedCommand::createMisMatchFile() {
                        
                        //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; } 
+                               if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } 
                        
                                string names = SharedList->get(i); 
                                
@@ -621,7 +636,7 @@ int SharedCommand::createMisMatchFile() {
                        
                        //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; } 
+                               if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; } 
                                
                                itMatch = namesInList.find(seqNames[i]);