]> git.donarmstrong.com Git - mothur.git/blobdiff - makelefsecommand.cpp
Merge remote-tracking branch 'origin'
[mothur.git] / makelefsecommand.cpp
index 8f2857e5e302b0587555a03e6a705082c47f9d45..9f895f60d42b9bc6bc1527f6826bfd6fd0d23767 100644 (file)
@@ -7,6 +7,7 @@
 //
 
 #include "makelefsecommand.h"
+#include "designmap.h"
 
 //**********************************************************************************************************************
 vector<string> MakeLefseCommand::setParameters(){
@@ -258,24 +259,33 @@ int MakeLefseCommand::runRelabund(map<string, consTax2>& consTax, vector<SharedR
         ofstream out;
         m->openOutputFile(outputFile, out);
 
-        GroupMap* designMap = NULL;
+        DesignMap* designMap = NULL;
         if (designfile != "") {
-            designMap = new GroupMap(designfile);
-            designMap->readDesignMap();
+            designMap = new DesignMap(designfile);
+            vector<string> categories = designMap->getNamesOfCategories();
             
-            out << "treatment\t";
-            for (int i = 0; i < lookup.size(); i++) {
-                string treatment = designMap->getGroup(lookup[i]->getGroup());
-                if (treatment == "not found") {
-                    m->mothurOut("[ERROR]: " + lookup[i]->getGroup() + " is not in your design file, please correct.\n"); 
-                }else { out << treatment << '\t'; }
+            if (categories.size() > 3) {  m->mothurOut("\n[NOTE]: LEfSe input files allow for a class, subclass and subject.  More than 3 categories can cause formatting errors.\n\n"); }
+            
+            for (int j = 0; j < categories.size(); j++) {
+                out << categories[j] << "\t";
+                for (int i = 0; i < lookup.size()-1; i++) {
+                    if (m->control_pressed) { out.close(); delete designMap; return 0; }
+                    string value = designMap->get(lookup[i]->getGroup(), categories[j]);
+                    if (value == "not found") {
+                        m->mothurOut("[ERROR]: " + lookup[i]->getGroup() + " is not in your design file, please correct.\n"); m->control_pressed = true;
+                    }else { out << value << '\t'; }
+                }
+                string value = designMap->get(lookup[lookup.size()-1]->getGroup(), categories[j]);
+                if (value == "not found") {
+                    m->mothurOut("[ERROR]: " + lookup[lookup.size()-1]->getGroup() + " is not in your design file, please correct.\n"); m->control_pressed = true;
+                }else { out << value; }
+                out << endl;
             }
-            out << endl;
         }
         
         out << "group\t";
-        for (int i = 0; i < lookup.size(); i++) {  out << lookup[i]->getGroup() << '\t'; }
-        out << endl;
+        for (int i = 0; i < lookup.size()-1; i++) {  out << lookup[i]->getGroup() << '\t'; }
+        out << lookup[lookup.size()-1]->getGroup() << endl;
         
         for (int i = 0; i < lookup[0]->getNumBins(); i++) { //process each otu
             if (m->control_pressed) { break; }
@@ -302,8 +312,9 @@ int MakeLefseCommand::runRelabund(map<string, consTax2>& consTax, vector<SharedR
             out << nameOfOtu << '\t';
             
             //print out relabunds for each otu
-            for (int j = 0; j < lookup.size(); j++) { out << lookup[j]->getAbundance(i) << '\t'; }
-            out << endl;
+            for (int j = 0; j < lookup.size()-1; j++) { out << lookup[j]->getAbundance(i) << '\t'; }
+            
+            out << lookup[lookup.size()-1]->getAbundance(i)<< endl;
         }
         out.close();