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 groupFileName = filename;
16 openInputFile(filename, fileHandle);
19 /************************************************************/
22 /************************************************************/
23 void TreeMap::readMap() {
24 string seqName, seqGroup;
27 fileHandle >> seqName; //read from first column
28 fileHandle >> seqGroup; //read from second column
30 namesOfSeqs.push_back(seqName);
31 setNamesOfGroups(seqGroup);
33 treemap[seqName].groupname = seqGroup; //store data in map
35 it2 = seqsPerGroup.find(seqGroup);
36 if (it2 == seqsPerGroup.end()) { //if it's a new group
37 seqsPerGroup[seqGroup] = 1;
38 }else {//it's a group we already have
39 seqsPerGroup[seqGroup]++;
46 /************************************************************/
48 int TreeMap::getNumGroups() {
50 return namesOfGroups.size();
53 /************************************************************/
55 int TreeMap::getNumSeqs() {
57 return namesOfSeqs.size();
61 /************************************************************/
63 string TreeMap::getGroup(string sequenceName) {
65 it = treemap.find(sequenceName);
66 if (it != treemap.end()) { //sequence name was in group file
67 return it->second.groupname;
73 /************************************************************/
74 void TreeMap::setIndex(string seq, int index) {
75 it = treemap.find(seq);
76 if (it != treemap.end()) { //sequence name was in group file
77 treemap[seq].vectorIndex = index;
79 treemap[seq].vectorIndex = index;
80 treemap[seq].groupname = "not found";
83 /************************************************************/
84 int TreeMap::getIndex(string seq) {
86 it = treemap.find(seq);
87 // if it is a valid sequence name then return index
88 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
89 // if not return error code
93 /************************************************************/
95 void TreeMap::setNamesOfGroups(string seqGroup) {
98 for (i=0; i<namesOfGroups.size(); i++) {
99 if (namesOfGroups[i] != seqGroup) {
100 count++; //you have not found this group
102 break; //you already have it
105 if (count == namesOfGroups.size()) {
106 namesOfGroups.push_back(seqGroup); //new group
109 /************************************************************/
110 bool TreeMap::isValidGroup(string groupname) {
112 for (int i = 0; i < namesOfGroups.size(); i++) {
113 if (groupname == namesOfGroups[i]) { return true; }
118 catch(exception& e) {
119 cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123 cout << "An unknown error has occurred in the TreeMap class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127 /***********************************************************************/
129 void TreeMap::print(ostream& output){
132 for(it = treemap.begin(); it != treemap.end(); it++){
133 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
136 catch(exception& e) {
137 cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141 cout << "An unknown error has occurred in the TreeMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146 /************************************************************/
147 void TreeMap::makeSim(GroupMap* groupmap) {
149 //set names of groups
150 namesOfGroups = groupmap->namesOfGroups;
152 //set names of seqs to names of groups
153 namesOfSeqs = groupmap->namesOfGroups;
155 // make map where key and value are both the group name since that what the tree.shared command wants
156 for (int i = 0; i < namesOfGroups.size(); i++) {
157 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
158 seqsPerGroup[namesOfGroups[i]] = 1;
161 numGroups = namesOfGroups.size();
164 catch(exception& e) {
165 cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function makeSim. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 cout << "An unknown error has occurred in the TreeMap class function makeSim. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 /************************************************************/
174 void TreeMap::makeSim(ListVector* list) {
176 //set names of groups
177 namesOfGroups.clear();
178 for(int i = 0; i < list->size(); i++) {
179 namesOfGroups.push_back(list->get(i));
182 //set names of seqs to names of groups
183 namesOfSeqs = namesOfGroups;
185 // make map where key and value are both the group name since that what the tree.shared command wants
186 for (int i = 0; i < namesOfGroups.size(); i++) {
187 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
188 seqsPerGroup[namesOfGroups[i]] = 1;
191 numGroups = namesOfGroups.size();
194 catch(exception& e) {
195 cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function makeSim. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 cout << "An unknown error has occurred in the TreeMap class function makeSim. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204 /************************************************************/