5 * Created by Sarah Westcott on 12/1/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
12 /************************************************************/
14 GroupMap::GroupMap(string filename) {
15 m = MothurOut::getInstance();
16 groupFileName = filename;
17 m->openInputFile(filename, fileHandle);
21 /************************************************************/
22 GroupMap::~GroupMap(){}
24 /************************************************************/
25 int GroupMap::readMap() {
26 string seqName, seqGroup;
30 fileHandle >> seqName; m->gobble(fileHandle); //read from first column
31 fileHandle >> seqGroup; //read from second column
33 if (m->control_pressed) { fileHandle.close(); return 1; }
35 setNamesOfGroups(seqGroup);
37 it = groupmap.find(seqName);
39 if (it != groupmap.end()) { error = 1; m->mothurOut("Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); }
41 groupmap[seqName] = seqGroup; //store data in map
42 seqsPerGroup[seqGroup]++; //increment number of seqs in that group
44 m->gobble(fileHandle);
47 m->setAllGroups(namesOfGroups);
50 /************************************************************/
51 int GroupMap::readDesignMap() {
52 string seqName, seqGroup;
56 fileHandle >> seqName; m->gobble(fileHandle); //read from first column
57 fileHandle >> seqGroup; //read from second column
59 if (m->control_pressed) { fileHandle.close(); return 1; }
61 setNamesOfGroups(seqGroup);
63 it = groupmap.find(seqName);
65 if (it != groupmap.end()) { error = 1; m->mothurOut("Your designfile contains more than 1 group named " + seqName + ", group names must be unique. Please correct."); m->mothurOutEndLine(); }
67 groupmap[seqName] = seqGroup; //store data in map
68 seqsPerGroup[seqGroup]++; //increment number of seqs in that group
70 m->gobble(fileHandle);
73 m->setAllGroups(namesOfGroups);
76 /************************************************************/
77 int GroupMap::readDesignMap(string filename) {
78 groupFileName = filename;
79 m->openInputFile(filename, fileHandle);
81 string seqName, seqGroup;
85 fileHandle >> seqName; m->gobble(fileHandle); //read from first column
86 fileHandle >> seqGroup; //read from second column
88 if (m->control_pressed) { fileHandle.close(); return 1; }
90 setNamesOfGroups(seqGroup);
92 it = groupmap.find(seqName);
94 if (it != groupmap.end()) { error = 1; m->mothurOut("Your designfile contains more than 1 group named " + seqName + ", group names must be unique. Please correct."); m->mothurOutEndLine(); }
96 groupmap[seqName] = seqGroup; //store data in map
97 seqsPerGroup[seqGroup]++; //increment number of seqs in that group
99 m->gobble(fileHandle);
102 m->setAllGroups(namesOfGroups);
105 /************************************************************/
106 int GroupMap::getNumGroups() { return namesOfGroups.size(); }
107 /************************************************************/
109 string GroupMap::getGroup(string sequenceName) {
111 it = groupmap.find(sequenceName);
112 if (it != groupmap.end()) { //sequence name was in group file
119 /************************************************************/
121 void GroupMap::setGroup(string sequenceName, string groupN) {
122 setNamesOfGroups(groupN);
124 it = groupmap.find(sequenceName);
126 if (it != groupmap.end()) { m->mothurOut("Your groupfile contains more than 1 sequence named " + sequenceName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); }
128 groupmap[sequenceName] = groupN; //store data in map
129 seqsPerGroup[groupN]++; //increment number of seqs in that group
133 /************************************************************/
134 void GroupMap::setNamesOfGroups(string seqGroup) {
137 for (i=0; i<namesOfGroups.size(); i++) {
138 if (namesOfGroups[i] != seqGroup) {
139 count++; //you have not found this group
141 break; //you already have it
144 if (count == namesOfGroups.size()) {
145 namesOfGroups.push_back(seqGroup); //new group
146 seqsPerGroup[seqGroup] = 0;
147 groupIndex[seqGroup] = index;
151 /************************************************************/
152 bool GroupMap::isValidGroup(string groupname) {
154 for (int i = 0; i < namesOfGroups.size(); i++) {
155 if (groupname == namesOfGroups[i]) { return true; }
160 catch(exception& e) {
161 m->errorOut(e, "GroupMap", "isValidGroup");
165 /************************************************************/
166 int GroupMap::getNumSeqs(string group) {
169 map<string, int>::iterator itNum;
171 itNum = seqsPerGroup.find(group);
173 if (itNum == seqsPerGroup.end()) { return 0; }
175 return seqsPerGroup[group];
178 catch(exception& e) {
179 m->errorOut(e, "GroupMap", "getNumSeqs");
184 /************************************************************/
185 vector<string> GroupMap::getNamesSeqs(){
188 vector<string> names;
190 for (it = groupmap.begin(); it != groupmap.end(); it++) {
191 names.push_back(it->first);
196 catch(exception& e) {
197 m->errorOut(e, "GroupMap", "getNamesSeqs");
201 /************************************************************/
202 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
205 vector<string> names;
207 for (it = groupmap.begin(); it != groupmap.end(); it++) {
208 //if you are belong to one the the groups in the picked vector add you
209 if (m->inUsersGroups(it->second, picked)) {
210 names.push_back(it->first);
216 catch(exception& e) {
217 m->errorOut(e, "GroupMap", "getNamesSeqs");
222 /************************************************************/