]> git.donarmstrong.com Git - mothur.git/blobdiff - getsharedotucommand.cpp
addition of trim.flows
[mothur.git] / getsharedotucommand.cpp
index 2e553d96db111a7a0828af855eb7251081014131..699581f4fbf7f1eb45095316147990a4836ce7ac 100644 (file)
@@ -9,8 +9,58 @@
 
 #include "getsharedotucommand.h"
 
-//**********************************************************************************************************************
 
+//**********************************************************************************************************************
+vector<string> GetSharedOTUCommand::getValidParameters(){      
+       try {
+               string Array[] =  {"label","unique","shared","fasta","list","group","output","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetSharedOTUCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+GetSharedOTUCommand::GetSharedOTUCommand(){    
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["accnos"] = tempOutNames;
+               outputTypes["sharedseqs"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> GetSharedOTUCommand::getRequiredParameters(){   
+       try {
+               string Array[] =  {"list","group"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetSharedOTUCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> GetSharedOTUCommand::getRequiredFiles(){        
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetSharedOTUCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
        try {
        
@@ -39,6 +89,12 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["accnos"] = tempOutNames;
+                       outputTypes["sharedseqs"] = tempOutNames;
+                       
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
                        
@@ -50,7 +106,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                                it = parameters.find("fasta");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
                                }
@@ -58,7 +114,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                                it = parameters.find("list");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["list"] = inputDir + it->second;             }
                                }
@@ -66,7 +122,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                                it = parameters.find("group");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["group"] = inputDir + it->second;            }
                                }
@@ -90,7 +146,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; }
                        else { 
-                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                else { allLines = 1;  }
                        }
                        
@@ -101,7 +157,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                        if (groups == "not found") { groups = ""; }
                        else { 
                                userGroups = "unique." + groups;
-                               splitAtDash(groups, Groups);
+                               m->splitAtDash(groups, Groups);
                                globaldata->Groups = Groups;
                                
                        }
@@ -110,7 +166,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option)  {
                        if (groups == "not found") { groups = "";  }
                        else { 
                                userGroups = groups;
-                               splitAtDash(groups, Groups);
+                               m->splitAtDash(groups, Groups);
                                globaldata->Groups = Groups;
                                unique = false;
                        }
@@ -168,6 +224,8 @@ int GetSharedOTUCommand::execute(){
                int error = groupMap->readMap();
                if (error == 1) { delete groupMap; return 0; }
                
+               if (m->control_pressed) { delete groupMap; return 0; }
+               
                globaldata->gGroupmap = groupMap;
                
                if (Groups.size() == 0) {
@@ -186,10 +244,12 @@ int GetSharedOTUCommand::execute(){
                
                if (fastafile != "") {
                        ifstream inFasta;
-                       openInputFile(fastafile, inFasta);
+                       m->openInputFile(fastafile, inFasta);
                        
                        while(!inFasta.eof()) {
-                               Sequence seq(inFasta); gobble(inFasta);
+                               if (m->control_pressed) { outputTypes.clear(); inFasta.close(); delete groupMap; return 0; }
+                               
+                               Sequence seq(inFasta); m->gobble(inFasta);
                                if (seq.getName() != "") {  seqs.push_back(seq);   }
                        }
                        inFasta.close();
@@ -203,11 +263,17 @@ int GetSharedOTUCommand::execute(){
                set<string> userLabels = labels;
                
                ifstream in;
-               openInputFile(listfile, in);
+               m->openInputFile(listfile, in);
                
                //as long as you are not at the end of the file or done wih the lines you want
                while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
+                       if (m->control_pressed) { 
+                               if (lastlist != NULL) {         delete lastlist;        }
+                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); }  outputTypes.clear();
+                               delete groupMap; return 0;
+                       }
+                       
                        list = new ListVector(in);
                        
                        if(allLines == 1 || labels.count(list->getLabel()) == 1){                       
@@ -218,7 +284,7 @@ int GetSharedOTUCommand::execute(){
                                userLabels.erase(list->getLabel());
                        }
                        
-                       if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                        string saveLabel = list->getLabel();
                                        
                                        m->mothurOut(lastlist->getLabel()); 
@@ -267,6 +333,7 @@ int GetSharedOTUCommand::execute(){
                
                if (lastlist != NULL) {         delete lastlist;        }
                
+               if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str()); }  delete groupMap; return 0; } 
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
@@ -283,7 +350,7 @@ int GetSharedOTUCommand::execute(){
        }
 }
 /***********************************************************/
-void GetSharedOTUCommand::process(ListVector* shared) {
+int GetSharedOTUCommand::process(ListVector* shared) {
        try {
                
                map<string, string> fastaMap;
@@ -291,19 +358,20 @@ void GetSharedOTUCommand::process(ListVector* shared) {
                ofstream outNames;
                string outputFileNames;
                
-               if (outputDir == "") { outputDir += hasPath(listfile); }
+               if (outputDir == "") { outputDir += m->hasPath(listfile); }
                if (output != "accnos") {
-                       outputFileNames = outputDir + getRootName(getSimpleName(listfile)) + shared->getLabel() + userGroups + ".shared.seqs";
+                       outputFileNames = outputDir + m->getRootName(m->getSimpleName(listfile)) + shared->getLabel() + userGroups + ".shared.seqs";
                }else {
-                       outputFileNames = outputDir + getRootName(getSimpleName(listfile)) + shared->getLabel() + userGroups + ".accnos";
+                       outputFileNames = outputDir + m->getRootName(m->getSimpleName(listfile)) + shared->getLabel() + userGroups + ".accnos";
                }
-               openOutputFile(outputFileNames, outNames);
+               m->openOutputFile(outputFileNames, outNames);
                
                bool wroteSomething = false;
                int num = 0;
                                
                //go through each bin, find out if shared
                for (int i = 0; i < shared->getNumBins(); i++) {
+                       if (m->control_pressed) { outNames.close(); remove(outputFileNames.c_str()); return 0; }
                        
                        bool uniqueOTU = true;
                        
@@ -322,7 +390,7 @@ void GetSharedOTUCommand::process(ListVector* shared) {
                                //find group
                                string seqGroup = groupMap->getGroup(name);
                                if (output != "accnos") {
-                                       namesOfSeqsInThisBin.push_back((name + "\t" + seqGroup + "\t" + toString(i+1)));
+                                       namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i+1)));
                                }else {  namesOfSeqsInThisBin.push_back(name);  }
                                
                                if (seqGroup == "not found") { m->mothurOut(name + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1);  }
@@ -336,7 +404,7 @@ void GetSharedOTUCommand::process(ListVector* shared) {
                        //get last name
                        string seqGroup = groupMap->getGroup(names);
                        if (output != "accnos") {
-                               namesOfSeqsInThisBin.push_back((names + "\t" + seqGroup + "\t" + toString(i+1)));
+                               namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i+1)));
                        }else {  namesOfSeqsInThisBin.push_back(names); }
                        
                        if (seqGroup == "not found") { m->mothurOut(names + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1);  }
@@ -367,8 +435,8 @@ void GetSharedOTUCommand::process(ListVector* shared) {
                                        
                                        if (fastafile != "") { 
                                                if (output != "accnos") {
-                                                       string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('\t'));
-                                                       seqName = seqName.substr(0,seqName.find_last_of('\t'));
+                                                       string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
+                                                       seqName = seqName.substr(0,seqName.find_last_of('|'));
                                                        fastaMap[seqName] = namesOfSeqsInThisBin[j];  //fastaMap needs to contain just the seq name for output later
                                                }else {
                                                        fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
@@ -394,17 +462,21 @@ void GetSharedOTUCommand::process(ListVector* shared) {
                }else { 
                        m->mothurOut("\t" + toString(num)); m->mothurOutEndLine(); 
                        outputNames.push_back(outputFileNames);
+                       if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
+                       else { outputTypes["accnos"].push_back(outputFileNames); }
                }
                
                //if fasta file provided output new fasta file
                if ((fastafile != "") && wroteSomething) {
-                       if (outputDir == "") { outputDir += hasPath(fastafile); }
-                       string outputFileFasta = outputDir + getRootName(getSimpleName(fastafile)) + shared->getLabel() + userGroups + ".shared.fasta";
+                       if (outputDir == "") { outputDir += m->hasPath(fastafile); }
+                       string outputFileFasta = outputDir + m->getRootName(m->getSimpleName(fastafile)) + shared->getLabel() + userGroups + ".shared.fasta";
                        ofstream outFasta;
-                       openOutputFile(outputFileFasta, outFasta);
-                       outputNames.push_back(outputFileFasta);
+                       m->openOutputFile(outputFileFasta, outFasta);
+                       outputNames.push_back(outputFileFasta); outputTypes["fasta"].push_back(outputFileFasta);
                        
                        for (int k = 0; k < seqs.size(); k++) {
+                               if (m->control_pressed) { outFasta.close(); return 0; }
+                       
                                //if this is a sequence we want, output it
                                it = fastaMap.find(seqs[k].getName());
                                if (it != fastaMap.end()) {
@@ -421,7 +493,8 @@ void GetSharedOTUCommand::process(ListVector* shared) {
                        
                        outFasta.close();
                }
-
+               
+               return 0;
                
        }
        catch(exception& e) {