+ /************************ fill lookup in order given by place *********************/
+ //for each bin
+ for (int i = 0; i < looktemp[0]->size(); i++) { //place
+ //fill lookup // 2 -> 1
+ for (int j = 0; j < looktemp.size(); j++) { // 3 -> 2
+ float newAbund = looktemp[j]->getAbundance(i); // 1 -> 3
+ lookup[j]->set(place[i], newAbund, looktemp[j]->getGroup()); //binNumber, abundance, group
+ }
+ }
+
+ //delete looktemp -- Sarah look at - this is causing segmentation faults
+ for (int j = 0; j < looktemp.size(); j++) {
+// delete looktemp[j];
+ }
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "HeatMap", "sortSharedVectors");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int HeatMap::sortRabund(RAbundVector*& r){
+ try {
+ map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+ map<int, int>::iterator it;
+
+ /****************** find order of otus **********************/
+ vector<binCount> totals;
+
+ //for each bin
+ for (int i = 0; i < r->getNumBins(); i++) {
+ binCount temp(i, r->get(i));
+
+ totals.push_back(temp);
+ }
+
+ sort(totals.begin(), totals.end(), comparebinCounts);
+
+ //fill place
+ for (int i = 0; i < totals.size(); i++) { place[totals[i].bin] = i; }
+
+ /******************* create copy of lookup *********************/
+ //create and initialize rtemp as a copy of r
+
+ RAbundVector* rtemp = new RAbundVector(r->getNumBins());
+ for (int i = 0; i < r->size(); i++) { rtemp->set(i, r->get(i)); }
+ rtemp->setLabel(r->getLabel());
+
+ /************************ fill lookup in order given by place *********************/
+ //for each bin
+ for (int i = 0; i < rtemp->size(); i++) { //place
+ //fill lookup // 2 -> 1
+ // 3 -> 2
+ int newAbund = rtemp->get(i); // 1 -> 3
+ r->set(place[i], newAbund); //binNumber, abundance
+ }
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "HeatMap", "sortRabund");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+map<int, int> HeatMap::orderShared(vector<SharedRAbundFloatVector*>& lookup){
+ try {
+
+ map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+ map<int, int>::iterator it;
+
+ vector<int> sharedBins;
+ vector<int> uniqueBins;
+
+ //for each bin
+ for (int i = 0; i < lookup[0]->size(); i++) {
+ int count = 0;
+
+ //is this bin shared
+ for (int j = 0; j < lookup.size(); j++) { if (lookup[j]->getAbundance(i) != 0) { count++; } }
+
+ if (count < 2) { uniqueBins.push_back(i); }
+ else { sharedBins.push_back(i); }
+ }
+
+ //fill place
+ for (int i = 0; i < sharedBins.size(); i++) { place[sharedBins[i]] = i; }
+ for (int i = 0; i < uniqueBins.size(); i++) { place[uniqueBins[i]] = (sharedBins.size() + i); }
+
+ return place;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "HeatMap", "orderShared");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+map<int, int> HeatMap::orderTopOtu(vector<SharedRAbundFloatVector*>& lookup){
+ try {
+
+ map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+ map<int, int>::iterator it;
+
+ vector<binCountFloat> totals;
+
+ //for each bin
+ for (int i = 0; i < lookup[0]->size(); i++) {
+ int total = 0;
+
+ for (int j = 0; j < lookup.size(); j++) { total += lookup[j]->getAbundance(i); }
+
+ binCountFloat temp(i, total);
+
+ totals.push_back(temp);
+ }
+
+ sort(totals.begin(), totals.end(), comparebinFloatCounts);
+
+ //fill place
+ for (int i = 0; i < totals.size(); i++) { place[totals[i].bin] = i; }
+
+ return place;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "HeatMap", "orderTopOtu");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+map<int, int> HeatMap::orderTopGroup(vector<SharedRAbundFloatVector*>& lookup){
+ try {
+
+ map<int, int> place; //spot in lookup where you insert shared by, ie, 3 -> 2 if they are shared by 3 inset into location 2.
+ map<int, int>::iterator it;
+
+ vector < vector<binCountFloat> > totals; //totals[0] = bin totals for group 0, totals[1] = bin totals for group 1, ...
+ totals.resize(lookup.size());
+
+ //for each bin
+ for (int i = 0; i < lookup[0]->size(); i++) {
+ for (int j = 0; j < lookup.size(); j++) {
+ binCountFloat temp(i, (lookup[j]->getAbundance(i)));
+ totals[j].push_back(temp);
+ }
+ }
+
+ for (int i = 0; i < totals.size(); i++) { sort(totals[i].begin(), totals[i].end(), comparebinFloatCounts); }
+
+ //fill place
+ //grab the top otu for each group adding it if its not already added
+ int count = 0;
+ for (int i = 0; i < totals[0].size(); i++) {
+
+ for (int j = 0; j < totals.size(); j++) {
+ it = place.find(totals[j][i].bin);
+
+ if (it == place.end()) { //not added yet
+ place[totals[j][i].bin] = count;
+ count++;
+ }
+ }
+ }
+
+ return place;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "HeatMap", "orderTopGroup");
+ exit(1);
+ }