]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
fixed some bugs
[mothur.git] / treemap.cpp
1 /*
2  *  treemap.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/26/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "treemap.h"
11
12 /************************************************************/
13
14  TreeMap::TreeMap(string filename) {
15         groupFileName = filename;
16         openInputFile(filename, fileHandle);
17 }
18
19 /************************************************************/
20  TreeMap::~TreeMap(){}
21
22 /************************************************************/
23 void TreeMap::readMap() {
24                 string seqName, seqGroup;
25         
26                 while(fileHandle){
27                         fileHandle >> seqName;                  //read from first column
28                         fileHandle >> seqGroup;                 //read from second column
29                         
30                         namesOfSeqs.push_back(seqName);
31                         setNamesOfGroups(seqGroup);
32                                                 
33                         treemap[seqName].groupname = seqGroup;  //store data in map
34                         
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]++;
40                         }
41
42                         gobble(fileHandle);
43                 }
44                 fileHandle.close();
45 }
46 /************************************************************/
47
48 int TreeMap::getNumGroups() {
49                         
50         return namesOfGroups.size();    
51                 
52 }
53 /************************************************************/
54
55 int TreeMap::getNumSeqs() {
56                         
57         return namesOfSeqs.size();      
58                 
59 }
60
61 /************************************************************/
62
63 string TreeMap::getGroup(string sequenceName) {
64                         
65         it = treemap.find(sequenceName);
66         if (it != treemap.end()) { //sequence name was in group file
67                 return it->second.groupname;    
68         }else {
69                 return "not found";
70         }
71                 
72 }
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;       
78         }else {
79                 treemap[seq].vectorIndex = index;
80                 treemap[seq].groupname = "not found";
81         }
82 }
83 /************************************************************/
84 int TreeMap::getIndex(string seq) {
85         
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
90         else { return -1; }
91         
92 }
93 /************************************************************/
94
95 void TreeMap::setNamesOfGroups(string seqGroup) {
96                         int i, count;
97                         count = 0;
98                         for (i=0; i<namesOfGroups.size(); i++) {
99                                 if (namesOfGroups[i] != seqGroup) {
100                                         count++; //you have not found this group
101                                 }else {
102                                         break; //you already have it
103                                 }
104                         }
105                         if (count == namesOfGroups.size()) {
106                                 namesOfGroups.push_back(seqGroup); //new group
107                         }
108 }
109 /************************************************************/
110 bool TreeMap::isValidGroup(string groupname) {
111         try {
112                 for (int i = 0; i < namesOfGroups.size(); i++) {
113                         if (groupname == namesOfGroups[i]) { return true; }
114                 }
115                 
116                 return false;
117         }
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";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the TreeMap class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }
126 }
127 /***********************************************************************/
128
129 void TreeMap::print(ostream& output){
130         try {
131                 
132                 for(it = treemap.begin(); it != treemap.end(); it++){
133                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
134                 }
135         }
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";
138                 exit(1);
139         }
140         catch(...) {
141                 cout << "An unknown error has occurred in the TreeMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142                 exit(1);
143         }
144 }
145
146 /************************************************************/
147 void TreeMap::makeSim(GroupMap* groupmap) {
148         try {
149                 //set names of groups
150                 namesOfGroups = groupmap->namesOfGroups;
151                 
152                 //set names of seqs to names of groups
153                 namesOfSeqs = groupmap->namesOfGroups;
154                 
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;
159                 }
160                 
161                 numGroups = namesOfGroups.size();
162                 
163         }
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";
166                 exit(1);
167         }
168         catch(...) {
169                 cout << "An unknown error has occurred in the TreeMap class function makeSim. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170                 exit(1);
171         }
172 }
173 /************************************************************/
174 void TreeMap::makeSim(ListVector* list) {
175         try {
176                 //set names of groups
177                 namesOfGroups.clear();
178                 for(int i = 0; i < list->size(); i++) {
179                         namesOfGroups.push_back(list->get(i));
180                 }
181                 
182                 //set names of seqs to names of groups
183                 namesOfSeqs = namesOfGroups;
184                 
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;
189                 }
190                 
191                 numGroups = namesOfGroups.size();
192                 
193         }
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";
196                 exit(1);
197         }
198         catch(...) {
199                 cout << "An unknown error has occurred in the TreeMap class function makeSim. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }
202 }
203
204 /************************************************************/
205