X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=parsimony.cpp;h=1b08e8a3fe8470e5d34944722e186fdca7e0731b;hp=2a673b0cb622bd97b8f91dc4f1c2e9c69de19e8d;hb=b206f634aae1b4ce13978d203247fb64757d5482;hpb=692e0c1f69a78b568dc85cbdcea9fb6c189e2e6c diff --git a/parsimony.cpp b/parsimony.cpp index 2a673b0..1b08e8a 100644 --- a/parsimony.cpp +++ b/parsimony.cpp @@ -10,87 +10,251 @@ #include "parsimony.h" /**************************************************************************************************/ -EstOutput Parsimony::getValues(Tree* t) { + +EstOutput Parsimony::getValues(Tree* t, int p, string o) { try { - globaldata = GlobalData::getInstance(); + processors = p; + outputDir = o; + CountTable* ct = t->getCountTable(); - data.resize(1,0); - - int score = 0; - - for(int i=t->getNumLeaves();igetNumNodes();i++){ - int lc = t->tree[i].getLChild(); - int rc = t->tree[i].getRChild(); - - int iSize = 0; - int rcSize = 0; - int lcSize = 0; + //if the users enters no groups then give them the score of all groups + vector mGroups = m->getGroups(); + int numGroups = mGroups.size(); + + //calculate number of comparsions + int numComp = 0; + vector< vector > namesOfGroupCombos; + for (int r=0; r groups; groups.push_back(mGroups[r]); groups.push_back(mGroups[l]); + //cout << globaldata->Groups[r] << '\t' << globaldata->Groups[l] << endl; + namesOfGroupCombos.push_back(groups); + } + } - //add in all the groups the users wanted - for (it = t->tree[i].pGroups.begin(); it != t->tree[i].pGroups.end(); it++) { - if (inUsersGroups(it->first) == true) { iSize++; } + //numComp+1 for AB, AC, BC, ABC + if (numComp != 1) { + vector groups; + if (numGroups == 0) { + //get score for all users groups + vector tGroups = ct->getNamesOfGroups(); + for (int i = 0; i < tGroups.size(); i++) { + if (tGroups[i] != "xxx") { + groups.push_back(tGroups[i]); + //cout << tmap->namesOfGroups[i] << endl; + } + } + namesOfGroupCombos.push_back(groups); + }else { + for (int i = 0; i < mGroups.size(); i++) { + groups.push_back(mGroups[i]); + //cout << globaldata->Groups[i] << endl; + } + namesOfGroupCombos.push_back(groups); } + } + + lines.clear(); + int remainingPairs = namesOfGroupCombos.size(); + int startIndex = 0; + for (int remainingProcessors = processors; remainingProcessors > 0; remainingProcessors--) { + int numPairs = remainingPairs; //case for last processor + if (remainingProcessors != 1) { numPairs = ceil(remainingPairs / remainingProcessors); } + lines.push_back(linePair(startIndex, numPairs)); //startIndex, numPairs + startIndex = startIndex + numPairs; + remainingPairs = remainingPairs - numPairs; + } + + data = createProcesses(t, namesOfGroupCombos, ct); + + return data; + + } + catch(exception& e) { + m->errorOut(e, "Parsimony", "getValues"); + exit(1); + } +} +/**************************************************************************************************/ - //if that leaves no groups give it 1 so it will cause no change to parent - if (iSize == 0) { iSize++; } - - //add in all the groups the users wanted - for (it = t->tree[rc].pGroups.begin(); it != t->tree[rc].pGroups.end(); it++) { +EstOutput Parsimony::createProcesses(Tree* t, vector< vector > namesOfGroupCombos, CountTable* ct) { + try { + int process = 1; + vector processIDS; + + EstOutput results; - if (inUsersGroups(it->first) == true) { rcSize++; } - } +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + + //loop through and create all the processes you want + while (process != processors) { + int pid = fork(); - //if that leaves no groups give it 1 so it will cause no change to parent - if (rcSize == 0) { rcSize++; } - + if (pid > 0) { + processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later + process++; + }else if (pid == 0){ + EstOutput myresults; + myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct); - //add in all the groups the users wanted - for (it = t->tree[lc].pGroups.begin(); it != t->tree[lc].pGroups.end(); it++) { - - if (inUsersGroups(it->first) == true) { lcSize++; } + if (m->control_pressed) { exit(0); } + + //pass numSeqs to parent + ofstream out; + string tempFile = outputDir + toString(getpid()) + ".pars.results.temp"; + m->openOutputFile(tempFile, out); + out << myresults.size() << endl; + for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl; + out.close(); + + exit(0); + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); } + } + + results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct); + + //force parent to wait until all the processes are done + for (int i=0;icontrol_pressed) { return results; } - //if that leaves no groups give it 1 so it will cause no change to parent - if (lcSize == 0) { lcSize++; } - - - //if you have more groups than either of your kids then theres been a change. - if(iSize > rcSize || iSize > lcSize){ - score++; - + //get data created by processes + for (int i=0;iopenInputFile(s, in); + + //get scores + if (!in.eof()) { + int num; + in >> num; m->gobble(in); + + if (m->control_pressed) { break; } + + double w; + for (int j = 0; j < num; j++) { + in >> w; + results.push_back(w); + } + m->gobble(in); } - } + in.close(); + m->mothurRemove(s); + } +#else + //fill in functions + vector pDataArray; + DWORD dwThreadIdArray[processors-1]; + HANDLE hThreadArray[processors-1]; + vector cts; + vector trees; - data[0] = score; + //Create processor worker threads. + for( int i=1; icopy(ct); + Tree* copyTree = new Tree(copyCount); + copyTree->getCopy(t); + + cts.push_back(copyCount); + trees.push_back(copyTree); + + parsData* temppars = new parsData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount); + pDataArray.push_back(temppars); + processIDS.push_back(i); + + hThreadArray[i-1] = CreateThread(NULL, 0, MyParsimonyThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]); + } - return data; + results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct); + + //Wait until all threads have terminated. + WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE); + + //Close all thread handles and free memory allocations. + for(int i=0; i < pDataArray.size(); i++){ + for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); } + delete cts[i]; + delete trees[i]; + CloseHandle(hThreadArray[i]); + delete pDataArray[i]; + } + +#endif + return results; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the Parsimony class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the Parsimony class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "Parsimony", "createProcesses"); exit(1); } - } /**************************************************************************************************/ - -bool Parsimony::inUsersGroups(string groupname) { +EstOutput Parsimony::driver(Tree* t, vector< vector > namesOfGroupCombos, int start, int num, CountTable* ct) { try { - for (int i = 0; i < globaldata->Groups.size(); i++) { - if (groupname == globaldata->Groups[i]) { return true; } + + EstOutput results; results.resize(num); + + Tree* copyTree = new Tree(ct); + int count = 0; + + for (int h = start; h < (start+num); h++) { + + if (m->control_pressed) { delete copyTree; return results; } + + int score = 0; + + //groups in this combo + vector groups = namesOfGroupCombos[h]; + + //copy users tree so that you can redo pgroups + copyTree->getCopy(t); + + //create pgroups that reflect the groups the user want to use + for(int i=copyTree->getNumLeaves();igetNumNodes();i++){ + copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups)); + } + + for(int i=copyTree->getNumLeaves();igetNumNodes();i++){ + + if (m->control_pressed) { return data; } + + int lc = copyTree->tree[i].getLChild(); + int rc = copyTree->tree[i].getRChild(); + + int iSize = copyTree->tree[i].pGroups.size(); + int rcSize = copyTree->tree[rc].pGroups.size(); + int lcSize = copyTree->tree[lc].pGroups.size(); + + //if isize are 0 then that branch is to be ignored + if (iSize == 0) { } + else if ((rcSize == 0) || (lcSize == 0)) { } + //if you have more groups than either of your kids then theres been a change. + else if(iSize > rcSize || iSize > lcSize){ + score++; + } + } + + results[count] = score; + count++; } - return false; + + delete copyTree; + + return results; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the Parsimony class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the Parsimony class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "Parsimony", "driver"); exit(1); } } + /**************************************************************************************************/ +