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();}
20 /***********************************************************************/
22 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
23 globaldata = GlobalData::getInstance();
26 for (int i=0; i< n; i++) {
29 data.push_back(newGuy);
33 /***********************************************************************
35 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
41 for(int i=0;i<data.size();i++){
42 if(data[i].abundance != 0) { numBins = i+1; }
43 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
44 numSeqs += data[i].abundance;
48 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58 /***********************************************************************/
60 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
62 globaldata = GlobalData::getInstance();
64 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
66 int num, inputData, pos, count;
68 string holdLabel, nextLabel, groupN;
71 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
74 //read in first row since you know there is at least 1 group.
75 f >> label >> groupN >> num;
78 //add new vector to lookup
79 SharedRAbundVector* temp = new SharedRAbundVector();
80 lookup.push_back(temp);
81 lookup[0]->setLabel(label);
82 lookup[0]->setGroup(groupN);
84 if (globaldata->gGroupmap == NULL) {
85 //save group in groupmap
86 groupmap->namesOfGroups.push_back(groupN);
87 groupmap->groupIndex[groupN] = 0;
90 //fill vector. data = first sharedrabund in file
91 for(int i=0;i<num;i++){
94 lookup[0]->push_back(inputData, i, groupN); //abundance, bin, group
95 push_back(inputData, i, groupN);
98 if (inputData > maxRank) { maxRank = inputData; }
102 //save position in file in case next line is a new label.
105 if (f.eof() != true) { f >> nextLabel; }
107 //read the rest of the groups info in
108 while ((nextLabel == holdLabel) && (f.eof() != true)) {
112 if (globaldata->gGroupmap == NULL) {
113 //save group in groupmap
114 groupmap->namesOfGroups.push_back(groupN);
115 groupmap->groupIndex[groupN] = count;
118 //add new vector to lookup
119 temp = new SharedRAbundVector();
120 lookup.push_back(temp);
121 lookup[count]->setLabel(label);
122 lookup[count]->setGroup(groupN);
125 for(int i=0;i<num;i++){
127 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
130 //save position in file in case next line is a new label.
133 if (f.eof() != true) { f >> nextLabel; }
136 //put file pointer back since you are now at a new distance label
137 f.seekg(pos, ios::beg);
139 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
142 catch(exception& e) {
143 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152 /***********************************************************************/
154 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
156 int oldBinSize = data[binNumber].abundance;
157 data[binNumber].abundance = newBinSize;
158 data[binNumber].group = groupname;
160 if(newBinSize > maxRank) { maxRank = newBinSize; }
162 numSeqs += (newBinSize - oldBinSize);
164 catch(exception& e) {
165 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 /***********************************************************************/
175 void SharedRAbundVector::setData(vector <individual> newData){
179 /***********************************************************************/
181 int SharedRAbundVector::getAbundance(int index){
182 return data[index].abundance;
185 /***********************************************************************/
187 int SharedRAbundVector::numNZ(){
189 for(int i = 1; i < numBins; i++)
190 if(data[i].abundance > 0)
194 /***********************************************************************/
196 void SharedRAbundVector::sortD(){
197 struct individual indObj;
198 sort(data.begin()+1, data.end(), indObj);
200 /***********************************************************************/
202 individual SharedRAbundVector::get(int index){
206 /***********************************************************************/
208 vector <individual> SharedRAbundVector::getData(){
211 /***********************************************************************/
213 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
216 newGuy.abundance = binSize;
217 newGuy.group = groupName;
220 data.push_back(newGuy);
223 if(binSize > maxRank){
229 catch(exception& e) {
230 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239 /***********************************************************************/
241 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
244 newGuy.abundance = binSize;
245 newGuy.group = groupName;
248 data.insert(data.begin()+otu, newGuy);
251 if(binSize > maxRank){
257 catch(exception& e) {
258 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267 /***********************************************************************/
269 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
272 newGuy.abundance = binSize;
273 newGuy.group = groupName;
276 data.insert(data.begin(), newGuy);
279 if(binSize > maxRank){
285 catch(exception& e) {
286 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 /***********************************************************************/
296 void SharedRAbundVector::pop_back(){
297 numSeqs -= data[data.size()-1].abundance;
299 return data.pop_back();
302 /***********************************************************************/
305 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
306 return data.rbegin();
309 /***********************************************************************/
311 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
315 /***********************************************************************/
316 void SharedRAbundVector::resize(int size){
321 /***********************************************************************/
323 int SharedRAbundVector::size(){
327 /***********************************************************************/
328 void SharedRAbundVector::print(ostream& output){
330 output << numBins << '\t';
332 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
335 catch(exception& e) {
336 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
344 /***********************************************************************/
345 string SharedRAbundVector::getGroup(){
349 /***********************************************************************/
351 void SharedRAbundVector::setGroup(string groupName){
354 /***********************************************************************/
355 int SharedRAbundVector::getGroupIndex() { return index; }
356 /***********************************************************************/
357 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
358 /***********************************************************************/
359 int SharedRAbundVector::getNumBins(){
363 /***********************************************************************/
365 int SharedRAbundVector::getNumSeqs(){
369 /***********************************************************************/
371 int SharedRAbundVector::getMaxRank(){
374 /***********************************************************************/
376 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
379 /***********************************************************************/
380 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
383 util = new SharedUtil();
385 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
387 for (int i = 0; i < lookup.size(); i++) {
388 //if this sharedrabund is not from a group the user wants then delete it.
389 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
391 lookup.erase(lookup.begin()+i);
398 catch(exception& e) {
399 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
407 /***********************************************************************/
409 RAbundVector SharedRAbundVector::getRAbundVector() {
411 RAbundVector rav(data.size());
413 for (int i = 0; i < data.size(); i++) {
414 rav.set(i, data[i].abundance);
419 catch(exception& e) {
420 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
424 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428 /***********************************************************************/
430 RAbundVector SharedRAbundVector::getRAbundVector2() {
433 for(int i = 0; i < numBins; i++)
434 if(data[i].abundance != 0)
435 rav.push_back(data[i].abundance-1);
438 catch(exception& e) {
439 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
443 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
447 /***********************************************************************/
449 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
451 SharedSAbundVector sav(maxRank+1);
453 for(int i=0;i<data.size();i++){
454 int abund = data[i].abundance;
455 sav.set(abund, sav.getAbundance(abund) + 1, group);
458 sav.set(0, 0, group);
464 catch(exception& e) {
465 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
469 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
473 /***********************************************************************/
475 SAbundVector SharedRAbundVector::getSAbundVector() {
477 SAbundVector sav(maxRank+1);
479 for(int i=0;i<data.size();i++){
480 int abund = data[i].abundance;
481 sav.set(abund, sav.get(abund) + 1);
487 catch(exception& e) {
488 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
492 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
497 /***********************************************************************/
499 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
501 SharedOrderVector ov;
503 for(int i=0;i<data.size();i++){
504 for(int j=0;j<data[i].abundance;j++){
505 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
508 random_shuffle(ov.begin(), ov.end());
515 catch(exception& e) {
516 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
520 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
524 /***********************************************************************/
526 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
530 for(int i=0;i<data.size();i++){
531 for(int j=0;j<data[i].abundance;j++){
535 random_shuffle(ov.begin(), ov.end());
540 catch(exception& e) {
541 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
545 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
550 /***********************************************************************/