]> git.donarmstrong.com Git - mothur.git/blob - sabundvector.cpp
working on pam
[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 void SAbundVector::clear(){
158         numBins = 0;
159         maxRank = 0;
160         numSeqs = 0;
161         data.clear();   
162 }
163 /***********************************************************************/
164 void SAbundVector::print(ostream& output){
165         try {
166                 output << label << '\t' << maxRank << '\t';
167         
168                 for(int i=1;i<=maxRank;i++){
169                         output << data[i] << '\t';
170                 }
171                 output << endl;
172         }
173         catch(exception& e) {
174                 m->errorOut(e, "SAbundVector", "print");
175                 exit(1);
176         }
177 }
178
179 /**********************************************************************/
180 int SAbundVector::getNumBins(){
181 //      if(needToUpdate == 1){  updateStats();  }
182         return numBins;
183 }
184
185 /***********************************************************************/
186
187 int SAbundVector::getNumSeqs(){
188 //      if(needToUpdate == 1){  updateStats();  }
189         return numSeqs;
190 }
191
192 /***********************************************************************/
193
194 int SAbundVector::getMaxRank(){
195 //      if(needToUpdate == 1){  updateStats();  }
196         return maxRank;
197 }
198 /***********************************************************************/
199 RAbundVector SAbundVector::getRAbundVector(){
200         try {
201                 RAbundVector rav;
202         
203                 for(int i=1;i < data.size();i++){
204                         for(int j=0;j<data[i];j++){
205                                 rav.push_back(i);
206                         }
207                 }
208                 sort(rav.rbegin(), rav.rend());
209
210                 rav.setLabel(label);
211                 return rav;
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "SAbundVector", "getRAbundVector");
215                 exit(1);
216         }
217 }
218
219 /***********************************************************************/
220
221 SAbundVector SAbundVector::getSAbundVector(){
222         return *this;                   
223 }
224
225 /***********************************************************************/
226
227 OrderVector SAbundVector::getOrderVector(map<string,int>* hold = NULL){
228         try {
229                 OrderVector ov;
230         
231                 int binIndex = 0;
232         
233                 for(int i=1;i<data.size();i++){
234                         for(int j=0;j<data[i];j++){
235                                 for(int k=0;k<i;k++){
236                                         ov.push_back(binIndex);
237                                 }
238                                 binIndex++;
239                         }
240                 }
241         
242                 random_shuffle(ov.begin(), ov.end());
243
244                 ov.setLabel(label);
245                 ov.getNumBins();
246                 return ov;
247         }
248         catch(exception& e) {
249                 m->errorOut(e, "SAbundVector", "getOrderVector");
250                 exit(1);
251         }
252 }
253
254 /***********************************************************************/