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 globaldata = GlobalData::getInstance();
32 maxRank = 0; numBins = 0; numSeqs = 0;
34 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
36 int num, inputData, count;
37 count = 0; numSeqs = 0;
38 string holdLabel, nextLabel, groupN;
41 //read in first row since you know there is at least 1 group.
42 f >> label >> groupN >> num;
45 if (globaldata->gGroupmap == NULL) {
46 //save group in groupmap
47 groupmap->namesOfGroups.push_back(groupN);
48 groupmap->groupIndex[groupN] = 0;
51 for(int i=0;i<num;i++){
54 for (int j = 0; j < inputData; j++) {
55 push_back(i, i, groupN);
60 if (f.eof() != true) { f >> nextLabel; }
62 //read the rest of the groups info in
63 while ((nextLabel == holdLabel) && (f.eof() != true)) {
67 if (globaldata->gGroupmap == NULL) {
68 //save group in groupmap
69 groupmap->namesOfGroups.push_back(groupN);
70 groupmap->groupIndex[groupN] = count;
73 for(int i=0;i<num;i++){
76 for (int j = 0; j < inputData; j++) {
77 push_back(i, i, groupN);
83 if (f.eof() != true) { f >> nextLabel; }
87 //put file pointer back since you are now at a new distance label
88 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
90 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
96 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 /***********************************************************************/
106 int SharedOrderVector::getNumBins(){
110 /***********************************************************************/
112 int SharedOrderVector::getNumSeqs(){
116 /***********************************************************************/
118 int SharedOrderVector::getMaxRank(){
123 /***********************************************************************/
127 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
129 data[index].group = groupName;
130 data[index].bin = binNumber;
131 data[index].abundance = abund;
132 //if (abund > maxRank) { maxRank = abund; }
136 /***********************************************************************/
138 individual SharedOrderVector::get(int index){
143 /***********************************************************************/
144 //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
145 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
147 newGuy.group = groupName;
148 newGuy.abundance = abund;
149 newGuy.bin = binNumber;
150 data.push_back(newGuy);
153 //if (abund > maxRank) { maxRank = abund; }
158 /***********************************************************************/
160 void SharedOrderVector::print(ostream& output){
162 output << label << '\t' << numSeqs << '\t';
164 for(int i=0;i<data.size();i++){
165 output << data[i].bin << '\t';
169 catch(exception& e) {
170 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180 /***********************************************************************/
182 void SharedOrderVector::resize(int){
183 cout << "resize() did nothing in class SharedOrderVector";
186 /***********************************************************************/
189 vector<individual>::iterator SharedOrderVector::begin(){
193 /***********************************************************************/
195 vector<individual>::iterator SharedOrderVector::end(){
199 /***********************************************************************/
201 int SharedOrderVector::size(){
205 /***********************************************************************/
207 RAbundVector SharedOrderVector::getRAbundVector(){
209 RAbundVector rav(data.size());
211 for(int i=0;i<numSeqs;i++){
212 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
214 sort(rav.rbegin(), rav.rend());
215 for(int i=numSeqs-1;i>=0;i--){
216 if(rav.get(i) == 0){ rav.pop_back(); }
225 catch(exception& e) {
226 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234 /***********************************************************************/
236 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
240 for (int i = 0; i < data.size(); i++) {
241 ov.push_back(data[i].bin);
244 random_shuffle(ov.begin(), ov.end());
249 catch(exception& e) {
250 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260 /***********************************************************************/
262 SAbundVector SharedOrderVector::getSAbundVector(){
264 RAbundVector rav(this->getRAbundVector());
265 return rav.getSAbundVector();
268 /***********************************************************************/
269 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
271 SharedRAbundVector sharedRav(data.size());
273 sharedRav.setLabel(label);
274 sharedRav.setGroup(group);
276 for (int i = 0; i < data.size(); i++) {
277 if (data[i].group == group) {
278 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
283 catch(exception& e) {
284 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293 /***********************************************************************/
294 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
297 util = new SharedUtil();
298 vector<SharedRAbundVector*> lookup;
300 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
301 util->getSharedVectors(globaldata->Groups, lookup, this);
305 catch(exception& e) {
306 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315 /***********************************************************************/
316 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
319 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
320 return sharedRav.getSharedSAbundVector();
323 catch(exception& e) {
324 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
328 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
334 /***********************************************************************/
336 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
337 random_shuffle(data.begin(), data.end());
341 /***********************************************************************/
343 void SharedOrderVector::updateStats(){
350 numSeqs = data.size();
352 vector<int> hold(numSeqs, 0);
353 for(int i=0;i<numSeqs;i++){
354 hold[data[i].bin] = hold[data[i].bin]+1;
357 for(int i=0;i<numSeqs;i++){
358 if(hold[i] > 0) { numBins++; }
359 if(hold[i] > maxRank) { maxRank = hold[i]; }
363 catch(exception& e) {
364 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
368 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373 /***********************************************************************/