]> git.donarmstrong.com Git - mothur.git/blobdiff - heatmapsimcommand.cpp
moved utilities out of mothur.h and into mothurOut class.
[mothur.git] / heatmapsimcommand.cpp
index cef790633454b7d8136556d6441a684f0e45e3de..61e29390c21ae0a851a3acaede4c9da681d4d7d4 100644 (file)
@@ -22,7 +22,7 @@
 
 //**********************************************************************************************************************
 
-HeatMapSimCommand::HeatMapSimCommand(string option){
+HeatMapSimCommand::HeatMapSimCommand(string option)  {
        try {
                globaldata = GlobalData::getInstance();
                abort = false;
@@ -30,7 +30,7 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                labels.clear();
                Groups.clear();
                Estimators.clear();
-               
+                       
                //allow user to run help
                if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
                
@@ -62,7 +62,7 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                                it = parameters.find("phylip");
                                //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["phylip"] = inputDir + it->second;           }
                                }
@@ -70,7 +70,7 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                                it = parameters.find("column");
                                //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["column"] = inputDir + it->second;           }
                                }
@@ -78,7 +78,7 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                                it = parameters.find("name");
                                //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["name"] = inputDir + it->second;             }
                                }
@@ -88,12 +88,12 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                        phylipfile = validParameter.validFile(parameters, "phylip", true);
                        if (phylipfile == "not open") { abort = true; }
                        else if (phylipfile == "not found") { phylipfile = ""; }        
-                       else {  format = "phylip";      if (outputDir == "") { outputDir += hasPath(phylipfile); }  }
+                       else {  format = "phylip";      if (outputDir == "") { outputDir += m->hasPath(phylipfile); }  }
                        
                        columnfile = validParameter.validFile(parameters, "column", true);
                        if (columnfile == "not open") { abort = true; } 
                        else if (columnfile == "not found") { columnfile = ""; }
-                       else {  format = "column";      if (outputDir == "") { outputDir += hasPath(columnfile); } }
+                       else {  format = "column";      if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }   
@@ -101,11 +101,11 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                        
                        
                        //error checking on files                       
-                       if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))  { mothurOut("You must run the read.otu command or provide a distance file before running the heatmap.sim command."); mothurOutEndLine(); abort = true; }
-                       else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When running the heatmap.sim command with a distance file you may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; }
+                       if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))  { m->mothurOut("You must run the read.otu command or provide a distance file before running the heatmap.sim command."); m->mothurOutEndLine(); abort = true; }
+                       else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When running the heatmap.sim command with a distance file you may not use both the column and the phylip parameters."); m->mothurOutEndLine(); abort = true; }
                        
                        if (columnfile != "") {
-                               if (namefile == "") {  mothurOut("You need to provide a namefile if you are going to use the column format."); mothurOutEndLine(); abort = true; }
+                               if (namefile == "") {  m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
                        }
                        
                        if (format == "") { format = "shared"; }
@@ -113,12 +113,12 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
                        if (format == "shared") {
-                               if (outputDir == "") { outputDir += hasPath(globaldata->getSharedFile()); }
+                               if (outputDir == "") { outputDir += m->hasPath(globaldata->getSharedFile()); }
                                
                                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;  }
                                }
                                
@@ -133,12 +133,12 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
                                else { 
                                        if (calc == "default")  {  calc = "jest-thetayc";  }
                                }
-                               splitAtDash(calc, Estimators);
+                               m->splitAtDash(calc, Estimators);
                                
                                groups = validParameter.validFile(parameters, "groups", false);                 
                                if (groups == "not found") { groups = ""; }
                                else { 
-                                       splitAtDash(groups, Groups);
+                                       m->splitAtDash(groups, Groups);
                                        globaldata->Groups = Groups;
                                }
                        }
@@ -180,7 +180,7 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
 
        }
        catch(exception& e) {
-               errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
+               m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
                exit(1);
        }
 }
