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 /***********************************************************************/
152 //commented updateStats out to improve speed, but whoever calls this must remember to update when they are done with all the pushbacks they are doing
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; }
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 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
305 util = new SharedUtil();
306 vector<SharedRAbundVector*> lookup;
308 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
309 util->getSharedVectors(globaldata->Groups, lookup, this);
313 catch(exception& e) {
314 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
323 /***********************************************************************/
324 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
327 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
328 return sharedRav.getSharedSAbundVector();
331 catch(exception& e) {
332 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342 /***********************************************************************/
344 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
345 random_shuffle(data.begin(), data.end());
349 /***********************************************************************/
351 void SharedOrderVector::updateStats(){
358 numSeqs = data.size();
360 vector<int> hold(numSeqs, 0);
361 for(int i=0;i<numSeqs;i++){
362 hold[data[i].bin] = hold[data[i].bin]+1;
365 for(int i=0;i<numSeqs;i++){
366 if(hold[i] > 0) { numBins++; }
367 if(hold[i] > maxRank) { maxRank = hold[i]; }
371 catch(exception& e) {
372 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
376 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381 /***********************************************************************/