X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=nameassignment.cpp;h=1e42111e574baacf2eb2f5c3407416c9029f9041;hp=21703e43fa965b4d8a6cc9dedf055c2ea406466e;hb=cf9987b67aa49777a4c91c2d21f96e58bf17aa82;hpb=74c78f9abd9e733f0c2f812efec97a76632fcbf8 diff --git a/nameassignment.cpp b/nameassignment.cpp index 21703e4..1e42111 100644 --- a/nameassignment.cpp +++ b/nameassignment.cpp @@ -5,51 +5,58 @@ //********************************************************************************************************************** NameAssignment::NameAssignment(string nameMapFile){ - - openInputFile(nameMapFile, fileHandle); + m = MothurOut::getInstance(); + m->openInputFile(nameMapFile, fileHandle); } - +//********************************************************************************************************************** +NameAssignment::NameAssignment(){ m = MothurOut::getInstance(); } //********************************************************************************************************************** -void NameAssignment::readMap(int colA, int colB){ +void NameAssignment::readMap(){ try{ string firstCol, secondCol, skip; // int index = 0; - - int skipNCols = colB-colA-1; - - map data; - - while(fileHandle){ - fileHandle >> firstCol; //read from first column + - for(int i=0;i> skip; - } + map::iterator itData; + int rowIndex = 0; + while(fileHandle){ + fileHandle >> firstCol; m->gobble(fileHandle); //read from first column fileHandle >> secondCol; //read from second column - - data[firstCol] = secondCol; //store data in map - - gobble(fileHandle); + + itData = (*this).find(firstCol); + if (itData == (*this).end()) { + + (*this)[firstCol] = rowIndex++; + list.push_back(secondCol); //adds data's value to list + reverse[rowIndex] = firstCol; + + }else{ m->mothurOut(firstCol + " is already in namesfile. I will use first definition."); m->mothurOutEndLine(); } + + m->gobble(fileHandle); } fileHandle.close(); - int rowIndex = 0; - map::iterator it = data.begin(); - for(it;it!=data.end();it++){ - list.push_back(it->second); //adds data's value to list - (*this)[it->first] = rowIndex; - rowIndex++; - } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the NameAssignment class Function readMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "NameAssignment", "readMap"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the NameAssignment class function readMap. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; +} +//********************************************************************************************************************** +void NameAssignment::push_back(string name) { + try{ + + int num = (*this).size(); + (*this)[name] = num; + reverse[num] = name; + + list.push_back(name); + } + catch(exception& e) { + m->errorOut(e, "NameAssignment", "push_back"); exit(1); } } @@ -64,19 +71,17 @@ ListVector NameAssignment::getListVector(void){ //********************************************************************************************************************** -void NameAssignment::print(void){ +void NameAssignment::print(ostream& out){ try { - map::iterator it = (*this).begin(); - for(it;it!=(*this).end();it++){ - cout << it->first << '\t' << it->second << endl; //prints out keys and values of the map this. + map::iterator it; +//cout << (*this).size() << endl; + for(it = (*this).begin(); it!=(*this).end(); it++){ + out << it->first << '\t' << it->second << endl; //prints out keys and values of the map this. + //out << it->first << '\t' << it->first << endl; } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the NameAssignment class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the NameAssignment class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "NameAssignment", "print"); exit(1); } } @@ -84,10 +89,35 @@ void NameAssignment::print(void){ //********************************************************************************************************************** int NameAssignment::get(string key){ - - return (*this)[key]; - + try { + map::iterator itGet = (*this).find(key); + + //if you can't find it + if (itGet == (*this).end()) { return -1; } + + return (*this)[key]; + } + catch(exception& e) { + m->errorOut(e, "NameAssignment", "get"); + exit(1); + } } +//********************************************************************************************************************** +string NameAssignment::get(int key){ + try { + + map::iterator itGet = reverse.find(key); + + if (itGet == reverse.end()) { return "not found"; } + + return reverse[key]; + + } + catch(exception& e) { + m->errorOut(e, "NameAssignment", "get"); + exit(1); + } +} //**********************************************************************************************************************