]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
fixed summary.shared bug and set jumble default to 1.
[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                         gobble(fileHandle);
36                 }
37                 fileHandle.close();
38 }
39 /************************************************************/
40
41 int TreeMap::getNumGroups() {
42                         
43         return namesOfGroups.size();    
44                 
45 }
46 /************************************************************/
47
48 int TreeMap::getNumSeqs() {
49                         
50         return namesOfSeqs.size();      
51                 
52 }
53
54 /************************************************************/
55
56 string TreeMap::getGroup(string sequenceName) {
57                         
58         it = treemap.find(sequenceName);
59         if (it != treemap.end()) { //sequence name was in group file
60                 return it->second.groupname;    
61         }else {
62                 return "not found";
63         }
64                 
65 }
66 /************************************************************/
67 void TreeMap::setIndex(string seq, int index) {
68         treemap[seq].vectorIndex = index;
69 }
70 /************************************************************/
71 int TreeMap::getIndex(string seq) {
72         
73         it = treemap.find(seq);
74         // if it is a valid sequence name then return index
75         if (it != treemap.end()) { return treemap[seq].vectorIndex; }
76         // if not return error code
77         else { return -1; }
78         
79 }
80 /************************************************************/
81
82 void TreeMap::setNamesOfGroups(string seqGroup) {
83                         int i, count;
84                         count = 0;
85                         for (i=0; i<namesOfGroups.size(); i++) {
86                                 if (namesOfGroups[i] != seqGroup) {
87                                         count++; //you have not found this group
88                                 }else {
89                                         break; //you already have it
90                                 }
91                         }
92                         if (count == namesOfGroups.size()) {
93                                 namesOfGroups.push_back(seqGroup); //new group
94                         }
95 }
96
97 /***********************************************************************/
98
99 void TreeMap::print(ostream& output){
100         try {
101                 
102                 for(it = treemap.begin(); it != treemap.end(); it++){
103                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
104                 }
105         }
106         catch(exception& e) {
107                 cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108                 exit(1);
109         }
110         catch(...) {
111                 cout << "An unknown error has occurred in the TreeMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112                 exit(1);
113         }
114 }
115
116 /************************************************************/