]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedcommand.cpp
added sequence name to error string in fastq.info. Changed np_shannon to npshannon.
[mothur.git] / sharedcommand.cpp
index 2b4d56ec44591400f977e365f6501e89170c9f02..de48158d9eb7d26adecce2dab1162b32332fec5f 100644 (file)
@@ -18,7 +18,7 @@ vector<string> SharedCommand::setParameters(){
        try {
                CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
                CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
-               CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup);
+               //CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup);
                CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
                CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
                CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
@@ -41,7 +41,7 @@ string SharedCommand::getHelpString(){
                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";
                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";
                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";
-               helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n";
+               //helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n";
                return helpString;
        }
        catch(exception& e) {
@@ -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 {
                        
@@ -129,14 +131,14 @@ SharedCommand::SharedCommand(string option)  {
                                 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 != "") { 
@@ -145,7 +147,8 @@ SharedCommand::SharedCommand(string option)  {
                                         
                                         int error = groupMap->readMap();
                                         if (error == 1) { abort = true; }
-                                        m->namesOfGroups = groupMap->namesOfGroups;
+                                        vector<string> allGroups = groupMap->getNamesOfGroups();
+                                        m->setAllGroups(allGroups);
                                 }
                                 else {         m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
                         }else {  
@@ -153,7 +156,8 @@ SharedCommand::SharedCommand(string option)  {
                         
                                 int error = groupMap->readMap();
                                 if (error == 1) { abort = true; }
-                                m->namesOfGroups = groupMap->namesOfGroups;
+                                vector<string> allGroups = groupMap->getNamesOfGroups();
+                                m->setAllGroups(allGroups);
                                 m->setGroupFile(groupfile);
                         }
                         
@@ -161,7 +165,7 @@ SharedCommand::SharedCommand(string option)  {
                         if (groups == "not found") { groups = ""; }
                         else { 
                                 m->splitAtDash(groups, Groups);
-                                m->Groups = Groups;
+                                m->setGroups(Groups);
                         }
                         
                         //check for optional parameter and set defaults
@@ -172,41 +176,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 +191,41 @@ int SharedCommand::execute(){
                
                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->getNamesOfGroups(); m->setGroups(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 = "";
@@ -237,16 +241,19 @@ 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; 
                }
-                               
-               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
+               
+               //sanity check
+               int error = ListGroupSameSeqs();
+               
+               if ((!pickedGroups) && (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
+                       m->mothurRemove(filename); //remove blank shared file you made
                        
                        createMisMatchFile();
                        
@@ -260,16 +267,20 @@ int SharedCommand::execute(){
                        return 0; 
                }
                
+               if (error == 1) { m->control_pressed = true; }
+               
                //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++) {
-                                       groups += m->Groups[i] + ".";
+                       if (m->getNumGroups() < 4) {
+                               for (int i = 0; i < m->getNumGroups(); i++) {
+                                       groups += (m->getGroups())[i] + ".";
                                }
                        }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);
                
@@ -277,7 +288,7 @@ int SharedCommand::execute(){
                        string groupName;
                        for (int i = 0; i < names.size(); i++) {
                                groupName = groupMap->getGroup(names[i]);
-                               if (isValidGroup(groupName, m->Groups)) {
+                               if (isValidGroup(groupName, m->getGroups())) {
                                        outGroups << names[i] << '\t' << groupName << endl;
                                }
                        }
@@ -292,8 +303,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; 
                        }
                
@@ -310,11 +321,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];  }
                                
@@ -339,11 +351,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];  }
                                        
@@ -384,11 +397,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;
@@ -403,8 +417,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; 
                }
                
@@ -420,6 +434,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();    }
@@ -440,20 +459,22 @@ void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
                if (order.size() == 0) { //user has not specified an order so do aplabetically
                        sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
                        
-                       m->Groups.clear();
+                       m->clearGroups();
+                       vector<string> Groups;
                        
                        //initialize bin values
                        for (int i = 0; i < thislookup.size(); i++) {
                                out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
                                thislookup[i]->print(out);
                                
-                               m->Groups.push_back(thislookup[i]->getGroup());
+                               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();
                        }
+                       m->setGroups(Groups);
                }else{
                        //create a map from groupName to each sharedrabund
                        map<string, SharedRAbundVector*> myMap;
@@ -463,7 +484,8 @@ void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
                                myMap[thislookup[i]->getGroup()] = thislookup[i];
                        }
                        
-                       m->Groups.clear();
+                       m->clearGroups();
+                       vector<string> Groups;
                        
                        //loop through ordered list and print the rabund
                        for (int i = 0; i < order.size(); i++) {
@@ -473,7 +495,7 @@ void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
                                        out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
                                        (myIt->second)->print(out);
                                        
-                                       m->Groups.push_back((myIt->second)->getGroup());
+                                       Groups.push_back((myIt->second)->getGroup());
                                
                                        RAbundVector rav = (myIt->second)->getRAbundVector();
                                        m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
@@ -483,6 +505,8 @@ void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
                                        m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
                                }
                        }
+                       
+                       m->setGroups(Groups);
                
                }
  
@@ -519,6 +543,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;  }
                }
@@ -552,7 +577,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); 
                                
@@ -623,7 +648,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]);
                                
@@ -642,7 +667,49 @@ int SharedCommand::createMisMatchFile() {
                exit(1);
        }
 }
-
+//**********************************************************************************************************************
+int SharedCommand::ListGroupSameSeqs() {
+       try {
+               
+               int error = 0; 
+               
+               vector<string> groupMapsSeqs = groupMap->getNamesSeqs();
+               
+               set<string> groupNamesSeqs;
+               for(int i = 0; i < groupMapsSeqs.size(); i++) {
+                       groupNamesSeqs.insert(groupMapsSeqs[i]);
+               }
+               
+               
+               //go through list and if group returns "not found" output it
+               for (int i = 0; i < SharedList->getNumBins(); i++) {
+                       if (m->control_pressed) { return 0; } 
+                       
+                       string names = SharedList->get(i); 
+                       
+                       vector<string> listNames;
+                       m->splitAtComma(names, listNames);
+                       
+                       for (int j = 0; j < listNames.size(); j++) {
+                               int num = groupNamesSeqs.count(listNames[j]);
+                               
+                               if (num == 0) { error = 1; m->mothurOut("[ERROR]: " + listNames[j] + " is in your listfile and not in your groupfile. Please correct."); m->mothurOutEndLine(); }
+                               else { groupNamesSeqs.erase(listNames[j]); }
+                       }
+               }
+               
+               for (set<string>::iterator itGroupSet = groupNamesSeqs.begin(); itGroupSet != groupNamesSeqs.end(); itGroupSet++) {
+                       error = 1; 
+                       m->mothurOut("[ERROR]: " + (*itGroupSet) + " is in your groupfile and not your listfile. Please correct."); m->mothurOutEndLine();
+               }
+                               
+               return error;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SharedCommand", "ListGroupSameSeqs");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 
 SharedCommand::~SharedCommand(){