]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedcommand.cpp
added headers to shared and relabund files
[mothur.git] / sharedcommand.cpp
index 179424b3c9f395ce88774b00aa7f367581d8f775..b322007bc5e8ef6cba50d4c6d2ff5fce0b87a0fa 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,12 +73,11 @@ 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 {
                        
-                        //valid paramters for this command
-                        string Array[] =  {"list","label","group","groups","ordergroup","outputdir","inputdir"};
-                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                        vector<string> myArray = setParameters();
                         
                         OptionParser parser(option);
                         map<string, string> parameters = parser.getParameters();
@@ -126,7 +126,7 @@ 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(); }
@@ -138,7 +138,7 @@ SharedCommand::SharedCommand(string option)  {
                         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 != "") { 
@@ -174,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"));
-                       }
-                       
                }
                
        }
@@ -222,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++) {
+                       remove((fileroot + Groups[i] + ".rabund").c_str());
+                       outputNames.push_back((fileroot + Groups[i] + ".rabund"));
+                       outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund"));
+               }
+               
                //lookup.clear();
                string errorOff = "no error";
                //errorOff = "";
@@ -261,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++) {
@@ -270,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);
                
@@ -315,6 +319,7 @@ int SharedCommand::execute(){
                                                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];  }
                                
@@ -344,6 +349,7 @@ int SharedCommand::execute(){
                                                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];  }
                                        
@@ -389,6 +395,7 @@ int SharedCommand::execute(){
                                        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;
@@ -420,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();    }