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 "sharedutilities.h"
16 /***********************************************************************/
18 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {}
20 /***********************************************************************/
22 SharedOrderVector::SharedOrderVector(string id, vector<individual> ov) :
23 DataVector(id), data(ov)
28 /***********************************************************************/
29 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
30 //if you don't use a list and groupfile.
32 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() { //reads in a shared file
34 globaldata = GlobalData::getInstance();
35 maxRank = 0; numBins = 0; numSeqs = 0;
37 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
39 int num, inputData, pos, count;
40 count = 0; numSeqs = 0;
41 string holdLabel, nextLabel, groupN;
44 //read in first row since you know there is at least 1 group.
45 f >> label >> groupN >> num;
48 if (globaldata->gGroupmap == NULL) {
49 //save group in groupmap
50 groupmap->namesOfGroups.push_back(groupN);
51 groupmap->groupIndex[groupN] = 0;
54 for(int i=0;i<num;i++){
57 for (int j = 0; j < inputData; j++) {
58 push_back(i, i, groupN);
63 //save position in file in case next line is a new label.
66 if (f.eof() != true) { f >> nextLabel; }
68 //read the rest of the groups info in
69 while ((nextLabel == holdLabel) && (f.eof() != true)) {
73 if (globaldata->gGroupmap == NULL) {
74 //save group in groupmap
75 groupmap->namesOfGroups.push_back(groupN);
76 groupmap->groupIndex[groupN] = count;
79 for(int i=0;i<num;i++){
82 for (int j = 0; j < inputData; j++) {
83 push_back(i, i, groupN);
88 //save position in file in case next line is a new label.
91 if (f.eof() != true) { f >> nextLabel; }
95 //put file pointer back since you are now at a new distance label
96 f.seekg(pos, ios::beg);
98 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(){
118 /***********************************************************************/
120 int SharedOrderVector::getNumSeqs(){
124 /***********************************************************************/
126 int SharedOrderVector::getMaxRank(){
131 /***********************************************************************/
135 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
137 data[index].group = groupName;
138 data[index].bin = binNumber;
139 data[index].abundance = abund;
140 //if (abund > maxRank) { maxRank = abund; }
144 /***********************************************************************/
146 individual SharedOrderVector::get(int index){
151 /***********************************************************************/
153 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
155 newGuy.group = groupName;
156 newGuy.abundance = abund;
157 newGuy.bin = binNumber;
158 data.push_back(newGuy);
161 //if (abund > maxRank) { maxRank = abund; }
165 /***********************************************************************/
167 void SharedOrderVector::print(ostream& output){
169 output << label << '\t' << numSeqs << '\t';
171 for(int i=0;i<data.size();i++){
172 output << data[i].bin << '\t';
176 catch(exception& e) {
177 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 /***********************************************************************/
189 void SharedOrderVector::resize(int){
190 cout << "resize() did nothing in class SharedOrderVector";
193 /***********************************************************************/
196 vector<individual>::iterator SharedOrderVector::begin(){
200 /***********************************************************************/
202 vector<individual>::iterator SharedOrderVector::end(){
206 /***********************************************************************/
208 int SharedOrderVector::size(){
212 /***********************************************************************/
214 RAbundVector SharedOrderVector::getRAbundVector(){
216 RAbundVector rav(data.size());
218 for(int i=0;i<numSeqs;i++){
219 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
221 sort(rav.rbegin(), rav.rend());
222 for(int i=numSeqs-1;i>=0;i--){
223 if(rav.get(i) == 0){ rav.pop_back(); }
232 catch(exception& e) {
233 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241 /***********************************************************************/
243 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
247 for (int i = 0; i < data.size(); i++) {
248 ov.push_back(data[i].bin);
251 random_shuffle(ov.begin(), ov.end());
256 catch(exception& e) {
257 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267 /***********************************************************************/
269 SAbundVector SharedOrderVector::getSAbundVector(){
271 RAbundVector rav(this->getRAbundVector());
272 return rav.getSAbundVector();
275 /***********************************************************************/
276 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
278 SharedRAbundVector sharedRav(data.size());
280 sharedRav.setLabel(label);
281 sharedRav.setGroup(group);
283 for (int i = 0; i < data.size(); i++) {
284 if (data[i].group == group) {
285 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
290 catch(exception& e) {
291 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 /***********************************************************************/
301 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
304 util = new SharedUtil();
305 vector<SharedRAbundVector*> lookup;
307 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
308 util->getSharedVectors(globaldata->Groups, lookup, this);
312 catch(exception& e) {
313 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
322 /***********************************************************************/
323 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
326 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
327 return sharedRav.getSharedSAbundVector();
330 catch(exception& e) {
331 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
335 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
341 /***********************************************************************/
343 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
344 random_shuffle(data.begin(), data.end());
348 /***********************************************************************/
350 void SharedOrderVector::updateStats(){
357 for(int i=0;i<data.size();i++){
358 if(data[i].bin != -1){
363 vector<individual> hold(numSeqs);
365 for(int i=0;i<numSeqs;i++){
366 if(data[i].bin != -1){
367 hold[data[i].bin].bin = hold[data[i].bin].bin+1;
371 for(int i=0;i<numSeqs;i++){
372 if(data[i].bin > numBins) { numBins = data[i].bin; }
373 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
375 numBins++; //if you have 10 bins largest .bin is 9 since we start at 0.
377 catch(exception& e) {
378 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
387 /***********************************************************************/