]> git.donarmstrong.com Git - mothur.git/blob - sabundvector.cpp
update .gitignore
[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                         set(i, inputData);
64                 }
65
66         }
67         catch(exception& e) {
68                 m->errorOut(e, "SAbundVector", "SAbundVector");
69                 exit(1);
70         }
71 }
72
73
74 /***********************************************************************/
75
76 void SAbundVector::set(int sabund, int abundance){
77         try {
78
79                 int initSize = data[sabund];
80                 data[sabund] = abundance;
81         
82                 if(sabund != 0){
83                         numBins += (abundance - initSize);
84                 }
85         
86                 numSeqs += sabund * (abundance - initSize);
87         
88                 if(sabund > maxRank)    {       maxRank = sabund;               }
89         }
90         catch(exception& e) {
91                 m->errorOut(e, "SAbundVector", "set");
92                 exit(1);
93         }
94 }
95
96
97 /***********************************************************************/
98
99 int SAbundVector::get(int index){
100         return data[index];
101 }
102
103 /***********************************************************************/
104
105 void SAbundVector::push_back(int abundance){
106         try {
107                 data.push_back(abundance);
108         
109                 maxRank++;      
110         
111                 numBins += abundance;
112         
113                 numSeqs += (maxRank * abundance);
114         }
115         catch(exception& e) {
116                 m->errorOut(e, "SAbundVector", "push_back");
117                 exit(1);
118         }
119 }
120 /***********************************************************************/
121
122 void SAbundVector::quicksort(){
123         sort(data.rbegin(), data.rend());
124 }
125
126 /***********************************************************************/
127
128 int SAbundVector::sum(){
129         VecCalc vecCalc;
130         return vecCalc.sumElements(data);
131 }
132
133 /***********************************************************************/
134
135 void SAbundVector::resize(int size){
136         data.resize(size);
137 }
138
139 /***********************************************************************/
140
141 int SAbundVector::size(){
142         return data.size();             
143 }
144
145 /***********************************************************************/
146 void SAbundVector::print(string prefix, ostream& output){
147         
148         output << prefix << '\t' << maxRank << '\t';
149         
150         for(int i=1;i<=maxRank;i++){
151                 output << data[i] << '\t';
152         }
153         output << endl;
154 }
155 /***********************************************************************/
156 void SAbundVector::clear(){
157         numBins = 0;
158         maxRank = 0;
159         numSeqs = 0;
160         data.clear();   
161 }
162 /***********************************************************************/
163 void SAbundVector::print(ostream& output){
164         try {
165                 output << label << '\t' << maxRank << '\t';
166         
167                 for(int i=1;i<=maxRank;i++){
168                         output << data[i] << '\t';
169                 }
170                 output << endl;
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "SAbundVector", "print");
174                 exit(1);
175         }
176 }
177
178 /**********************************************************************/
179 int SAbundVector::getNumBins(){
180 //      if(needToUpdate == 1){  updateStats();  }
181         return numBins;
182 }
183
184 /***********************************************************************/
185
186 int SAbundVector::getNumSeqs(){
187 //      if(needToUpdate == 1){  updateStats();  }
188         return numSeqs;
189 }
190
191 /***********************************************************************/
192
193 int SAbundVector::getMaxRank(){
194 //      if(needToUpdate == 1){  updateStats();  }
195         return maxRank;
196 }
197 /***********************************************************************/
198 RAbundVector SAbundVector::getRAbundVector(){
199         try {
200                 RAbundVector rav;
201         
202                 for(int i=1;i < data.size();i++){
203                         for(int j=0;j<data[i];j++){
204                                 rav.push_back(i);
205                         }
206                 }
207                 sort(rav.rbegin(), rav.rend());
208
209                 rav.setLabel(label);
210                 return rav;
211         }
212         catch(exception& e) {
213                 m->errorOut(e, "SAbundVector", "getRAbundVector");
214                 exit(1);
215         }
216 }
217
218 /***********************************************************************/
219
220 SAbundVector SAbundVector::getSAbundVector(){
221         return *this;                   
222 }
223
224 /***********************************************************************/
225
226 OrderVector SAbundVector::getOrderVector(map<string,int>* hold = NULL){
227         try {
228                 OrderVector ov;
229         
230                 int binIndex = 0;
231         
232                 for(int i=1;i<data.size();i++){
233                         for(int j=0;j<data[i];j++){
234                                 for(int k=0;k<i;k++){
235                                         ov.push_back(binIndex);
236                                 }
237                                 binIndex++;
238                         }
239                 }
240         
241                 random_shuffle(ov.begin(), ov.end());
242
243                 ov.setLabel(label);
244                 ov.getNumBins();
245                 return ov;
246         }
247         catch(exception& e) {
248                 m->errorOut(e, "SAbundVector", "getOrderVector");
249                 exit(1);
250         }
251 }
252
253 /***********************************************************************/