]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
added logfile feature
[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                 if (f.eof() != true) { f >> nextLabel; }
61                 
62                 //read the rest of the groups info in
63                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
64                         f >> groupN >> num;
65                         count++;
66                         
67                         if (globaldata->gGroupmap == NULL) { 
68                                 //save group in groupmap
69                                 groupmap->namesOfGroups.push_back(groupN);
70                                 groupmap->groupIndex[groupN] = count;
71                         }
72                         
73                         for(int i=0;i<num;i++){
74                                 f >> inputData;
75                                 
76                                 for (int j = 0; j < inputData; j++) {
77                                         push_back(i, i, groupN);
78                                         numSeqs++;
79                                 }
80                         }
81                         
82                                 
83                         if (f.eof() != true) { f >> nextLabel; }
84
85                 }
86                 
87                 //put file pointer back since you are now at a new distance label
88                 for (int i = 0; i < nextLabel.length(); i++) { f.unget();  }
89         
90                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
91                 
92                 updateStats();
93                 
94         }
95         catch(exception& e) {
96                 errorOut(e, "SharedOrderVector", "SharedOrderVector");
97                 exit(1);
98         }
99 }
100 /***********************************************************************/
101
102 int SharedOrderVector::getNumBins(){
103         return numBins;
104 }
105
106 /***********************************************************************/
107
108 int SharedOrderVector::getNumSeqs(){
109         return numSeqs;
110 }
111
112 /***********************************************************************/
113
114 int SharedOrderVector::getMaxRank(){
115         return maxRank;
116 }
117
118
119 /***********************************************************************/
120
121
122
123 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
124         
125         data[index].group = groupName;
126         data[index].bin = binNumber;
127         data[index].abundance = abund;
128         //if (abund > maxRank) { maxRank = abund; }
129         updateStats();
130 }
131
132 /***********************************************************************/
133
134 individual SharedOrderVector::get(int index){
135         return data[index];                     
136 }
137
138
139 /***********************************************************************/
140 //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 
141 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
142         individual newGuy;
143         newGuy.group = groupName;
144         newGuy.abundance = abund;
145         newGuy.bin = binNumber;
146         data.push_back(newGuy);
147         //numSeqs++;
148         //numBins++;
149         //if (abund > maxRank) { maxRank = abund; }
150         
151         //updateStats();
152 }
153
154 /***********************************************************************/
155
156 void SharedOrderVector::print(ostream& output){
157         try {
158                 output << label << '\t' << numSeqs << '\t';
159         
160                 for(int i=0;i<data.size();i++){
161                         output << data[i].bin << '\t';
162                 }
163                 output << endl;
164         }
165         catch(exception& e) {
166                 errorOut(e, "SharedOrderVector", "print");
167                 exit(1);
168         }
169 }
170
171
172 /***********************************************************************/
173
174 void SharedOrderVector::resize(int){
175         mothurOut("resize() did nothing in class SharedOrderVector");
176 }
177
178 /***********************************************************************/
179
180
181 vector<individual>::iterator SharedOrderVector::begin(){
182         return data.begin();    
183 }
184
185 /***********************************************************************/
186
187 vector<individual>::iterator SharedOrderVector::end(){
188         return data.end();              
189 }
190
191 /***********************************************************************/
192
193 int SharedOrderVector::size(){
194         return data.size();                                     
195 }
196
197 /***********************************************************************/
198
199 RAbundVector SharedOrderVector::getRAbundVector(){
200         try {
201                 RAbundVector rav(data.size());
202         
203                 for(int i=0;i<numSeqs;i++){
204                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
205                 }       
206                 sort(rav.rbegin(), rav.rend());
207                 for(int i=numSeqs-1;i>=0;i--){
208                         if(rav.get(i) == 0){    rav.pop_back(); }
209                         else{
210                                 break;
211                         }
212                 }
213                 rav.setLabel(label);
214
215                 return rav;
216         }
217         catch(exception& e) {
218                 errorOut(e, "SharedOrderVector", "getRAbundVector");
219                 exit(1);
220         }
221 }
222 /***********************************************************************/
223
224 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
225         try {
226                 OrderVector ov;
227         
228                 for (int i = 0; i < data.size(); i++) {
229                         ov.push_back(data[i].bin);
230                 }
231                 
232                 random_shuffle(ov.begin(), ov.end());
233
234                 ov.setLabel(label);     
235                 return ov;
236         }
237         catch(exception& e) {
238                 errorOut(e, "SharedOrderVector", "getOrderVector");
239                 exit(1);
240         }
241 }
242
243
244 /***********************************************************************/
245
246 SAbundVector SharedOrderVector::getSAbundVector(){
247         
248         RAbundVector rav(this->getRAbundVector());
249         return rav.getSAbundVector();
250
251 }
252 /***********************************************************************/
253 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
254         try {
255                 SharedRAbundVector sharedRav(data.size());
256                 
257                 sharedRav.setLabel(label);
258                 sharedRav.setGroup(group);
259                 
260                 for (int i = 0; i < data.size(); i++) {
261                         if (data[i].group == group) {
262                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
263                         }
264                 }
265                 return sharedRav;
266         }
267         catch(exception& e) {
268                 errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
269                 exit(1);
270         }
271 }
272 /***********************************************************************/
273 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
274         try {
275                 SharedUtil* util;
276                 util = new SharedUtil();
277                 vector<SharedRAbundVector*> lookup;
278                 
279                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
280                 util->getSharedVectors(globaldata->Groups, lookup, this);
281                 
282                 return lookup;
283         }
284         catch(exception& e) {
285                 errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
286                 exit(1);
287         }
288 }
289 /***********************************************************************/
290 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
291         try {
292                 
293                 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
294                 return sharedRav.getSharedSAbundVector();
295                                 
296         }
297         catch(exception& e) {
298                 errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
299                 exit(1);
300         }
301 }
302
303 /***********************************************************************/
304
305 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
306         random_shuffle(data.begin(), data.end());
307         return *this;                   
308 }
309
310 /***********************************************************************/
311
312 void SharedOrderVector::updateStats(){
313         try {
314                 needToUpdate = 0;
315                 numSeqs = 0;
316                 numBins = 0;
317                 maxRank = 0;
318         
319                 numSeqs = data.size();
320                                 
321                 vector<int> hold(numSeqs, 0);
322                 for(int i=0;i<numSeqs;i++){
323                         hold[data[i].bin] = hold[data[i].bin]+1;
324                 }       
325                 
326                 for(int i=0;i<numSeqs;i++){
327                         if(hold[i] > 0)                         {       numBins++;                              }
328                         if(hold[i] > maxRank)           {       maxRank = hold[i];              }
329                 }
330                 
331         }
332         catch(exception& e) {
333                 errorOut(e, "SharedOrderVector", "updateStats");
334                 exit(1);
335         }
336 }
337
338 /***********************************************************************/
339
340