X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=listvector.cpp;h=90df802cfea64f2894733bcd377936b4476ac05b;hp=9005dc0ad450161c580a2ed2dc5074afb8e53912;hb=499f4ac6e321f9f03d4c3aa25c3b6880892c8b83;hpb=bfbc55964f1977da72c2cea984288a427d370a59 diff --git a/listvector.cpp b/listvector.cpp index 9005dc0..90df802 100644 --- a/listvector.cpp +++ b/listvector.cpp @@ -7,23 +7,41 @@ * */ -using namespace std; - #include "sabundvector.hpp" #include "rabundvector.hpp" #include "ordervector.hpp" -#include "utilities.hpp" #include "listvector.hpp" +//sorts highest to lowest +/***********************************************************************/ +inline bool abundNamesSort(string left, string right){ + + int countLeft = 0; + if(left != ""){ + countLeft = 1; + for(int i=0;i countRight) { + return true; + } + return false; +} /***********************************************************************/ -ListVector::ListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){}; +ListVector::ListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){} /***********************************************************************/ -ListVector::ListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){}; +ListVector::ListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){} /***********************************************************************/ @@ -31,7 +49,7 @@ ListVector::ListVector(string id, vector lv) : DataVector(id), data(lv){ try { for(int i=0;igetNumNames(data[i]); numBins = i+1; if(binSize > maxRank) { maxRank = binSize; } numSeqs += binSize; @@ -39,11 +57,7 @@ ListVector::ListVector(string id, vector lv) : DataVector(id), data(lv){ } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "ListVector"); exit(1); } } @@ -53,8 +67,59 @@ ListVector::ListVector(string id, vector lv) : DataVector(id), data(lv){ ListVector::ListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) { try { int hold; - f >> label >> hold; + + //are we at the beginning of the file?? + if (m->saveNextLabel == "") { + f >> label; + + //is this a shared file that has headers + if (label == "label") { + + //gets "numOtus" + f >> label; m->gobble(f); + + //eat rest of line + label = m->getline(f); m->gobble(f); + + //parse labels to save + istringstream iStringStream(label); + m->listBinLabelsInFile.clear(); + while(!iStringStream.eof()){ + if (m->control_pressed) { break; } + string temp; + iStringStream >> temp; m->gobble(iStringStream); + + m->listBinLabelsInFile.push_back(temp); + } + + f >> label >> hold; + }else { + //read in first row + f >> hold; + + //make binlabels because we don't have any + string snumBins = toString(hold); + m->listBinLabelsInFile.clear(); + for (int i = 0; i < hold; i++) { + //if there is a bin label use it otherwise make one + string binLabel = "Otu"; + string sbinNumber = toString(i+1); + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + m->listBinLabelsInFile.push_back(binLabel); + } + } + m->saveNextLabel = label; + }else { + f >> label >> hold; + m->saveNextLabel = label; + } + binLabels.assign(m->listBinLabelsInFile.begin(), m->listBinLabelsInFile.begin()+hold); + data.assign(hold, ""); string inputData = ""; @@ -62,13 +127,12 @@ ListVector::ListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numS f >> inputData; set(i, inputData); } + m->gobble(f); + + if (f.eof()) { m->saveNextLabel = ""; } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "ListVector"); exit(1); } } @@ -77,9 +141,9 @@ ListVector::ListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numS void ListVector::set(int binNumber, string seqNames){ try { - int nNames_old = getNumNames(data[binNumber]); + int nNames_old = m->getNumNames(data[binNumber]); data[binNumber] = seqNames; - int nNames_new = getNumNames(seqNames); + int nNames_new = m->getNumNames(seqNames); if(nNames_old == 0) { numBins++; } if(nNames_new == 0) { numBins--; } @@ -88,11 +152,7 @@ void ListVector::set(int binNumber, string seqNames){ numSeqs += (nNames_new - nNames_old); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "set"); exit(1); } } @@ -102,13 +162,67 @@ void ListVector::set(int binNumber, string seqNames){ string ListVector::get(int index){ return data[index]; } +/***********************************************************************/ + +void ListVector::setLabels(vector labels){ + try { + binLabels = labels; + } + catch(exception& e) { + m->errorOut(e, "ListVector", "setLabels"); + exit(1); + } +} + +/***********************************************************************/ +//could potentially end up with duplicate binlabel names with code below. +//we don't currently use them in a way that would do that. +//if you had a listfile that had been subsampled and then added to it, dup names would be possible. +vector ListVector::getLabels(){ + try { + + string tagHeader = "Otu"; + if (m->sharedHeaderMode == "tax") { tagHeader = "PhyloType"; } + + if (binLabels.size() < data.size()) { + string snumBins = toString(numBins); + + for (int i = 0; i < numBins; i++) { + string binLabel = tagHeader; + + if (i < binLabels.size()) { //label exists, check leading zeros length + string sbinNumber = m->getSimpleLabel(binLabels[i]); + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + binLabels[i] = binLabel; + }else{ + string sbinNumber = toString(i+1); + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + binLabels.push_back(binLabel); + } + } + } + return binLabels; + } + catch(exception& e) { + m->errorOut(e, "ListVector", "getLabels"); + exit(1); + } +} /***********************************************************************/ void ListVector::push_back(string seqNames){ try { data.push_back(seqNames); - int nNames = getNumNames(seqNames); + int nNames = m->getNumNames(seqNames); numBins++; @@ -117,11 +231,7 @@ void ListVector::push_back(string seqNames){ numSeqs += nNames; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "push_back"); exit(1); } } @@ -147,25 +257,70 @@ void ListVector::clear(){ } +/***********************************************************************/ +void ListVector::printHeaders(ostream& output){ + try { + string snumBins = toString(numBins); + output << "label\tnumOtus\t"; + if (m->sharedHeaderMode == "tax") { + for (int i = 0; i < numBins; i++) { + + //if there is a bin label use it otherwise make one + string binLabel = "PhyloType"; + string sbinNumber = toString(i+1); + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + if (i < binLabels.size()) { binLabel = binLabels[i]; } + + output << binLabel << '\t'; + } + output << endl; + }else { + for (int i = 0; i < numBins; i++) { + //if there is a bin label use it otherwise make one + string binLabel = "Otu"; + string sbinNumber = toString(i+1); + if (sbinNumber.length() < snumBins.length()) { + int diff = snumBins.length() - sbinNumber.length(); + for (int h = 0; h < diff; h++) { binLabel += "0"; } + } + binLabel += sbinNumber; + if (i < binLabels.size()) { binLabel = binLabels[i]; } + + output << binLabel << '\t'; + } + + output << endl; + } + m->printedListHeaders = true; + } + catch(exception& e) { + m->errorOut(e, "ListVector", "printHeaders"); + exit(1); + } +} + /***********************************************************************/ void ListVector::print(ostream& output){ try { output << label << '\t' << numBins << '\t'; - for(int i=0;i hold = data; + sort(hold.begin(), hold.end(), abundNamesSort); + + for(int i=0;ierrorOut(e, "ListVector", "print"); exit(1); } } @@ -178,7 +333,7 @@ RAbundVector ListVector::getRAbundVector(){ RAbundVector rav; for(int i=0;igetNumNames(data[i]); rav.push_back(binSize); } @@ -196,11 +351,7 @@ RAbundVector ListVector::getRAbundVector(){ return rav; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "getRAbundVector"); exit(1); } } @@ -212,7 +363,7 @@ SAbundVector ListVector::getSAbundVector(){ SAbundVector sav(maxRank+1); for(int i=0;igetNumNames(data[i]); sav.set(binSize, sav.get(binSize) + 1); } sav.set(0, 0); @@ -221,11 +372,7 @@ SAbundVector ListVector::getSAbundVector(){ return sav; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "getSAbundVector"); exit(1); } } @@ -239,7 +386,7 @@ OrderVector ListVector::getOrderVector(map* orderMap = NULL){ OrderVector ov; for(int i=0;igetNumNames(data[i]); for(int j=0;j* orderMap = NULL){ } else{ if(orderMap->count(seqName) == 0){ - cerr << seqName << " not found, check *.names file\n"; + m->mothurOut(seqName + " not found, check *.names file\n"); exit(1); } @@ -277,7 +424,7 @@ OrderVector ListVector::getOrderVector(map* orderMap = NULL){ } if(orderMap->count(seqName) == 0){ - cerr << seqName << " not found, check *.names file\n"; + m->mothurOut(seqName + " not found, check *.names file\n"); exit(1); } ov.set((*orderMap)[seqName], i); @@ -290,11 +437,7 @@ OrderVector ListVector::getOrderVector(map* orderMap = NULL){ } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the ListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ListVector", "getOrderVector"); exit(1); } }