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 /************************************************************/
48 int TreeMap::getNumGroups() {
50 return namesOfGroups.size();
53 /************************************************************/
55 int TreeMap::getNumSeqs() {
57 return namesOfSeqs.size();
61 /************************************************************/
63 string TreeMap::getGroup(string sequenceName) {
65 it = treemap.find(sequenceName);
66 if (it != treemap.end()) { //sequence name was in group file
67 return it->second.groupname;
73 /************************************************************/
74 void TreeMap::setIndex(string seq, int index) {
75 it = treemap.find(seq);
76 if (it != treemap.end()) { //sequence name was in group file
77 treemap[seq].vectorIndex = index;
79 treemap[seq].vectorIndex = index;
80 treemap[seq].groupname = "not found";
83 /************************************************************/
84 int TreeMap::getIndex(string seq) {
86 it = treemap.find(seq);
87 // if it is a valid sequence name then return index
88 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
89 // if not return error code
93 /************************************************************/
95 void TreeMap::setNamesOfGroups(string seqGroup) {
98 for (i=0; i<namesOfGroups.size(); i++) {
99 if (namesOfGroups[i] != seqGroup) {
100 count++; //you have not found this group
102 break; //you already have it
105 if (count == namesOfGroups.size()) {
106 namesOfGroups.push_back(seqGroup); //new group
109 /************************************************************/
110 bool TreeMap::isValidGroup(string groupname) {
112 for (int i = 0; i < namesOfGroups.size(); i++) {
113 if (groupname == namesOfGroups[i]) { return true; }
118 catch(exception& e) {
119 errorOut(e, "TreeMap", "isValidGroup");
123 /***********************************************************************/
125 void TreeMap::print(ostream& output){
128 for(it = treemap.begin(); it != treemap.end(); it++){
129 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
132 catch(exception& e) {
133 errorOut(e, "TreeMap", "print");
138 /************************************************************/
139 void TreeMap::makeSim(GroupMap* groupmap) {
141 //set names of groups
142 namesOfGroups = groupmap->namesOfGroups;
144 //set names of seqs to names of groups
145 namesOfSeqs = groupmap->namesOfGroups;
147 // make map where key and value are both the group name since that what the tree.shared command wants
148 for (int i = 0; i < namesOfGroups.size(); i++) {
149 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
150 seqsPerGroup[namesOfGroups[i]] = 1;
153 numGroups = namesOfGroups.size();
156 catch(exception& e) {
157 errorOut(e, "TreeMap", "makeSim");
161 /************************************************************/
162 void TreeMap::makeSim(ListVector* list) {
164 //set names of groups
165 namesOfGroups.clear();
166 for(int i = 0; i < list->size(); i++) {
167 namesOfGroups.push_back(list->get(i));
170 //set names of seqs to names of groups
171 namesOfSeqs = namesOfGroups;
173 // make map where key and value are both the group name since that what the tree.shared command wants
174 for (int i = 0; i < namesOfGroups.size(); i++) {
175 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
176 seqsPerGroup[namesOfGroups[i]] = 1;
179 numGroups = namesOfGroups.size();
182 catch(exception& e) {
183 errorOut(e, "TreeMap", "makeSim");
188 /************************************************************/