5 * Created by Sarah Westcott on 12/5/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedrabundvector.h"
11 #include "sabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedutilities.h"
16 /***********************************************************************/
18 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {globaldata = GlobalData::getInstance();}
19 /***********************************************************************/
21 SharedRAbundVector::~SharedRAbundVector() {
22 //for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
26 /***********************************************************************/
28 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
29 globaldata = GlobalData::getInstance();
32 for (int i=0; i< n; i++) {
35 data.push_back(newGuy);
39 /***********************************************************************
41 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
47 for(int i=0;i<data.size();i++){
48 if(data[i].abundance != 0) { numBins = i+1; }
49 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
50 numSeqs += data[i].abundance;
54 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
64 /***********************************************************************/
66 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
68 globaldata = GlobalData::getInstance();
70 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
72 int num, inputData, count;
74 string holdLabel, nextLabel, groupN;
77 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
80 //read in first row since you know there is at least 1 group.
81 f >> label >> groupN >> num;
84 //add new vector to lookup
85 SharedRAbundVector* temp = new SharedRAbundVector();
86 lookup.push_back(temp);
87 lookup[0]->setLabel(label);
88 lookup[0]->setGroup(groupN);
90 if (globaldata->gGroupmap == NULL) {
91 //save group in groupmap
92 groupmap->namesOfGroups.push_back(groupN);
93 groupmap->groupIndex[groupN] = 0;
96 //fill vector. data = first sharedrabund in file
97 for(int i=0;i<num;i++){
100 lookup[0]->push_back(inputData, i, groupN); //abundance, bin, group
101 push_back(inputData, i, groupN);
102 numSeqs += inputData;
104 if (inputData > maxRank) { maxRank = inputData; }
108 if (f.eof() != true) { f >> nextLabel; }
110 //read the rest of the groups info in
111 while ((nextLabel == holdLabel) && (f.eof() != true)) {
115 if (globaldata->gGroupmap == NULL) {
116 //save group in groupmap
117 groupmap->namesOfGroups.push_back(groupN);
118 groupmap->groupIndex[groupN] = count;
121 //add new vector to lookup
122 temp = new SharedRAbundVector();
123 lookup.push_back(temp);
124 lookup[count]->setLabel(label);
125 lookup[count]->setGroup(groupN);
128 for(int i=0;i<num;i++){
130 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
134 if (f.eof() != true) { f >> nextLabel; }
137 //put file pointer back since you are now at a new distance label
138 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
140 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
143 catch(exception& e) {
144 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153 /***********************************************************************/
155 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
157 int oldBinSize = data[binNumber].abundance;
158 data[binNumber].abundance = newBinSize;
159 data[binNumber].group = groupname;
161 if(newBinSize > maxRank) { maxRank = newBinSize; }
163 numSeqs += (newBinSize - oldBinSize);
165 catch(exception& e) {
166 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 /***********************************************************************/
176 void SharedRAbundVector::setData(vector <individual> newData){
180 /***********************************************************************/
182 int SharedRAbundVector::getAbundance(int index){
183 return data[index].abundance;
186 /***********************************************************************/
188 int SharedRAbundVector::numNZ(){
190 for(int i = 1; i < numBins; i++)
191 if(data[i].abundance > 0)
195 /***********************************************************************/
197 void SharedRAbundVector::sortD(){
198 struct individual indObj;
199 sort(data.begin()+1, data.end(), indObj);
201 /***********************************************************************/
203 individual SharedRAbundVector::get(int index){
207 /***********************************************************************/
209 vector <individual> SharedRAbundVector::getData(){
212 /***********************************************************************/
214 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
217 newGuy.abundance = binSize;
218 newGuy.group = groupName;
221 data.push_back(newGuy);
224 if(binSize > maxRank){
230 catch(exception& e) {
231 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 /***********************************************************************/
242 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
245 newGuy.abundance = binSize;
246 newGuy.group = groupName;
249 data.insert(data.begin()+otu, newGuy);
252 if(binSize > maxRank){
258 catch(exception& e) {
259 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268 /***********************************************************************/
270 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
273 newGuy.abundance = binSize;
274 newGuy.group = groupName;
277 data.insert(data.begin(), newGuy);
280 if(binSize > maxRank){
286 catch(exception& e) {
287 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296 /***********************************************************************/
297 void SharedRAbundVector::pop_back(){
298 numSeqs -= data[data.size()-1].abundance;
300 return data.pop_back();
303 /***********************************************************************/
306 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
307 return data.rbegin();
310 /***********************************************************************/
312 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
316 /***********************************************************************/
317 void SharedRAbundVector::resize(int size){
322 /***********************************************************************/
324 int SharedRAbundVector::size(){
328 /***********************************************************************/
329 void SharedRAbundVector::print(ostream& output){
331 output << numBins << '\t';
333 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
336 catch(exception& e) {
337 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
341 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345 /***********************************************************************/
346 string SharedRAbundVector::getGroup(){
350 /***********************************************************************/
352 void SharedRAbundVector::setGroup(string groupName){
355 /***********************************************************************/
356 int SharedRAbundVector::getGroupIndex() { return index; }
357 /***********************************************************************/
358 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
359 /***********************************************************************/
360 int SharedRAbundVector::getNumBins(){
364 /***********************************************************************/
366 int SharedRAbundVector::getNumSeqs(){
370 /***********************************************************************/
372 int SharedRAbundVector::getMaxRank(){
375 /***********************************************************************/
377 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
380 /***********************************************************************/
381 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
384 util = new SharedUtil();
386 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
388 for (int i = 0; i < lookup.size(); i++) {
389 //if this sharedrabund is not from a group the user wants then delete it.
390 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
392 lookup.erase(lookup.begin()+i);
401 catch(exception& e) {
402 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
406 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
410 /***********************************************************************/
412 RAbundVector SharedRAbundVector::getRAbundVector() {
414 RAbundVector rav(data.size());
416 for (int i = 0; i < data.size(); i++) {
417 rav.set(i, data[i].abundance);
422 catch(exception& e) {
423 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
427 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
431 /***********************************************************************/
433 RAbundVector SharedRAbundVector::getRAbundVector2() {
436 for(int i = 0; i < numBins; i++)
437 if(data[i].abundance != 0)
438 rav.push_back(data[i].abundance-1);
441 catch(exception& e) {
442 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
446 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
450 /***********************************************************************/
452 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
454 SharedSAbundVector sav(maxRank+1);
456 for(int i=0;i<data.size();i++){
457 int abund = data[i].abundance;
458 sav.set(abund, sav.getAbundance(abund) + 1, group);
461 sav.set(0, 0, group);
467 catch(exception& e) {
468 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
472 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
476 /***********************************************************************/
478 SAbundVector SharedRAbundVector::getSAbundVector() {
480 SAbundVector sav(maxRank+1);
482 for(int i=0;i<data.size();i++){
483 int abund = data[i].abundance;
484 sav.set(abund, sav.get(abund) + 1);
490 catch(exception& e) {
491 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
495 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
500 /***********************************************************************/
502 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
504 SharedOrderVector ov;
506 for(int i=0;i<data.size();i++){
507 for(int j=0;j<data[i].abundance;j++){
508 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
511 random_shuffle(ov.begin(), ov.end());
518 catch(exception& e) {
519 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
523 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
527 /***********************************************************************/
529 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
533 for(int i=0;i<data.size();i++){
534 for(int j=0;j<data[i].abundance;j++){
538 random_shuffle(ov.begin(), ov.end());
543 catch(exception& e) {
544 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
548 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
553 /***********************************************************************/