Debian Codegraph @

src/dr/evomodel/antigenic/phyloclustering/TreeClusteringSharedRoutines.java

raw symbols SCIP
1 package dr.evomodel.antigenic.phyloclustering;
2
3 import java.util.LinkedList;
4
5 import dr.evolution.tree.NodeRef;
6 import dr.evomodel.tree.TreeModel;
7 import dr.inference.model.MatrixParameter;
8 import dr.inference.model.Parameter;
9
10
11
12 public class TreeClusteringSharedRoutines {
13
14
15 public static int[] setMembershipTreeToVirusIndexes(int numdata, MatrixParameter virusLocations, int numNodes, TreeModel treeModel ){
16
17 //I suspect this is an expensive operation, so I don't want to do it many times,
18 //which is also unnecessary - MAY have to update whenever a different tree is used.
19 int[] correspondingTreeIndexForVirus = new int[numdata];
20 for(int i=0; i < numdata; i++){
21 Parameter v = virusLocations.getParameter(i);
22 String curName = v.getParameterName();
23 // System.out.println(curName);
24 int isFound = 0;
25 for(int j=0; j < numNodes; j++){
26 String treeId = treeModel.getTaxonId(j);
27 if(curName.equals(treeId) ){
28 // System.out.println(" isFound at j=" + j);
29 correspondingTreeIndexForVirus[i] = j;
30 isFound=1;
31 break;
32 }
33 }
34 if(isFound ==0){
35 System.out.println("not found. Exit now.");
36 System.exit(0);
37 }
38 }
39
40 return(correspondingTreeIndexForVirus);
41 }
42
43 public static void updateUndriftedVirusLocations(int numNodes, int numdata, TreeModel treeModel, MatrixParameter virusLocationsTreeNode, Parameter indicators, MatrixParameter mu, MatrixParameter virusLocations, int[] correspondingTreeIndexForVirus){
44 double[][] nodeloc = new double[numNodes][2];
45
46 //process the tree and get the vLoc of the viruses..
47 //breadth first depth first..
48 NodeRef cNode = treeModel.getRoot();
49 LinkedList<NodeRef> visitlist = new LinkedList<NodeRef>();
50
51 visitlist.add(cNode);
52
53 int countProcessed=0;
54 while(visitlist.size() > 0){
55 countProcessed++;
56 //assign value to the current node...
57 if(treeModel.getParent(cNode) == null){ //this means it is a root node
58 Parameter curMu = mu.getParameter( cNode.getNumber() );
59 //Parameter curMu = mu.getParameter(0);
60 nodeloc[cNode.getNumber()][0] = curMu.getParameterValue(0);
61 nodeloc[cNode.getNumber() ][1] = curMu.getParameterValue(1);
62
63
64 Parameter curVirusLoc = virusLocationsTreeNode.getParameter(cNode.getNumber());
65 curVirusLoc.setParameterValue(0, curMu.getParameterValue(0) );
66 curVirusLoc.setParameterValue(1, curMu.getParameterValue(1) );
67 }
68 else{
69 nodeloc[cNode.getNumber()][0] = nodeloc[treeModel.getParent(cNode).getNumber()][0];
70 nodeloc[cNode.getNumber()][1] = nodeloc[treeModel.getParent(cNode).getNumber()][1];
71
72 if( (int) indicators.getParameterValue(cNode.getNumber()) == 1){
73 Parameter curMu = mu.getParameter(cNode.getNumber() ); // no +1 because I don't need another mu- the root's mu takes care of the first cluster's mu
74 //Parameter curMu = mu.getParameter(cNode.getNumber() +1); //+1 because mu0 is reserved for the root.
75 nodeloc[cNode.getNumber()][0] += curMu.getParameterValue(0);
76 nodeloc[cNode.getNumber()][1] += curMu.getParameterValue(1);
77 }
78
79 Parameter curVirusLoc = virusLocationsTreeNode.getParameter(cNode.getNumber());
80 curVirusLoc.setParameterValue(0, nodeloc[cNode.getNumber()][0] );
81 curVirusLoc.setParameterValue(1,nodeloc[cNode.getNumber()][1] );
82 }
83
84 //add all the children to the queue
85 for(int childNum=0; childNum < treeModel.getChildCount(cNode); childNum++){
86 NodeRef node= treeModel.getChild(cNode,childNum);
87 visitlist.add(node);
88 }
89
90
91 visitlist.pop(); //now that we have finished visiting this node, pops it out of the queue
92
93 if(visitlist.size() > 0){
94 cNode = visitlist.getFirst(); //set the new first node in the queue to visit
95 }
96
97
98 }
99
100 //write the virus locations
101 for(int i=0; i < numdata; i++){
102 Parameter vLocParameter = virusLocations.getParameter(i);
103 vLocParameter.setParameterValue(0, nodeloc[correspondingTreeIndexForVirus[i]][0]);
104 vLocParameter.setParameterValue(1, nodeloc[correspondingTreeIndexForVirus[i]][1]);
105 }
106
107
108 //for(int i=0; i < numdata; i++){
109 //Parameter vLocP= virusLocations.getParameter(i);
110 //System.out.println("virus " + vLocP.getId() + "\t" + vLocP.getParameterValue(0) + "," + vLocP.getParameterValue(1) );
111 //}
112
113 }
114
115
116
117
118
119 //may be very inefficient
120 public static int findAnOnNodeIncludingRootRandomly(int numNodes, Parameter indicators) {
121 int isOn= 0;
122 int I_selected = -1;
123 while(isOn ==0){
124 I_selected = (int) (Math.floor(Math.random()*numNodes));
125 isOn = (int) indicators.getParameterValue(I_selected);
126 }
127
128 return I_selected;
129 }
130
131
132
133 //Copied from TreeClusterAlgorithm - should have put into the shared class...
134
135
136 public static LinkedList<Integer> findActiveBreakpointsChildren(int selectedNodeNumber, int numNodes, TreeModel treeModel, Parameter indicators) {
137
138 //a list of breakpoints...
139
140 LinkedList<Integer> linkedList = new LinkedList<Integer>();
141 int[] nodeBreakpointNumber = new int[numNodes];
142
143 //int[] nodeStatus = new int[numNodes];
144 //for(int i=0; i < numNodes; i ++){
145 // nodeStatus[i] = -1;
146 //}
147
148 //convert to easy process format.
149 //for(int i=0; i < (binSize ); i++){
150 // if((int) indicators.getParameterValue(i) ==1){
151 // nodeStatus[(int)breakPoints.getParameterValue(i)] = i;
152 // }
153 //}
154
155 //process the tree and get the vLoc of the viruses..
156 //breadth first depth first..
157 NodeRef cNode = treeModel.getRoot();
158 LinkedList<NodeRef> visitlist = new LinkedList<NodeRef>();
159
160
161 visitlist.add(cNode);
162
163
164 //I am not sure if it still works......
165
166 int countProcessed=0;
167 while(visitlist.size() > 0){
168
169
170 countProcessed++;
171 //assign value to the current node...
172 if(treeModel.getParent(cNode) == null){
173 //Parameter curMu = mu.getParameter(0);
174 nodeBreakpointNumber[cNode.getNumber()] = cNode.getNumber();
175 }
176 else{
177 nodeBreakpointNumber[cNode.getNumber()] = nodeBreakpointNumber[treeModel.getParent(cNode).getNumber()];
178 //System.out.println("node#" + cNode.getNumber() + " is " + nodeBreakpointNumber[cNode.getNumber()]);
179
180 if( (int) indicators.getParameterValue(cNode.getNumber()) == 1){
181 //System.out.println(cNode.getNumber() + " is a break point");
182 //Parameter curMu = mu.getParameter(cNode.getNumber() +1); //+1 because mu0 is reserved for the root.
183 //Parameter curMu = mu.getParameter(cNode.getNumber() ); //+1 because mu0 is reserved for the root.
184
185 //see if parent's status is the same as the selectedIndex
186 if( nodeBreakpointNumber[cNode.getNumber()] == selectedNodeNumber ){
187 //System.out.println("hihi");
188 linkedList.add( cNode.getNumber() );
189 }
190 //now, replace this nodeBreakpointNumber with its own node number
191 nodeBreakpointNumber[cNode.getNumber()] = cNode.getNumber();
192
193 }
194 }
195
196
197 //add all the children to the queue
198 for(int childNum=0; childNum < treeModel.getChildCount(cNode); childNum++){
199 NodeRef node= treeModel.getChild(cNode,childNum);
200 visitlist.add(node);
201 }
202
203
204 visitlist.pop(); //now that we have finished visiting this node, pops it out of the queue
205
206 if(visitlist.size() > 0){
207 cNode = visitlist.getFirst(); //set the new first node in the queue to visit
208 }
209
210
211 }
212
213 //System.out.println("Now printing children of " + selectedNodeNumber+":");
214 //for(int i=0; i < linkedList.size(); i++){
215 // System.out.println( linkedList.get(i) );
216 //}
217
218 return linkedList;
219 }
220
221
222
223
224 }