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 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
158 int oldBinSize = data[binNumber].abundance;
159 data[binNumber].abundance = newBinSize;
160 data[binNumber].group = groupname;
162 if(newBinSize > maxRank) { maxRank = newBinSize; }
164 numSeqs += (newBinSize - oldBinSize);
166 catch(exception& e) {
167 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175 /***********************************************************************/
177 void SharedRAbundVector::setData(vector <individual> newData){
181 /***********************************************************************/
183 int SharedRAbundVector::getAbundance(int index){
184 return data[index].abundance;
187 /***********************************************************************/
189 int SharedRAbundVector::numNZ(){
191 for(int i = 1; i < numBins; i++)
192 if(data[i].abundance > 0)
196 /***********************************************************************/
198 void SharedRAbundVector::sortD(){
199 struct individual indObj;
200 sort(data.begin()+1, data.end(), indObj);
202 /***********************************************************************/
204 individual SharedRAbundVector::get(int index){
208 /***********************************************************************/
210 vector <individual> SharedRAbundVector::getData(){
213 /***********************************************************************/
215 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
218 newGuy.abundance = binSize;
219 newGuy.group = groupName;
222 data.push_back(newGuy);
225 if(binSize > maxRank){
231 catch(exception& e) {
232 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241 /***********************************************************************/
243 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
246 newGuy.abundance = binSize;
247 newGuy.group = groupName;
250 data.insert(data.begin()+otu, newGuy);
253 if(binSize > maxRank){
259 catch(exception& e) {
260 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269 /***********************************************************************/
271 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
274 newGuy.abundance = binSize;
275 newGuy.group = groupName;
278 data.insert(data.begin(), newGuy);
281 if(binSize > maxRank){
287 catch(exception& e) {
288 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297 /***********************************************************************/
298 void SharedRAbundVector::pop_back(){
299 numSeqs -= data[data.size()-1].abundance;
301 return data.pop_back();
304 /***********************************************************************/
307 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
308 return data.rbegin();
311 /***********************************************************************/
313 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
317 /***********************************************************************/
318 void SharedRAbundVector::resize(int size){
323 /***********************************************************************/
325 int SharedRAbundVector::size(){
329 /***********************************************************************/
330 void SharedRAbundVector::print(ostream& output){
332 output << numBins << '\t';
334 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
337 catch(exception& e) {
338 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
346 /***********************************************************************/
347 string SharedRAbundVector::getGroup(){
351 /***********************************************************************/
353 void SharedRAbundVector::setGroup(string groupName){
356 /***********************************************************************/
357 int SharedRAbundVector::getGroupIndex() { return index; }
358 /***********************************************************************/
359 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
360 /***********************************************************************/
361 int SharedRAbundVector::getNumBins(){
365 /***********************************************************************/
367 int SharedRAbundVector::getNumSeqs(){
371 /***********************************************************************/
373 int SharedRAbundVector::getMaxRank(){
376 /***********************************************************************/
378 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
381 /***********************************************************************/
382 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
385 util = new SharedUtil();
387 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
389 for (int i = 0; i < lookup.size(); i++) {
390 //if this sharedrabund is not from a group the user wants then delete it.
391 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
393 lookup.erase(lookup.begin()+i);
400 catch(exception& e) {
401 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
405 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409 /***********************************************************************/
411 RAbundVector SharedRAbundVector::getRAbundVector() {
413 RAbundVector rav(data.size());
415 for (int i = 0; i < data.size(); i++) {
416 rav.set(i, data[i].abundance);
421 catch(exception& e) {
422 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
426 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
430 /***********************************************************************/
432 RAbundVector SharedRAbundVector::getRAbundVector2() {
435 for(int i = 0; i < numBins; i++)
436 if(data[i].abundance != 0)
437 rav.push_back(data[i].abundance-1);
440 catch(exception& e) {
441 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
445 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449 /***********************************************************************/
451 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
453 SharedSAbundVector sav(maxRank+1);
455 for(int i=0;i<data.size();i++){
456 int abund = data[i].abundance;
457 sav.set(abund, sav.getAbundance(abund) + 1, group);
460 sav.set(0, 0, group);
466 catch(exception& e) {
467 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
471 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475 /***********************************************************************/
477 SAbundVector SharedRAbundVector::getSAbundVector() {
479 SAbundVector sav(maxRank+1);
481 for(int i=0;i<data.size();i++){
482 int abund = data[i].abundance;
483 sav.set(abund, sav.get(abund) + 1);
489 catch(exception& e) {
490 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
494 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
499 /***********************************************************************/
501 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
503 SharedOrderVector ov;
505 for(int i=0;i<data.size();i++){
506 for(int j=0;j<data[i].abundance;j++){
507 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
510 random_shuffle(ov.begin(), ov.end());
517 catch(exception& e) {
518 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
522 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
526 /***********************************************************************/
528 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
532 for(int i=0;i<data.size();i++){
533 for(int j=0;j<data[i].abundance;j++){
537 random_shuffle(ov.begin(), ov.end());
542 catch(exception& e) {
543 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
547 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
552 /***********************************************************************/