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 maxRank = 0; numBins = 0; numSeqs = 0;
33 groupmap = new GroupMap();
35 int num, inputData, count;
36 count = 0; numSeqs = 0;
37 string holdLabel, nextLabel, groupN;
40 //read in first row since you know there is at least 1 group.
41 //are we at the beginning of the file??
42 if (m->saveNextLabel == "") {
45 //is this a shared file that has headers
46 if (label == "label") {
48 f >> label; m->gobble(f);
51 f >> label; m->gobble(f);
54 label = m->getline(f); m->gobble(f);
56 //parse labels to save
57 istringstream iStringStream(label);
58 m->binLabelsInFile.clear();
59 while(!iStringStream.eof()){
60 if (m->control_pressed) { break; }
62 iStringStream >> temp; m->gobble(iStringStream);
64 m->binLabelsInFile.push_back(temp);
69 }else { label = m->saveNextLabel; }
71 //reset labels, currentLabels may have gotten changed as otus were eliminated because of group choices or sampling
72 m->currentBinLabels = m->binLabelsInFile;
74 //read in first row since you know there is at least 1 group.
80 vector<string> allGroups;
81 //save group in groupmap
82 allGroups.push_back(groupN);
83 groupmap->groupIndex[groupN] = 0;
86 for(int i=0;i<num;i++){
89 for (int j = 0; j < inputData; j++) {
90 push_back(i, i, groupN);
97 if (!(f.eof())) { f >> nextLabel; }
99 //read the rest of the groups info in
100 while ((nextLabel == holdLabel) && (f.eof() != true)) {
105 //save group in groupmap
106 allGroups.push_back(groupN);
107 groupmap->groupIndex[groupN] = count;
110 for(int i=0;i<num;i++){
113 for (int j = 0; j < inputData; j++) {
114 push_back(i, i, groupN);
121 if (f.eof() != true) { f >> nextLabel; }
125 m->saveNextLabel = nextLabel;
127 groupmap->setNamesOfGroups(allGroups);
128 m->setAllGroups(allGroups);
133 catch(exception& e) {
134 m->errorOut(e, "SharedOrderVector", "SharedOrderVector");
138 /***********************************************************************/
140 int SharedOrderVector::getNumBins(){
144 /***********************************************************************/
146 int SharedOrderVector::getNumSeqs(){
150 /***********************************************************************/
152 int SharedOrderVector::getMaxRank(){
157 /***********************************************************************/
161 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
163 data[index].group = groupName;
164 data[index].bin = binNumber;
165 data[index].abundance = abund;
166 //if (abund > maxRank) { maxRank = abund; }
170 /***********************************************************************/
172 individual SharedOrderVector::get(int index){
177 /***********************************************************************/
178 //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
179 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
181 newGuy.group = groupName;
182 newGuy.abundance = abund;
183 newGuy.bin = binNumber;
184 data.push_back(newGuy);
187 //if (abund > maxRank) { maxRank = abund; }
192 /***********************************************************************/
194 void SharedOrderVector::print(ostream& output){
196 output << label << '\t' << numSeqs << '\t';
198 for(int i=0;i<data.size();i++){
199 output << data[i].bin << '\t';
203 catch(exception& e) {
204 m->errorOut(e, "SharedOrderVector", "print");
209 /***********************************************************************/
211 void SharedOrderVector::clear(){
217 /***********************************************************************/
219 void SharedOrderVector::resize(int){
220 m->mothurOut("resize() did nothing in class SharedOrderVector");
223 /***********************************************************************/
226 vector<individual>::iterator SharedOrderVector::begin(){
230 /***********************************************************************/
232 vector<individual>::iterator SharedOrderVector::end(){
236 /***********************************************************************/
238 int SharedOrderVector::size(){
242 /***********************************************************************/
244 RAbundVector SharedOrderVector::getRAbundVector(){
246 RAbundVector rav(data.size());
248 for(int i=0;i<numSeqs;i++){
249 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
251 sort(rav.rbegin(), rav.rend());
252 for(int i=numSeqs-1;i>=0;i--){
253 if(rav.get(i) == 0){ rav.pop_back(); }
262 catch(exception& e) {
263 m->errorOut(e, "SharedOrderVector", "getRAbundVector");
267 /***********************************************************************/
269 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
273 for (int i = 0; i < data.size(); i++) {
274 ov.push_back(data[i].bin);
277 random_shuffle(ov.begin(), ov.end());
282 catch(exception& e) {
283 m->errorOut(e, "SharedOrderVector", "getOrderVector");
289 /***********************************************************************/
291 SAbundVector SharedOrderVector::getSAbundVector(){
293 RAbundVector rav(this->getRAbundVector());
294 return rav.getSAbundVector();
297 /***********************************************************************/
298 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
300 SharedRAbundVector sharedRav(data.size());
302 sharedRav.setLabel(label);
303 sharedRav.setGroup(group);
305 for (int i = 0; i < data.size(); i++) {
306 if (data[i].group == group) {
307 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
312 catch(exception& e) {
313 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
317 /***********************************************************************/
318 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
321 util = new SharedUtil();
322 vector<SharedRAbundVector*> lookup;
324 vector<string> Groups = m->getGroups();
325 vector<string> allGroups = m->getAllGroups();
326 util->setGroups(Groups, allGroups);
327 util->getSharedVectors(Groups, lookup, this);
328 m->setGroups(Groups);
329 m->setAllGroups(allGroups);
333 catch(exception& e) {
334 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
338 /***********************************************************************/
339 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
342 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
343 return sharedRav.getSharedSAbundVector();
346 catch(exception& e) {
347 m->errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
352 /***********************************************************************/
354 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
355 random_shuffle(data.begin(), data.end());
359 /***********************************************************************/
361 void SharedOrderVector::updateStats(){
368 numSeqs = data.size();
370 vector<int> hold(numSeqs, 0);
371 for(int i=0;i<numSeqs;i++){
372 hold[data[i].bin] = hold[data[i].bin]+1;
375 for(int i=0;i<numSeqs;i++){
376 if(hold[i] > 0) { numBins++; }
377 if(hold[i] > maxRank) { maxRank = hold[i]; }
381 catch(exception& e) {
382 m->errorOut(e, "SharedOrderVector", "updateStats");
387 /***********************************************************************/