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, 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 if (f.eof() != true) { f >> nextLabel; }
62 //read the rest of the groups info in
63 while ((nextLabel == holdLabel) && (f.eof() != true)) {
67 if (globaldata->gGroupmap == NULL) {
68 //save group in groupmap
69 groupmap->namesOfGroups.push_back(groupN);
70 groupmap->groupIndex[groupN] = count;
73 for(int i=0;i<num;i++){
76 for (int j = 0; j < inputData; j++) {
77 push_back(i, i, groupN);
83 if (f.eof() != true) { f >> nextLabel; }
87 //put file pointer back since you are now at a new distance label
88 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
90 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
96 errorOut(e, "SharedOrderVector", "SharedOrderVector");
100 /***********************************************************************/
102 int SharedOrderVector::getNumBins(){
106 /***********************************************************************/
108 int SharedOrderVector::getNumSeqs(){
112 /***********************************************************************/
114 int SharedOrderVector::getMaxRank(){
119 /***********************************************************************/
123 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
125 data[index].group = groupName;
126 data[index].bin = binNumber;
127 data[index].abundance = abund;
128 //if (abund > maxRank) { maxRank = abund; }
132 /***********************************************************************/
134 individual SharedOrderVector::get(int index){
139 /***********************************************************************/
140 //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
141 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
143 newGuy.group = groupName;
144 newGuy.abundance = abund;
145 newGuy.bin = binNumber;
146 data.push_back(newGuy);
149 //if (abund > maxRank) { maxRank = abund; }
154 /***********************************************************************/
156 void SharedOrderVector::print(ostream& output){
158 output << label << '\t' << numSeqs << '\t';
160 for(int i=0;i<data.size();i++){
161 output << data[i].bin << '\t';
165 catch(exception& e) {
166 errorOut(e, "SharedOrderVector", "print");
172 /***********************************************************************/
174 void SharedOrderVector::resize(int){
175 mothurOut("resize() did nothing in class SharedOrderVector");
178 /***********************************************************************/
181 vector<individual>::iterator SharedOrderVector::begin(){
185 /***********************************************************************/
187 vector<individual>::iterator SharedOrderVector::end(){
191 /***********************************************************************/
193 int SharedOrderVector::size(){
197 /***********************************************************************/
199 RAbundVector SharedOrderVector::getRAbundVector(){
201 RAbundVector rav(data.size());
203 for(int i=0;i<numSeqs;i++){
204 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
206 sort(rav.rbegin(), rav.rend());
207 for(int i=numSeqs-1;i>=0;i--){
208 if(rav.get(i) == 0){ rav.pop_back(); }
217 catch(exception& e) {
218 errorOut(e, "SharedOrderVector", "getRAbundVector");
222 /***********************************************************************/
224 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
228 for (int i = 0; i < data.size(); i++) {
229 ov.push_back(data[i].bin);
232 random_shuffle(ov.begin(), ov.end());
237 catch(exception& e) {
238 errorOut(e, "SharedOrderVector", "getOrderVector");
244 /***********************************************************************/
246 SAbundVector SharedOrderVector::getSAbundVector(){
248 RAbundVector rav(this->getRAbundVector());
249 return rav.getSAbundVector();
252 /***********************************************************************/
253 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
255 SharedRAbundVector sharedRav(data.size());
257 sharedRav.setLabel(label);
258 sharedRav.setGroup(group);
260 for (int i = 0; i < data.size(); i++) {
261 if (data[i].group == group) {
262 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
267 catch(exception& e) {
268 errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
272 /***********************************************************************/
273 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
276 util = new SharedUtil();
277 vector<SharedRAbundVector*> lookup;
279 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
280 util->getSharedVectors(globaldata->Groups, lookup, this);
284 catch(exception& e) {
285 errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
289 /***********************************************************************/
290 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
293 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
294 return sharedRav.getSharedSAbundVector();
297 catch(exception& e) {
298 errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
303 /***********************************************************************/
305 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
306 random_shuffle(data.begin(), data.end());
310 /***********************************************************************/
312 void SharedOrderVector::updateStats(){
319 numSeqs = data.size();
321 vector<int> hold(numSeqs, 0);
322 for(int i=0;i<numSeqs;i++){
323 hold[data[i].bin] = hold[data[i].bin]+1;
326 for(int i=0;i<numSeqs;i++){
327 if(hold[i] > 0) { numBins++; }
328 if(hold[i] > maxRank) { maxRank = hold[i]; }
332 catch(exception& e) {
333 errorOut(e, "SharedOrderVector", "updateStats");
338 /***********************************************************************/