+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "NormalizeSharedCommand", "normalize");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+
+int NormalizeSharedCommand::normalize(vector<SharedRAbundFloatVector*>& thisLookUp){
+ try {
+
+ //save mothurOut's binLabels to restore for next label
+ vector<string> saveBinLabels = m->currentBinLabels;
+
+ string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputfile)) + thisLookUp[0]->getLabel() + "." + getOutputFileNameTag("shared");
+ ofstream out;
+ m->openOutputFile(outputFileName, out);
+ outputNames.push_back(outputFileName); outputTypes["shared"].push_back(outputFileName);
+
+
+ if (pickedGroups) { eliminateZeroOTUS(thisLookUp); }
+
+ if (method == "totalgroup") {
+
+ //save numSeqs since they will change as the data is normalized
+ vector<float> sizes;
+ for (int i = 0; i < thisLookUp.size(); i++) { sizes.push_back(thisLookUp[i]->getNumSeqs()); }
+
+ for (int j = 0; j < thisLookUp[0]->getNumBins(); j++) {
+
+ for (int i = 0; i < thisLookUp.size(); i++) {
+
+ if (m->control_pressed) { out.close(); return 0; }
+
+ float abund = thisLookUp[i]->getAbundance(j);
+
+ float relabund = abund / (float) sizes[i];
+ float newNorm = relabund * norm;
+
+ thisLookUp[i]->set(j, newNorm, thisLookUp[i]->getGroup());
+ }
+ }
+
+ }else if (method == "zscore") {
+ for (int j = 0; j < thisLookUp[0]->getNumBins(); j++) {
+
+ if (m->control_pressed) { out.close(); return 0; }
+
+ //calc mean
+ float mean = 0.0;
+ for (int i = 0; i < thisLookUp.size(); i++) { mean += thisLookUp[i]->getAbundance(j); }
+ mean /= (float) thisLookUp.size();
+
+ //calc standard deviation
+ float sumSquared = 0.0;
+ for (int i = 0; i < thisLookUp.size(); i++) { sumSquared += ((thisLookUp[i]->getAbundance(j) - mean) * (thisLookUp[i]->getAbundance(j) - mean)); }
+ sumSquared /= (float) thisLookUp.size();
+
+ float standardDev = sqrt(sumSquared);
+
+ for (int i = 0; i < thisLookUp.size(); i++) {
+ float newNorm = 0.0;
+ if (standardDev != 0) { // stop divide by zero
+ newNorm = (thisLookUp[i]->getAbundance(j) - mean) / standardDev;
+ }
+ thisLookUp[i]->set(j, newNorm, thisLookUp[i]->getGroup());
+ }
+ }
+
+ }else{ m->mothurOut(method + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
+
+
+ eliminateZeroOTUS(thisLookUp);
+
+ thisLookUp[0]->printHeaders(out);
+
+ for (int i = 0; i < thisLookUp.size(); i++) {
+ out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t';
+ thisLookUp[i]->print(out);
+ }
+
+ out.close();
+
+ m->currentBinLabels = saveBinLabels;
+
+ return 0;