+//**********************************************************************************************************************
+int SummaryCommand::process(SAbundVector*& sabund, ofstream& outputFileHandle, ofstream& outAve) {
+ try {
+
+ //calculator -> data -> values
+ vector< vector< vector<double> > > results; results.resize(sumCalculators.size());
+
+ outputFileHandle << sabund->getLabel();
+
+ SubSample sample;
+ for (int thisIter = 0; thisIter < iters+1; thisIter++) {
+
+ SAbundVector* thisIterSabund = sabund;
+
+ //we want the summary results for the whole dataset, then the subsampling
+ if ((thisIter > 0) && subsample) { //subsample sabund and run it
+ //copy sabund since getSample destroys it
+ RAbundVector rabund = sabund->getRAbundVector();
+ SAbundVector* newSabund = new SAbundVector();
+ *newSabund = rabund.getSAbundVector();
+
+ sample.getSample(newSabund, subsampleSize);
+ thisIterSabund = newSabund;
+ }
+
+ for(int i=0;i<sumCalculators.size();i++){
+ vector<double> data = sumCalculators[i]->getValues(thisIterSabund);
+
+ if (m->control_pressed) { return 0; }
+
+ if (thisIter == 0) {
+ outputFileHandle << '\t';
+ sumCalculators[i]->print(outputFileHandle);
+ }else {
+ //some of the calc have hci and lci need to make room for that
+ if (results[i].size() == 0) { results[i].resize(data.size()); }
+ //save results for ave and std.
+ for (int j = 0; j < data.size(); j++) {
+ if (m->control_pressed) { return 0; }
+ results[i][j].push_back(data[j]);
+ }
+ }
+ }
+
+ //cleanup memory
+ if ((thisIter > 0) && subsample) { delete thisIterSabund; }
+ }
+ outputFileHandle << endl;
+
+ if (subsample) {
+ outAve << sabund->getLabel() << '\t' << "ave\t";
+ //find ave and std for this label and output
+ //will need to modify the createGroupSummary to combine results and not mess with the .summary file.
+
+ //calcs -> values
+ vector< vector<double> > calcAverages; calcAverages.resize(sumCalculators.size());
+ for (int i = 0; i < calcAverages.size(); i++) { calcAverages[i].resize(results[i].size(), 0); }
+
+ for (int thisIter = 0; thisIter < iters; thisIter++) { //sum all groups dists for each calculator
+ for (int i = 0; i < calcAverages.size(); i++) { //initialize sums to zero.
+ for (int j = 0; j < calcAverages[i].size(); j++) {
+ calcAverages[i][j] += results[i][j][thisIter];
+ }
+ }
+ }
+
+ for (int i = 0; i < calcAverages.size(); i++) { //finds average.
+ for (int j = 0; j < calcAverages[i].size(); j++) {
+ calcAverages[i][j] /= (float) iters;
+ outAve << calcAverages[i][j] << '\t';
+ }
+ }
+
+ //find standard deviation
+ vector< vector<double> > stdDev; stdDev.resize(sumCalculators.size());
+ for (int i = 0; i < stdDev.size(); i++) { stdDev[i].resize(results[i].size(), 0); }
+
+ for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each
+ for (int i = 0; i < stdDev.size(); i++) {
+ for (int j = 0; j < stdDev[i].size(); j++) {
+ stdDev[i][j] += ((results[i][j][thisIter] - calcAverages[i][j]) * (results[i][j][thisIter] - calcAverages[i][j]));
+ }
+ }
+ }
+
+ outAve << endl << sabund->getLabel() << '\t' << "std\t";
+ for (int i = 0; i < stdDev.size(); i++) { //finds average.
+ for (int j = 0; j < stdDev[i].size(); j++) {
+ stdDev[i][j] /= (float) iters;
+ stdDev[i][j] = sqrt(stdDev[i][j]);
+ outAve << stdDev[i][j] << '\t';
+ }
+ }
+ outAve << endl;
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SummaryCommand", "process");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+vector<string> SummaryCommand::parseSharedFile(string filename) {
+ try {
+ vector<string> filenames;
+
+ map<string, ofstream*> filehandles;
+ map<string, ofstream*>::iterator it3;
+
+ input = new InputData(filename, "sharedfile");
+ vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
+
+ string sharedFileRoot = m->getRootName(filename);
+
+ /******************************************************/
+ if (subsample) {
+ if (subsampleSize == -1) { //user has not set size, set size = smallest samples size
+ subsampleSize = lookup[0]->getNumSeqs();
+ for (int i = 1; i < lookup.size(); i++) {
+ int thisSize = lookup[i]->getNumSeqs();
+
+ if (thisSize < subsampleSize) { subsampleSize = thisSize; }
+ }
+ }else {
+ m->clearGroups();
+ vector<string> Groups;
+ vector<SharedRAbundVector*> temp;
+ for (int i = 0; i < lookup.size(); i++) {
+ if (lookup[i]->getNumSeqs() < subsampleSize) {
+ m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
+ delete lookup[i];
+ }else {
+ Groups.push_back(lookup[i]->getGroup());
+ temp.push_back(lookup[i]);
+ }
+ }
+ lookup = temp;
+ m->setGroups(Groups);
+ }
+
+ if (lookup.size() < 1) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; delete input; return filenames; }
+ }
+
+
+ /******************************************************/
+
+ //clears file before we start to write to it below
+ for (int i=0; i<lookup.size(); i++) {
+ m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
+ filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
+ }
+
+ ofstream* temp;
+ for (int i=0; i<lookup.size(); i++) {
+ temp = new ofstream;
+ filehandles[lookup[i]->getGroup()] = temp;
+ groups.push_back(lookup[i]->getGroup());
+ }