]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
created mothurOut class to handle logfiles
[mothur.git] / sharedordervector.cpp
1 /*
2  *  sharedSharedOrderVector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/9/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedordervector.h"
11 #include "sharedutilities.h"
12
13 /***********************************************************************/
14
15 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0)  {}
16
17 /***********************************************************************/
18
19 SharedOrderVector::SharedOrderVector(string id, vector<individual>  ov) : 
20                                                                                         DataVector(id), data(ov)
21 {
22                 updateStats();
23 }
24
25 /***********************************************************************/
26 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
27 //if you don't use a list and groupfile.  
28
29 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {  //reads in a shared file
30         try {
31                 globaldata = GlobalData::getInstance();
32                 maxRank = 0; numBins = 0; numSeqs = 0;
33                 
34                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
35                 
36                 int num, inputData, count;
37                 count = 0;  numSeqs = 0;
38                 string holdLabel, nextLabel, groupN;
39                 individual newguy;
40                 
41                 //read in first row since you know there is at least 1 group.
42                 f >> label >> groupN >> num;
43                 holdLabel = label;
44                 
45                 if (globaldata->gGroupmap == NULL) { 
46                         //save group in groupmap
47                         groupmap->namesOfGroups.push_back(groupN);
48                         groupmap->groupIndex[groupN] = 0;
49                 }
50                 
51                 for(int i=0;i<num;i++){
52                         f >> inputData;
53                         
54                         for (int j = 0; j < inputData; j++) {
55                                 push_back(i, i, groupN);
56                                 numSeqs++;
57                         }
58                 }
59                 
60                 gobble(f); 
61                 
62                 if (f.eof() != true) { f >> nextLabel; }
63                 
64                 //read the rest of the groups info in
65                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
66                         f >> groupN >> num;
67                         count++;
68                         
69                         if (globaldata->gGroupmap == NULL) { 
70                                 //save group in groupmap
71                                 groupmap->namesOfGroups.push_back(groupN);
72                                 groupmap->groupIndex[groupN] = count;
73                         }
74                         
75                         for(int i=0;i<num;i++){
76                                 f >> inputData;
77                                 
78                                 for (int j = 0; j < inputData; j++) {
79                                         push_back(i, i, groupN);
80                                         numSeqs++;
81                                 }
82                         }
83                         
84                         gobble(f);
85                                 
86                         if (f.eof() != true) { f >> nextLabel; }
87
88                 }
89                 
90                 //put file pointer back since you are now at a new distance label
91                 for (int i = 0; i < nextLabel.length(); i++) { f.unget();  }
92         
93                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
94                 
95                 updateStats();
96                 
97         }
98         catch(exception& e) {
99                 m->errorOut(e, "SharedOrderVector", "SharedOrderVector");
100                 exit(1);
101         }
102 }
103 /***********************************************************************/
104
105 int SharedOrderVector::getNumBins(){
106         return numBins;
107 }
108
109 /***********************************************************************/
110
111 int SharedOrderVector::getNumSeqs(){
112         return numSeqs;
113 }
114
115 /***********************************************************************/
116
117 int SharedOrderVector::getMaxRank(){
118         return maxRank;
119 }
120
121
122 /***********************************************************************/
123
124
125
126 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
127         
128         data[index].group = groupName;
129         data[index].bin = binNumber;
130         data[index].abundance = abund;
131         //if (abund > maxRank) { maxRank = abund; }
132         updateStats();
133 }
134
135 /***********************************************************************/
136
137 individual SharedOrderVector::get(int index){
138         return data[index];                     
139 }
140
141
142 /***********************************************************************/
143 //commented updateStats out to improve speed, but whoever calls this must remember to update when they are done with all the pushbacks they are doing 
144 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
145         individual newGuy;
146         newGuy.group = groupName;
147         newGuy.abundance = abund;
148         newGuy.bin = binNumber;
149         data.push_back(newGuy);
150         //numSeqs++;
151         //numBins++;
152         //if (abund > maxRank) { maxRank = abund; }
153         
154         //updateStats();
155 }
156
157 /***********************************************************************/
158
159 void SharedOrderVector::print(ostream& output){
160         try {
161                 output << label << '\t' << numSeqs << '\t';
162         
163                 for(int i=0;i<data.size();i++){
164                         output << data[i].bin << '\t';
165                 }
166                 output << endl;
167         }
168         catch(exception& e) {
169                 m->errorOut(e, "SharedOrderVector", "print");
170                 exit(1);
171         }
172 }
173
174
175 /***********************************************************************/
176
177 void SharedOrderVector::resize(int){
178         m->mothurOut("resize() did nothing in class SharedOrderVector");
179 }
180
181 /***********************************************************************/
182
183
184 vector<individual>::iterator SharedOrderVector::begin(){
185         return data.begin();    
186 }
187
188 /***********************************************************************/
189
190 vector<individual>::iterator SharedOrderVector::end(){
191         return data.end();              
192 }
193
194 /***********************************************************************/
195
196 int SharedOrderVector::size(){
197         return data.size();                                     
198 }
199
200 /***********************************************************************/
201
202 RAbundVector SharedOrderVector::getRAbundVector(){
203         try {
204                 RAbundVector rav(data.size());
205         
206                 for(int i=0;i<numSeqs;i++){
207                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
208                 }       
209                 sort(rav.rbegin(), rav.rend());
210                 for(int i=numSeqs-1;i>=0;i--){
211                         if(rav.get(i) == 0){    rav.pop_back(); }
212                         else{
213                                 break;
214                         }
215                 }
216                 rav.setLabel(label);
217
218                 return rav;
219         }
220         catch(exception& e) {
221                 m->errorOut(e, "SharedOrderVector", "getRAbundVector");
222                 exit(1);
223         }
224 }
225 /***********************************************************************/
226
227 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
228         try {
229                 OrderVector ov;
230         
231                 for (int i = 0; i < data.size(); i++) {
232                         ov.push_back(data[i].bin);
233                 }
234                 
235                 random_shuffle(ov.begin(), ov.end());
236
237                 ov.setLabel(label);     
238                 return ov;
239         }
240         catch(exception& e) {
241                 m->errorOut(e, "SharedOrderVector", "getOrderVector");
242                 exit(1);
243         }
244 }
245
246
247 /***********************************************************************/
248
249 SAbundVector SharedOrderVector::getSAbundVector(){
250         
251         RAbundVector rav(this->getRAbundVector());
252         return rav.getSAbundVector();
253
254 }
255 /***********************************************************************/
256 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
257         try {
258                 SharedRAbundVector sharedRav(data.size());
259                 
260                 sharedRav.setLabel(label);
261                 sharedRav.setGroup(group);
262                 
263                 for (int i = 0; i < data.size(); i++) {
264                         if (data[i].group == group) {
265                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
266                         }
267                 }
268                 return sharedRav;
269         }
270         catch(exception& e) {
271                 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
272                 exit(1);
273         }
274 }
275 /***********************************************************************/
276 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
277         try {
278                 SharedUtil* util;
279                 util = new SharedUtil();
280                 vector<SharedRAbundVector*> lookup;
281                 
282                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
283                 util->getSharedVectors(globaldata->Groups, lookup, this);
284                 
285                 return lookup;
286         }
287         catch(exception& e) {
288                 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
289                 exit(1);
290         }
291 }
292 /***********************************************************************/
293 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
294         try {
295                 
296                 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
297                 return sharedRav.getSharedSAbundVector();
298                                 
299         }
300         catch(exception& e) {
301                 m->errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
302                 exit(1);
303         }
304 }
305
306 /***********************************************************************/
307
308 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
309         random_shuffle(data.begin(), data.end());
310         return *this;                   
311 }
312
313 /***********************************************************************/
314
315 void SharedOrderVector::updateStats(){
316         try {
317                 needToUpdate = 0;
318                 numSeqs = 0;
319                 numBins = 0;
320                 maxRank = 0;
321         
322                 numSeqs = data.size();
323                                 
324                 vector<int> hold(numSeqs, 0);
325                 for(int i=0;i<numSeqs;i++){
326                         hold[data[i].bin] = hold[data[i].bin]+1;
327                 }       
328                 
329                 for(int i=0;i<numSeqs;i++){
330                         if(hold[i] > 0)                         {       numBins++;                              }
331                         if(hold[i] > maxRank)           {       maxRank = hold[i];              }
332                 }
333                 
334         }
335         catch(exception& e) {
336                 m->errorOut(e, "SharedOrderVector", "updateStats");
337                 exit(1);
338         }
339 }
340
341 /***********************************************************************/
342
343