2 * sharedSharedOrderVector.cpp
5 * Created by Sarah Westcott on 12/9/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
13 #include "sharedordervector.h"
14 #include "utilities.hpp"
17 /***********************************************************************/
19 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {}
21 /***********************************************************************/
23 SharedOrderVector::SharedOrderVector(string id, vector<individual> ov) :
24 DataVector(id), data(ov)
29 /***********************************************************************/
30 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
31 //if you don't use a list and groupfile.
33 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {
35 globaldata = GlobalData::getInstance();
36 maxRank = 0; numBins = 0; numSeqs = 0;
38 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
40 int num, inputData, pos, count;
41 count = 0; numSeqs = 0;
42 string holdLabel, nextLabel, groupN;
45 //read in first row since you know there is at least 1 group.
46 f >> label >> groupN >> num;
49 if (globaldata->gGroupmap == NULL) {
50 //save group in groupmap
51 groupmap->namesOfGroups.push_back(groupN);
52 groupmap->groupIndex[groupN] = 0;
55 for(int i=0;i<num;i++){
58 for (int j = 0; j < inputData; j++) {
59 push_back(i+1, i+1, groupN);
64 //save position in file in case next line is a new label.
67 if (f.eof() != true) { f >> nextLabel; }
69 //read the rest of the groups info in
70 while ((nextLabel == holdLabel) && (f.eof() != true)) {
74 if (globaldata->gGroupmap == NULL) {
75 //save group in groupmap
76 groupmap->namesOfGroups.push_back(groupN);
77 groupmap->groupIndex[groupN] = count;
80 for(int i=0;i<num;i++){
83 for (int j = 0; j < inputData; j++) {
84 push_back(i+1, i+1, groupN);
89 //save position in file in case next line is a new label.
92 if (f.eof() != true) { f >> nextLabel; }
96 //put file pointer back since you are now at a new distance label
97 f.seekg(pos, ios::beg);
99 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
103 catch(exception& e) {
104 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112 /***********************************************************************/
114 int SharedOrderVector::getNumBins(){
115 if(needToUpdate == 1){ updateStats(); }
119 /***********************************************************************/
121 int SharedOrderVector::getNumSeqs(){
122 if(needToUpdate == 1){ updateStats(); }
126 /***********************************************************************/
128 int SharedOrderVector::getMaxRank(){
129 if(needToUpdate == 1){ updateStats(); }
134 /***********************************************************************/
138 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
140 data[index].group = groupName;
141 data[index].bin = binNumber;
142 data[index].abundance = abund;
147 /***********************************************************************/
149 individual SharedOrderVector::get(int index){
154 /***********************************************************************/
156 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
158 newGuy.group = groupName;
159 newGuy.abundance = abund;
160 newGuy.bin = binNumber;
161 data.push_back(newGuy);
166 /***********************************************************************/
168 void SharedOrderVector::print(ostream& output){
170 output << label << '\t' << numSeqs << '\t';
172 for(int i=0;i<data.size();i++){
173 output << data[i].bin << '\t';
177 catch(exception& e) {
178 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 /***********************************************************************/
190 void SharedOrderVector::resize(int){
191 cout << "resize() did nothing in class SharedOrderVector";
194 /***********************************************************************/
197 vector<individual>::iterator SharedOrderVector::begin(){
201 /***********************************************************************/
203 vector<individual>::iterator SharedOrderVector::end(){
207 /***********************************************************************/
209 int SharedOrderVector::size(){
213 /***********************************************************************/
215 RAbundVector SharedOrderVector::getRAbundVector(){
217 RAbundVector rav(data.size());
219 for(int i=0;i<numSeqs;i++){
220 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
222 sort(rav.rbegin(), rav.rend());
223 for(int i=numSeqs-1;i>=0;i--){
224 if(rav.get(i) == 0){ rav.pop_back(); }
233 catch(exception& e) {
234 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
242 /***********************************************************************/
244 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
248 for (int i = 0; i < data.size(); i++) {
249 ov.push_back(data[i].bin);
252 random_shuffle(ov.begin(), ov.end());
257 catch(exception& e) {
258 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268 /***********************************************************************/
270 SAbundVector SharedOrderVector::getSAbundVector(){
272 RAbundVector rav(this->getRAbundVector());
273 return rav.getSAbundVector();
276 /***********************************************************************/
277 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
279 SharedRAbundVector sharedRav(data.size());
281 sharedRav.setLabel(label);
282 sharedRav.setGroup(group);
284 for (int i = 0; i < data.size(); i++) {
285 if (data[i].group == group) {
286 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
291 catch(exception& e) {
292 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301 /***********************************************************************/
302 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
305 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
306 return sharedRav.getSharedSAbundVector();
309 catch(exception& e) {
310 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
314 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
320 /***********************************************************************/
322 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
326 /***********************************************************************/
328 void SharedOrderVector::updateStats(){
335 for(int i=0;i<data.size();i++){
336 if(data[i].bin != -1){
341 vector<individual> hold(numSeqs);
343 for(int i=0;i<numSeqs;i++){
344 if(data[i].bin != -1){
345 hold[data[i].bin].bin = hold[data[i].bin].bin+1;
349 for(int i=0;i<numSeqs;i++){
350 if(data[i].bin > numBins) { numBins = data[i].bin; }
351 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
353 numBins++; //if you have 10 bins largest .bin is 9 since we start at 0.
355 catch(exception& e) {
356 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
360 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
365 /***********************************************************************/