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 SharedRAbundVector* temp = new SharedRAbundVector();
82 lookup.push_back(temp);
83 lookup[0]->setLabel(label);
84 lookup[0]->setGroup(groupN);
86 if (globaldata->gGroupmap == NULL) {
87 //save group in groupmap
88 groupmap->namesOfGroups.push_back(groupN);
89 groupmap->groupIndex[groupN] = 0;
92 //fill vector. data = first sharedrabund in file
93 for(int i=0;i<num;i++){
96 lookup[0]->push_back(inputData, i, groupN); //abundance, bin, group
97 push_back(inputData, i, groupN);
100 if (inputData > maxRank) { maxRank = inputData; }
104 //save position in file in case next line is a new label.
107 if (f.eof() != true) { f >> nextLabel; }
109 //read the rest of the groups info in
110 while ((nextLabel == holdLabel) && (f.eof() != true)) {
114 if (globaldata->gGroupmap == NULL) {
115 //save group in groupmap
116 groupmap->namesOfGroups.push_back(groupN);
117 groupmap->groupIndex[groupN] = count;
120 //add new vector to lookup
121 temp = new SharedRAbundVector();
122 lookup.push_back(temp);
123 lookup[count]->setLabel(label);
124 lookup[count]->setGroup(groupN);
127 for(int i=0;i<num;i++){
129 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
132 //save position in file in case next line is a new label.
135 if (f.eof() != true) { f >> nextLabel; }
138 //put file pointer back since you are now at a new distance label
139 f.seekg(pos, ios::beg);
141 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
144 catch(exception& e) {
145 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
154 /***********************************************************************/
156 SharedRAbundVector::~SharedRAbundVector() {
160 /***********************************************************************/
162 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
164 int oldBinSize = data[binNumber].abundance;
165 data[binNumber].abundance = newBinSize;
166 data[binNumber].group = groupname;
168 if(newBinSize > maxRank) { maxRank = newBinSize; }
170 numSeqs += (newBinSize - oldBinSize);
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181 /***********************************************************************/
183 void SharedRAbundVector::setData(vector <individual> newData){
187 /***********************************************************************/
189 int SharedRAbundVector::getAbundance(int index){
190 return data[index].abundance;
193 /***********************************************************************/
195 int SharedRAbundVector::numNZ(){
197 for(int i = 1; i < numBins; i++)
198 if(data[i].abundance > 0)
202 /***********************************************************************/
204 void SharedRAbundVector::sortD(){
205 struct individual indObj;
206 sort(data.begin()+1, data.end(), indObj);
208 /***********************************************************************/
210 individual SharedRAbundVector::get(int index){
214 /***********************************************************************/
216 vector <individual> SharedRAbundVector::getData(){
219 /***********************************************************************/
221 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
224 newGuy.abundance = binSize;
225 newGuy.group = groupName;
228 data.push_back(newGuy);
231 if(binSize > maxRank){
237 catch(exception& e) {
238 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
242 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247 /***********************************************************************/
249 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
252 newGuy.abundance = binSize;
253 newGuy.group = groupName;
256 data.insert(data.begin()+otu, newGuy);
259 if(binSize > maxRank){
265 catch(exception& e) {
266 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275 /***********************************************************************/
277 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
280 newGuy.abundance = binSize;
281 newGuy.group = groupName;
284 data.insert(data.begin(), newGuy);
287 if(binSize > maxRank){
293 catch(exception& e) {
294 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 /***********************************************************************/
304 void SharedRAbundVector::pop_back(){
305 numSeqs -= data[data.size()-1].abundance;
307 return data.pop_back();
310 /***********************************************************************/
313 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
314 return data.rbegin();
317 /***********************************************************************/
319 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
323 /***********************************************************************/
324 void SharedRAbundVector::resize(int size){
329 /***********************************************************************/
331 int SharedRAbundVector::size(){
335 /***********************************************************************/
336 void SharedRAbundVector::print(ostream& output){
338 output << numBins << '\t';
340 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
343 catch(exception& e) {
344 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
348 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352 /***********************************************************************/
353 string SharedRAbundVector::getGroup(){
357 /***********************************************************************/
359 void SharedRAbundVector::setGroup(string groupName){
362 /***********************************************************************/
363 int SharedRAbundVector::getGroupIndex() { return index; }
364 /***********************************************************************/
365 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
366 /***********************************************************************/
367 int SharedRAbundVector::getNumBins(){
371 /***********************************************************************/
373 int SharedRAbundVector::getNumSeqs(){
377 /***********************************************************************/
379 int SharedRAbundVector::getMaxRank(){
382 /***********************************************************************/
384 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
387 /***********************************************************************/
388 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
391 util = new SharedUtil();
393 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
395 for (int i = 0; i < lookup.size(); i++) {
396 //if this sharedrabund is not from a group the user wants then delete it.
397 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
399 lookup.erase(lookup.begin()+i);
406 catch(exception& e) {
407 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
411 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
415 /***********************************************************************/
417 RAbundVector SharedRAbundVector::getRAbundVector() {
419 RAbundVector rav(data.size());
421 for (int i = 0; i < data.size(); i++) {
422 rav.set(i, data[i].abundance);
427 catch(exception& e) {
428 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
432 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
436 /***********************************************************************/
438 RAbundVector SharedRAbundVector::getRAbundVector2() {
441 for(int i = 0; i < numBins; i++)
442 if(data[i].abundance != 0)
443 rav.push_back(data[i].abundance-1);
446 catch(exception& e) {
447 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
451 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
455 /***********************************************************************/
457 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
459 SharedSAbundVector sav(maxRank+1);
461 for(int i=0;i<data.size();i++){
462 int abund = data[i].abundance;
463 sav.set(abund, sav.getAbundance(abund) + 1, group);
466 sav.set(0, 0, group);
472 catch(exception& e) {
473 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
477 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
481 /***********************************************************************/
483 SAbundVector SharedRAbundVector::getSAbundVector() {
485 SAbundVector sav(maxRank+1);
487 for(int i=0;i<data.size();i++){
488 int abund = data[i].abundance;
489 sav.set(abund, sav.get(abund) + 1);
495 catch(exception& e) {
496 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
500 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
505 /***********************************************************************/
507 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
509 SharedOrderVector ov;
511 for(int i=0;i<data.size();i++){
512 for(int j=0;j<data[i].abundance;j++){
513 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
516 random_shuffle(ov.begin(), ov.end());
523 catch(exception& e) {
524 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
528 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
532 /***********************************************************************/
534 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
538 for(int i=0;i<data.size();i++){
539 for(int j=0;j<data[i].abundance;j++){
543 random_shuffle(ov.begin(), ov.end());
548 catch(exception& e) {
549 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
553 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
558 /***********************************************************************/