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
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, groupN); //abundance, bin, group
134 if (f.eof() != true) { f >> nextLabel; }
137 //put file pointer back since you are now at a new distance label
138 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
140 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
143 catch(exception& e) {
144 m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
149 /***********************************************************************/
151 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
153 int oldBinSize = data[binNumber].abundance;
154 data[binNumber].abundance = newBinSize;
155 data[binNumber].group = groupname;
157 if(newBinSize > maxRank) { maxRank = newBinSize; }
159 numSeqs += (newBinSize - oldBinSize);
161 catch(exception& e) {
162 m->errorOut(e, "SharedRAbundVector", "set");
166 /***********************************************************************/
168 void SharedRAbundVector::setData(vector <individual> newData){
172 /***********************************************************************/
174 int SharedRAbundVector::getAbundance(int index){
175 return data[index].abundance;
178 /***********************************************************************/
180 int SharedRAbundVector::numNZ(){
182 for(int i = 1; i < numBins; i++)
183 if(data[i].abundance > 0)
187 /***********************************************************************/
189 void SharedRAbundVector::sortD(){
190 struct individual indObj;
191 sort(data.begin()+1, data.end(), indObj);
193 /***********************************************************************/
195 individual SharedRAbundVector::get(int index){
199 /***********************************************************************/
201 vector <individual> SharedRAbundVector::getData(){
204 /***********************************************************************/
206 void SharedRAbundVector::push_back(int binSize, string groupName){
209 newGuy.abundance = binSize;
210 newGuy.group = groupName;
211 newGuy.bin = data.size();
213 data.push_back(newGuy);
216 if(binSize > maxRank){
222 catch(exception& e) {
223 m->errorOut(e, "SharedRAbundVector", "push_back");
228 /***********************************************************************/
230 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
233 newGuy.abundance = binSize;
234 newGuy.group = groupName;
237 data.insert(data.begin()+otu, newGuy);
240 if(binSize > maxRank){
246 catch(exception& e) {
247 m->errorOut(e, "SharedRAbundVector", "insert");
252 /***********************************************************************/
254 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
257 newGuy.abundance = binSize;
258 newGuy.group = groupName;
261 data.insert(data.begin(), newGuy);
264 if(binSize > maxRank){
270 catch(exception& e) {
271 m->errorOut(e, "SharedRAbundVector", "push_front");
276 /***********************************************************************/
277 void SharedRAbundVector::pop_back(){
278 numSeqs -= data[data.size()-1].abundance;
280 return data.pop_back();
283 /***********************************************************************/
286 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
287 return data.rbegin();
290 /***********************************************************************/
292 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
296 /***********************************************************************/
297 void SharedRAbundVector::resize(int size){
302 /***********************************************************************/
304 int SharedRAbundVector::size(){
308 /***********************************************************************/
309 void SharedRAbundVector::print(ostream& output){
311 output << numBins << '\t';
313 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
316 catch(exception& e) {
317 m->errorOut(e, "SharedRAbundVector", "print");
321 /***********************************************************************/
322 string SharedRAbundVector::getGroup(){
326 /***********************************************************************/
328 void SharedRAbundVector::setGroup(string groupName){
331 /***********************************************************************/
332 int SharedRAbundVector::getGroupIndex() { return index; }
333 /***********************************************************************/
334 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
335 /***********************************************************************/
336 int SharedRAbundVector::getNumBins(){
340 /***********************************************************************/
342 int SharedRAbundVector::getNumSeqs(){
346 /***********************************************************************/
348 int SharedRAbundVector::getMaxRank(){
351 /***********************************************************************/
353 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
356 /***********************************************************************/
357 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
360 util = new SharedUtil();
362 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
364 for (int i = 0; i < lookup.size(); i++) {
365 //if this sharedrabund is not from a group the user wants then delete it.
366 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
367 delete lookup[i]; lookup[i] = NULL;
368 lookup.erase(lookup.begin()+i);
377 catch(exception& e) {
378 m->errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
382 /***********************************************************************/
384 RAbundVector SharedRAbundVector::getRAbundVector() {
388 for (int i = 0; i < data.size(); i++) {
389 if(data[i].abundance != 0) {
390 rav.push_back(data[i].abundance);
397 catch(exception& e) {
398 m->errorOut(e, "SharedRAbundVector", "getRAbundVector");
402 /***********************************************************************/
404 RAbundVector SharedRAbundVector::getRAbundVector2() {
407 for(int i = 0; i < numBins; i++)
408 if(data[i].abundance != 0)
409 rav.push_back(data[i].abundance-1);
412 catch(exception& e) {
413 m->errorOut(e, "SharedRAbundVector", "getRAbundVector2");
417 /***********************************************************************/
419 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
421 SharedSAbundVector sav(maxRank+1);
423 for(int i=0;i<data.size();i++){
424 int abund = data[i].abundance;
425 sav.set(abund, sav.getAbundance(abund) + 1, group);
428 sav.set(0, 0, group);
434 catch(exception& e) {
435 m->errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
439 /***********************************************************************/
441 SAbundVector SharedRAbundVector::getSAbundVector() {
443 SAbundVector sav(maxRank+1);
445 for(int i=0;i<data.size();i++){
446 int abund = data[i].abundance;
447 sav.set(abund, sav.get(abund) + 1);
453 catch(exception& e) {
454 m->errorOut(e, "SharedRAbundVector", "getSAbundVector");
459 /***********************************************************************/
461 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
463 SharedOrderVector ov;
465 for(int i=0;i<data.size();i++){
466 for(int j=0;j<data[i].abundance;j++){
467 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
470 random_shuffle(ov.begin(), ov.end());
477 catch(exception& e) {
478 m->errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
482 /***********************************************************************/
484 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
488 for(int i=0;i<data.size();i++){
489 for(int j=0;j<data[i].abundance;j++){
493 random_shuffle(ov.begin(), ov.end());
498 catch(exception& e) {
499 m->errorOut(e, "SharedRAbundVector", "getOrderVector");
504 /***********************************************************************/