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 f >> label >> groupN >> num;
44 vector<string> allGroups;
45 //save group in groupmap
46 allGroups.push_back(groupN);
47 groupmap->groupIndex[groupN] = 0;
50 for(int i=0;i<num;i++){
53 for (int j = 0; j < inputData; j++) {
54 push_back(i, i, groupN);
61 if (f.eof() != true) { f >> nextLabel; }
63 //read the rest of the groups info in
64 while ((nextLabel == holdLabel) && (f.eof() != true)) {
69 //save group in groupmap
70 allGroups.push_back(groupN);
71 groupmap->groupIndex[groupN] = count;
74 for(int i=0;i<num;i++){
77 for (int j = 0; j < inputData; j++) {
78 push_back(i, i, groupN);
85 if (f.eof() != true) { f >> nextLabel; }
89 //put file pointer back since you are now at a new distance label
90 for (int i = 0; i < nextLabel.length(); i++) { f.unget(); }
92 groupmap->setNamesOfGroups(allGroups);
93 m->setAllGroups(allGroups);
99 m->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 m->errorOut(e, "SharedOrderVector", "print");
174 /***********************************************************************/
176 void SharedOrderVector::clear(){
182 /***********************************************************************/
184 void SharedOrderVector::resize(int){
185 m->mothurOut("resize() did nothing in class SharedOrderVector");
188 /***********************************************************************/
191 vector<individual>::iterator SharedOrderVector::begin(){
195 /***********************************************************************/
197 vector<individual>::iterator SharedOrderVector::end(){
201 /***********************************************************************/
203 int SharedOrderVector::size(){
207 /***********************************************************************/
209 RAbundVector SharedOrderVector::getRAbundVector(){
211 RAbundVector rav(data.size());
213 for(int i=0;i<numSeqs;i++){
214 rav.set(data[i].bin, rav.get(data[i].bin) + 1);
216 sort(rav.rbegin(), rav.rend());
217 for(int i=numSeqs-1;i>=0;i--){
218 if(rav.get(i) == 0){ rav.pop_back(); }
227 catch(exception& e) {
228 m->errorOut(e, "SharedOrderVector", "getRAbundVector");
232 /***********************************************************************/
234 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
238 for (int i = 0; i < data.size(); i++) {
239 ov.push_back(data[i].bin);
242 random_shuffle(ov.begin(), ov.end());
247 catch(exception& e) {
248 m->errorOut(e, "SharedOrderVector", "getOrderVector");
254 /***********************************************************************/
256 SAbundVector SharedOrderVector::getSAbundVector(){
258 RAbundVector rav(this->getRAbundVector());
259 return rav.getSAbundVector();
262 /***********************************************************************/
263 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
265 SharedRAbundVector sharedRav(data.size());
267 sharedRav.setLabel(label);
268 sharedRav.setGroup(group);
270 for (int i = 0; i < data.size(); i++) {
271 if (data[i].group == group) {
272 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
277 catch(exception& e) {
278 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
282 /***********************************************************************/
283 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
286 util = new SharedUtil();
287 vector<SharedRAbundVector*> lookup;
289 vector<string> Groups = m->getGroups();
290 vector<string> allGroups = m->getAllGroups();
291 util->setGroups(Groups, allGroups);
292 util->getSharedVectors(Groups, lookup, this);
293 m->setGroups(Groups);
294 m->setAllGroups(allGroups);
298 catch(exception& e) {
299 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
303 /***********************************************************************/
304 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
307 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
308 return sharedRav.getSharedSAbundVector();
311 catch(exception& e) {
312 m->errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
317 /***********************************************************************/
319 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
320 random_shuffle(data.begin(), data.end());
324 /***********************************************************************/
326 void SharedOrderVector::updateStats(){
333 numSeqs = data.size();
335 vector<int> hold(numSeqs, 0);
336 for(int i=0;i<numSeqs;i++){
337 hold[data[i].bin] = hold[data[i].bin]+1;
340 for(int i=0;i<numSeqs;i++){
341 if(hold[i] > 0) { numBins++; }
342 if(hold[i] > maxRank) { maxRank = hold[i]; }
346 catch(exception& e) {
347 m->errorOut(e, "SharedOrderVector", "updateStats");
352 /***********************************************************************/