1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include "nameassignment.hpp"
//**********************************************************************************************************************
NameAssignment::NameAssignment(string nameMapFile){
m = MothurOut::getInstance();
m->openInputFile(nameMapFile, fileHandle);
}
//**********************************************************************************************************************
NameAssignment::NameAssignment(){ m = MothurOut::getInstance(); }
//**********************************************************************************************************************
void NameAssignment::readMap(){
try{
string firstCol, secondCol, skip;
// int index = 0;
map<string, int>::iterator itData;
int rowIndex = 0;
while(fileHandle){
fileHandle >> firstCol; m->gobble(fileHandle); //read from first column
fileHandle >> secondCol; //read from second column
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();
}
catch(exception& e) {
m->errorOut(e, "NameAssignment", "readMap");
exit(1);
}
}
//**********************************************************************************************************************
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);
}
}
//**********************************************************************************************************************
ListVector NameAssignment::getListVector(void){
return list;
}
//**********************************************************************************************************************
void NameAssignment::print(ostream& out){
try {
map<string,int>::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) {
m->errorOut(e, "NameAssignment", "print");
exit(1);
}
}
//**********************************************************************************************************************
int NameAssignment::get(string key){
try {
map<string, int>::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<int, string>::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);
}
}
//**********************************************************************************************************************
|