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);
65 /************************************************************/
67 int TreeMap::getNumGroups() {
69 return namesOfGroups.size();
72 /************************************************************/
74 int TreeMap::getNumSeqs() {
76 return namesOfSeqs.size();
80 /************************************************************/
82 string TreeMap::getGroup(string sequenceName) {
84 it = treemap.find(sequenceName);
85 if (it != treemap.end()) { //sequence name was in group file
86 return it->second.groupname;
92 /************************************************************/
93 void TreeMap::setIndex(string seq, int index) {
94 it = treemap.find(seq);
95 if (it != treemap.end()) { //sequence name was in group file
96 treemap[seq].vectorIndex = index;
98 treemap[seq].vectorIndex = index;
99 treemap[seq].groupname = "not found";
102 /************************************************************/
103 int TreeMap::getIndex(string seq) {
105 it = treemap.find(seq);
106 // if it is a valid sequence name then return index
107 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
108 // if not return error code
112 /************************************************************/
114 void TreeMap::setNamesOfGroups(string seqGroup) {
117 for (i=0; i<namesOfGroups.size(); i++) {
118 if (namesOfGroups[i] != seqGroup) {
119 count++; //you have not found this group
121 break; //you already have it
124 if (count == namesOfGroups.size()) {
125 namesOfGroups.push_back(seqGroup); //new group
128 /************************************************************/
129 bool TreeMap::isValidGroup(string groupname) {
131 for (int i = 0; i < namesOfGroups.size(); i++) {
132 if (groupname == namesOfGroups[i]) { return true; }
137 catch(exception& e) {
138 m->errorOut(e, "TreeMap", "isValidGroup");
142 /***********************************************************************/
144 void TreeMap::print(ostream& output){
147 for(it = treemap.begin(); it != treemap.end(); it++){
148 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
151 catch(exception& e) {
152 m->errorOut(e, "TreeMap", "print");
157 /************************************************************/
158 void TreeMap::makeSim(GroupMap* groupmap) {
160 //set names of groups
161 namesOfGroups = groupmap->namesOfGroups;
163 //set names of seqs to names of groups
164 namesOfSeqs = groupmap->namesOfGroups;
166 // make map where key and value are both the group name since that what the tree.shared command wants
167 for (int i = 0; i < namesOfGroups.size(); i++) {
168 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
169 seqsPerGroup[namesOfGroups[i]] = 1;
172 numGroups = namesOfGroups.size();
175 catch(exception& e) {
176 m->errorOut(e, "TreeMap", "makeSim");
180 /************************************************************/
181 void TreeMap::makeSim(ListVector* list) {
183 //set names of groups
184 namesOfGroups.clear();
185 for(int i = 0; i < list->size(); i++) {
186 namesOfGroups.push_back(list->get(i));
189 //set names of seqs to names of groups
190 namesOfSeqs = namesOfGroups;
192 // make map where key and value are both the group name since that what the tree.shared command wants
193 for (int i = 0; i < namesOfGroups.size(); i++) {
194 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
195 seqsPerGroup[namesOfGroups[i]] = 1;
198 numGroups = namesOfGroups.size();
201 catch(exception& e) {
202 m->errorOut(e, "TreeMap", "makeSim");
207 /************************************************************/