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)
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();
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 /**************************************************************************************************/