]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
added read.shared, broke up globaldata a bit
[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 using namespace std;
11
12
13 #include "sharedordervector.h"
14 #include "utilities.hpp"
15 #include <exception>
16
17 /***********************************************************************/
18
19 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0)  {}
20
21 /***********************************************************************/
22
23 SharedOrderVector::SharedOrderVector(string id, vector<individual>  ov) : 
24                                                                                         DataVector(id), data(ov)
25 {
26                 updateStats();
27 }
28
29 /***********************************************************************/
30 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
31 //if you don't use a list and groupfile.  
32
33 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {
34         try {
35                 globaldata = GlobalData::getInstance();
36                 
37                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
38                 
39                 int num, inputData, pos, count;
40                 count = 0;  numSeqs = 0;
41                 string holdLabel, nextLabel, groupN;
42                 individual newguy;
43                 
44                 //read in first row since you know there is at least 1 group.
45                 f >> label >> groupN >> num;
46                 holdLabel = label;
47                 
48                 if (globaldata->gGroupmap == NULL) { 
49                         //save group in groupmap
50                         groupmap->namesOfGroups.push_back(groupN);
51                         groupmap->groupIndex[groupN] = 0;
52                 }
53                 
54                 for(int i=0;i<num;i++){
55                         f >> inputData;
56                         for (int j = 0; j < inputData; j++) {
57                                 push_back(i+1, i+1, groupN);
58                                 numSeqs++;
59                         }
60                 }
61                 
62                 //save position in file in case next line is a new label.
63                 pos = f.tellg();
64                 
65                 if (f.eof() != true) { f >> nextLabel; }
66                 
67                 //read the rest of the groups info in
68                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
69                         f >> groupN >> num;
70                         count++;
71                         
72                         if (globaldata->gGroupmap == NULL) { 
73                                 //save group in groupmap
74                                 groupmap->namesOfGroups.push_back(groupN);
75                                 groupmap->groupIndex[groupN] = count;
76                         }
77                         
78                         for(int i=0;i<num;i++){
79                                 f >> inputData;
80                                 for (int j = 0; j < inputData; j++) {
81                                         push_back(i+1, i+1, groupN);
82                                         numSeqs++;
83                                 }
84                         }
85                         
86                         //save position in file in case next line is a new label.
87                         pos = f.tellg();
88         
89                         if (f.eof() != true) { f >> nextLabel; }
90
91                 }
92                 
93                 //put file pointer back since you are now at a new distance label
94                 f.seekg(pos, ios::beg);
95         
96                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
97         
98                 updateStats();
99         }
100         catch(exception& e) {
101                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }
104         catch(...) {
105                 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106                 exit(1);
107         }
108 }
109 /***********************************************************************/
110
111 int SharedOrderVector::getNumBins(){
112         if(needToUpdate == 1){  updateStats();  }
113         return numBins;
114 }
115
116 /***********************************************************************/
117
118 int SharedOrderVector::getNumSeqs(){
119         if(needToUpdate == 1){  updateStats();  }
120         return numSeqs;
121 }
122
123 /***********************************************************************/
124
125 int SharedOrderVector::getMaxRank(){
126         if(needToUpdate == 1){  updateStats();  }
127         return maxRank;
128 }
129
130
131 /***********************************************************************/
132
133
134
135 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
136         
137         data[index].group = groupName;
138         data[index].bin = binNumber;
139         data[index].abundance = abund;
140         needToUpdate = 1;
141         
142 }
143
144 /***********************************************************************/
145
146 individual SharedOrderVector::get(int index){
147         return data[index];                     
148 }
149
150
151 /***********************************************************************/
152
153 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
154         individual newGuy;
155         newGuy.group = groupName;
156         newGuy.abundance = abund;
157         newGuy.bin = binNumber;
158         data.push_back(newGuy);
159         needToUpdate = 1;
160         
161 }
162
163 /***********************************************************************/
164
165 void SharedOrderVector::print(ostream& output){
166         try {
167                 output << label << '\t' << numSeqs << '\t';
168         
169                 for(int i=0;i<data.size();i++){
170                         output << data[i].bin << '\t';
171                 }
172                 output << endl;
173         }
174         catch(exception& e) {
175                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176                 exit(1);
177         }
178         catch(...) {
179                 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }
182 }
183
184
185 /***********************************************************************/
186
187 void SharedOrderVector::resize(int){
188         cout << "resize() did nothing in class SharedOrderVector";
189 }
190
191 /***********************************************************************/
192
193
194 vector<individual>::iterator SharedOrderVector::begin(){
195         return data.begin();    
196 }
197
198 /***********************************************************************/
199
200 vector<individual>::iterator SharedOrderVector::end(){
201         return data.end();              
202 }
203
204 /***********************************************************************/
205
206 int SharedOrderVector::size(){
207         return data.size();                                     
208 }
209
210 /***********************************************************************/
211
212 RAbundVector SharedOrderVector::getRAbundVector(){
213         try {
214                 RAbundVector rav(data.size());
215         
216                 for(int i=0;i<numSeqs;i++){
217                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
218                 }       
219                 sort(rav.rbegin(), rav.rend());
220                 for(int i=numSeqs-1;i>=0;i--){
221                         if(rav.get(i) == 0){    rav.pop_back(); }
222                         else{
223                                 break;
224                         }
225                 }
226                 rav.setLabel(label);
227
228                 return rav;
229         }
230         catch(exception& e) {
231                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232                 exit(1);
233         }
234         catch(...) {
235                 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236                 exit(1);
237         }       
238 }
239 /***********************************************************************/
240
241 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
242         try {
243                 OrderVector ov;
244         
245                 for (int i = 0; i < data.size(); i++) {
246                         ov.push_back(data[i].bin);
247                 }
248                 
249                 random_shuffle(ov.begin(), ov.end());
250
251                 ov.setLabel(label);     
252                 return ov;
253         }
254         catch(exception& e) {
255                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256                 exit(1);
257         }
258         catch(...) {
259                 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260                 exit(1);
261         }
262 }
263
264
265 /***********************************************************************/
266
267 SAbundVector SharedOrderVector::getSAbundVector(){
268         
269         RAbundVector rav(this->getRAbundVector());
270         return rav.getSAbundVector();
271
272 }
273 /***********************************************************************/
274 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
275         try {
276                 SharedRAbundVector sharedRav(data.size());
277                 
278                 sharedRav.setLabel(label);
279                 sharedRav.setGroup(group);
280                 
281                 for (int i = 0; i < data.size(); i++) {
282                         if (data[i].group == group) {
283                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
284                         }
285                 }
286                 return sharedRav;
287         }
288         catch(exception& e) {
289                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }
292         catch(...) {
293                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
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                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308                 exit(1);
309         }
310         catch(...) {
311                 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312                 exit(1);
313         }
314         
315 }
316
317 /***********************************************************************/
318
319 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
320         return *this;                   
321 }
322
323 /***********************************************************************/
324
325 void SharedOrderVector::updateStats(){
326         try {
327                 needToUpdate = 0;
328                 numSeqs = 0;
329                 numBins = 0;
330                 maxRank = 0;
331         
332                 for(int i=0;i<data.size();i++){
333                         if(data[i].bin != -1){
334                                 numSeqs++;
335                         }
336                 }
337         
338                 vector<individual> hold(numSeqs);
339         
340                 for(int i=0;i<numSeqs;i++){
341                         if(data[i].bin != -1){
342                                 hold[data[i].bin].bin = hold[data[i].bin].bin+1;
343                         }
344                 }       
345
346                 for(int i=0;i<numSeqs;i++){
347                         if(data[i].bin > numBins) { numBins = data[i].bin;      } 
348                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
349                 }
350                 numBins++; //if you have 10 bins largest .bin is 9 since we start at 0.
351         }
352         catch(exception& e) {
353                 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
354                 exit(1);
355         }
356         catch(...) {
357                 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
358                 exit(1);
359         }       
360 }
361
362 /***********************************************************************/
363
364