X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=listvector.cpp;h=90df802cfea64f2894733bcd377936b4476ac05b;hb=250e3b11b1c9c1e1ad458ab6c7e71ac2e67e11d9;hp=2758c94264a1ba527e8d7c3970e0bac53254579b;hpb=006601d68abe8d0061f77e8d28323b160750e343;p=mothur.git diff --git a/listvector.cpp b/listvector.cpp index 2758c94..90df802 100644 --- a/listvector.cpp +++ b/listvector.cpp @@ -67,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 = ""; @@ -77,6 +128,8 @@ ListVector::ListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numS set(i, inputData); } m->gobble(f); + + if (f.eof()) { m->saveNextLabel = ""; } } catch(exception& e) { m->errorOut(e, "ListVector", "ListVector"); @@ -109,6 +162,60 @@ 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); + } +} /***********************************************************************/ @@ -150,6 +257,52 @@ 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){