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 //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, groupN); //abundance, bin, group
97 push_back(inputData, groupN);
98 //numSeqs += inputData;
100 if (inputData > maxRank) { maxRank = inputData; }
106 if (f.eof() != true) { f >> nextLabel; }
108 //read the rest of the groups info in
109 while ((nextLabel == holdLabel) && (f.eof() != true)) {
113 if (globaldata->gGroupmap == NULL) {
114 //save group in groupmap
115 groupmap->namesOfGroups.push_back(groupN);
116 groupmap->groupIndex[groupN] = count;
119 //add new vector to lookup
120 temp = new SharedRAbundVector();
121 lookup.push_back(temp);
122 lookup[count]->setLabel(label);
123 lookup[count]->setGroup(groupN);
126 for(int i=0;i<num;i++){
128 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
133 if (f.eof() != true) { f >> nextLabel; }
136 //put file pointer back since you are now at a new distance label
137 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
139 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
142 catch(exception& e) {
143 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
148 /***********************************************************************/
150 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
152 int oldBinSize = data[binNumber].abundance;
153 data[binNumber].abundance = newBinSize;
154 data[binNumber].group = groupname;
156 if(newBinSize > maxRank) { maxRank = newBinSize; }
158 numSeqs += (newBinSize - oldBinSize);
160 catch(exception& e) {
161 m->errorOut(e, "SharedRAbundVector", "set");
165 /***********************************************************************/
167 void SharedRAbundVector::setData(vector <individual> newData){
171 /***********************************************************************/
173 int SharedRAbundVector::getAbundance(int index){
174 return data[index].abundance;
177 /***********************************************************************/
179 int SharedRAbundVector::numNZ(){
181 for(int i = 1; i < numBins; i++)
182 if(data[i].abundance > 0)
186 /***********************************************************************/
188 void SharedRAbundVector::sortD(){
189 struct individual indObj;
190 sort(data.begin()+1, data.end(), indObj);
192 /***********************************************************************/
194 individual SharedRAbundVector::get(int index){
198 /***********************************************************************/
200 vector <individual> SharedRAbundVector::getData(){
203 /***********************************************************************/
205 void SharedRAbundVector::push_back(int binSize, string groupName){
208 newGuy.abundance = binSize;
209 newGuy.group = groupName;
210 newGuy.bin = data.size();
212 data.push_back(newGuy);
215 if(binSize > maxRank){
221 catch(exception& e) {
222 m->errorOut(e, "SharedRAbundVector", "push_back");
227 /***********************************************************************/
229 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
232 newGuy.abundance = binSize;
233 newGuy.group = groupName;
236 data.insert(data.begin()+otu, newGuy);
239 if(binSize > maxRank){
245 catch(exception& e) {
246 m->errorOut(e, "SharedRAbundVector", "insert");
251 /***********************************************************************/
253 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
256 newGuy.abundance = binSize;
257 newGuy.group = groupName;
260 data.insert(data.begin(), newGuy);
263 if(binSize > maxRank){
269 catch(exception& e) {
270 m->errorOut(e, "SharedRAbundVector", "push_front");
275 /***********************************************************************/
276 void SharedRAbundVector::pop_back(){
277 numSeqs -= data[data.size()-1].abundance;
279 return data.pop_back();
282 /***********************************************************************/
285 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
286 return data.rbegin();
289 /***********************************************************************/
291 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
295 /***********************************************************************/
296 void SharedRAbundVector::resize(int size){
301 /***********************************************************************/
303 int SharedRAbundVector::size(){
307 /***********************************************************************/
308 void SharedRAbundVector::print(ostream& output){
310 output << numBins << '\t';
312 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
315 catch(exception& e) {
316 m->errorOut(e, "SharedRAbundVector", "print");
320 /***********************************************************************/
321 string SharedRAbundVector::getGroup(){
325 /***********************************************************************/
327 void SharedRAbundVector::setGroup(string groupName){
330 /***********************************************************************/
331 int SharedRAbundVector::getGroupIndex() { return index; }
332 /***********************************************************************/
333 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
334 /***********************************************************************/
335 int SharedRAbundVector::getNumBins(){
339 /***********************************************************************/
341 int SharedRAbundVector::getNumSeqs(){
345 /***********************************************************************/
347 int SharedRAbundVector::getMaxRank(){
350 /***********************************************************************/
352 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
355 /***********************************************************************/
356 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
359 util = new SharedUtil();
361 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
363 for (int i = 0; i < lookup.size(); i++) {
364 //if this sharedrabund is not from a group the user wants then delete it.
365 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
366 delete lookup[i]; lookup[i] = NULL;
367 lookup.erase(lookup.begin()+i);
376 catch(exception& e) {
377 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
381 /***********************************************************************/
383 RAbundVector SharedRAbundVector::getRAbundVector() {
387 for (int i = 0; i < data.size(); i++) {
388 if(data[i].abundance != 0) {
389 rav.push_back(data[i].abundance);
396 catch(exception& e) {
397 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
401 /***********************************************************************/
403 RAbundVector SharedRAbundVector::getRAbundVector2() {
406 for(int i = 0; i < numBins; i++)
407 if(data[i].abundance != 0)
408 rav.push_back(data[i].abundance-1);
411 catch(exception& e) {
412 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
416 /***********************************************************************/
418 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
420 SharedSAbundVector sav(maxRank+1);
422 for(int i=0;i<data.size();i++){
423 int abund = data[i].abundance;
424 sav.set(abund, sav.getAbundance(abund) + 1, group);
427 sav.set(0, 0, group);
433 catch(exception& e) {
434 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
438 /***********************************************************************/
440 SAbundVector SharedRAbundVector::getSAbundVector() {
442 SAbundVector sav(maxRank+1);
444 for(int i=0;i<data.size();i++){
445 int abund = data[i].abundance;
446 sav.set(abund, sav.get(abund) + 1);
452 catch(exception& e) {
453 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
458 /***********************************************************************/
460 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
462 SharedOrderVector ov;
464 for(int i=0;i<data.size();i++){
465 for(int j=0;j<data[i].abundance;j++){
466 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
469 random_shuffle(ov.begin(), ov.end());
476 catch(exception& e) {
477 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
481 /***********************************************************************/
483 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
487 for(int i=0;i<data.size();i++){
488 for(int j=0;j<data[i].abundance;j++){
492 random_shuffle(ov.begin(), ov.end());
497 catch(exception& e) {
498 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
503 /***********************************************************************/