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 m = MothurOut::getInstance();
16 groupFileName = filename;
17 m->openInputFile(filename, fileHandle);
20 /************************************************************/
22 /************************************************************/
23 int TreeMap::readMap(string gf) {
26 m->openInputFile(gf, fileHandle);
28 string seqName, seqGroup;
32 fileHandle >> seqName; m->gobble(fileHandle); //read from first column
33 fileHandle >> seqGroup; //read from second column
35 if (m->control_pressed) { fileHandle.close(); return 1; }
37 setNamesOfGroups(seqGroup);
39 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
40 if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); }
42 namesOfSeqs.push_back(seqName);
43 treemap[seqName].groupname = seqGroup; //store data in map
45 it2 = seqsPerGroup.find(seqGroup);
46 if (it2 == seqsPerGroup.end()) { //if it's a new group
47 seqsPerGroup[seqGroup] = 1;
48 }else {//it's a group we already have
49 seqsPerGroup[seqGroup]++;
53 m->gobble(fileHandle);
60 /************************************************************/
61 int TreeMap::readMap() {
62 string seqName, seqGroup;
66 fileHandle >> seqName; m->gobble(fileHandle); //read from first column
67 fileHandle >> seqGroup; //read from second column
69 if (m->control_pressed) { fileHandle.close(); return 1; }
71 setNamesOfGroups(seqGroup);
73 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
74 if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); }
76 namesOfSeqs.push_back(seqName);
77 treemap[seqName].groupname = seqGroup; //store data in map
79 it2 = seqsPerGroup.find(seqGroup);
80 if (it2 == seqsPerGroup.end()) { //if it's a new group
81 seqsPerGroup[seqGroup] = 1;
82 }else {//it's a group we already have
83 seqsPerGroup[seqGroup]++;
87 m->gobble(fileHandle);
94 /************************************************************/
95 void TreeMap::addSeq(string seqName, string seqGroup) {
97 namesOfSeqs.push_back(seqName);
98 setNamesOfGroups(seqGroup);
100 treemap[seqName].groupname = seqGroup; //store data in map
102 it2 = seqsPerGroup.find(seqGroup);
103 if (it2 == seqsPerGroup.end()) { //if it's a new group
104 seqsPerGroup[seqGroup] = 1;
105 }else {//it's a group we already have
106 seqsPerGroup[seqGroup]++;
109 /************************************************************/
110 void TreeMap::removeSeq(string seqName) {
112 //erase name from namesOfSeqs
113 for (int i = 0; i < namesOfSeqs.size(); i++) {
114 if (namesOfSeqs[i] == seqName) {
115 namesOfSeqs.erase(namesOfSeqs.begin()+i);
120 //decrement sequences in this group
121 string group = treemap[seqName].groupname;
122 seqsPerGroup[group]--;
124 //remove seq from treemap
125 it = treemap.find(seqName);
128 /************************************************************/
130 int TreeMap::getNumGroups() {
132 return namesOfGroups.size();
135 /************************************************************/
137 int TreeMap::getNumSeqs() {
139 return namesOfSeqs.size();
143 /************************************************************/
145 string TreeMap::getGroup(string sequenceName) {
147 it = treemap.find(sequenceName);
148 if (it != treemap.end()) { //sequence name was in group file
149 return it->second.groupname;
155 /************************************************************/
156 void TreeMap::setIndex(string seq, int index) {
157 it = treemap.find(seq);
158 if (it != treemap.end()) { //sequence name was in group file
159 treemap[seq].vectorIndex = index;
161 treemap[seq].vectorIndex = index;
162 treemap[seq].groupname = "not found";
165 /************************************************************/
166 int TreeMap::getIndex(string seq) {
168 it = treemap.find(seq);
169 // if it is a valid sequence name then return index
170 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
171 // if not return error code
175 /************************************************************/
177 void TreeMap::setNamesOfGroups(string seqGroup) {
180 for (i=0; i<namesOfGroups.size(); i++) {
181 if (namesOfGroups[i] != seqGroup) {
182 count++; //you have not found this group
184 break; //you already have it
187 if (count == namesOfGroups.size()) {
188 namesOfGroups.push_back(seqGroup); //new group
191 /************************************************************/
192 bool TreeMap::isValidGroup(string groupname) {
194 for (int i = 0; i < namesOfGroups.size(); i++) {
195 if (groupname == namesOfGroups[i]) { return true; }
200 catch(exception& e) {
201 m->errorOut(e, "TreeMap", "isValidGroup");
205 /***********************************************************************/
207 void TreeMap::print(ostream& output){
210 for(it = treemap.begin(); it != treemap.end(); it++){
211 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
214 catch(exception& e) {
215 m->errorOut(e, "TreeMap", "print");
220 /************************************************************/
221 void TreeMap::makeSim(vector<string> ThisnamesOfGroups) {
223 //set names of groups
224 namesOfGroups = ThisnamesOfGroups;
226 //set names of seqs to names of groups
227 namesOfSeqs = ThisnamesOfGroups;
229 // make map where key and value are both the group name since that what the tree.shared command wants
230 for (int i = 0; i < namesOfGroups.size(); i++) {
231 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
232 seqsPerGroup[namesOfGroups[i]] = 1;
235 numGroups = namesOfGroups.size();
238 catch(exception& e) {
239 m->errorOut(e, "TreeMap", "makeSim");
243 /************************************************************/
244 void TreeMap::makeSim(ListVector* list) {
246 //set names of groups
247 namesOfGroups.clear();
248 for(int i = 0; i < list->size(); i++) {
249 namesOfGroups.push_back(list->get(i));
252 //set names of seqs to names of groups
253 namesOfSeqs = namesOfGroups;
255 // make map where key and value are both the group name since that what the tree.shared command wants
256 for (int i = 0; i < namesOfGroups.size(); i++) {
257 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
258 seqsPerGroup[namesOfGroups[i]] = 1;
261 numGroups = namesOfGroups.size();
264 catch(exception& e) {
265 m->errorOut(e, "TreeMap", "makeSim");
269 /************************************************************/
270 int TreeMap::getCopy(TreeMap& copy){
273 namesOfGroups = copy.getNamesOfGroups();
274 numGroups = copy.getNumGroups();
275 namesOfSeqs = copy.namesOfSeqs;
276 seqsPerGroup = copy.seqsPerGroup;
277 treemap = copy.treemap;
281 catch(exception& e) {
282 m->errorOut(e, "TreeMap", "getCopy");
286 /************************************************************/
287 vector<string> TreeMap::getNamesSeqs(){
290 vector<string> names;
292 for(it = treemap.begin(); it != treemap.end(); it++){
293 names.push_back(it->first);
298 catch(exception& e) {
299 m->errorOut(e, "TreeMap", "getNamesSeqs");
303 /************************************************************/
304 vector<string> TreeMap::getNamesSeqs(vector<string> picked){
307 vector<string> names;
309 for(it = treemap.begin(); it != treemap.end(); it++){
310 //if you are belong to one the the groups in the picked vector add you
311 if (m->inUsersGroups(it->second.groupname, picked)) {
312 names.push_back(it->first);
318 catch(exception& e) {
319 m->errorOut(e, "TreeMap", "getNamesSeqs");
324 /************************************************************/