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