5 * Created by Sarah Westcott on 1/26/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "parsimony.h"
12 /**************************************************************************************************/
14 EstOutput Parsimony::getValues(Tree* t, int p, string o) {
19 //if the users enters no groups then give them the score of all groups
20 vector<string> mGroups = m->getGroups();
21 int numGroups = mGroups.size();
23 //calculate number of comparsions
25 vector< vector<string> > namesOfGroupCombos;
26 for (int r=0; r<numGroups; r++) {
27 for (int l = 0; l < r; l++) {
29 vector<string> groups; groups.push_back(mGroups[r]); groups.push_back(mGroups[l]);
30 //cout << globaldata->Groups[r] << '\t' << globaldata->Groups[l] << endl;
31 namesOfGroupCombos.push_back(groups);
35 //numComp+1 for AB, AC, BC, ABC
37 vector<string> groups;
39 //get score for all users groups
40 vector<string> tGroups = tmap->getNamesOfGroups();
41 for (int i = 0; i < tGroups.size(); i++) {
42 if (tGroups[i] != "xxx") {
43 groups.push_back(tGroups[i]);
44 //cout << tmap->namesOfGroups[i] << endl;
47 namesOfGroupCombos.push_back(groups);
49 for (int i = 0; i < mGroups.size(); i++) {
50 groups.push_back(mGroups[i]);
51 //cout << globaldata->Groups[i] << endl;
53 namesOfGroupCombos.push_back(groups);
57 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
59 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
62 int numPairs = namesOfGroupCombos.size();
64 int numPairsPerProcessor = numPairs / processors;
66 for (int i = 0; i < processors; i++) {
67 int startPos = i * numPairsPerProcessor;
69 if(i == processors - 1){
70 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
73 lines.push_back(linePair(startPos, numPairsPerProcessor));
76 data = createProcesses(t, namesOfGroupCombos);
79 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
86 m->errorOut(e, "Parsimony", "getValues");
90 /**************************************************************************************************/
92 EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
94 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
96 vector<int> processIDS;
100 //loop through and create all the processes you want
101 while (process != processors) {
105 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
109 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
111 if (m->control_pressed) { exit(0); }
113 //pass numSeqs to parent
115 string tempFile = outputDir + toString(getpid()) + ".pars.results.temp";
116 m->openOutputFile(tempFile, out);
117 out << myresults.size() << endl;
118 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
123 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
124 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
129 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
131 //force parent to wait until all the processes are done
132 for (int i=0;i<processIDS.size();i++) {
133 int temp = processIDS[i];
137 if (m->control_pressed) { return results; }
139 //get data created by processes
140 for (int i=0;i<processIDS.size();i++) {
142 string s = outputDir + toString(processIDS[i]) + ".pars.results.temp";
143 m->openInputFile(s, in);
148 in >> num; m->gobble(in);
150 if (m->control_pressed) { break; }
153 for (int j = 0; j < num; j++) {
155 results.push_back(w);
166 catch(exception& e) {
167 m->errorOut(e, "Parsimony", "createProcesses");
171 /**************************************************************************************************/
172 EstOutput Parsimony::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
175 EstOutput results; results.resize(num);
177 Tree* copyTree = new Tree(tmap);
180 for (int h = start; h < (start+num); h++) {
182 if (m->control_pressed) { delete copyTree; return results; }
186 //groups in this combo
187 vector<string> groups = namesOfGroupCombos[h];
189 //copy users tree so that you can redo pgroups
190 copyTree->getCopy(t);
192 //create pgroups that reflect the groups the user want to use
193 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
194 copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
197 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
199 if (m->control_pressed) { return data; }
201 int lc = copyTree->tree[i].getLChild();
202 int rc = copyTree->tree[i].getRChild();
204 int iSize = copyTree->tree[i].pGroups.size();
205 int rcSize = copyTree->tree[rc].pGroups.size();
206 int lcSize = copyTree->tree[lc].pGroups.size();
208 //if isize are 0 then that branch is to be ignored
210 else if ((rcSize == 0) || (lcSize == 0)) { }
211 //if you have more groups than either of your kids then theres been a change.
212 else if(iSize > rcSize || iSize > lcSize){
217 results[count] = score;
225 catch(exception& e) {
226 m->errorOut(e, "Parsimony", "driver");
231 /**************************************************************************************************/