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 int numGroups = m->Groups.size();
22 //calculate number of comparsions
24 vector< vector<string> > namesOfGroupCombos;
25 for (int r=0; r<numGroups; r++) {
26 for (int l = r+1; l < numGroups; l++) {
28 vector<string> groups; groups.push_back(m->Groups[r]); groups.push_back(m->Groups[l]);
29 //cout << globaldata->Groups[r] << '\t' << globaldata->Groups[l] << endl;
30 namesOfGroupCombos.push_back(groups);
34 //numComp+1 for AB, AC, BC, ABC
36 vector<string> groups;
38 //get score for all users groups
39 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
40 if (tmap->namesOfGroups[i] != "xxx") {
41 groups.push_back(tmap->namesOfGroups[i]);
42 //cout << tmap->namesOfGroups[i] << endl;
45 namesOfGroupCombos.push_back(groups);
47 for (int i = 0; i < m->Groups.size(); i++) {
48 groups.push_back(m->Groups[i]);
49 //cout << globaldata->Groups[i] << endl;
51 namesOfGroupCombos.push_back(groups);
55 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
57 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
60 int numPairs = namesOfGroupCombos.size();
62 int numPairsPerProcessor = numPairs / processors;
64 for (int i = 0; i < processors; i++) {
65 int startPos = i * numPairsPerProcessor;
67 if(i == processors - 1){
68 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
71 lines.push_back(linePair(startPos, numPairsPerProcessor));
74 data = createProcesses(t, namesOfGroupCombos);
77 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
84 m->errorOut(e, "Parsimony", "getValues");
88 /**************************************************************************************************/
90 EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
92 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
94 vector<int> processIDS;
98 //loop through and create all the processes you want
99 while (process != processors) {
103 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
107 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
109 if (m->control_pressed) { exit(0); }
111 //pass numSeqs to parent
113 string tempFile = outputDir + toString(getpid()) + ".pars.results.temp";
114 m->openOutputFile(tempFile, out);
115 out << myresults.size() << endl;
116 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
121 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
122 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
127 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
129 //force parent to wait until all the processes are done
130 for (int i=0;i<processIDS.size();i++) {
131 int temp = processIDS[i];
135 if (m->control_pressed) { return results; }
137 //get data created by processes
138 for (int i=0;i<processIDS.size();i++) {
140 string s = outputDir + toString(processIDS[i]) + ".pars.results.temp";
141 m->openInputFile(s, in);
146 in >> num; m->gobble(in);
148 if (m->control_pressed) { break; }
151 for (int j = 0; j < num; j++) {
153 results.push_back(w);
164 catch(exception& e) {
165 m->errorOut(e, "Parsimony", "createProcesses");
169 /**************************************************************************************************/
170 EstOutput Parsimony::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
173 EstOutput results; results.resize(num);
175 Tree* copyTree = new Tree(tmap);
178 for (int h = start; h < (start+num); h++) {
180 if (m->control_pressed) { delete copyTree; return results; }
184 //groups in this combo
185 vector<string> groups = namesOfGroupCombos[h];
187 //copy users tree so that you can redo pgroups
188 copyTree->getCopy(t);
190 //create pgroups that reflect the groups the user want to use
191 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
192 copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
195 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
197 if (m->control_pressed) { return data; }
199 int lc = copyTree->tree[i].getLChild();
200 int rc = copyTree->tree[i].getRChild();
202 int iSize = copyTree->tree[i].pGroups.size();
203 int rcSize = copyTree->tree[rc].pGroups.size();
204 int lcSize = copyTree->tree[lc].pGroups.size();
206 //if isize are 0 then that branch is to be ignored
208 else if ((rcSize == 0) || (lcSize == 0)) { }
209 //if you have more groups than either of your kids then theres been a change.
210 else if(iSize > rcSize || iSize > lcSize){
215 results[count] = score;
223 catch(exception& e) {
224 m->errorOut(e, "Parsimony", "driver");
229 /**************************************************************************************************/