]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
adding treeclimber and unifrac pieces
[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 seqsPerGroup.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         treemap[seq].vectorIndex = index;
76 }
77 /************************************************************/
78 int TreeMap::getIndex(string seq) {
79         
80         it = treemap.find(seq);
81         // if it is a valid sequence name then return index
82         if (it != treemap.end()) { return treemap[seq].vectorIndex; }
83         // if not return error code
84         else { return -1; }
85         
86 }
87 /************************************************************/
88
89 void TreeMap::setNamesOfGroups(string seqGroup) {
90                         int i, count;
91                         count = 0;
92                         for (i=0; i<namesOfGroups.size(); i++) {
93                                 if (namesOfGroups[i] != seqGroup) {
94                                         count++; //you have not found this group
95                                 }else {
96                                         break; //you already have it
97                                 }
98                         }
99                         if (count == namesOfGroups.size()) {
100                                 namesOfGroups.push_back(seqGroup); //new group
101                         }
102 }
103
104 /***********************************************************************/
105
106 void TreeMap::print(ostream& output){
107         try {
108                 
109                 for(it = treemap.begin(); it != treemap.end(); it++){
110                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
111                 }
112         }
113         catch(exception& e) {
114                 cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
115                 exit(1);
116         }
117         catch(...) {
118                 cout << "An unknown error has occurred in the TreeMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
119                 exit(1);
120         }
121 }
122
123 /************************************************************/