5 * Created by Sarah Westcott on 1/26/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /************************************************************/
14 TreeMap::TreeMap(string filename) {
15 m = MothurOut::getInstance();
16 groupFileName = filename;
17 m->openInputFile(filename, fileHandle);
20 /************************************************************/
23 /************************************************************/
24 int TreeMap::readMap() {
25 string seqName, seqGroup;
29 fileHandle >> seqName; //read from first column
30 fileHandle >> seqGroup; //read from second column
32 if (m->control_pressed) { fileHandle.close(); return 1; }
34 setNamesOfGroups(seqGroup);
36 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
37 if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); }
39 namesOfSeqs.push_back(seqName);
40 treemap[seqName].groupname = seqGroup; //store data in map
42 it2 = seqsPerGroup.find(seqGroup);
43 if (it2 == seqsPerGroup.end()) { //if it's a new group
44 seqsPerGroup[seqGroup] = 1;
45 }else {//it's a group we already have
46 seqsPerGroup[seqGroup]++;
50 m->gobble(fileHandle);
57 /************************************************************/
58 void TreeMap::addSeq(string seqName, string seqGroup) {
60 namesOfSeqs.push_back(seqName);
61 setNamesOfGroups(seqGroup);
63 treemap[seqName].groupname = seqGroup; //store data in map
65 it2 = seqsPerGroup.find(seqGroup);
66 if (it2 == seqsPerGroup.end()) { //if it's a new group
67 seqsPerGroup[seqGroup] = 1;
68 }else {//it's a group we already have
69 seqsPerGroup[seqGroup]++;
72 /************************************************************/
73 void TreeMap::removeSeq(string seqName) {
75 //erase name from namesOfSeqs
76 for (int i = 0; i < namesOfSeqs.size(); i++) {
77 if (namesOfSeqs[i] == seqName) {
78 namesOfSeqs.erase(namesOfSeqs.begin()+i);
83 //decrement sequences in this group
84 string group = treemap[seqName].groupname;
85 seqsPerGroup[group]--;
87 //remove seq from treemap
88 it = treemap.find(seqName);
91 /************************************************************/
93 int TreeMap::getNumGroups() {
95 return namesOfGroups.size();
98 /************************************************************/
100 int TreeMap::getNumSeqs() {
102 return namesOfSeqs.size();
106 /************************************************************/
108 string TreeMap::getGroup(string sequenceName) {
110 it = treemap.find(sequenceName);
111 if (it != treemap.end()) { //sequence name was in group file
112 return it->second.groupname;
118 /************************************************************/
119 void TreeMap::setIndex(string seq, int index) {
120 it = treemap.find(seq);
121 if (it != treemap.end()) { //sequence name was in group file
122 treemap[seq].vectorIndex = index;
124 treemap[seq].vectorIndex = index;
125 treemap[seq].groupname = "not found";
128 /************************************************************/
129 int TreeMap::getIndex(string seq) {
131 it = treemap.find(seq);
132 // if it is a valid sequence name then return index
133 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
134 // if not return error code
138 /************************************************************/
140 void TreeMap::setNamesOfGroups(string seqGroup) {
143 for (i=0; i<namesOfGroups.size(); i++) {
144 if (namesOfGroups[i] != seqGroup) {
145 count++; //you have not found this group
147 break; //you already have it
150 if (count == namesOfGroups.size()) {
151 namesOfGroups.push_back(seqGroup); //new group
154 /************************************************************/
155 bool TreeMap::isValidGroup(string groupname) {
157 for (int i = 0; i < namesOfGroups.size(); i++) {
158 if (groupname == namesOfGroups[i]) { return true; }
163 catch(exception& e) {
164 m->errorOut(e, "TreeMap", "isValidGroup");
168 /***********************************************************************/
170 void TreeMap::print(ostream& output){
173 for(it = treemap.begin(); it != treemap.end(); it++){
174 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
177 catch(exception& e) {
178 m->errorOut(e, "TreeMap", "print");
183 /************************************************************/
184 void TreeMap::makeSim(vector<string> ThisnamesOfGroups) {
186 //set names of groups
187 namesOfGroups = ThisnamesOfGroups;
189 //set names of seqs to names of groups
190 namesOfSeqs = ThisnamesOfGroups;
192 // make map where key and value are both the group name since that what the tree.shared command wants
193 for (int i = 0; i < namesOfGroups.size(); i++) {
194 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
195 seqsPerGroup[namesOfGroups[i]] = 1;
198 numGroups = namesOfGroups.size();
201 catch(exception& e) {
202 m->errorOut(e, "TreeMap", "makeSim");
206 /************************************************************/
207 void TreeMap::makeSim(ListVector* list) {
209 //set names of groups
210 namesOfGroups.clear();
211 for(int i = 0; i < list->size(); i++) {
212 namesOfGroups.push_back(list->get(i));
215 //set names of seqs to names of groups
216 namesOfSeqs = namesOfGroups;
218 // make map where key and value are both the group name since that what the tree.shared command wants
219 for (int i = 0; i < namesOfGroups.size(); i++) {
220 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
221 seqsPerGroup[namesOfGroups[i]] = 1;
224 numGroups = namesOfGroups.size();
227 catch(exception& e) {
228 m->errorOut(e, "TreeMap", "makeSim");
233 /************************************************************/