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);
49 /************************************************************/
50 int GroupMap::readDesignMap() {
51 string seqName, seqGroup;
55 fileHandle >> seqName; m->gobble(fileHandle); //read from first column
56 fileHandle >> seqGroup; //read from second column
58 if (m->control_pressed) { fileHandle.close(); return 1; }
60 setNamesOfGroups(seqGroup);
62 it = groupmap.find(seqName);
64 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(); }
66 groupmap[seqName] = seqGroup; //store data in map
67 seqsPerGroup[seqGroup]++; //increment number of seqs in that group
69 m->gobble(fileHandle);
75 /************************************************************/
76 int GroupMap::getNumGroups() { return namesOfGroups.size(); }
77 /************************************************************/
79 string GroupMap::getGroup(string sequenceName) {
81 it = groupmap.find(sequenceName);
82 if (it != groupmap.end()) { //sequence name was in group file
89 /************************************************************/
91 void GroupMap::setGroup(string sequenceName, string groupN) {
92 groupmap[sequenceName] = groupN;
95 /************************************************************/
96 void GroupMap::setNamesOfGroups(string seqGroup) {
99 for (i=0; i<namesOfGroups.size(); i++) {
100 if (namesOfGroups[i] != seqGroup) {
101 count++; //you have not found this group
103 break; //you already have it
106 if (count == namesOfGroups.size()) {
107 namesOfGroups.push_back(seqGroup); //new group
108 seqsPerGroup[seqGroup] = 0;
109 groupIndex[seqGroup] = index;
113 /************************************************************/
114 bool GroupMap::isValidGroup(string groupname) {
116 for (int i = 0; i < namesOfGroups.size(); i++) {
117 if (groupname == namesOfGroups[i]) { return true; }
122 catch(exception& e) {
123 m->errorOut(e, "GroupMap", "isValidGroup");
127 /************************************************************/
128 int GroupMap::getNumSeqs(string group) {
131 map<string, int>::iterator itNum;
133 itNum = seqsPerGroup.find(group);
135 if (itNum == seqsPerGroup.end()) { return 0; }
137 return seqsPerGroup[group];
140 catch(exception& e) {
141 m->errorOut(e, "GroupMap", "getNumSeqs");
146 /************************************************************/
147 vector<string> GroupMap::getNamesSeqs(){
150 vector<string> names;
152 for (it = groupmap.begin(); it != groupmap.end(); it++) {
153 names.push_back(it->first);
158 catch(exception& e) {
159 m->errorOut(e, "GroupMap", "getNamesSeqs");
163 /************************************************************/
164 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
167 vector<string> names;
169 for (it = groupmap.begin(); it != groupmap.end(); it++) {
170 //if you are belong to one the the groups in the picked vector add you
171 if (m->inUsersGroups(it->second, picked)) {
172 names.push_back(it->first);
178 catch(exception& e) {
179 m->errorOut(e, "GroupMap", "getNamesSeqs");
184 /************************************************************/