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