@@ -189,27 +189,27 @@ HeatMapSimCommand::HeatMapSimCommand(string option){
 
 void HeatMapSimCommand::help(){
        try {
-               mothurOut("The heatmap.sim command can only be executed after a successful read.otu command, or by providing a distance file.\n");
-               mothurOut("The heatmap.sim command parameters are phylip, column, name, groups, calc and label.  No parameters are required.\n");
-               mothurOut("There are two ways to use the heatmap.sim command. The first is with the read.otu command. \n");
-               mothurOut("With the read.otu command you may use the groups, label and calc parameters. \n");
-               mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
-               mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like a heatmap created for, and is also separated by dashes.\n");
-               mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n");
-               mothurOut("Example heatmap.sim(groups=A-B-C, calc=jabund).\n");
-               mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
+               m->mothurOut("The heatmap.sim command can only be executed after a successful read.otu command, or by providing a distance file.\n");
+               m->mothurOut("The heatmap.sim command parameters are phylip, column, name, groups, calc and label.  No parameters are required.\n");
+               m->mothurOut("There are two ways to use the heatmap.sim command. The first is with the read.otu command. \n");
+               m->mothurOut("With the read.otu command you may use the groups, label and calc parameters. \n");
+               m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
+               m->mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like a heatmap created for, and is also separated by dashes.\n");
+               m->mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n");
+               m->mothurOut("Example heatmap.sim(groups=A-B-C, calc=jabund).\n");
+               m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
                validCalculator->printCalc("heat", cout);
-               mothurOut("The default value for calc is jclass-thetayc.\n");
-               mothurOut("The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n");
-               mothurOut("The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n");
-               mothurOut("Using the command this way, the phylip or column parameter are required, and only one may be used.  If you use a column file the name filename is required. \n");
-               mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n");
-               mothurOut("Example heatmap.sim(phylip=amazonGroups.dist).\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
+               m->mothurOut("The default value for calc is jclass-thetayc.\n");
+               m->mothurOut("The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n");
+               m->mothurOut("The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n");
+               m->mothurOut("Using the command this way, the phylip or column parameter are required, and only one may be used.  If you use a column file the name filename is required. \n");
+               m->mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n");
+               m->mothurOut("Example heatmap.sim(phylip=amazonGroups.dist).\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
 
        }
        catch(exception& e) {
-               errorOut(e, "HeatMapSimCommand", "help");
+               m->errorOut(e, "HeatMapSimCommand", "help");
                exit(1);
        }
 }
@@ -239,11 +239,18 @@ int HeatMapSimCommand::execute(){
                
                delete heatmap;
                delete validCalculator;
-
+               
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
+               
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+               for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
+               m->mothurOutEndLine();
+               
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "HeatMapSimCommand", "execute");
+               m->errorOut(e, "HeatMapSimCommand", "execute");
                exit(1);
        }
 }
@@ -252,7 +259,7 @@ int HeatMapSimCommand::execute(){
 int HeatMapSimCommand::runCommandShared() {
        try {
                //if the users entered no valid calculators don't execute command
-               if (heatCalculators.size() == 0) { mothurOut("No valid calculators."); mothurOutEndLine(); return 0; }
+               if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
                
                //you have groups
                read = new ReadOTUFile(globaldata->inputFileName);      
@@ -262,32 +269,38 @@ int HeatMapSimCommand::runCommandShared() {
                lookup = input->getSharedRAbundVectors();
                string lastLabel = lookup[0]->getLabel();
                
-               if (lookup.size() < 2) { mothurOut("You have not provided enough valid groups.  I cannot run the command."); mothurOutEndLine(); return 0;}
+               if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0;}
                                
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
                set<string> userLabels = labels;
                
+               if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  globaldata->Groups.clear(); return 0; }
+               
                //as long as you are not at the end of the file or done wih the lines you want
                while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
-               
+                       
+                       if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } globaldata->Groups.clear(); return 0; }
+
                        if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
        
-                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
-                               heatmap->getPic(lookup, heatCalculators);
+                               m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+                               vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
+                               for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
                                        
                                processedLabels.insert(lookup[0]->getLabel());
                                userLabels.erase(lookup[0]->getLabel());
                        }
                                
-                       if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                string saveLabel = lookup[0]->getLabel();
                        
                                for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                                lookup = input->getSharedRAbundVectors(lastLabel);                              
 
