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();
17 m->openOutputFileAppend(filename, out2);
18 out2 << endl; out2.close();
19 groupFileName = filename;
20 m->openInputFile(filename, fileHandle);
23 /************************************************************/
25 /************************************************************/
26 int TreeMap::readMap(string gf) {
29 m->openOutputFileAppend(gf, out2);
30 out2 << endl; out2.close();
33 m->openInputFile(gf, fileHandle);
35 string seqName, seqGroup;
40 bool pairDone = false;
41 bool columnOne = true;
43 while (!fileHandle.eof()) {
44 if (m->control_pressed) { fileHandle.close(); return 1; }
46 fileHandle.read(buffer, 4096);
47 vector<string> pieces = m->splitWhiteSpace(rest, buffer, fileHandle.gcount());
49 for (int i = 0; i < pieces.size(); i++) {
50 if (columnOne) { seqName = pieces[i]; columnOne=false; }
51 else { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
54 setNamesOfGroups(seqGroup);
56 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
57 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(); }
59 namesOfSeqs.push_back(seqName);
60 treemap[seqName].groupname = seqGroup; //store data in map
62 it2 = seqsPerGroup.find(seqGroup);
63 if (it2 == seqsPerGroup.end()) { //if it's a new group
64 seqsPerGroup[seqGroup] = 1;
65 }else {//it's a group we already have
66 seqsPerGroup[seqGroup]++;
76 vector<string> pieces = m->splitWhiteSpace(rest);
78 for (int i = 0; i < pieces.size(); i++) {
79 if (columnOne) { seqName = pieces[i]; columnOne=false; }
80 else { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
83 setNamesOfGroups(seqGroup);
85 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
86 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(); }
88 namesOfSeqs.push_back(seqName);
89 treemap[seqName].groupname = seqGroup; //store data in map
91 it2 = seqsPerGroup.find(seqGroup);
92 if (it2 == seqsPerGroup.end()) { //if it's a new group
93 seqsPerGroup[seqGroup] = 1;
94 }else {//it's a group we already have
95 seqsPerGroup[seqGroup]++;
105 catch(exception& e) {
106 m->errorOut(e, "TreeMap", "readMap");
111 /************************************************************/
112 int TreeMap::readMap() {
114 string seqName, seqGroup;
119 bool pairDone = false;
120 bool columnOne = true;
122 while (!fileHandle.eof()) {
123 if (m->control_pressed) { fileHandle.close(); return 1; }
125 fileHandle.read(buffer, 4096);
126 vector<string> pieces = m->splitWhiteSpace(rest, buffer, fileHandle.gcount());
128 for (int i = 0; i < pieces.size(); i++) {
129 if (columnOne) { seqName = pieces[i]; columnOne=false; }
130 else { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
133 setNamesOfGroups(seqGroup);
135 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
136 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(); }
138 namesOfSeqs.push_back(seqName);
139 treemap[seqName].groupname = seqGroup; //store data in map
141 it2 = seqsPerGroup.find(seqGroup);
142 if (it2 == seqsPerGroup.end()) { //if it's a new group
143 seqsPerGroup[seqGroup] = 1;
144 }else {//it's a group we already have
145 seqsPerGroup[seqGroup]++;
155 vector<string> pieces = m->splitWhiteSpace(rest);
157 for (int i = 0; i < pieces.size(); i++) {
158 if (columnOne) { seqName = pieces[i]; columnOne=false; }
159 else { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
162 setNamesOfGroups(seqGroup);
164 map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
165 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(); }
167 namesOfSeqs.push_back(seqName);
168 treemap[seqName].groupname = seqGroup; //store data in map
170 it2 = seqsPerGroup.find(seqGroup);
171 if (it2 == seqsPerGroup.end()) { //if it's a new group
172 seqsPerGroup[seqGroup] = 1;
173 }else {//it's a group we already have
174 seqsPerGroup[seqGroup]++;
184 catch(exception& e) {
185 m->errorOut(e, "TreeMap", "readMap");
189 /************************************************************/
190 void TreeMap::addSeq(string seqName, string seqGroup) {
192 namesOfSeqs.push_back(seqName);
193 setNamesOfGroups(seqGroup);
195 treemap[seqName].groupname = seqGroup; //store data in map
197 it2 = seqsPerGroup.find(seqGroup);
198 if (it2 == seqsPerGroup.end()) { //if it's a new group
199 seqsPerGroup[seqGroup] = 1;
200 }else {//it's a group we already have
201 seqsPerGroup[seqGroup]++;
204 /************************************************************/
205 void TreeMap::removeSeq(string seqName) {
207 //erase name from namesOfSeqs
208 for (int i = 0; i < namesOfSeqs.size(); i++) {
209 if (namesOfSeqs[i] == seqName) {
210 namesOfSeqs.erase(namesOfSeqs.begin()+i);
215 //decrement sequences in this group
216 string group = treemap[seqName].groupname;
217 seqsPerGroup[group]--;
219 //remove seq from treemap
220 it = treemap.find(seqName);
223 /************************************************************/
225 int TreeMap::getNumGroups() {
227 return namesOfGroups.size();
230 /************************************************************/
232 int TreeMap::getNumSeqs() {
234 return namesOfSeqs.size();
238 /************************************************************/
240 string TreeMap::getGroup(string sequenceName) {
242 it = treemap.find(sequenceName);
243 if (it != treemap.end()) { //sequence name was in group file
244 return it->second.groupname;
250 /************************************************************
251 void TreeMap::setIndex(string seq, int index) {
252 it = treemap.find(seq);
253 if (it != treemap.end()) { //sequence name was in group file
254 treemap[seq].vectorIndex = index;
256 treemap[seq].vectorIndex = index;
257 treemap[seq].groupname = "not found";
260 /************************************************************
261 int TreeMap::getIndex(string seq) {
263 it = treemap.find(seq);
264 // if it is a valid sequence name then return index
265 if (it != treemap.end()) { return treemap[seq].vectorIndex; }
266 // if not return error code
270 /************************************************************/
272 void TreeMap::setNamesOfGroups(string seqGroup) {
275 for (i=0; i<namesOfGroups.size(); i++) {
276 if (namesOfGroups[i] != seqGroup) {
277 count++; //you have not found this group
279 break; //you already have it
282 if (count == namesOfGroups.size()) {
283 namesOfGroups.push_back(seqGroup); //new group
286 /************************************************************/
287 bool TreeMap::isValidGroup(string groupname) {
289 for (int i = 0; i < namesOfGroups.size(); i++) {
290 if (groupname == namesOfGroups[i]) { return true; }
295 catch(exception& e) {
296 m->errorOut(e, "TreeMap", "isValidGroup");
300 /***********************************************************************/
302 void TreeMap::print(ostream& output){
305 for(it = treemap.begin(); it != treemap.end(); it++){
306 output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
309 catch(exception& e) {
310 m->errorOut(e, "TreeMap", "print");
315 /************************************************************/
316 void TreeMap::makeSim(vector<string> ThisnamesOfGroups) {
318 //set names of groups
319 namesOfGroups = ThisnamesOfGroups;
321 //set names of seqs to names of groups
322 namesOfSeqs = ThisnamesOfGroups;
324 // make map where key and value are both the group name since that what the tree.shared command wants
325 for (int i = 0; i < namesOfGroups.size(); i++) {
326 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
327 seqsPerGroup[namesOfGroups[i]] = 1;
330 numGroups = namesOfGroups.size();
333 catch(exception& e) {
334 m->errorOut(e, "TreeMap", "makeSim");
338 /************************************************************/
339 void TreeMap::makeSim(ListVector* list) {
341 //set names of groups
342 namesOfGroups.clear();
343 for(int i = 0; i < list->size(); i++) {
344 namesOfGroups.push_back(list->get(i));
347 //set names of seqs to names of groups
348 namesOfSeqs = namesOfGroups;
350 // make map where key and value are both the group name since that what the tree.shared command wants
351 for (int i = 0; i < namesOfGroups.size(); i++) {
352 treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
353 seqsPerGroup[namesOfGroups[i]] = 1;
356 numGroups = namesOfGroups.size();
359 catch(exception& e) {
360 m->errorOut(e, "TreeMap", "makeSim");
364 /************************************************************/
365 int TreeMap::getCopy(TreeMap& copy){
368 namesOfGroups = copy.getNamesOfGroups();
369 numGroups = copy.getNumGroups();
370 namesOfSeqs = copy.namesOfSeqs;
371 seqsPerGroup = copy.seqsPerGroup;
372 treemap = copy.treemap;
376 catch(exception& e) {
377 m->errorOut(e, "TreeMap", "getCopy");
381 /************************************************************/
382 vector<string> TreeMap::getNamesSeqs(){
385 vector<string> names;
387 for(it = treemap.begin(); it != treemap.end(); it++){
388 names.push_back(it->first);
393 catch(exception& e) {
394 m->errorOut(e, "TreeMap", "getNamesSeqs");
398 /************************************************************/
399 vector<string> TreeMap::getNamesSeqs(vector<string> picked){
402 vector<string> names;
404 for(it = treemap.begin(); it != treemap.end(); it++){
405 //if you are belong to one the the groups in the picked vector add you
406 if (m->inUsersGroups(it->second.groupname, picked)) {
407 names.push_back(it->first);
413 catch(exception& e) {
414 m->errorOut(e, "TreeMap", "getNamesSeqs");
419 /************************************************************/