]> git.donarmstrong.com Git - mothur.git/blobdiff - matrixoutputcommand.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / matrixoutputcommand.cpp
index 73c38f7ca3b06cd5b38b45b070beb2c8fd2f0a8b..05cd18a720e567e3e7c1852b436b68b1a38814f0 100644 (file)
@@ -65,7 +65,6 @@ MatrixOutputCommand::MatrixOutputCommand(){
                setParameters();
                vector<string> tempOutNames;
                outputTypes["phylip"] = tempOutNames;
-        outputTypes["subsample"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
@@ -100,7 +99,6 @@ MatrixOutputCommand::MatrixOutputCommand(string option)  {
                        //initialize outputTypes
                        vector<string> tempOutNames;
                        outputTypes["phylip"] = tempOutNames;
-            outputTypes["subsample"] = tempOutNames;
                        
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
@@ -300,13 +298,6 @@ int MatrixOutputCommand::execute(){
                set<string> userLabels = labels;
                                        
                if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } return 0;}
-               
-               numGroups = lookup.size();
-               lines.resize(processors);
-               for (int i = 0; i < processors; i++) {
-                       lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
-                       lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
-               }       
         
         if (subsample) { 
             if (subsampleSize == -1) { //user has not set size, set size = smallest samples size
@@ -332,8 +323,17 @@ int MatrixOutputCommand::execute(){
                 lookup = temp;
                 m->setGroups(Groups);
             }
+            
+            if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; delete input; return 0; }
         }
-               
+        
+               numGroups = lookup.size();
+        lines.resize(processors);
+               for (int i = 0; i < processors; i++) {
+                       lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
+                       lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
+               }       
+        
                if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->clearGroups(); return 0;  }
                                
                //as long as you are not at the end of the file or done wih the lines you want
@@ -434,18 +434,18 @@ void MatrixOutputCommand::printSims(ostream& out, vector< vector<double> >& simM
                out << simMatrix.size() << endl;
                
                if (output == "lt") {
-                       for (int m = 0; m < simMatrix.size(); m++)      {
-                               out << lookup[m]->getGroup() << '\t';
-                               for (int n = 0; n < m; n++)     {
-                                       out << simMatrix[m][n] << '\t'; 
+                       for (int b = 0; b < simMatrix.size(); b++)      {
+                               out << lookup[b]->getGroup() << '\t';
+                               for (int n = 0; n < b; n++)     {
+                                       out << simMatrix[b][n] << '\t'; 
                                }
                                out << endl;
                        }
                }else{
-                       for (int m = 0; m < simMatrix.size(); m++)      {
-                               out << lookup[m]->getGroup() << '\t';
-                               for (int n = 0; n < simMatrix[m].size(); n++)   {
-                                       out << simMatrix[m][n] << '\t'; 
+                       for (int b = 0; b < simMatrix.size(); m++)      {
+                               out << lookup[b]->getGroup() << '\t';
+                               for (int n = 0; n < simMatrix[b].size(); n++)   {
+                                       out << simMatrix[b][n] << '\t'; 
                                }
                                out << endl;
                        }
@@ -459,17 +459,14 @@ void MatrixOutputCommand::printSims(ostream& out, vector< vector<double> >& simM
 /***********************************************************/
 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
        try {
-               EstOutput data;
-               vector<SharedRAbundVector*> subset;
                vector< vector< vector<seqDist> > > calcDistsTotals;  //each iter, one for each calc, then each groupCombos dists. this will be used to make .dist files
-
         vector< vector<seqDist>  > calcDists; calcDists.resize(matrixCalculators.size());              
 
-        for (int thisIter = 0; thisIter < iters; thisIter++) {
+        for (int thisIter = 0; thisIter < iters+1; thisIter++) {
             
             vector<SharedRAbundVector*> thisItersLookup = thisLookup;
             
-            if (subsample) {
+            if (subsample && (thisIter != 0)) {
                 SubSample sample;
                 vector<string> tempLabels; //dont need since we arent printing the sampled sharedRabunds
                 
@@ -622,14 +619,40 @@ int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
                 #endif
             }
             
-            calcDistsTotals.push_back(calcDists);
-            
-            if (subsample) {  
-                
+            if (subsample && (thisIter != 0)) {  
+                calcDistsTotals.push_back(calcDists);
                 //clean up memory
                 for (int i = 0; i < thisItersLookup.size(); i++) { delete thisItersLookup[i]; }
                 thisItersLookup.clear();
                 for (int i = 0; i < calcDists.size(); i++) {  calcDists[i].clear(); }
+            }else { //print results for whole dataset
+                for (int i = 0; i < calcDists.size(); i++) {
+                    if (m->control_pressed) { break; }
+                    
+                    //initialize matrix
+                    vector< vector<double> > matrix; //square matrix to represent the distance
+                    matrix.resize(thisLookup.size());
+                    for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
+                    
+                    for (int j = 0; j < calcDists[i].size(); j++) {
+                        int row = calcDists[i][j].seq1;
+                        int column = calcDists[i][j].seq2;
+                        double dist = calcDists[i][j].dist;
+                        
+                        matrix[row][column] = dist;
+                        matrix[column][row] = dist;
+                    }
+                    
+                    string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel()  + "." + output + ".dist";
+                    outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
+                    ofstream outDist;
+                    m->openOutputFile(distFileName, outDist);
+                    outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
+                    
+                    printSims(outDist, matrix);
+                    
+                    outDist.close();
+                }
             }
                }
                
@@ -732,35 +755,6 @@ int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
                 outSTD.close();
 
             }
-        }else {
-        
-            for (int i = 0; i < calcDists.size(); i++) {
-                if (m->control_pressed) { break; }
-                
-                //initialize matrix
-                vector< vector<double> > matrix; //square matrix to represent the distance
-                matrix.resize(thisLookup.size());
-                for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
-                
-                for (int j = 0; j < calcDists[i].size(); j++) {
-                    int row = calcDists[i][j].seq1;
-                    int column = calcDists[i][j].seq2;
-                    double dist = calcDists[i][j].dist;
-                    
-                    matrix[row][column] = dist;
-                    matrix[column][row] = dist;
-                }
-                
-                string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel()  + "." + output + ".dist";
-                outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
-                ofstream outDist;
-                m->openOutputFile(distFileName, outDist);
-                outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
-                
-                printSims(outDist, matrix);
-                
-                outDist.close();
-            }
         }
                
                return 0;