]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
created mothurOut class to handle logfiles
[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 void TreeMap::removeSeq(string seqName) {
48         
49         //erase name from namesOfSeqs
50         for (int i = 0; i < namesOfSeqs.size(); i++) {
51                 if (namesOfSeqs[i] == seqName)  {
52                         namesOfSeqs.erase(namesOfSeqs.begin()+i);
53                         break;
54                 }
55         }
56         
57         //decrement sequences in this group
58         string group = treemap[seqName].groupname;
59         seqsPerGroup[group]--;
60         
61         //remove seq from treemap
62         it = treemap.find(seqName);
63         treemap.erase(it);
64 }
65 /************************************************************/
66
67 int TreeMap::getNumGroups() {
68                         
69         return namesOfGroups.size();    
70                 
71 }
72 /************************************************************/
73
74 int TreeMap::getNumSeqs() {
75                         
76         return namesOfSeqs.size();      
77                 
78 }
79
80 /************************************************************/
81
82 string TreeMap::getGroup(string sequenceName) {
83                         
84         it = treemap.find(sequenceName);
85         if (it != treemap.end()) { //sequence name was in group file
86                 return it->second.groupname;    
87         }else {
88                 return "not found";
89         }
90                 
91 }
92 /************************************************************/
93 void TreeMap::setIndex(string seq, int index) {
94         it = treemap.find(seq);
95         if (it != treemap.end()) { //sequence name was in group file
96                 treemap[seq].vectorIndex = index;       
97         }else {
98                 treemap[seq].vectorIndex = index;
99                 treemap[seq].groupname = "not found";
100         }
101 }
102 /************************************************************/
103 int TreeMap::getIndex(string seq) {
104         
105         it = treemap.find(seq);
106         // if it is a valid sequence name then return index
107         if (it != treemap.end()) { return treemap[seq].vectorIndex; }
108         // if not return error code
109         else { return -1; }
110         
111 }
112 /************************************************************/
113
114 void TreeMap::setNamesOfGroups(string seqGroup) {
115                         int i, count;
116                         count = 0;
117                         for (i=0; i<namesOfGroups.size(); i++) {
118                                 if (namesOfGroups[i] != seqGroup) {
119                                         count++; //you have not found this group
120                                 }else {
121                                         break; //you already have it
122                                 }
123                         }
124                         if (count == namesOfGroups.size()) {
125                                 namesOfGroups.push_back(seqGroup); //new group
126                         }
127 }
128 /************************************************************/
129 bool TreeMap::isValidGroup(string groupname) {
130         try {
131                 for (int i = 0; i < namesOfGroups.size(); i++) {
132                         if (groupname == namesOfGroups[i]) { return true; }
133                 }
134                 
135                 return false;
136         }
137         catch(exception& e) {
138                 m->errorOut(e, "TreeMap", "isValidGroup");
139                 exit(1);
140         }
141 }
142 /***********************************************************************/
143
144 void TreeMap::print(ostream& output){
145         try {
146                 
147                 for(it = treemap.begin(); it != treemap.end(); it++){
148                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
149                 }
150         }
151         catch(exception& e) {
152                 m->errorOut(e, "TreeMap", "print");
153                 exit(1);
154         }
155 }
156
157 /************************************************************/
158 void TreeMap::makeSim(GroupMap* groupmap) {
159         try {
160                 //set names of groups
161                 namesOfGroups = groupmap->namesOfGroups;
162                 
163                 //set names of seqs to names of groups
164                 namesOfSeqs = groupmap->namesOfGroups;
165                 
166                 // make map where key and value are both the group name since that what the tree.shared command wants
167                 for (int i = 0; i < namesOfGroups.size(); i++) {
168                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
169                         seqsPerGroup[namesOfGroups[i]] = 1;
170                 }
171                 
172                 numGroups = namesOfGroups.size();
173                 
174         }
175         catch(exception& e) {
176                 m->errorOut(e, "TreeMap", "makeSim");
177                 exit(1);
178         }
179 }
180 /************************************************************/
181 void TreeMap::makeSim(ListVector* list) {
182         try {
183                 //set names of groups
184                 namesOfGroups.clear();
185                 for(int i = 0; i < list->size(); i++) {
186                         namesOfGroups.push_back(list->get(i));
187                 }
188                 
189                 //set names of seqs to names of groups
190                 namesOfSeqs = namesOfGroups;
191                 
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;
196                 }
197                 
198                 numGroups = namesOfGroups.size();
199                 
200         }
201         catch(exception& e) {
202                 m->errorOut(e, "TreeMap", "makeSim");
203                 exit(1);
204         }
205 }
206
207 /************************************************************/
208