-                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
-                               heatmap->getPic(lookup, heatCalculators);
+                               m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+                               vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
+                               for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
                                        
                                processedLabels.insert(lookup[0]->getLabel());
                                userLabels.erase(lookup[0]->getLabel());
@@ -304,30 +317,38 @@ int HeatMapSimCommand::runCommandShared() {
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                        lookup = input->getSharedRAbundVectors();                               
                }
+               
                        
+               if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; globaldata->Groups.clear();  return 0; }
+
                //output error messages about any remaining user labels
                set<string>::iterator it;
                bool needToRun = false;
                for (it = userLabels.begin(); it != userLabels.end(); it++) {  
-                       mothurOut("Your file does not include the label " + *it); 
+                       m->mothurOut("Your file does not include the label " + *it); 
                        if (processedLabels.count(lastLabel) != 1) {
-                               mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
+                               m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
                                needToRun = true;
                        }else {
-                               mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
+                               m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
                        }
                }
                
+               if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL;  globaldata->Groups.clear(); return 0; }
+               
                //run last label if you need to
                if (needToRun == true)  {
                        for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) { delete lookup[i]; } } 
                        lookup = input->getSharedRAbundVectors(lastLabel);                              
 
-                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
-                       heatmap->getPic(lookup, heatCalculators);
+                       m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
+                       vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
+                       for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
+                       
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                }
                
+               if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL;  globaldata->Groups.clear(); return 0; }
                        
                //reset groups parameter
                globaldata->Groups.clear();  
@@ -338,7 +359,7 @@ int HeatMapSimCommand::runCommandShared() {
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "HeatMapSimCommand", "runCommandShared");
+               m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
                exit(1);
        }
 }
@@ -353,7 +374,7 @@ int HeatMapSimCommand::runCommandDist() {
                //read distance file and create distance vector and names vector
                if (format == "phylip") {
                        //read phylip file
-                       openInputFile(phylipfile, in);
+                       m->openInputFile(phylipfile, in);
                        
                        string name;
                        int numSeqs;
@@ -398,8 +419,10 @@ int HeatMapSimCommand::runCommandDist() {
                                        in >> name;             
                                        names.push_back(name);
                                        
-                                       for(int j=0;j<numSeqs;j++) {  in >> matrix[i][j];  }
-                                       gobble(in);
+                                       if (m->control_pressed) { return 0; }
+                                       
+                                       for(int j=0;j<numSeqs;j++) { in >> matrix[i][j];  }
+                                       m->gobble(in);
                                }
                        }else { 
                                double dist;
@@ -407,11 +430,13 @@ int HeatMapSimCommand::runCommandDist() {
                                        in >> name;     
                                        names.push_back(name);  
                                        
+                                       if (m->control_pressed) { return 0; }
+                                       
                                        for(int j=0;j<i;j++){
                                                in >> dist;
                                                matrix[i][j] = dist;  matrix[j][i] = dist;
                                        }
-                                       gobble(in);
+                                       m->gobble(in);
                                }
                        }
                        in.close();
@@ -434,10 +459,12 @@ int HeatMapSimCommand::runCommandDist() {
                        //read column file
                        string first, second;
                        double dist;
-                       openInputFile(columnfile, in);
+                       m->openInputFile(columnfile, in);
                        
                        while (!in.eof()) {
-                               in >> first >> second >> dist; gobble(in);
+                               in >> first >> second >> dist; m->gobble(in);
+                               
+                               if (m->control_pressed) { return 0; }
                                
                                map<string, int>::iterator itA = nameMap->find(first);
                                map<string, int>::iterator itB = nameMap->find(second);
@@ -455,12 +482,12 @@ int HeatMapSimCommand::runCommandDist() {
                }
                
 
-               heatmap->getPic(matrix, names); //vector<vector<double>>, vector<string>
+               outputNames.push_back(heatmap->getPic(matrix, names)); //vector<vector<double>>, vector<string>
                
                return 0;
        }
        catch(exception& e) {
-               errorOut(e, "HeatMapSimCommand", "runCommandDist");
+               m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
                exit(1);
        }
 }