]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
Merge remote-tracking branch 'mothur/master'
[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         m = MothurOut::getInstance();
16         groupFileName = filename;
17         m->openInputFile(filename, fileHandle);
18         index = 0;
19 }
20
21 /************************************************************/
22  GroupMap::~GroupMap(){}
23
24 /************************************************************/
25 int GroupMap::readMap() {
26                 string seqName, seqGroup;
27                 int error = 0;
28
29                 while(fileHandle){
30                         fileHandle >> seqName;  m->gobble(fileHandle);          //read from first column
31                         fileHandle >> seqGroup;                 //read from second column
32                         
33                         if (m->control_pressed) {  fileHandle.close();  return 1; }
34         
35                         setNamesOfGroups(seqGroup);
36                         
37                         it = groupmap.find(seqName);
38                         
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();  }
40                         else {
41                                 groupmap[seqName] = seqGroup;   //store data in map
42                                 seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
43                         }
44                         m->gobble(fileHandle);
45                 }
46                 fileHandle.close();
47                 m->setAllGroups(namesOfGroups);
48                 return error;
49 }
50 /************************************************************/
51 int GroupMap::readDesignMap() {
52                 string seqName, seqGroup;
53                 int error = 0;
54
55                 while(fileHandle){
56                         fileHandle >> seqName;  m->gobble(fileHandle);          //read from first column
57                         fileHandle >> seqGroup;                 //read from second column
58                         
59                         if (m->control_pressed) {  fileHandle.close();  return 1; }
60         
61                         setNamesOfGroups(seqGroup);
62                         
63                         it = groupmap.find(seqName);
64                         
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();  }
66                         else {
67                                 groupmap[seqName] = seqGroup;   //store data in map
68                                 seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
69                         }
70                         m->gobble(fileHandle);
71                 }
72                 fileHandle.close();
73                 m->setAllGroups(namesOfGroups);
74                 return error;
75 }
76 /************************************************************/
77 int GroupMap::readDesignMap(string filename) {
78     groupFileName = filename;
79         m->openInputFile(filename, fileHandle);
80         index = 0;
81     string seqName, seqGroup;
82     int error = 0;
83     
84     while(fileHandle){
85         fileHandle >> seqName;  m->gobble(fileHandle);          //read from first column
86         fileHandle >> seqGroup;                 //read from second column
87         
88         if (m->control_pressed) {  fileHandle.close();  return 1; }
89         
90         setNamesOfGroups(seqGroup);
91         
92         it = groupmap.find(seqName);
93         
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();  }
95         else {
96             groupmap[seqName] = seqGroup;       //store data in map
97             seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
98         }
99         m->gobble(fileHandle);
100     }
101     fileHandle.close();
102     m->setAllGroups(namesOfGroups);
103     return error;
104 }
105 /************************************************************/
106 int GroupMap::getNumGroups() { return namesOfGroups.size();     }
107 /************************************************************/
108
109 string GroupMap::getGroup(string sequenceName) {
110                         
111         it = groupmap.find(sequenceName);
112         if (it != groupmap.end()) { //sequence name was in group file
113                 return it->second;      
114         }else {
115                 return "not found";
116         }
117 }
118
119 /************************************************************/
120
121 void GroupMap::setGroup(string sequenceName, string groupN) {
122         setNamesOfGroups(groupN);
123         
124         it = groupmap.find(sequenceName);
125         
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();  }
127         else {
128                 groupmap[sequenceName] = groupN;        //store data in map
129                 seqsPerGroup[groupN]++;  //increment number of seqs in that group
130         }
131 }
132
133 /************************************************************/
134 void GroupMap::setNamesOfGroups(string seqGroup) {
135         int i, count;
136         count = 0;
137         for (i=0; i<namesOfGroups.size(); i++) {
138                 if (namesOfGroups[i] != seqGroup) {
139                         count++; //you have not found this group
140                 }else {
141                         break; //you already have it
142                 }
143         }
144         if (count == namesOfGroups.size()) {
145                 namesOfGroups.push_back(seqGroup); //new group
146                 seqsPerGroup[seqGroup] = 0;
147                 groupIndex[seqGroup] = index;
148                 index++;
149         }
150 }
151 /************************************************************/
152 bool GroupMap::isValidGroup(string groupname) {
153         try {
154                 for (int i = 0; i < namesOfGroups.size(); i++) {
155                         if (groupname == namesOfGroups[i]) { return true; }
156                 }
157                 
158                 return false;
159         }
160         catch(exception& e) {
161                 m->errorOut(e, "GroupMap", "isValidGroup");
162                 exit(1);
163         }
164 }
165 /************************************************************/
166 int GroupMap::getNumSeqs(string group) {
167         try {
168                 
169                 map<string, int>::iterator itNum;
170                 
171                 itNum = seqsPerGroup.find(group);
172                 
173                 if (itNum == seqsPerGroup.end()) { return 0; }
174                 
175                 return seqsPerGroup[group];
176                 
177         }
178         catch(exception& e) {
179                 m->errorOut(e, "GroupMap", "getNumSeqs");
180                 exit(1);
181         }
182 }
183
184 /************************************************************/
185 vector<string> GroupMap::getNamesSeqs(){
186         try {
187         
188                 vector<string> names;
189                 
190                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
191                         names.push_back(it->first);
192                 }
193                 
194                 return names;
195         }
196         catch(exception& e) {
197                 m->errorOut(e, "GroupMap", "getNamesSeqs");
198                 exit(1);
199         }
200 }
201 /************************************************************/
202 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
203         try {
204                 
205                 vector<string> names;
206                 
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);
211                         }
212                 }
213                 
214                 return names;
215         }
216         catch(exception& e) {
217                 m->errorOut(e, "GroupMap", "getNamesSeqs");
218                 exit(1);
219         }
220 }
221
222 /************************************************************/
223