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