]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
added ability for user to select which groups to analyze with the collect.shared...
[mothur.git] / groupmap.cpp
1 /*
2  *  groupmap.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/1/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "groupmap.h"
11
12 /************************************************************/
13
14  GroupMap::GroupMap(string filename) {
15         groupFileName = filename;
16         openInputFile(filename, fileHandle);
17         index = 0;
18 }
19
20 /************************************************************/
21  GroupMap::~GroupMap(){};
22
23 /************************************************************/
24 void GroupMap::readMap() {
25                 string seqName, seqGroup;
26         
27                 while(fileHandle){
28                         fileHandle >> seqName;                  //read from first column
29                         fileHandle >> seqGroup;                 //read from second column
30                         
31                         setNamesOfGroups(seqGroup);
32                                                 
33                         groupmap[seqName] = seqGroup;   //store data in map
34                 
35                         gobble(fileHandle);
36                 }
37                 fileHandle.close();
38 }
39 /************************************************************/
40 int GroupMap::getNumGroups() { return namesOfGroups.size();     }
41 /************************************************************/
42
43 string GroupMap::getGroup(string sequenceName) {
44                         
45         it = groupmap.find(sequenceName);
46         if (it != groupmap.end()) { //sequence name was in group file
47                 return it->second;      
48         }else {
49                 return "not found";
50         }
51 }
52
53 /************************************************************/
54 void GroupMap::setGroup(string sequenceName, string groupN) {
55         groupmap[sequenceName] = groupN;
56 }
57 /************************************************************/
58 void GroupMap::setNamesOfGroups(string seqGroup) {
59                         int i, count;
60                         count = 0;
61                         for (i=0; i<namesOfGroups.size(); i++) {
62                                 if (namesOfGroups[i] != seqGroup) {
63                                         count++; //you have not found this group
64                                 }else {
65                                         break; //you already have it
66                                 }
67                         }
68                         if (count == namesOfGroups.size()) {
69                                 namesOfGroups.push_back(seqGroup); //new group
70                                 groupIndex[seqGroup] = index;
71                                 index++;
72                         }
73 }
74 /************************************************************/
75 bool GroupMap::isValidGroup(string groupname) {
76         try {
77                 for (int i = 0; i < namesOfGroups.size(); i++) {
78                         if (groupname == namesOfGroups[i]) { return true; }
79                 }
80                 
81                 return false;
82         }
83         catch(exception& e) {
84                 cout << "Standard Error: " << e.what() << " has occurred in the GroupMap class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85                 exit(1);
86         }
87         catch(...) {
88                 cout << "An unknown error has occurred in the GroupMap class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
89                 exit(1);
90         }
91 }