]> git.donarmstrong.com Git - mothur.git/blob - sabundvector.cpp
created mothurOut class to handle logfiles
[mothur.git] / sabundvector.cpp
1 /*
2  *  sabund.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/8/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights resesaved.
7  *
8  */
9
10 #include "sabundvector.hpp"
11 #include "calculator.h"
12
13 /***********************************************************************/
14
15 SAbundVector::SAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){}
16
17 /***********************************************************************/
18
19 SAbundVector::SAbundVector(int size) :  DataVector(), data(size, 0), maxRank(0), numBins(0), numSeqs(0) {}
20
21 /***********************************************************************/
22
23 SAbundVector::SAbundVector(string id, vector<int> sav) : DataVector(id), data(sav) {
24         try {
25                 for(int i=0;i<sav.size();i++){
26                         if(data[i] != 0){       maxRank = i;    }
27                         numSeqs += i*data[i];
28                         numBins += data[i];
29                 }
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "SAbundVector", "SAbundVector");
33                 exit(1);
34         }
35 }
36
37 /***********************************************************************/
38
39 SAbundVector::SAbundVector(vector <int> dataVec, int mr, int nb, int ns) {
40         try {
41                 data = dataVec;
42                 maxRank = mr;
43                 numBins = nb;
44                 numSeqs = ns;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "SAbundVector", "SAbundVector");
48                 exit(1);
49         }
50 }
51 /***********************************************************************/
52
53 SAbundVector::SAbundVector(ifstream& f): DataVector(), maxRank(0), numBins(0), numSeqs(0) {
54         try {
55                 int hold;
56                 f >> label >> hold;
57         
58                 data.assign(hold+1, 0);
59                 int inputData;
60         
61                 for(int i=1;i<=hold;i++){
62                         f >> inputData;
63
64                         set(i, inputData);
65                 }
66
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "SAbundVector", "SAbundVector");
70                 exit(1);
71         }
72 }
73
74
75 /***********************************************************************/
76
77 void SAbundVector::set(int sabund, int abundance){
78         try {
79
80                 int initSize = data[sabund];
81                 data[sabund] = abundance;
82         
83                 if(sabund != 0){
84                         numBins += (abundance - initSize);
85                 }
86         
87                 numSeqs += sabund * (abundance - initSize);
88         
89                 if(sabund > maxRank)    {       maxRank = sabund;               }
90         }
91         catch(exception& e) {
92                 m->errorOut(e, "SAbundVector", "set");
93                 exit(1);
94         }
95 }
96
97
98 /***********************************************************************/
99
100 int SAbundVector::get(int index){
101         return data[index];
102 }
103
104 /***********************************************************************/
105
106 void SAbundVector::push_back(int abundance){
107         try {
108                 data.push_back(abundance);
109         
110                 maxRank++;      
111         
112                 numBins += abundance;
113         
114                 numSeqs += (maxRank * abundance);
115         }
116         catch(exception& e) {
117                 m->errorOut(e, "SAbundVector", "push_back");
118                 exit(1);
119         }
120 }
121 /***********************************************************************/
122
123 void SAbundVector::quicksort(){
124         sort(data.rbegin(), data.rend());
125 }
126
127 /***********************************************************************/
128
129 int SAbundVector::sum(){
130         VecCalc vecCalc;
131         return vecCalc.sumElements(data);
132 }
133
134 /***********************************************************************/
135
136 void SAbundVector::resize(int size){
137         data.resize(size);
138 }
139
140 /***********************************************************************/
141
142 int SAbundVector::size(){
143         return data.size();             
144 }
145
146 /***********************************************************************/
147 void SAbundVector::print(string prefix, ostream& output){
148         
149         output << prefix << '\t' << maxRank << '\t';
150         
151         for(int i=1;i<=maxRank;i++){
152                 output << data[i] << '\t';
153         }
154         output << endl;
155 }
156
157 /***********************************************************************/
158 void SAbundVector::print(ostream& output){
159         try {
160                 output << label << '\t' << maxRank << '\t';
161         
162                 for(int i=1;i<=maxRank;i++){
163                         output << data[i] << '\t';
164                 }
165                 output << endl;
166         }
167         catch(exception& e) {
168                 m->errorOut(e, "SAbundVector", "print");
169                 exit(1);
170         }
171 }
172
173 /**********************************************************************/
174 int SAbundVector::getNumBins(){
175 //      if(needToUpdate == 1){  updateStats();  }
176         return numBins;
177 }
178
179 /***********************************************************************/
180
181 int SAbundVector::getNumSeqs(){
182 //      if(needToUpdate == 1){  updateStats();  }
183         return numSeqs;
184 }
185
186 /***********************************************************************/
187
188 int SAbundVector::getMaxRank(){
189 //      if(needToUpdate == 1){  updateStats();  }
190         return maxRank;
191 }
192 /***********************************************************************/
193 RAbundVector SAbundVector::getRAbundVector(){
194         try {
195                 RAbundVector rav;
196         
197                 for(int i=1;i < data.size();i++){
198                         for(int j=0;j<data[i];j++){
199                                 rav.push_back(i);
200                         }
201                 }
202                 sort(rav.rbegin(), rav.rend());
203
204                 rav.setLabel(label);
205                 return rav;
206         }
207         catch(exception& e) {
208                 m->errorOut(e, "SAbundVector", "getRAbundVector");
209                 exit(1);
210         }
211 }
212
213 /***********************************************************************/
214
215 SAbundVector SAbundVector::getSAbundVector(){
216         return *this;                   
217 }
218
219 /***********************************************************************/
220
221 OrderVector SAbundVector::getOrderVector(map<string,int>* hold = NULL){
222         try {
223                 OrderVector ov;
224         
225                 int binIndex = 0;
226         
227                 for(int i=1;i<data.size();i++){
228                         for(int j=0;j<data[i];j++){
229                                 for(int k=0;k<i;k++){
230                                         ov.push_back(binIndex);
231                                 }
232                                 binIndex++;
233                         }
234                 }
235         
236                 random_shuffle(ov.begin(), ov.end());
237
238                 ov.setLabel(label);
239                 ov.getNumBins();
240                 return ov;
241         }
242         catch(exception& e) {
243                 m->errorOut(e, "SAbundVector", "getOrderVector");
244                 exit(1);
245         }
246 }
247
248 /***********************************************************************/