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, 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 if (f.eof() != true) { f >> nextLabel; }
104 //read the rest of the groups info in
105 while ((nextLabel == holdLabel) && (f.eof() != true)) {
109 if (globaldata->gGroupmap == NULL) {
110 //save group in groupmap
111 groupmap->namesOfGroups.push_back(groupN);
112 groupmap->groupIndex[groupN] = count;
115 //add new vector to lookup
116 temp = new SharedRAbundVector();
117 lookup.push_back(temp);
118 lookup[count]->setLabel(label);
119 lookup[count]->setGroup(groupN);
122 for(int i=0;i<num;i++){
124 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
128 if (f.eof() != true) { f >> nextLabel; }
131 //put file pointer back since you are now at a new distance label
132 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
134 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
137 catch(exception& e) {
138 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142 cout << "An unknown error has occurred in the SharedRAbundVector class function SharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147 /***********************************************************************/
149 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
151 int oldBinSize = data[binNumber].abundance;
152 data[binNumber].abundance = newBinSize;
153 data[binNumber].group = groupname;
155 if(newBinSize > maxRank) { maxRank = newBinSize; }
157 numSeqs += (newBinSize - oldBinSize);
159 catch(exception& e) {
160 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164 cout << "An unknown error has occurred in the SharedRAbundVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168 /***********************************************************************/
170 void SharedRAbundVector::setData(vector <individual> newData){
174 /***********************************************************************/
176 int SharedRAbundVector::getAbundance(int index){
177 return data[index].abundance;
180 /***********************************************************************/
182 int SharedRAbundVector::numNZ(){
184 for(int i = 1; i < numBins; i++)
185 if(data[i].abundance > 0)
189 /***********************************************************************/
191 void SharedRAbundVector::sortD(){
192 struct individual indObj;
193 sort(data.begin()+1, data.end(), indObj);
195 /***********************************************************************/
197 individual SharedRAbundVector::get(int index){
201 /***********************************************************************/
203 vector <individual> SharedRAbundVector::getData(){
206 /***********************************************************************/
208 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
211 newGuy.abundance = binSize;
212 newGuy.group = groupName;
215 data.push_back(newGuy);
218 if(binSize > maxRank){
224 catch(exception& e) {
225 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229 cout << "An unknown error has occurred in the SharedRAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234 /***********************************************************************/
236 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
239 newGuy.abundance = binSize;
240 newGuy.group = groupName;
243 data.insert(data.begin()+otu, newGuy);
246 if(binSize > maxRank){
252 catch(exception& e) {
253 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257 cout << "An unknown error has occurred in the SharedRAbundVector class function insert. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262 /***********************************************************************/
264 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
267 newGuy.abundance = binSize;
268 newGuy.group = groupName;
271 data.insert(data.begin(), newGuy);
274 if(binSize > maxRank){
280 catch(exception& e) {
281 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
285 cout << "An unknown error has occurred in the SharedRAbundVector class function push_front. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290 /***********************************************************************/
291 void SharedRAbundVector::pop_back(){
292 numSeqs -= data[data.size()-1].abundance;
294 return data.pop_back();
297 /***********************************************************************/
300 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
301 return data.rbegin();
304 /***********************************************************************/
306 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
310 /***********************************************************************/
311 void SharedRAbundVector::resize(int size){
316 /***********************************************************************/
318 int SharedRAbundVector::size(){
322 /***********************************************************************/
323 void SharedRAbundVector::print(ostream& output){
325 output << numBins << '\t';
327 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
330 catch(exception& e) {
331 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
335 cout << "An unknown error has occurred in the SharedRAbundVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339 /***********************************************************************/
340 string SharedRAbundVector::getGroup(){
344 /***********************************************************************/
346 void SharedRAbundVector::setGroup(string groupName){
349 /***********************************************************************/
350 int SharedRAbundVector::getGroupIndex() { return index; }
351 /***********************************************************************/
352 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
353 /***********************************************************************/
354 int SharedRAbundVector::getNumBins(){
358 /***********************************************************************/
360 int SharedRAbundVector::getNumSeqs(){
364 /***********************************************************************/
366 int SharedRAbundVector::getMaxRank(){
369 /***********************************************************************/
371 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
374 /***********************************************************************/
375 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
378 util = new SharedUtil();
380 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
382 for (int i = 0; i < lookup.size(); i++) {
383 //if this sharedrabund is not from a group the user wants then delete it.
384 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
386 lookup.erase(lookup.begin()+i);
395 catch(exception& e) {
396 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
400 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedRAbundVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
404 /***********************************************************************/
406 RAbundVector SharedRAbundVector::getRAbundVector() {
408 RAbundVector rav(data.size());
410 for (int i = 0; i < data.size(); i++) {
411 rav.set(i, data[i].abundance);
416 catch(exception& e) {
417 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
421 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
425 /***********************************************************************/
427 RAbundVector SharedRAbundVector::getRAbundVector2() {
430 for(int i = 0; i < numBins; i++)
431 if(data[i].abundance != 0)
432 rav.push_back(data[i].abundance-1);
435 catch(exception& e) {
436 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
440 cout << "An unknown error has occurred in the SharedRAbundVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
444 /***********************************************************************/
446 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
448 SharedSAbundVector sav(maxRank+1);
450 for(int i=0;i<data.size();i++){
451 int abund = data[i].abundance;
452 sav.set(abund, sav.getAbundance(abund) + 1, group);
455 sav.set(0, 0, group);
461 catch(exception& e) {
462 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
466 cout << "An unknown error has occurred in the SharedRAbundVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
470 /***********************************************************************/
472 SAbundVector SharedRAbundVector::getSAbundVector() {
474 SAbundVector sav(maxRank+1);
476 for(int i=0;i<data.size();i++){
477 int abund = data[i].abundance;
478 sav.set(abund, sav.get(abund) + 1);
484 catch(exception& e) {
485 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
489 cout << "An unknown error has occurred in the SharedRAbundVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
494 /***********************************************************************/
496 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
498 SharedOrderVector ov;
500 for(int i=0;i<data.size();i++){
501 for(int j=0;j<data[i].abundance;j++){
502 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
505 random_shuffle(ov.begin(), ov.end());
512 catch(exception& e) {
513 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
517 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
521 /***********************************************************************/
523 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
527 for(int i=0;i<data.size();i++){
528 for(int j=0;j<data[i].abundance;j++){
532 random_shuffle(ov.begin(), ov.end());
537 catch(exception& e) {
538 cout << "Standard Error: " << e.what() << " has occurred in the SharedRAbundVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
542 cout << "An unknown error has occurred in the SharedRAbundVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
547 /***********************************************************************/