5 * Created by Sarah Westcott on 1/26/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /************************************************************/
14 TreeMap::TreeMap(string filename) {
15 groupFileName = filename;
16 openInputFile(filename, fileHandle);
19 /************************************************************/
22 /************************************************************/
23 void TreeMap::readMap() {
24 string seqName, seqGroup;
27 fileHandle >> seqName; //read from first column
28 fileHandle >> seqGroup; //read from second column
30 namesOfSeqs.push_back(seqName);
31 setNamesOfGroups(seqGroup);
33 treemap[seqName].groupname = seqGroup; //store data in map
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]++;
46 /************************************************************/
47 void TreeMap::removeSeq(string seqName) {
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);
57 //decrement sequences in this group
58 string group = treemap[seqName].groupname;
59 seqsPerGroup[group]--;
61 //remove seq from treemap
62 it = treemap.find(seqName);
67 /************************************************************/
69 int TreeMap::getNumGroups() {
71 return namesOfGroups.size();
74 /************************************************************/
76 int TreeMap::getNumSeqs() {
78 return namesOfSeqs.size();
82 /************************************************************/
84 string TreeMap::getGroup(string sequenceName) {
86 it = treemap.find(sequenceName);
87 if (it != treemap.end()) { //sequence name was in group file
88 return it->second.groupname;
94 /************************************************************/
95 void TreeMap::setIndex(string seq, int index) {
96 it = treemap.find(seq);
97 if (it != treemap.end()) { //sequence name was in group file
98 treemap[seq].vectorIndex = index;
100 treemap[seq].vectorIndex = index;
101 treemap[seq].groupname = "not found";
104 /************************************************************/
105 int TreeMap::getIndex(string seq) {
107 it = treemap.find(seq);
108 // if it is a valid sequence name then return index
109 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
110 // if not return error code
114 /************************************************************/
116 void TreeMap::setNamesOfGroups(string seqGroup) {
119 for (i=0; i<namesOfGroups.size(); i++) {
120 if (namesOfGroups[i] != seqGroup) {
121 count++; //you have not found this group
123 break; //you already have it
126 if (count == namesOfGroups.size()) {
127 namesOfGroups.push_back(seqGroup); //new group
130 /************************************************************/
131 bool TreeMap::isValidGroup(string groupname) {
133 for (int i = 0; i < namesOfGroups.size(); i++) {
134 if (groupname == namesOfGroups[i]) { return true; }
139 catch(exception& e) {
140 errorOut(e, "TreeMap", "isValidGroup");
144 /***********************************************************************/
146 void TreeMap::print(ostream& output){
149 for(it = treemap.begin(); it != treemap.end(); it++){
150 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
153 catch(exception& e) {
154 errorOut(e, "TreeMap", "print");
159 /************************************************************/
160 void TreeMap::makeSim(GroupMap* groupmap) {
162 //set names of groups
163 namesOfGroups = groupmap->namesOfGroups;
165 //set names of seqs to names of groups
166 namesOfSeqs = groupmap->namesOfGroups;
168 // make map where key and value are both the group name since that what the tree.shared command wants
169 for (int i = 0; i < namesOfGroups.size(); i++) {
170 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
171 seqsPerGroup[namesOfGroups[i]] = 1;
174 numGroups = namesOfGroups.size();
177 catch(exception& e) {
178 errorOut(e, "TreeMap", "makeSim");
182 /************************************************************/
183 void TreeMap::makeSim(ListVector* list) {
185 //set names of groups
186 namesOfGroups.clear();
187 for(int i = 0; i < list->size(); i++) {
188 namesOfGroups.push_back(list->get(i));
191 //set names of seqs to names of groups
192 namesOfSeqs = namesOfGroups;
194 // make map where key and value are both the group name since that what the tree.shared command wants
195 for (int i = 0; i < namesOfGroups.size(); i++) {
196 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
197 seqsPerGroup[namesOfGroups[i]] = 1;
200 numGroups = namesOfGroups.size();
203 catch(exception& e) {
204 errorOut(e, "TreeMap", "makeSim");
209 /************************************************************/