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);
77 /************************************************************/
78 int GroupMap::getNumGroups() { return namesOfGroups.size(); }
79 /************************************************************/
81 string GroupMap::getGroup(string sequenceName) {
83 it = groupmap.find(sequenceName);
84 if (it != groupmap.end()) { //sequence name was in group file
91 /************************************************************/
93 void GroupMap::setGroup(string sequenceName, string groupN) {
94 setNamesOfGroups(groupN);
96 it = groupmap.find(sequenceName);
98 if (it != groupmap.end()) { m->mothurOut("Your groupfile contains more than 1 sequence named " + sequenceName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); }
100 groupmap[sequenceName] = groupN; //store data in map
101 seqsPerGroup[groupN]++; //increment number of seqs in that group
105 /************************************************************/
106 void GroupMap::setNamesOfGroups(string seqGroup) {
109 for (i=0; i<namesOfGroups.size(); i++) {
110 if (namesOfGroups[i] != seqGroup) {
111 count++; //you have not found this group
113 break; //you already have it
116 if (count == namesOfGroups.size()) {
117 namesOfGroups.push_back(seqGroup); //new group
118 seqsPerGroup[seqGroup] = 0;
119 groupIndex[seqGroup] = index;
123 /************************************************************/
124 bool GroupMap::isValidGroup(string groupname) {
126 for (int i = 0; i < namesOfGroups.size(); i++) {
127 if (groupname == namesOfGroups[i]) { return true; }
132 catch(exception& e) {
133 m->errorOut(e, "GroupMap", "isValidGroup");
137 /************************************************************/
138 int GroupMap::getNumSeqs(string group) {
141 map<string, int>::iterator itNum;
143 itNum = seqsPerGroup.find(group);
145 if (itNum == seqsPerGroup.end()) { return 0; }
147 return seqsPerGroup[group];
150 catch(exception& e) {
151 m->errorOut(e, "GroupMap", "getNumSeqs");
156 /************************************************************/
157 vector<string> GroupMap::getNamesSeqs(){
160 vector<string> names;
162 for (it = groupmap.begin(); it != groupmap.end(); it++) {
163 names.push_back(it->first);
168 catch(exception& e) {
169 m->errorOut(e, "GroupMap", "getNamesSeqs");
173 /************************************************************/
174 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
177 vector<string> names;
179 for (it = groupmap.begin(); it != groupmap.end(); it++) {
180 //if you are belong to one the the groups in the picked vector add you
181 if (m->inUsersGroups(it->second, picked)) {
182 names.push_back(it->first);
188 catch(exception& e) {
189 m->errorOut(e, "GroupMap", "getNamesSeqs");
194 /************************************************************/