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);
62 if (f.eof() != true) { f >> nextLabel; }
64 //read the rest of the groups info in
65 while ((nextLabel == holdLabel) && (f.eof() != true)) {
69 if (globaldata->gGroupmap == NULL) {
70 //save group in groupmap
71 groupmap->namesOfGroups.push_back(groupN);
72 groupmap->groupIndex[groupN] = count;
75 for(int i=0;i<num;i++){
78 for (int j = 0; j < inputData; j++) {
79 push_back(i, i, groupN);
86 if (f.eof() != true) { f >> nextLabel; }
90 //put file pointer back since you are now at a new distance label
91 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
93 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap; }
99 errorOut(e, "SharedOrderVector", "SharedOrderVector");
103 /***********************************************************************/
105 int SharedOrderVector::getNumBins(){
109 /***********************************************************************/
111 int SharedOrderVector::getNumSeqs(){
115 /***********************************************************************/
117 int SharedOrderVector::getMaxRank(){
122 /***********************************************************************/
126 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
128 data[index].group = groupName;
129 data[index].bin = binNumber;
130 data[index].abundance = abund;
131 //if (abund > maxRank) { maxRank = abund; }
135 /***********************************************************************/
137 individual SharedOrderVector::get(int index){
142 /***********************************************************************/
143 //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
144 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
146 newGuy.group = groupName;
147 newGuy.abundance = abund;
148 newGuy.bin = binNumber;
149 data.push_back(newGuy);
152 //if (abund > maxRank) { maxRank = abund; }
157 /***********************************************************************/
159 void SharedOrderVector::print(ostream& output){
161 output << label << '\t' << numSeqs << '\t';
163 for(int i=0;i<data.size();i++){
164 output << data[i].bin << '\t';
168 catch(exception& e) {
169 errorOut(e, "SharedOrderVector", "print");
175 /***********************************************************************/
177 void SharedOrderVector::resize(int){
178 mothurOut("resize() did nothing in class SharedOrderVector");
181 /***********************************************************************/
184 vector<individual>::iterator SharedOrderVector::begin(){
188 /***********************************************************************/
190 vector<individual>::iterator SharedOrderVector::end(){
194 /***********************************************************************/
196 int SharedOrderVector::size(){
200 /***********************************************************************/
202 RAbundVector SharedOrderVector::getRAbundVector(){
204 RAbundVector rav(data.size());
206 for(int i=0;i<numSeqs;i++){
207 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
209 sort(rav.rbegin(), rav.rend());
210 for(int i=numSeqs-1;i>=0;i--){
211 if(rav.get(i) == 0){ rav.pop_back(); }
220 catch(exception& e) {
221 errorOut(e, "SharedOrderVector", "getRAbundVector");
225 /***********************************************************************/
227 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
231 for (int i = 0; i < data.size(); i++) {
232 ov.push_back(data[i].bin);
235 random_shuffle(ov.begin(), ov.end());
240 catch(exception& e) {
241 errorOut(e, "SharedOrderVector", "getOrderVector");
247 /***********************************************************************/
249 SAbundVector SharedOrderVector::getSAbundVector(){
251 RAbundVector rav(this->getRAbundVector());
252 return rav.getSAbundVector();
255 /***********************************************************************/
256 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
258 SharedRAbundVector sharedRav(data.size());
260 sharedRav.setLabel(label);
261 sharedRav.setGroup(group);
263 for (int i = 0; i < data.size(); i++) {
264 if (data[i].group == group) {
265 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
270 catch(exception& e) {
271 errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
275 /***********************************************************************/
276 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
279 util = new SharedUtil();
280 vector<SharedRAbundVector*> lookup;
282 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
283 util->getSharedVectors(globaldata->Groups, lookup, this);
287 catch(exception& e) {
288 errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
292 /***********************************************************************/
293 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
296 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
297 return sharedRav.getSharedSAbundVector();
300 catch(exception& e) {
301 errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
306 /***********************************************************************/
308 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
309 random_shuffle(data.begin(), data.end());
313 /***********************************************************************/
315 void SharedOrderVector::updateStats(){
322 numSeqs = data.size();
324 vector<int> hold(numSeqs, 0);
325 for(int i=0;i<numSeqs;i++){
326 hold[data[i].bin] = hold[data[i].bin]+1;
329 for(int i=0;i<numSeqs;i++){
330 if(hold[i] > 0) { numBins++; }
331 if(hold[i] > maxRank) { maxRank = hold[i]; }
335 catch(exception& e) {
336 errorOut(e, "SharedOrderVector", "updateStats");
341 /***********************************************************************/