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 m->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]++;
42 m->gobble(fileHandle);
46 /************************************************************/
47 void TreeMap::addSeq(string seqName, string seqGroup) {
49 namesOfSeqs.push_back(seqName);
50 setNamesOfGroups(seqGroup);
52 treemap[seqName].groupname = seqGroup; //store data in map
54 it2 = seqsPerGroup.find(seqGroup);
55 if (it2 == seqsPerGroup.end()) { //if it's a new group
56 seqsPerGroup[seqGroup] = 1;
57 }else {//it's a group we already have
58 seqsPerGroup[seqGroup]++;
61 /************************************************************/
62 void TreeMap::removeSeq(string seqName) {
64 //erase name from namesOfSeqs
65 for (int i = 0; i < namesOfSeqs.size(); i++) {
66 if (namesOfSeqs[i] == seqName) {
67 namesOfSeqs.erase(namesOfSeqs.begin()+i);
72 //decrement sequences in this group
73 string group = treemap[seqName].groupname;
74 seqsPerGroup[group]--;
76 //remove seq from treemap
77 it = treemap.find(seqName);
80 /************************************************************/
82 int TreeMap::getNumGroups() {
84 return namesOfGroups.size();
87 /************************************************************/
89 int TreeMap::getNumSeqs() {
91 return namesOfSeqs.size();
95 /************************************************************/
97 string TreeMap::getGroup(string sequenceName) {
99 it = treemap.find(sequenceName);
100 if (it != treemap.end()) { //sequence name was in group file
101 return it->second.groupname;
107 /************************************************************/
108 void TreeMap::setIndex(string seq, int index) {
109 it = treemap.find(seq);
110 if (it != treemap.end()) { //sequence name was in group file
111 treemap[seq].vectorIndex = index;
113 treemap[seq].vectorIndex = index;
114 treemap[seq].groupname = "not found";
117 /************************************************************/
118 int TreeMap::getIndex(string seq) {
120 it = treemap.find(seq);
121 // if it is a valid sequence name then return index
122 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
123 // if not return error code
127 /************************************************************/
129 void TreeMap::setNamesOfGroups(string seqGroup) {
132 for (i=0; i<namesOfGroups.size(); i++) {
133 if (namesOfGroups[i] != seqGroup) {
134 count++; //you have not found this group
136 break; //you already have it
139 if (count == namesOfGroups.size()) {
140 namesOfGroups.push_back(seqGroup); //new group
143 /************************************************************/
144 bool TreeMap::isValidGroup(string groupname) {
146 for (int i = 0; i < namesOfGroups.size(); i++) {
147 if (groupname == namesOfGroups[i]) { return true; }
152 catch(exception& e) {
153 m->errorOut(e, "TreeMap", "isValidGroup");
157 /***********************************************************************/
159 void TreeMap::print(ostream& output){
162 for(it = treemap.begin(); it != treemap.end(); it++){
163 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
166 catch(exception& e) {
167 m->errorOut(e, "TreeMap", "print");
172 /************************************************************/
173 void TreeMap::makeSim(GroupMap* groupmap) {
175 //set names of groups
176 namesOfGroups = groupmap->namesOfGroups;
178 //set names of seqs to names of groups
179 namesOfSeqs = groupmap->namesOfGroups;
181 // make map where key and value are both the group name since that what the tree.shared command wants
182 for (int i = 0; i < namesOfGroups.size(); i++) {
183 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
184 seqsPerGroup[namesOfGroups[i]] = 1;
187 numGroups = namesOfGroups.size();
190 catch(exception& e) {
191 m->errorOut(e, "TreeMap", "makeSim");
195 /************************************************************/
196 void TreeMap::makeSim(ListVector* list) {
198 //set names of groups
199 namesOfGroups.clear();
200 for(int i = 0; i < list->size(); i++) {
201 namesOfGroups.push_back(list->get(i));
204 //set names of seqs to names of groups
205 namesOfSeqs = namesOfGroups;
207 // make map where key and value are both the group name since that what the tree.shared command wants
208 for (int i = 0; i < namesOfGroups.size(); i++) {
209 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
210 seqsPerGroup[namesOfGroups[i]] = 1;
213 numGroups = namesOfGroups.size();
216 catch(exception& e) {
217 m->errorOut(e, "TreeMap", "makeSim");
222 /************************************************************/