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();
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++){
56 for (int j = 0; j < inputData; j++) {
57 push_back(i+1, i+1, groupN);
62 //save position in file in case next line is a new label.
65 if (f.eof() != true) { f >> nextLabel; }
67 //read the rest of the groups info in
68 while ((nextLabel == holdLabel) && (f.eof() != true)) {
72 if (globaldata->gGroupmap == NULL) {
73 //save group in groupmap
74 groupmap->namesOfGroups.push_back(groupN);
75 groupmap->groupIndex[groupN] = count;
78 for(int i=0;i<num;i++){
80 for (int j = 0; j < inputData; j++) {
81 push_back(i+1, i+1, groupN);
86 //save position in file in case next line is a new label.
89 if (f.eof() != true) { f >> nextLabel; }
93 //put file pointer back since you are now at a new distance label
94 f.seekg(pos, ios::beg);
96 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(){
112 if(needToUpdate == 1){ updateStats(); }
116 /***********************************************************************/
118 int SharedOrderVector::getNumSeqs(){
119 if(needToUpdate == 1){ updateStats(); }
123 /***********************************************************************/
125 int SharedOrderVector::getMaxRank(){
126 if(needToUpdate == 1){ updateStats(); }
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;
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);
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 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
302 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
303 return sharedRav.getSharedSAbundVector();
306 catch(exception& e) {
307 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311 cout << "An unknown error has occurred in the SharedOrderVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317 /***********************************************************************/
319 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
323 /***********************************************************************/
325 void SharedOrderVector::updateStats(){
332 for(int i=0;i<data.size();i++){
333 if(data[i].bin != -1){
338 vector<individual> hold(numSeqs);
340 for(int i=0;i<numSeqs;i++){
341 if(data[i].bin != -1){
342 hold[data[i].bin].bin = hold[data[i].bin].bin+1;
346 for(int i=0;i<numSeqs;i++){
347 if(data[i].bin > numBins) { numBins = data[i].bin; }
348 if(data[i].abundance > maxRank) { maxRank = data[i].abundance; }
350 numBins++; //if you have 10 bins largest .bin is 9 since we start at 0.
352 catch(exception& e) {
353 cout << "Standard Error: " << e.what() << " has occurred in the SharedOrderVector class Function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
357 cout << "An unknown error has occurred in the SharedOrderVector class function updateStats. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
362 /***********************************************************************/