2 * sharedSharedOrderVector.cpp
5 * Created by Sarah Westcott on 12/9/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedordervector.h"
11 #include "sharedutilities.h"
13 /***********************************************************************/
15 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {}
17 /***********************************************************************/
19 SharedOrderVector::SharedOrderVector(string id, vector<individual> ov) :
20 DataVector(id), data(ov)
25 /***********************************************************************/
26 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
27 //if you don't use a list and groupfile.
29 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() { //reads in a shared file
31 maxRank = 0; numBins = 0; numSeqs = 0;
33 groupmap = new GroupMap();
35 int num, inputData, count;
36 count = 0; numSeqs = 0;
37 string holdLabel, nextLabel, groupN;
40 //read in first row since you know there is at least 1 group.
41 f >> label >> groupN >> num;
45 //save group in groupmap
46 groupmap->namesOfGroups.push_back(groupN);
47 groupmap->groupIndex[groupN] = 0;
50 for(int i=0;i<num;i++){
53 for (int j = 0; j < inputData; j++) {
54 push_back(i, i, groupN);
61 if (f.eof() != true) { f >> nextLabel; }
63 //read the rest of the groups info in
64 while ((nextLabel == holdLabel) && (f.eof() != true)) {
69 //save group in groupmap
70 groupmap->namesOfGroups.push_back(groupN);
71 groupmap->groupIndex[groupN] = count;
74 for(int i=0;i<num;i++){
77 for (int j = 0; j < inputData; j++) {
78 push_back(i, i, groupN);
85 if (f.eof() != true) { f >> nextLabel; }
89 //put file pointer back since you are now at a new distance label
90 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
92 m->namesOfGroups = groupmap->namesOfGroups;
98 m->errorOut(e, "SharedOrderVector", "SharedOrderVector");
102 /***********************************************************************/
104 int SharedOrderVector::getNumBins(){
108 /***********************************************************************/
110 int SharedOrderVector::getNumSeqs(){
114 /***********************************************************************/
116 int SharedOrderVector::getMaxRank(){
121 /***********************************************************************/
125 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
127 data[index].group = groupName;
128 data[index].bin = binNumber;
129 data[index].abundance = abund;
130 //if (abund > maxRank) { maxRank = abund; }
134 /***********************************************************************/
136 individual SharedOrderVector::get(int index){
141 /***********************************************************************/
142 //commented updateStats out to improve speed, but whoever calls this must remember to update when they are done with all the pushbacks they are doing
143 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
145 newGuy.group = groupName;
146 newGuy.abundance = abund;
147 newGuy.bin = binNumber;
148 data.push_back(newGuy);
151 //if (abund > maxRank) { maxRank = abund; }
156 /***********************************************************************/
158 void SharedOrderVector::print(ostream& output){
160 output << label << '\t' << numSeqs << '\t';
162 for(int i=0;i<data.size();i++){
163 output << data[i].bin << '\t';
167 catch(exception& e) {
168 m->errorOut(e, "SharedOrderVector", "print");
173 /***********************************************************************/
175 void SharedOrderVector::clear(){
181 /***********************************************************************/
183 void SharedOrderVector::resize(int){
184 m->mothurOut("resize() did nothing in class SharedOrderVector");
187 /***********************************************************************/
190 vector<individual>::iterator SharedOrderVector::begin(){
194 /***********************************************************************/
196 vector<individual>::iterator SharedOrderVector::end(){
200 /***********************************************************************/
202 int SharedOrderVector::size(){
206 /***********************************************************************/
208 RAbundVector SharedOrderVector::getRAbundVector(){
210 RAbundVector rav(data.size());
212 for(int i=0;i<numSeqs;i++){
213 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
215 sort(rav.rbegin(), rav.rend());
216 for(int i=numSeqs-1;i>=0;i--){
217 if(rav.get(i) == 0){ rav.pop_back(); }
226 catch(exception& e) {
227 m->errorOut(e, "SharedOrderVector", "getRAbundVector");
231 /***********************************************************************/
233 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
237 for (int i = 0; i < data.size(); i++) {
238 ov.push_back(data[i].bin);
241 random_shuffle(ov.begin(), ov.end());
246 catch(exception& e) {
247 m->errorOut(e, "SharedOrderVector", "getOrderVector");
253 /***********************************************************************/
255 SAbundVector SharedOrderVector::getSAbundVector(){
257 RAbundVector rav(this->getRAbundVector());
258 return rav.getSAbundVector();
261 /***********************************************************************/
262 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
264 SharedRAbundVector sharedRav(data.size());
266 sharedRav.setLabel(label);
267 sharedRav.setGroup(group);
269 for (int i = 0; i < data.size(); i++) {
270 if (data[i].group == group) {
271 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
276 catch(exception& e) {
277 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
281 /***********************************************************************/
282 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
285 util = new SharedUtil();
286 vector<SharedRAbundVector*> lookup;
288 util->setGroups(m->Groups, m->namesOfGroups);
289 util->getSharedVectors(m->Groups, lookup, this);
293 catch(exception& e) {
294 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
298 /***********************************************************************/
299 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
302 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
303 return sharedRav.getSharedSAbundVector();
306 catch(exception& e) {
307 m->errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
312 /***********************************************************************/
314 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
315 random_shuffle(data.begin(), data.end());
319 /***********************************************************************/
321 void SharedOrderVector::updateStats(){
328 numSeqs = data.size();
330 vector<int> hold(numSeqs, 0);
331 for(int i=0;i<numSeqs;i++){
332 hold[data[i].bin] = hold[data[i].bin]+1;
335 for(int i=0;i<numSeqs;i++){
336 if(hold[i] > 0) { numBins++; }
337 if(hold[i] > maxRank) { maxRank = hold[i]; }
341 catch(exception& e) {
342 m->errorOut(e, "SharedOrderVector", "updateStats");
347 /***********************************************************************/