]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
added logfile feature
[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                 errorOut(e, "TreeMap", "isValidGroup");
120                 exit(1);
121         }
122 }
123 /***********************************************************************/
124
125 void TreeMap::print(ostream& output){
126         try {
127                 
128                 for(it = treemap.begin(); it != treemap.end(); it++){
129                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
130                 }
131         }
132         catch(exception& e) {
133                 errorOut(e, "TreeMap", "print");
134                 exit(1);
135         }
136 }
137
138 /************************************************************/
139 void TreeMap::makeSim(GroupMap* groupmap) {
140         try {
141                 //set names of groups
142                 namesOfGroups = groupmap->namesOfGroups;
143                 
144                 //set names of seqs to names of groups
145                 namesOfSeqs = groupmap->namesOfGroups;
146                 
147                 // make map where key and value are both the group name since that what the tree.shared command wants
148                 for (int i = 0; i < namesOfGroups.size(); i++) {
149                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
150                         seqsPerGroup[namesOfGroups[i]] = 1;
151                 }
152                 
153                 numGroups = namesOfGroups.size();
154                 
155         }
156         catch(exception& e) {
157                 errorOut(e, "TreeMap", "makeSim");
158                 exit(1);
159         }
160 }
161 /************************************************************/
162 void TreeMap::makeSim(ListVector* list) {
163         try {
164                 //set names of groups
165                 namesOfGroups.clear();
166                 for(int i = 0; i < list->size(); i++) {
167                         namesOfGroups.push_back(list->get(i));
168                 }
169                 
170                 //set names of seqs to names of groups
171                 namesOfSeqs = namesOfGroups;
172                 
173                 // make map where key and value are both the group name since that what the tree.shared command wants
174                 for (int i = 0; i < namesOfGroups.size(); i++) {
175                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
176                         seqsPerGroup[namesOfGroups[i]] = 1;
177                 }
178                 
179                 numGroups = namesOfGroups.size();
180                 
181         }
182         catch(exception& e) {
183                 errorOut(e, "TreeMap", "makeSim");
184                 exit(1);
185         }
186 }
187
188 /************************************************************/
189