5 * Created by Sarah Westcott on 11/18/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
11 //#include "ordervector.hpp"
13 /***********************************************************************/
15 int Rarefact::getCurve(float percentFreq = 0.01, int nIters = 1000){
17 RarefactionCurveData* rcd = new RarefactionCurveData();
18 for(int i=0;i<displays.size();i++){
19 rcd->registerDisplay(displays[i]);
22 //convert freq percentage to number
24 if (percentFreq < 1.0) { increment = numSeqs * percentFreq; }
25 else { increment = percentFreq; }
27 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
29 driver(rcd, increment, nIters);
31 vector<int> procIters;
33 int numItersPerProcessor = nIters / processors;
35 //divide iters between processes
36 for (int i = 0; i < processors; i++) {
37 if(i == processors - 1){
38 numItersPerProcessor = nIters - i * numItersPerProcessor;
40 procIters.push_back(numItersPerProcessor);
43 createProcesses(procIters, rcd, increment);
47 driver(rcd, increment, nIters);
50 for(int i=0;i<displays.size();i++){
58 m->errorOut(e, "Rarefact", "getCurve");
62 /***********************************************************************/
63 int Rarefact::driver(RarefactionCurveData* rcd, int increment, int nIters = 1000){
66 for(int iter=0;iter<nIters;iter++){
68 for(int i=0;i<displays.size();i++){
69 displays[i]->init(label);
72 RAbundVector* lookup = new RAbundVector(order->getNumBins());
73 SAbundVector* rank = new SAbundVector(order->getMaxRank()+1);
74 random_shuffle(order->begin(), order->end());
76 for(int i=0;i<numSeqs;i++){
78 if (m->control_pressed) { delete lookup; delete rank; delete rcd; return 0; }
80 int binNumber = order->get(i);
81 int abundance = lookup->get(binNumber);
83 rank->set(abundance, rank->get(abundance)-1);
86 lookup->set(binNumber, abundance);
87 rank->set(abundance, rank->get(abundance)+1);
89 if((i == 0) || (i+1) % increment == 0){
90 rcd->updateRankData(rank);
94 if(numSeqs % increment != 0){
95 rcd->updateRankData(rank);
98 for(int i=0;i<displays.size();i++){
108 catch(exception& e) {
109 m->errorOut(e, "Rarefact", "driver");
113 /**************************************************************************************************/
115 int Rarefact::createProcesses(vector<int>& procIters, RarefactionCurveData* rcd, int increment) {
117 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
120 vector<int> processIDS;
124 //loop through and create all the processes you want
125 while (process != processors) {
129 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
132 driver(rcd, increment, procIters[process]);
134 //pass numSeqs to parent
135 for(int i=0;i<displays.size();i++){
136 string tempFile = toString(getpid()) + toString(i) + ".rarefact.temp";
137 displays[i]->outputTempFiles(tempFile);
140 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
143 driver(rcd, increment, procIters[0]);
145 //force parent to wait until all the processes are done
146 for (int i=0;i<(processors-1);i++) {
147 int temp = processIDS[i];
151 //get data created by processes
152 for (int i=0;i<(processors-1);i++) {
153 for(int j=0;j<displays.size();j++){
154 string s = toString(processIDS[i]) + toString(j) + ".rarefact.temp";
155 displays[j]->inputTempFiles(s);
163 catch(exception& e) {
164 m->errorOut(e, "Rarefact", "createProcesses");
168 /***********************************************************************/
170 int Rarefact::getSharedCurve(float percentFreq = 0.01, int nIters = 1000){
172 SharedRarefactionCurveData* rcd = new SharedRarefactionCurveData();
174 label = lookup[0]->getLabel();
176 //register the displays
177 for(int i=0;i<displays.size();i++){
178 rcd->registerDisplay(displays[i]);
181 //if jumble is false all iters will be the same
182 if (globaldata->jumble == false) { nIters = 1; }
184 //convert freq percentage to number
186 if (percentFreq < 1.0) { increment = numSeqs * percentFreq; }
187 else { increment = percentFreq; }
189 for(int iter=0;iter<nIters;iter++){
191 for(int i=0;i<displays.size();i++){
192 displays[i]->init(label);
195 if (globaldata->jumble == true) {
196 //randomize the groups
197 random_shuffle(lookup.begin(), lookup.end());
200 //make merge the size of lookup[0]
201 SharedRAbundVector* merge = new SharedRAbundVector(lookup[0]->size());
203 //make copy of lookup zero
204 for(int i = 0; i<lookup[0]->size(); i++) {
205 merge->set(i, lookup[0]->getAbundance(i), "merge");
208 vector<SharedRAbundVector*> subset;
209 //send each group one at a time
210 for (int k = 0; k < lookup.size(); k++) {
211 if (m->control_pressed) { delete merge; delete rcd; return 0; }
213 subset.clear(); //clears out old pair of sharedrabunds
214 //add in new pair of sharedrabunds
215 subset.push_back(merge); subset.push_back(lookup[k]);
217 rcd->updateSharedData(subset, k+1, numGroupComb);
218 mergeVectors(merge, lookup[k]);
221 //resets output files
222 for(int i=0;i<displays.size();i++){
223 displays[i]->reset();
229 for(int i=0;i<displays.size();i++){
230 displays[i]->close();
236 catch(exception& e) {
237 m->errorOut(e, "Rarefact", "getSharedCurve");
242 /**************************************************************************************/
243 void Rarefact::mergeVectors(SharedRAbundVector* shared1, SharedRAbundVector* shared2) {
245 for (int k = 0; k < shared1->size(); k++) {
246 //merge new species into shared1
247 shared1->set(k, (shared1->getAbundance(k) + shared2->getAbundance(k)), "combo"); //set to 'combo' since this vector now contains multiple groups
250 catch(exception& e) {
251 m->errorOut(e, "Rarefact", "mergeVectors");