File: parsimony.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (156 lines) | stat: -rwxr-xr-x 5,573 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *  parsimony.cpp
 *  Mothur
 *
 *  Created by Sarah Westcott on 1/26/09.
 *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
 *
 */

#include "parsimony.h"

/**************************************************************************************************/
Parsimony::Parsimony(vector<string> G) : Groups(G) {
    try {
        int numGroups = Groups.size();
        
        //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
        for (int i=0; i<numGroups; i++) {
            for (int l = 0; l < i; l++) {
                vector<string> groups; groups.push_back(Groups[i]); groups.push_back(Groups[l]);
                namesOfGroupCombos.push_back(groups);
            }
        }
    }
    catch(exception& e) {
        m->errorOut(e, "Parsimony", "Parsimony");
        exit(1);
    }
}
/**************************************************************************************************/

EstOutput Parsimony::getValues(Tree* t, int p, string o) {
	try {
		processors = p; outputDir = o;
        CountTable* ct = t->getCountTable();
        Treenames = t->getTreeNames();
		
		return (createProcesses(t, ct));
		
	}
	catch(exception& e) {
		m->errorOut(e, "Parsimony", "getValues");
		exit(1);
	}
}
/**************************************************************************************************/
void driverPars(parsData* params) {
    try {
        
        Tree copyTree(params->ct, params->Treenames);
        int count = 0;
        
        for (int h = params->start; h < (params->start+params->num); h++) {
            
            if (params->m->getControl_pressed()) { break; }
            
            int score = 0;
            
            //groups in this combo
            vector<string> groups = params->namesOfGroupCombos[h];
            
            //copy users tree so that you can redo pgroups
            copyTree.getCopy(params->t);
            
            //create pgroups that reflect the groups the user want to use
            for(int i=copyTree.getNumLeaves();i<copyTree.getNumNodes();i++){
                copyTree.tree[i].pGroups = (copyTree.mergeUserGroups(i, groups));
            }
            
            for(int i=copyTree.getNumLeaves();i<copyTree.getNumNodes();i++){
                
                if (params->m->getControl_pressed()) { break; }
                
                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++;
                }
            } 
            
            params->results[count] = score;
            count++;
        }
    }
    catch(exception& e) {
        params->m->errorOut(e, "Parsimony", "driver");
        exit(1);
    }
}
/**************************************************************************************************/

EstOutput Parsimony::createProcesses(Tree* t, CountTable* ct) {
	try {
        vector<linePair> lines;
        int remainingPairs = namesOfGroupCombos.size();
        if (remainingPairs < processors) { processors = remainingPairs; }
        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;
        }

        //create array of worker threads
        vector<std::thread*> workerThreads;
        vector<parsData*> data;
        
        //Lauch worker threads
        for (int i = 0; i < processors-1; i++) {
            CountTable* copyCount = new CountTable();
            copyCount->copy(ct);
            Tree* copyTree = new Tree(copyCount, Treenames);
            copyTree->getCopy(t);
            
            parsData* dataBundle = new parsData(lines[i+1].start, lines[i+1].end, namesOfGroupCombos, copyTree, copyCount);
            data.push_back(dataBundle);
            
            workerThreads.push_back(new std::thread(driverPars, dataBundle));
        }
        
        parsData* dataBundle = new parsData(lines[0].start, lines[0].end, namesOfGroupCombos, t, ct);
        driverPars(dataBundle);
        EstOutput results = dataBundle->results;
        delete dataBundle;
        
        for (int i = 0; i < processors-1; i++) {
            workerThreads[i]->join();
            
            for (int j = 0; j < data[i]->results.size(); j++) {  results.push_back(data[i]->results[j]);  }
            
            delete data[i]->t;
            delete data[i]->ct;
            delete data[i];
            delete workerThreads[i];
        }
		
        return results;
	}
	catch(exception& e) {
		m->errorOut(e, "Parsimony", "createProcesses");
		exit(1);
	}
}
/**************************************************************************************************/