2 * sharedSharedOrderVector.cpp
5 * Created by Sarah Westcott on 12/9/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedordervector.h"
11 #include "sharedutilities.h"
13 /***********************************************************************/
15 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {}
17 /***********************************************************************/
19 SharedOrderVector::SharedOrderVector(string id, vector<individual> ov) :
20 DataVector(id), data(ov)
25 /***********************************************************************/
26 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
27 //if you don't use a list and groupfile.
29 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() { //reads in a shared file
31 globaldata = GlobalData::getInstance();
32 maxRank = 0; numBins = 0; numSeqs = 0;
34 if (globaldata->gGroupmap == NULL) { groupmap = new GroupMap(); }
36 int num, inputData, pos, count;
37 count = 0; numSeqs = 0;
38 string holdLabel, nextLabel, groupN;
41 //read in first row since you know there is at least 1 group.
42 f >> label >> groupN >> num;
45 if (globaldata->gGroupmap == NULL) {
46 //save group in groupmap
47 groupmap->namesOfGroups.push_back(groupN);
48 groupmap->groupIndex[groupN] = 0;
51 for(int i=0;i<num;i++){
54 for (int j = 0; j < inputData; j++) {
55 push_back(i, i, groupN);
60 //save position in file in case next line is a new label.
63 if (f.eof() != true) { f >> nextLabel; }
65 //read the rest of the groups info in
66 while ((nextLabel == holdLabel) && (f.eof() != true)) {
70 if (globaldata->gGroupmap == NULL) {
71 //save group in groupmap
72 groupmap->namesOfGroups.push_back(groupN);
73 groupmap->groupIndex[groupN] = count;
76 for(int i=0;i<num;i++){
79 for (int j = 0; j < inputData; j++) {
80 push_back(i, i, groupN);
85 //save position in file in case next line is a new label.
88 if (f.eof() != true) { f >> nextLabel; }
92 //put file pointer back since you are now at a new distance label
93 f.seekg(pos, ios::beg);
95 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
100 catch(exception& e) {
101 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 cout << "An unknown error has occurred in the SharedOrderVector class function SharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109 /***********************************************************************/
111 int SharedOrderVector::getNumBins(){
115 /***********************************************************************/
117 int SharedOrderVector::getNumSeqs(){
121 /***********************************************************************/
123 int SharedOrderVector::getMaxRank(){
128 /***********************************************************************/
132 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
134 data[index].group = groupName;
135 data[index].bin = binNumber;
136 data[index].abundance = abund;
137 //if (abund > maxRank) { maxRank = abund; }
141 /***********************************************************************/
143 individual SharedOrderVector::get(int index){
148 /***********************************************************************/
149 //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
150 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
152 newGuy.group = groupName;
153 newGuy.abundance = abund;
154 newGuy.bin = binNumber;
155 data.push_back(newGuy);
158 //if (abund > maxRank) { maxRank = abund; }
163 /***********************************************************************/
165 void SharedOrderVector::print(ostream& output){
167 output << label << '\t' << numSeqs << '\t';
169 for(int i=0;i<data.size();i++){
170 output << data[i].bin << '\t';
174 catch(exception& e) {
175 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 cout << "An unknown error has occurred in the SharedOrderVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185 /***********************************************************************/
187 void SharedOrderVector::resize(int){
188 cout << "resize() did nothing in class SharedOrderVector";
191 /***********************************************************************/
194 vector<individual>::iterator SharedOrderVector::begin(){
198 /***********************************************************************/
200 vector<individual>::iterator SharedOrderVector::end(){
204 /***********************************************************************/
206 int SharedOrderVector::size(){
210 /***********************************************************************/
212 RAbundVector SharedOrderVector::getRAbundVector(){
214 RAbundVector rav(data.size());
216 for(int i=0;i<numSeqs;i++){
217 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
219 sort(rav.rbegin(), rav.rend());
220 for(int i=numSeqs-1;i>=0;i--){
221 if(rav.get(i) == 0){ rav.pop_back(); }
230 catch(exception& e) {
231 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 cout << "An unknown error has occurred in the SharedOrderVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239 /***********************************************************************/
241 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
245 for (int i = 0; i < data.size(); i++) {
246 ov.push_back(data[i].bin);
249 random_shuffle(ov.begin(), ov.end());
254 catch(exception& e) {
255 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 cout << "An unknown error has occurred in the SharedOrderVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265 /***********************************************************************/
267 SAbundVector SharedOrderVector::getSAbundVector(){
269 RAbundVector rav(this->getRAbundVector());
270 return rav.getSAbundVector();
273 /***********************************************************************/
274 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
276 SharedRAbundVector sharedRav(data.size());
278 sharedRav.setLabel(label);
279 sharedRav.setGroup(group);
281 for (int i = 0; i < data.size(); i++) {
282 if (data[i].group == group) {
283 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
288 catch(exception& e) {
289 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298 /***********************************************************************/
299 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
302 util = new SharedUtil();
303 vector<SharedRAbundVector*> lookup;
305 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
306 util->getSharedVectors(globaldata->Groups, lookup, this);
310 catch(exception& e) {
311 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
315 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
320 /***********************************************************************/
321 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
324 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
325 return sharedRav.getSharedSAbundVector();
328 catch(exception& e) {
329 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
333 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339 /***********************************************************************/
341 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
342 random_shuffle(data.begin(), data.end());
346 /***********************************************************************/
348 void SharedOrderVector::updateStats(){
355 numSeqs = data.size();
357 vector<int> hold(numSeqs, 0);
358 for(int i=0;i<numSeqs;i++){
359 hold[data[i].bin] = hold[data[i].bin]+1;
362 for(int i=0;i<numSeqs;i++){
363 if(hold[i] > 0) { numBins++; }
364 if(hold[i] > maxRank) { maxRank = hold[i]; }
368 catch(exception& e) {
369 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378 /***********************************************************************/