]> git.donarmstrong.com Git - mothur.git/blob - groupmap.cpp
612b2364d617432d64819413b8d8eb03d9865a64
[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     try {
27         string seqName, seqGroup;
28                 int error = 0;
29         string rest = "";
30         char buffer[4096];
31         bool pairDone = false;
32         bool columnOne = true;
33     
34         while (!fileHandle.eof()) {
35             if (m->control_pressed) { fileHandle.close();  return 1; }
36         
37             fileHandle.read(buffer, 4096);
38             vector<string> pieces = m->splitWhiteSpace(rest, buffer, fileHandle.gcount());
39         
40             for (int i = 0; i < pieces.size(); i++) {
41                 if (columnOne) {  seqName = pieces[i]; columnOne=false; }
42                 else  { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
43             
44                 if (pairDone) { 
45                     setNamesOfGroups(seqGroup);
46                     
47                     it = groupmap.find(seqName);
48                     
49                     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();  }
50                     else {
51                         groupmap[seqName] = seqGroup;   //store data in map
52                         seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
53                     }
54                     pairDone = false; 
55                 } 
56             }
57         }
58                 fileHandle.close();
59         
60                 m->setAllGroups(namesOfGroups);
61                 return error;
62     }
63         catch(exception& e) {
64                 m->errorOut(e, "GroupMap", "readMap");
65                 exit(1);
66         }
67 }
68 /************************************************************/
69 int GroupMap::readDesignMap() {
70     try {
71         string seqName, seqGroup;
72                 int error = 0;
73         string rest = "";
74         char buffer[4096];
75         bool pairDone = false;
76         bool columnOne = true;
77         
78         while (!fileHandle.eof()) {
79             if (m->control_pressed) { fileHandle.close();  return 1; }
80             
81             fileHandle.read(buffer, 4096);
82             vector<string> pieces = m->splitWhiteSpace(rest, buffer, fileHandle.gcount());
83             
84             for (int i = 0; i < pieces.size(); i++) {
85                 if (columnOne) {  seqName = pieces[i]; columnOne=false; }
86                 else  { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
87                 
88                 if (pairDone) { 
89                     setNamesOfGroups(seqGroup);
90                     
91                     it = groupmap.find(seqName);
92                     
93                     if (it != groupmap.end()) { error = 1; m->mothurOut("Your designfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
94                     else {
95                         groupmap[seqName] = seqGroup;   //store data in map
96                         seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
97                     }
98                     pairDone = false; 
99                 } 
100             }
101         }
102                 fileHandle.close();
103         
104                 m->setAllGroups(namesOfGroups);
105                 return error;
106     }
107         catch(exception& e) {
108                 m->errorOut(e, "GroupMap", "readDesignMap");
109                 exit(1);
110         }
111 }
112 /************************************************************/
113 int GroupMap::readDesignMap(string filename) {
114     try {
115         groupFileName = filename;
116         m->openInputFile(filename, fileHandle);
117         index = 0;
118         string seqName, seqGroup;
119                 int error = 0;
120         string rest = "";
121         char buffer[4096];
122         bool pairDone = false;
123         bool columnOne = true;
124         
125         while (!fileHandle.eof()) {
126             if (m->control_pressed) { fileHandle.close();  return 1; }
127             
128             fileHandle.read(buffer, 4096);
129             vector<string> pieces = m->splitWhiteSpace(rest, buffer, fileHandle.gcount());
130             
131             for (int i = 0; i < pieces.size(); i++) {
132                 if (columnOne) {  seqName = pieces[i]; columnOne=false; }
133                 else  { seqGroup = pieces[i]; pairDone = true; columnOne=true; }
134                 
135                 if (pairDone) { 
136                     setNamesOfGroups(seqGroup);
137                     
138                     it = groupmap.find(seqName);
139                     
140                     if (it != groupmap.end()) { error = 1; m->mothurOut("Your designfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
141                     else {
142                         groupmap[seqName] = seqGroup;   //store data in map
143                         seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
144                     }
145                     pairDone = false; 
146                 } 
147             }
148         }
149                 fileHandle.close();
150         
151                 m->setAllGroups(namesOfGroups);
152                 return error;
153     }
154         catch(exception& e) {
155                 m->errorOut(e, "GroupMap", "readDesignMap");
156                 exit(1);
157         }
158 }
159 /************************************************************/
160 int GroupMap::getNumGroups() { return namesOfGroups.size();     }
161 /************************************************************/
162
163 string GroupMap::getGroup(string sequenceName) {
164                         
165         it = groupmap.find(sequenceName);
166         if (it != groupmap.end()) { //sequence name was in group file
167                 return it->second;      
168         }else {
169                 return "not found";
170         }
171 }
172
173 /************************************************************/
174
175 void GroupMap::setGroup(string sequenceName, string groupN) {
176         setNamesOfGroups(groupN);
177         
178         it = groupmap.find(sequenceName);
179         
180         if (it != groupmap.end()) {  m->mothurOut("Your groupfile contains more than 1 sequence named " + sequenceName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
181         else {
182                 groupmap[sequenceName] = groupN;        //store data in map
183                 seqsPerGroup[groupN]++;  //increment number of seqs in that group
184         }
185 }
186
187 /************************************************************/
188 void GroupMap::setNamesOfGroups(string seqGroup) {
189         int i, count;
190         count = 0;
191         for (i=0; i<namesOfGroups.size(); i++) {
192                 if (namesOfGroups[i] != seqGroup) {
193                         count++; //you have not found this group
194                 }else {
195                         break; //you already have it
196                 }
197         }
198         if (count == namesOfGroups.size()) {
199                 namesOfGroups.push_back(seqGroup); //new group
200                 seqsPerGroup[seqGroup] = 0;
201                 groupIndex[seqGroup] = index;
202                 index++;
203         }
204 }
205 /************************************************************/
206 bool GroupMap::isValidGroup(string groupname) {
207         try {
208                 for (int i = 0; i < namesOfGroups.size(); i++) {
209                         if (groupname == namesOfGroups[i]) { return true; }
210                 }
211                 
212                 return false;
213         }
214         catch(exception& e) {
215                 m->errorOut(e, "GroupMap", "isValidGroup");
216                 exit(1);
217         }
218 }
219 /************************************************************/
220 int GroupMap::getNumSeqs(string group) {
221         try {
222                 
223                 map<string, int>::iterator itNum;
224                 
225                 itNum = seqsPerGroup.find(group);
226                 
227                 if (itNum == seqsPerGroup.end()) { return 0; }
228                 
229                 return seqsPerGroup[group];
230                 
231         }
232         catch(exception& e) {
233                 m->errorOut(e, "GroupMap", "getNumSeqs");
234                 exit(1);
235         }
236 }
237
238 /************************************************************/
239 vector<string> GroupMap::getNamesSeqs(){
240         try {
241         
242                 vector<string> names;
243                 
244                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
245                         names.push_back(it->first);
246                 }
247                 
248                 return names;
249         }
250         catch(exception& e) {
251                 m->errorOut(e, "GroupMap", "getNamesSeqs");
252                 exit(1);
253         }
254 }
255 /************************************************************/
256 vector<string> GroupMap::getNamesSeqs(vector<string> picked){
257         try {
258                 
259                 vector<string> names;
260                 
261                 for (it = groupmap.begin(); it != groupmap.end(); it++) {
262                         //if you are belong to one the the groups in the picked vector add you
263                         if (m->inUsersGroups(it->second, picked)) {
264                                 names.push_back(it->first);
265                         }
266                 }
267                 
268                 return names;
269         }
270         catch(exception& e) {
271                 m->errorOut(e, "GroupMap", "getNamesSeqs");
272                 exit(1);
273         }
274 }
275
276 /************************************************************/
277