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();}
19 /***********************************************************************/
21 SharedRAbundVector::~SharedRAbundVector() {
22 //for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
26 /***********************************************************************/
28 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
29 globaldata = GlobalData::getInstance();
32 for (int i=0; i< n; i++) {
35 data.push_back(newGuy);
39 /***********************************************************************
41 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
47 for(int i=0;i<data.size();i++){
48 if(data[i].abundance != 0) { numBins = i+1; }
49 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
50 numSeqs += data[i].abundance;
54 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
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, count;
70 string holdLabel, nextLabel, groupN;
73 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
76 if (globaldata->saveNextLabel == "") { f >> label; }
77 else { label = globaldata->saveNextLabel; }
79 //read in first row since you know there is at least 1 group.
84 //add new vector to lookup
85 SharedRAbundVector* temp = new SharedRAbundVector();
86 lookup.push_back(temp);
87 lookup[0]->setLabel(label);
88 lookup[0]->setGroup(groupN);
90 if (globaldata->gGroupmap == NULL) {
91 //save group in groupmap
92 groupmap->namesOfGroups.push_back(groupN);
93 groupmap->groupIndex[groupN] = 0;
96 //fill vector. data = first sharedrabund in file
97 for(int i=0;i<num;i++){
100 lookup[0]->push_back(inputData, groupN); //abundance, bin, group
101 push_back(inputData, groupN);
102 //numSeqs += inputData;
104 if (inputData > maxRank) { maxRank = inputData; }
109 if (!(f.eof())) { f >> nextLabel; }
111 //read the rest of the groups info in
112 while ((nextLabel == holdLabel) && (f.eof() != true)) {
116 if (globaldata->gGroupmap == NULL) {
117 //save group in groupmap
119 groupmap->namesOfGroups.push_back(groupN);
120 groupmap->groupIndex[groupN] = count;
123 //add new vector to lookup
124 temp = new SharedRAbundVector();
125 lookup.push_back(temp);
126 lookup[count]->setLabel(label);
127 lookup[count]->setGroup(groupN);
130 for(int i=0;i<num;i++){
132 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
137 if (f.eof() != true) { f >> nextLabel; }
140 globaldata->saveNextLabel = nextLabel;
142 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
145 catch(exception& e) {
146 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
151 /***********************************************************************/
153 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
155 int oldBinSize = data[binNumber].abundance;
156 data[binNumber].abundance = newBinSize;
157 data[binNumber].group = groupname;
159 if(newBinSize > maxRank) { maxRank = newBinSize; }
161 numSeqs += (newBinSize - oldBinSize);
163 catch(exception& e) {
164 m->errorOut(e, "SharedRAbundVector", "set");
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, string groupName){
211 newGuy.abundance = binSize;
212 newGuy.group = groupName;
213 newGuy.bin = data.size();
215 data.push_back(newGuy);
218 if(binSize > maxRank){
224 catch(exception& e) {
225 m->errorOut(e, "SharedRAbundVector", "push_back");
230 /***********************************************************************/
232 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
235 newGuy.abundance = binSize;
236 newGuy.group = groupName;
239 data.insert(data.begin()+otu, newGuy);
242 if(binSize > maxRank){
248 catch(exception& e) {
249 m->errorOut(e, "SharedRAbundVector", "insert");
254 /***********************************************************************/
256 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
259 newGuy.abundance = binSize;
260 newGuy.group = groupName;
263 data.insert(data.begin(), newGuy);
266 if(binSize > maxRank){
272 catch(exception& e) {
273 m->errorOut(e, "SharedRAbundVector", "push_front");
278 /***********************************************************************/
279 void SharedRAbundVector::pop_back(){
280 numSeqs -= data[data.size()-1].abundance;
282 return data.pop_back();
285 /***********************************************************************/
288 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
289 return data.rbegin();
292 /***********************************************************************/
294 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
298 /***********************************************************************/
299 void SharedRAbundVector::resize(int size){
304 /***********************************************************************/
306 int SharedRAbundVector::size(){
310 /***********************************************************************/
311 void SharedRAbundVector::print(ostream& output){
313 output << numBins << '\t';
315 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
318 catch(exception& e) {
319 m->errorOut(e, "SharedRAbundVector", "print");
323 /***********************************************************************/
324 string SharedRAbundVector::getGroup(){
328 /***********************************************************************/
330 void SharedRAbundVector::setGroup(string groupName){
333 /***********************************************************************/
334 int SharedRAbundVector::getGroupIndex() { return index; }
335 /***********************************************************************/
336 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
337 /***********************************************************************/
338 int SharedRAbundVector::getNumBins(){
342 /***********************************************************************/
344 int SharedRAbundVector::getNumSeqs(){
348 /***********************************************************************/
350 int SharedRAbundVector::getMaxRank(){
353 /***********************************************************************/
355 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
358 /***********************************************************************/
359 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
362 util = new SharedUtil();
364 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
366 for (int i = 0; i < lookup.size(); i++) {
367 //if this sharedrabund is not from a group the user wants then delete it.
368 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
369 delete lookup[i]; lookup[i] = NULL;
370 lookup.erase(lookup.begin()+i);
379 catch(exception& e) {
380 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
384 /***********************************************************************/
386 RAbundVector SharedRAbundVector::getRAbundVector() {
390 for (int i = 0; i < data.size(); i++) {
391 if(data[i].abundance != 0) {
392 rav.push_back(data[i].abundance);
399 catch(exception& e) {
400 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
404 /***********************************************************************/
406 RAbundVector SharedRAbundVector::getRAbundVector2() {
409 for(int i = 0; i < numBins; i++)
410 if(data[i].abundance != 0)
411 rav.push_back(data[i].abundance-1);
414 catch(exception& e) {
415 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
419 /***********************************************************************/
421 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
423 SharedSAbundVector sav(maxRank+1);
425 for(int i=0;i<data.size();i++){
426 int abund = data[i].abundance;
427 sav.set(abund, sav.getAbundance(abund) + 1, group);
430 sav.set(0, 0, group);
436 catch(exception& e) {
437 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
441 /***********************************************************************/
443 SAbundVector SharedRAbundVector::getSAbundVector() {
445 SAbundVector sav(maxRank+1);
447 for(int i=0;i<data.size();i++){
448 int abund = data[i].abundance;
449 sav.set(abund, sav.get(abund) + 1);
455 catch(exception& e) {
456 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
461 /***********************************************************************/
463 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
465 SharedOrderVector ov;
467 for(int i=0;i<data.size();i++){
468 for(int j=0;j<data[i].abundance;j++){
469 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
472 random_shuffle(ov.begin(), ov.end());
479 catch(exception& e) {
480 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
484 /***********************************************************************/
486 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
490 for(int i=0;i<data.size();i++){
491 for(int j=0;j<data[i].abundance;j++){
495 random_shuffle(ov.begin(), ov.end());
500 catch(exception& e) {
501 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
506 /***********************************************************************/