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