From dbcb14c17c128987e59147f510950f32dbeb6698 Mon Sep 17 00:00:00 2001 From: westcott Date: Wed, 1 Jun 2011 17:37:36 +0000 Subject: [PATCH] added headers to shared and relabund files --- datavector.hpp | 1 + engine.cpp | 3 +++ getrelabundcommand.cpp | 5 +++-- inputdata.cpp | 3 +++ mergegroupscommand.cpp | 4 ++++ mothurout.h | 5 ++++- normalizesharedcommand.cpp | 12 +++++++++--- removerarecommand.cpp | 3 +++ sharedcommand.cpp | 3 +++ sharedrabundfloatvector.cpp | 31 +++++++++++++++++++++++++++++-- sharedrabundfloatvector.h | 1 + sharedrabundvector.cpp | 34 +++++++++++++++++++++++++++++++--- sharedrabundvector.h | 1 + sharedsabundvector.cpp | 2 +- subsamplecommand.cpp | 3 +++ validparameter.cpp | 4 ++++ 16 files changed, 103 insertions(+), 12 deletions(-) diff --git a/datavector.hpp b/datavector.hpp index 734ace5..e6c839d 100644 --- a/datavector.hpp +++ b/datavector.hpp @@ -36,6 +36,7 @@ public: virtual void resize(int) = 0; virtual int size() = 0; virtual void print(ostream&) = 0; + virtual void printHeaders(ostream&) {}; virtual void clear() = 0; void setLabel(string l) { label = l; } diff --git a/engine.cpp b/engine.cpp index d595be3..469ff40 100644 --- a/engine.cpp +++ b/engine.cpp @@ -185,6 +185,7 @@ bool InteractEngine::getInput(){ mout->Treenames.clear(); mout->names.clear(); mout->saveNextLabel = ""; + mout->printedHeaders = false; Command* command = cFactory->getCommand(commandName, options); quitCommandCalled = command->execute(); @@ -366,6 +367,7 @@ bool BatchEngine::getInput(){ mout->Treenames.clear(); mout->names.clear(); mout->saveNextLabel = ""; + mout->printedHeaders = false; Command* command = cFactory->getCommand(commandName, options); quitCommandCalled = command->execute(); @@ -529,6 +531,7 @@ bool ScriptEngine::getInput(){ mout->Treenames.clear(); mout->names.clear(); mout->saveNextLabel = ""; + mout->printedHeaders = false; Command* command = cFactory->getCommand(commandName, options); quitCommandCalled = command->execute(); diff --git a/getrelabundcommand.cpp b/getrelabundcommand.cpp index 91384fa..d4caf5b 100644 --- a/getrelabundcommand.cpp +++ b/getrelabundcommand.cpp @@ -175,6 +175,7 @@ int GetRelAbundCommand::execute(){ if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } getRelAbundance(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -187,7 +188,7 @@ int GetRelAbundCommand::execute(){ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } lookup = input->getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } getRelAbundance(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -228,7 +229,7 @@ int GetRelAbundCommand::execute(){ lookup = input->getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } getRelAbundance(lookup, out); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } diff --git a/inputdata.cpp b/inputdata.cpp index 08bab60..ac215ca 100644 --- a/inputdata.cpp +++ b/inputdata.cpp @@ -20,12 +20,14 @@ InputData::InputData(string fName, string f) : format(f){ filename = fName; m->saveNextLabel = ""; + } /***********************************************************************/ InputData::~InputData(){ fileHandle.close(); m->saveNextLabel = ""; + } /***********************************************************************/ @@ -48,6 +50,7 @@ InputData::InputData(string fName, string orderFileName, string f) : format(f){ ofHandle.close(); m->openInputFile(fName, fileHandle); + m->saveNextLabel = ""; } catch(exception& e) { m->errorOut(e, "InputData", "InputData"); diff --git a/mergegroupscommand.cpp b/mergegroupscommand.cpp index df74ade..f62bb0c 100644 --- a/mergegroupscommand.cpp +++ b/mergegroupscommand.cpp @@ -192,6 +192,8 @@ int MergeGroupsCommand::execute(){ if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } process(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -205,6 +207,7 @@ int MergeGroupsCommand::execute(){ lookup = input.getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } process(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -246,6 +249,7 @@ int MergeGroupsCommand::execute(){ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } process(lookup, out); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } diff --git a/mothurout.h b/mothurout.h index fb5b432..98cd6a3 100644 --- a/mothurout.h +++ b/mothurout.h @@ -41,7 +41,8 @@ class MothurOut { vector Treenames; map names; vector namesOfGroups; - string saveNextLabel, argv; + string saveNextLabel, argv, sharedHeaderMode; + bool printedHeaders; //functions from mothur.h //file operations @@ -179,6 +180,8 @@ class MothurOut { processors = "1"; flowfile = ""; gui = false; + printedHeaders = false; + sharedHeaderMode = ""; }; ~MothurOut(); diff --git a/normalizesharedcommand.cpp b/normalizesharedcommand.cpp index 718eeb1..07c353a 100644 --- a/normalizesharedcommand.cpp +++ b/normalizesharedcommand.cpp @@ -240,6 +240,7 @@ int NormalizeSharedCommand::execute(){ m->mothurOut("Normalizing to " + toString(norm) + "."); m->mothurOutEndLine(); } + //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))) { @@ -248,6 +249,7 @@ int NormalizeSharedCommand::execute(){ if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } normalize(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -260,7 +262,7 @@ int NormalizeSharedCommand::execute(){ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } lookup = input->getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } normalize(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -301,7 +303,7 @@ int NormalizeSharedCommand::execute(){ lookup = input->getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); - + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } normalize(lookup, out); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } @@ -351,6 +353,8 @@ int NormalizeSharedCommand::execute(){ if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){ m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine(); + + if (!m->printedHeaders) { lookupFloat[0]->printHeaders(out); } normalize(lookupFloat, out); processedLabels.insert(lookupFloat[0]->getLabel()); @@ -362,8 +366,9 @@ int NormalizeSharedCommand::execute(){ for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } lookupFloat = input->getSharedRAbundFloatVectors(lastLabel); - m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine(); + m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookupFloat[0]->printHeaders(out); } normalize(lookupFloat, out); processedLabels.insert(lookupFloat[0]->getLabel()); @@ -405,6 +410,7 @@ int NormalizeSharedCommand::execute(){ m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookupFloat[0]->printHeaders(out); } normalize(lookupFloat, out); for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } diff --git a/removerarecommand.cpp b/removerarecommand.cpp index cd0b602..255448e 100644 --- a/removerarecommand.cpp +++ b/removerarecommand.cpp @@ -646,6 +646,7 @@ int RemoveRareCommand::processShared(){ processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } processLookup(lookup, out); } @@ -659,6 +660,7 @@ int RemoveRareCommand::processShared(){ processedLabels.insert(lookup[0]->getLabel()); userLabels.erase(lookup[0]->getLabel()); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } processLookup(lookup, out); //restore real lastlabel to save below @@ -693,6 +695,7 @@ int RemoveRareCommand::processShared(){ m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } processLookup(lookup, out); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } diff --git a/sharedcommand.cpp b/sharedcommand.cpp index 54b06fc..b322007 100644 --- a/sharedcommand.cpp +++ b/sharedcommand.cpp @@ -319,6 +319,7 @@ int SharedCommand::execute(){ 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]; } @@ -348,6 +349,7 @@ int SharedCommand::execute(){ 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]; } @@ -393,6 +395,7 @@ int SharedCommand::execute(){ 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; diff --git a/sharedrabundfloatvector.cpp b/sharedrabundfloatvector.cpp index 6c2dea1..e7ab225 100644 --- a/sharedrabundfloatvector.cpp +++ b/sharedrabundfloatvector.cpp @@ -44,8 +44,17 @@ SharedRAbundFloatVector::SharedRAbundFloatVector(ifstream& f) : DataVector(), ma for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } lookup.clear(); - if (m->saveNextLabel == "") { f >> label; } - else { label = m->saveNextLabel; } + //are we at the beginning of the file?? + if (m->saveNextLabel == "") { + f >> label; + + //is this a shared file that has headers + if (label == "label") { + //eat rest of line + label = m->getline(f); m->gobble(f); + f >> label; + } + }else { label = m->saveNextLabel; } //read in first row since you know there is at least 1 group. f >> groupN >> num; @@ -218,6 +227,24 @@ int SharedRAbundFloatVector::size(){ return data.size(); } /***********************************************************************/ +void SharedRAbundFloatVector::printHeaders(ostream& output){ + try { + output << "label\tGroup\tnumOtus\t"; + if (m->sharedHeaderMode == "tax") { + for (int i = 0; i < numBins; i++) { output << "PhyloType" << (i+1) << '\t'; } + output << endl; + }else { + for (int i = 0; i < numBins; i++) { output << "Otu" << (i+1) << '\t'; } + output << endl; + } + m->printedHeaders = true; + } + catch(exception& e) { + m->errorOut(e, "SharedRAbundVector", "printHeaders"); + exit(1); + } +} +/***********************************************************************/ void SharedRAbundFloatVector::print(ostream& output){ try { output << numBins << '\t'; diff --git a/sharedrabundfloatvector.h b/sharedrabundfloatvector.h index 06fb604..12df9e8 100644 --- a/sharedrabundfloatvector.h +++ b/sharedrabundfloatvector.h @@ -55,6 +55,7 @@ public: int size(); void print(ostream&); + void printHeaders(ostream&); RAbundVector getRAbundVector(); SAbundVector getSAbundVector(); diff --git a/sharedrabundvector.cpp b/sharedrabundvector.cpp index b41c5fa..8d0df60 100644 --- a/sharedrabundvector.cpp +++ b/sharedrabundvector.cpp @@ -68,8 +68,17 @@ SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } lookup.clear(); - if (m->saveNextLabel == "") { f >> label; } - else { label = m->saveNextLabel; } + //are we at the beginning of the file?? + if (m->saveNextLabel == "") { + f >> label; + + //is this a shared file that has headers + if (label == "label") { + //eat rest of line + label = m->getline(f); m->gobble(f); + f >> label; + } + }else { label = m->saveNextLabel; } //read in first row since you know there is at least 1 group. f >> groupN >> num; @@ -300,8 +309,27 @@ int SharedRAbundVector::size(){ return data.size(); } + +/***********************************************************************/ +void SharedRAbundVector::printHeaders(ostream& output){ + try { + output << "label\tGroup\tnumOtus\t"; + if (m->sharedHeaderMode == "tax") { + for (int i = 0; i < numBins; i++) { output << "PhyloType" << (i+1) << '\t'; } + output << endl; + }else { + for (int i = 0; i < numBins; i++) { output << "Otu" << (i+1) << '\t'; } + output << endl; + } + m->printedHeaders = true; + } + catch(exception& e) { + m->errorOut(e, "SharedRAbundVector", "printHeaders"); + exit(1); + } +} /***********************************************************************/ -void SharedRAbundVector::print(ostream& output){ +void SharedRAbundVector::print(ostream& output) { try { output << numBins << '\t'; diff --git a/sharedrabundvector.h b/sharedrabundvector.h index 887478f..02dc9f4 100644 --- a/sharedrabundvector.h +++ b/sharedrabundvector.h @@ -61,6 +61,7 @@ public: vector::reverse_iterator rend(); void print(ostream&); + void printHeaders(ostream&); RAbundVector getRAbundVector(); RAbundVector getRAbundVector2(); diff --git a/sharedsabundvector.cpp b/sharedsabundvector.cpp index 1df608b..efd14bc 100644 --- a/sharedsabundvector.cpp +++ b/sharedsabundvector.cpp @@ -13,7 +13,7 @@ /***********************************************************************/ -SharedSAbundVector::SharedSAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){} +SharedSAbundVector::SharedSAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){ } /***********************************************************************/ diff --git a/subsamplecommand.cpp b/subsamplecommand.cpp index 66ea7e9..0f7bdec 100644 --- a/subsamplecommand.cpp +++ b/subsamplecommand.cpp @@ -702,6 +702,7 @@ int SubSampleCommand::getSubSampleShared() { m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } processShared(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -716,6 +717,7 @@ int SubSampleCommand::getSubSampleShared() { lookup = input->getSharedRAbundVectors(lastLabel); m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } processShared(lookup, out); processedLabels.insert(lookup[0]->getLabel()); @@ -756,6 +758,7 @@ int SubSampleCommand::getSubSampleShared() { m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + if (!m->printedHeaders) { lookup[0]->printHeaders(out); } processShared(lookup, out); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } diff --git a/validparameter.cpp b/validparameter.cpp index 7855dde..3e1f349 100644 --- a/validparameter.cpp +++ b/validparameter.cpp @@ -221,6 +221,10 @@ string ValidParameters::validFile(map& container, string paramet if(it != container.end()){ //no parameter given if(isFile == true) { + + int pos = (it->second).find(".tx."); + if (pos != string::npos) { m->sharedHeaderMode = "tax"; } + else { m->sharedHeaderMode = "otu"; } #ifdef USE_MPI int pid, processors; -- 2.39.2