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) {
16 globaldata = GlobalData::getInstance();
20 //if the users enters no groups then give them the score of all groups
21 int numGroups = globaldata->Groups.size();
23 //calculate number of comparsions
25 vector< vector<string> > namesOfGroupCombos;
26 for (int r=0; r<numGroups; r++) {
27 for (int l = r+1; l < numGroups; l++) {
29 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[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 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
41 if (tmap->namesOfGroups[i] != "xxx") {
42 groups.push_back(tmap->namesOfGroups[i]);
43 //cout << tmap->namesOfGroups[i] << endl;
46 namesOfGroupCombos.push_back(groups);
48 for (int i = 0; i < globaldata->Groups.size(); i++) {
49 groups.push_back(globaldata->Groups[i]);
50 //cout << globaldata->Groups[i] << endl;
52 namesOfGroupCombos.push_back(groups);
56 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
58 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
61 int numPairs = namesOfGroupCombos.size();
63 int numPairsPerProcessor = numPairs / processors;
65 for (int i = 0; i < processors; i++) {
66 int startPos = i * numPairsPerProcessor;
68 if(i == processors - 1){
69 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
72 lines.push_back(linePair(startPos, numPairsPerProcessor));
75 data = createProcesses(t, namesOfGroupCombos);
78 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
85 m->errorOut(e, "Parsimony", "getValues");
89 /**************************************************************************************************/
91 EstOutput Parsimony::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
93 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
95 vector<int> processIDS;
99 //loop through and create all the processes you want
100 while (process != processors) {
104 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
108 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
110 if (m->control_pressed) { exit(0); }
112 //pass numSeqs to parent
114 string tempFile = outputDir + toString(getpid()) + ".pars.results.temp";
115 m->openOutputFile(tempFile, out);
116 out << myresults.size() << endl;
117 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
122 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
123 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
128 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
130 //force parent to wait until all the processes are done
131 for (int i=0;i<processIDS.size();i++) {
132 int temp = processIDS[i];
136 if (m->control_pressed) { return results; }
138 //get data created by processes
139 for (int i=0;i<processIDS.size();i++) {
141 string s = outputDir + toString(processIDS[i]) + ".pars.results.temp";
142 m->openInputFile(s, in);
147 in >> num; m->gobble(in);
149 if (m->control_pressed) { break; }
152 for (int j = 0; j < num; j++) {
154 results.push_back(w);
165 catch(exception& e) {
166 m->errorOut(e, "Parsimony", "createProcesses");
170 /**************************************************************************************************/
171 EstOutput Parsimony::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
174 EstOutput results; results.resize(num);
176 Tree* copyTree = new Tree();
179 for (int h = start; h < (start+num); h++) {
181 if (m->control_pressed) { delete copyTree; return results; }
185 //groups in this combo
186 vector<string> groups = namesOfGroupCombos[h];
188 //copy users tree so that you can redo pgroups
189 copyTree->getCopy(t);
191 //create pgroups that reflect the groups the user want to use
192 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
193 copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
196 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
198 if (m->control_pressed) { return data; }
200 int lc = copyTree->tree[i].getLChild();
201 int rc = copyTree->tree[i].getRChild();
203 int iSize = copyTree->tree[i].pGroups.size();
204 int rcSize = copyTree->tree[rc].pGroups.size();
205 int lcSize = copyTree->tree[lc].pGroups.size();
207 //if isize are 0 then that branch is to be ignored
209 else if ((rcSize == 0) || (lcSize == 0)) { }
210 //if you have more groups than either of your kids then theres been a change.
211 else if(iSize > rcSize || iSize > lcSize){
216 results[count] = score;
224 catch(exception& e) {
225 m->errorOut(e, "Parsimony", "driver");
230 /**************************************************************************************************/