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