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::clear(){
213 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
216 /***********************************************************************/
218 void SharedRAbundVector::push_back(int binSize, string groupName){
221 newGuy.abundance = binSize;
222 newGuy.group = groupName;
223 newGuy.bin = data.size();
225 data.push_back(newGuy);
228 if(binSize > maxRank){
234 catch(exception& e) {
235 m->errorOut(e, "SharedRAbundVector", "push_back");
240 /***********************************************************************/
242 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
245 newGuy.abundance = binSize;
246 newGuy.group = groupName;
249 data.insert(data.begin()+otu, newGuy);
252 if(binSize > maxRank){
258 catch(exception& e) {
259 m->errorOut(e, "SharedRAbundVector", "insert");
264 /***********************************************************************/
266 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
269 newGuy.abundance = binSize;
270 newGuy.group = groupName;
273 data.insert(data.begin(), newGuy);
276 if(binSize > maxRank){
282 catch(exception& e) {
283 m->errorOut(e, "SharedRAbundVector", "push_front");
288 /***********************************************************************/
289 void SharedRAbundVector::pop_back(){
290 numSeqs -= data[data.size()-1].abundance;
292 return data.pop_back();
295 /***********************************************************************/
298 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
299 return data.rbegin();
302 /***********************************************************************/
304 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
308 /***********************************************************************/
309 void SharedRAbundVector::resize(int size){
314 /***********************************************************************/
316 int SharedRAbundVector::size(){
320 /***********************************************************************/
321 void SharedRAbundVector::print(ostream& output){
323 output << numBins << '\t';
325 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
328 catch(exception& e) {
329 m->errorOut(e, "SharedRAbundVector", "print");
333 /***********************************************************************/
334 string SharedRAbundVector::getGroup(){
338 /***********************************************************************/
340 void SharedRAbundVector::setGroup(string groupName){
343 /***********************************************************************/
344 int SharedRAbundVector::getGroupIndex() { return index; }
345 /***********************************************************************/
346 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
347 /***********************************************************************/
348 int SharedRAbundVector::getNumBins(){
352 /***********************************************************************/
354 int SharedRAbundVector::getNumSeqs(){
358 /***********************************************************************/
360 int SharedRAbundVector::getMaxRank(){
363 /***********************************************************************/
365 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
368 /***********************************************************************/
369 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
372 util = new SharedUtil();
374 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
376 for (int i = 0; i < lookup.size(); i++) {
377 //if this sharedrabund is not from a group the user wants then delete it.
378 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
379 delete lookup[i]; lookup[i] = NULL;
380 lookup.erase(lookup.begin()+i);
389 catch(exception& e) {
390 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
394 /***********************************************************************/
396 RAbundVector SharedRAbundVector::getRAbundVector() {
400 for (int i = 0; i < data.size(); i++) {
401 if(data[i].abundance != 0) {
402 rav.push_back(data[i].abundance);
409 catch(exception& e) {
410 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
414 /***********************************************************************/
416 RAbundVector SharedRAbundVector::getRAbundVector2() {
419 for(int i = 0; i < numBins; i++)
420 if(data[i].abundance != 0)
421 rav.push_back(data[i].abundance-1);
424 catch(exception& e) {
425 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
429 /***********************************************************************/
431 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
433 SharedSAbundVector sav(maxRank+1);
435 for(int i=0;i<data.size();i++){
436 int abund = data[i].abundance;
437 sav.set(abund, sav.getAbundance(abund) + 1, group);
440 sav.set(0, 0, group);
446 catch(exception& e) {
447 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
451 /***********************************************************************/
453 SAbundVector SharedRAbundVector::getSAbundVector() {
455 SAbundVector sav(maxRank+1);
457 for(int i=0;i<data.size();i++){
458 int abund = data[i].abundance;
459 sav.set(abund, sav.get(abund) + 1);
465 catch(exception& e) {
466 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
471 /***********************************************************************/
473 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
475 SharedOrderVector ov;
477 for(int i=0;i<data.size();i++){
478 for(int j=0;j<data[i].abundance;j++){
479 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
482 random_shuffle(ov.begin(), ov.end());
489 catch(exception& e) {
490 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
494 /***********************************************************************/
496 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
499 for(int i=0;i<numBins;i++){
500 for(int j=0;j<data[i].abundance;j++){
504 random_shuffle(ov.begin(), ov.end());
510 catch(exception& e) {
511 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
516 /***********************************************************************/