]> git.donarmstrong.com Git - mothur.git/blob - sharedsabundvector.cpp
changing command name classify.shared to classifyrf.shared
[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                 m->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                 m->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                 m->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                 m->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                 m->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                 for(int i=1;i<data.size();i++){         
195                         for(int j=0;j<data[i].abundance;j++){
196                                 rav.push_back(i, data[i].group);
197                         }
198                 }
199                 sort(rav.rbegin(), rav.rend(), compareMembers);
200         
201                 rav.setLabel(label);
202                 rav.setGroup(group);
203                 
204                 return rav;
205         }
206         catch(exception& e) {
207                 m->errorOut(e, "SharedSAbundVector", "getSharedRAbundVector");
208                 exit(1);
209         }
210 }
211
212
213 /***********************************************************************/
214
215 SharedSAbundVector SharedSAbundVector::getSharedSAbundVector(){
216         return *this;                   
217 }
218
219 /***********************************************************************/
220 SharedOrderVector SharedSAbundVector::getSharedOrderVector() {
221         try {
222                 SharedRAbundVector rav;
223                 SharedOrderVector ov;
224                 
225                 rav = this->getSharedRAbundVector();
226                 ov = rav.getSharedOrderVector();
227                 
228                 ov.updateStats();
229                 
230                 return ov;
231         }
232         catch(exception& e) {
233                 m->errorOut(e, "SharedSAbundVector", "getSharedOrderVector");
234                 exit(1);
235         }
236 }
237 /***********************************************************************/
238
239 void SharedSAbundVector::clear(){
240         numBins = 0;
241         maxRank = 0;
242         numSeqs = 0;
243         data.clear();
244 }
245
246 /***********************************************************************/
247 OrderVector SharedSAbundVector::getOrderVector(map<string,int>* hold = NULL){
248         try {
249                 OrderVector ov;
250         
251                 int binIndex = 0;
252         
253                 for(int i=1;i<data.size();i++){
254                         for(int j=0;j<data[i].abundance;j++){
255                                 for(int k=0;k<i;k++){
256                                         ov.push_back(binIndex);
257                                 }
258                                 binIndex++;
259                         }
260                 }
261         
262                 random_shuffle(ov.begin(), ov.end());
263
264                 ov.setLabel(label);
265                 ov.getNumBins();
266                 return ov;
267         }
268         catch(exception& e) {
269                 m->errorOut(e, "SharedSAbundVector", "getOrderVector");
270                 exit(1);
271         }
272 }
273
274 /***********************************************************************/
275