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 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]; }
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, i, groupN); //abundance, bin, group
97 push_back(inputData, i, groupN);
100 if (inputData > maxRank) { maxRank = inputData; }
104 if (f.eof() != true) { f >> nextLabel; }
106 //read the rest of the groups info in
107 while ((nextLabel == holdLabel) && (f.eof() != true)) {
111 if (globaldata->gGroupmap == NULL) {
112 //save group in groupmap
113 groupmap->namesOfGroups.push_back(groupN);
114 groupmap->groupIndex[groupN] = count;
117 //add new vector to lookup
118 temp = new SharedRAbundVector();
119 lookup.push_back(temp);
120 lookup[count]->setLabel(label);
121 lookup[count]->setGroup(groupN);
124 for(int i=0;i<num;i++){
126 lookup[count]->push_back(inputData, i, groupN); //abundance, bin, group
130 if (f.eof() != true) { f >> nextLabel; }
133 //put file pointer back since you are now at a new distance label
134 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
136 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
139 catch(exception& e) {
140 errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
145 /***********************************************************************/
147 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
149 int oldBinSize = data[binNumber].abundance;
150 data[binNumber].abundance = newBinSize;
151 data[binNumber].group = groupname;
153 if(newBinSize > maxRank) { maxRank = newBinSize; }
155 numSeqs += (newBinSize - oldBinSize);
157 catch(exception& e) {
158 errorOut(e, "SharedRAbundVector", "set");
162 /***********************************************************************/
164 void SharedRAbundVector::setData(vector <individual> newData){
168 /***********************************************************************/
170 int SharedRAbundVector::getAbundance(int index){
171 return data[index].abundance;
174 /***********************************************************************/
176 int SharedRAbundVector::numNZ(){
178 for(int i = 1; i < numBins; i++)
179 if(data[i].abundance > 0)
183 /***********************************************************************/
185 void SharedRAbundVector::sortD(){
186 struct individual indObj;
187 sort(data.begin()+1, data.end(), indObj);
189 /***********************************************************************/
191 individual SharedRAbundVector::get(int index){
195 /***********************************************************************/
197 vector <individual> SharedRAbundVector::getData(){
200 /***********************************************************************/
202 void SharedRAbundVector::push_back(int binSize, int otu, string groupName){
205 newGuy.abundance = binSize;
206 newGuy.group = groupName;
209 data.push_back(newGuy);
212 if(binSize > maxRank){
218 catch(exception& e) {
219 errorOut(e, "SharedRAbundVector", "push_back");
224 /***********************************************************************/
226 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
229 newGuy.abundance = binSize;
230 newGuy.group = groupName;
233 data.insert(data.begin()+otu, newGuy);
236 if(binSize > maxRank){
242 catch(exception& e) {
243 errorOut(e, "SharedRAbundVector", "insert");
248 /***********************************************************************/
250 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
253 newGuy.abundance = binSize;
254 newGuy.group = groupName;
257 data.insert(data.begin(), newGuy);
260 if(binSize > maxRank){
266 catch(exception& e) {
267 errorOut(e, "SharedRAbundVector", "push_front");
272 /***********************************************************************/
273 void SharedRAbundVector::pop_back(){
274 numSeqs -= data[data.size()-1].abundance;
276 return data.pop_back();
279 /***********************************************************************/
282 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
283 return data.rbegin();
286 /***********************************************************************/
288 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
292 /***********************************************************************/
293 void SharedRAbundVector::resize(int size){
298 /***********************************************************************/
300 int SharedRAbundVector::size(){
304 /***********************************************************************/
305 void SharedRAbundVector::print(ostream& output){
307 output << numBins << '\t';
309 for(int i=0;i<data.size();i++){ output << data[i].abundance << '\t'; }
312 catch(exception& e) {
313 errorOut(e, "SharedRAbundVector", "print");
317 /***********************************************************************/
318 string SharedRAbundVector::getGroup(){
322 /***********************************************************************/
324 void SharedRAbundVector::setGroup(string groupName){
327 /***********************************************************************/
328 int SharedRAbundVector::getGroupIndex() { return index; }
329 /***********************************************************************/
330 void SharedRAbundVector::setGroupIndex(int vIndex) { index = vIndex; }
331 /***********************************************************************/
332 int SharedRAbundVector::getNumBins(){
336 /***********************************************************************/
338 int SharedRAbundVector::getNumSeqs(){
342 /***********************************************************************/
344 int SharedRAbundVector::getMaxRank(){
347 /***********************************************************************/
349 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
352 /***********************************************************************/
353 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
356 util = new SharedUtil();
358 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
360 for (int i = 0; i < lookup.size(); i++) {
361 //if this sharedrabund is not from a group the user wants then delete it.
362 if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) {
364 lookup.erase(lookup.begin()+i);
373 catch(exception& e) {
374 errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
378 /***********************************************************************/
380 RAbundVector SharedRAbundVector::getRAbundVector() {
384 for (int i = 0; i < data.size(); i++) {
385 if(data[i].abundance != 0) {
386 rav.push_back(data[i].abundance);
393 catch(exception& e) {
394 errorOut(e, "SharedRAbundVector", "getRAbundVector");
398 /***********************************************************************/
400 RAbundVector SharedRAbundVector::getRAbundVector2() {
403 for(int i = 0; i < numBins; i++)
404 if(data[i].abundance != 0)
405 rav.push_back(data[i].abundance-1);
408 catch(exception& e) {
409 errorOut(e, "SharedRAbundVector", "getRAbundVector2");
413 /***********************************************************************/
415 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
417 SharedSAbundVector sav(maxRank+1);
419 for(int i=0;i<data.size();i++){
420 int abund = data[i].abundance;
421 sav.set(abund, sav.getAbundance(abund) + 1, group);
424 sav.set(0, 0, group);
430 catch(exception& e) {
431 errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
435 /***********************************************************************/
437 SAbundVector SharedRAbundVector::getSAbundVector() {
439 SAbundVector sav(maxRank+1);
441 for(int i=0;i<data.size();i++){
442 int abund = data[i].abundance;
443 sav.set(abund, sav.get(abund) + 1);
449 catch(exception& e) {
450 errorOut(e, "SharedRAbundVector", "getSAbundVector");
455 /***********************************************************************/
457 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
459 SharedOrderVector ov;
461 for(int i=0;i<data.size();i++){
462 for(int j=0;j<data[i].abundance;j++){
463 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
466 random_shuffle(ov.begin(), ov.end());
473 catch(exception& e) {
474 errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
478 /***********************************************************************/
480 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
484 for(int i=0;i<data.size();i++){
485 for(int j=0;j<data[i].abundance;j++){
489 random_shuffle(ov.begin(), ov.end());
494 catch(exception& e) {
495 errorOut(e, "SharedRAbundVector", "getOrderVector");
500 /***********************************************************************/