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->namesOfGroups = 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->namesOfGroups = 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 groupmap[sequenceName] = groupN;
97 /************************************************************/
98 void GroupMap::setNamesOfGroups(string seqGroup) {
101 for (i=0; i<namesOfGroups.size(); i++) {
102 if (namesOfGroups[i] != seqGroup) {
103 count++; //you have not found this group
105 break; //you already have it
108 if (count == namesOfGroups.size()) {
109 namesOfGroups.push_back(seqGroup); //new group
110 seqsPerGroup[seqGroup] = 0;
111 groupIndex[seqGroup] = index;
115 /************************************************************/
116 bool GroupMap::isValidGroup(string groupname) {
118 for (int i = 0; i < namesOfGroups.size(); i++) {
119 if (groupname == namesOfGroups[i]) { return true; }
124 catch(exception& e) {
125 m->errorOut(e, "GroupMap", "isValidGroup");
129 /************************************************************/
130 int GroupMap::getNumSeqs(string group) {
133 map<string, int>::iterator itNum;
135 itNum = seqsPerGroup.find(group);
137 if (itNum == seqsPerGroup.end()) { return 0; }
139 return seqsPerGroup[group];
142 catch(exception& e) {
143 m->errorOut(e, "GroupMap", "getNumSeqs");
148 /************************************************************/
149 vector<string> GroupMap::getNamesSeqs(){
152 vector<string> names;
154 for (it = groupmap.begin(); it != groupmap.end(); it++) {
155 names.push_back(it->first);
160 catch(exception& e) {
161 m->errorOut(e, "GroupMap", "getNamesSeqs");
165 /************************************************************/
166 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
169 vector<string> names;
171 for (it = groupmap.begin(); it != groupmap.end(); it++) {
172 //if you are belong to one the the groups in the picked vector add you
173 if (m->inUsersGroups(it->second, picked)) {
174 names.push_back(it->first);
180 catch(exception& e) {
181 m->errorOut(e, "GroupMap", "getNamesSeqs");
186 /************************************************************/