5 * Created by Sarah Westcott on 12/5/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
13 #include "sharedrabundvector.h"
14 #include "sabundvector.hpp"
15 #include "ordervector.hpp"
16 #include "sharedutilities.h"
19 /***********************************************************************/
21 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {};
23 /***********************************************************************/
25 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
28 for (int i=0; i< n; i++) {
31 data.push_back(newGuy);
35 /***********************************************************************
37 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
43 for(int i=0;i<data.size();i++){
44 if(data[i].abundance != 0) { numBins = i+1; }
45 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
46 numSeqs += data[i].abundance;
50 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
54 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
60 /***********************************************************************/
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
64 globaldata = GlobalData::getInstance();
66 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
68 int num, inputData, pos, count;
70 string holdLabel, nextLabel, groupN;
73 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
76 //read in first row since you know there is at least 1 group.
77 f >> label >> groupN >> num;
80 //add new vector to lookup
81 lookup.push_back(new SharedRAbundVector());
82 lookup[0]->setLabel(label);
83 lookup[0]->setGroup(groupN);
85 if (globaldata->gGroupmap == NULL) {
86 //save group in groupmap
87 groupmap->namesOfGroups.push_back(groupN);
88 groupmap->groupIndex[groupN] = 0;
91 //fill vector. data = first sharedrabund in file
92 for(int i=0;i<num;i++){
95 lookup[0]->push_back(inputData, i, groupN); //abundance, bin, group
96 push_back(inputData, i, groupN);
99 if (inputData > maxRank) { maxRank = inputData; }
103 //save position in file in case next line is a new label.
106 if (f.eof() != true) { f >> nextLabel; }
108 //read the rest of the groups info in
109 while ((nextLabel == holdLabel) && (f.eof() != true)) {
113 if (globaldata->gGroupmap == NULL) {
114 //save group in groupmap
115 groupmap->namesOfGroups.push_back(groupN);
116 groupmap->groupIndex[groupN] = count;
119 //add new vector to lookup
120 lookup.push_back(new SharedRAbundVector());
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 SharedRAbundVector::~SharedRAbundVector() {
158 /***********************************************************************/
160 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
162 int oldBinSize = data[binNumber].abundance;
163 data[binNumber].abundance = newBinSize;
164 data[binNumber].group = groupname;
166 if(newBinSize > maxRank) { maxRank = newBinSize; }
168 numSeqs += (newBinSize - oldBinSize);
170 catch(exception& e) {
171 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 /***********************************************************************/
181 void SharedRAbundVector::setData(vector <individual> newData){
185 /***********************************************************************/
187 int SharedRAbundVector::getAbundance(int index){
188 return data[index].abundance;
191 /***********************************************************************/
193 int SharedRAbundVector::numNZ(){
195 for(int i = 1; i < numBins; i++)
196 if(data[i].abundance > 0)
200 /***********************************************************************/
202 void SharedRAbundVector::sortD(){
203 struct individual indObj;
204 sort(data.begin()+1, data.end(), indObj);
206 /***********************************************************************/
208 individual SharedRAbundVector::get(int index){
212 /***********************************************************************/
214 vector <individual> SharedRAbundVector::getData(){
217 /***********************************************************************/
219 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
222 newGuy.abundance = binSize;
223 newGuy.group = groupName;
226 data.push_back(newGuy);
229 if(binSize > maxRank){
235 catch(exception& e) {
236 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245 /***********************************************************************/
247 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
250 newGuy.abundance = binSize;
251 newGuy.group = groupName;
254 data.insert(data.begin()+otu, newGuy);
257 if(binSize > maxRank){
263 catch(exception& e) {
264 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273 /***********************************************************************/
275 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
278 newGuy.abundance = binSize;
279 newGuy.group = groupName;
282 data.insert(data.begin(), newGuy);
285 if(binSize > maxRank){
291 catch(exception& e) {
292 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301 /***********************************************************************/
302 void SharedRAbundVector::pop_back(){
303 numSeqs -= data[data.size()-1].abundance;
305 return data.pop_back();
308 /***********************************************************************/
311 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
312 return data.rbegin();
315 /***********************************************************************/
317 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
321 /***********************************************************************/
322 void SharedRAbundVector::resize(int size){
327 /***********************************************************************/
329 int SharedRAbundVector::size(){
333 /***********************************************************************/
334 void SharedRAbundVector::print(ostream& output){
336 output << numBins << '\t';
338 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
341 catch(exception& e) {
342 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
346 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
350 /***********************************************************************/
351 string SharedRAbundVector::getGroup(){
355 /***********************************************************************/
357 void SharedRAbundVector::setGroup(string groupName){
360 /***********************************************************************/
361 int SharedRAbundVector::getGroupIndex() { return index; }
362 /***********************************************************************/
363 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
364 /***********************************************************************/
365 int SharedRAbundVector::getNumBins(){
369 /***********************************************************************/
371 int SharedRAbundVector::getNumSeqs(){
375 /***********************************************************************/
377 int SharedRAbundVector::getMaxRank(){
380 /***********************************************************************/
382 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
385 /***********************************************************************/
386 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
389 util = new SharedUtil();
391 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
393 for (int i = 0; i < lookup.size(); i++) {
394 //if this sharedrabund is not from a group the user wants then delete it.
395 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
397 lookup.erase(lookup.begin()+i);
404 catch(exception& e) {
405 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
413 /***********************************************************************/
415 RAbundVector SharedRAbundVector::getRAbundVector() {
417 RAbundVector rav(data.size());
419 for (int i = 0; i < data.size(); i++) {
420 rav.set(i, data[i].abundance);
425 catch(exception& e) {
426 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
430 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
434 /***********************************************************************/
436 RAbundVector SharedRAbundVector::getRAbundVector2() {
439 for(int i = 0; i < numBins; i++)
440 if(data[i].abundance != 0)
441 rav.push_back(data[i].abundance-1);
444 catch(exception& e) {
445 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
453 /***********************************************************************/
455 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
457 SharedSAbundVector sav(maxRank+1);
459 for(int i=0;i<data.size();i++){
460 int abund = data[i].abundance;
461 sav.set(abund, sav.getAbundance(abund) + 1, group);
464 sav.set(0, 0, group);
470 catch(exception& e) {
471 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
479 /***********************************************************************/
481 SAbundVector SharedRAbundVector::getSAbundVector() {
483 SAbundVector sav(maxRank+1);
485 for(int i=0;i<data.size();i++){
486 int abund = data[i].abundance;
487 sav.set(abund, sav.get(abund) + 1);
493 catch(exception& e) {
494 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
498 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
503 /***********************************************************************/
505 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
507 SharedOrderVector ov;
509 for(int i=0;i<data.size();i++){
510 for(int j=0;j<data[i].abundance;j++){
511 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
514 random_shuffle(ov.begin(), ov.end());
521 catch(exception& e) {
522 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
526 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
530 /***********************************************************************/
532 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
536 for(int i=0;i<data.size();i++){
537 for(int j=0;j<data[i].abundance;j++){
541 random_shuffle(ov.begin(), ov.end());
546 catch(exception& e) {
547 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
551 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
556 /***********************************************************************/