File: TreeNode.java

package info (click to toggle)
proalign 0.603-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: java: 8,673; sh: 27; makefile: 4
file content (317 lines) | stat: -rw-r--r-- 8,962 bytes parent folder | download | duplicates (5)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
 * Title:        ProAlign<p>
 * Description:  <p>
 * Copyright:    Copyright (c) Ari Loytynoja<p>
 * License:      GNU GENERAL PUBLIC LICENSE<p>
 * @see          http://www.gnu.org/copyleft/gpl.html
 * Company:      ULB<p>
 * @author Ari Loytynoja
 * @version 1.0
 */
package proalign;

class TreeNode {

    String tree;
    String[] subTrees;
    String[] revTrees;
    static String mpTree;

    TreeNode parent;
    TreeNode child0;
    TreeNode child1;

    static float halfLength;
    static float maxSpan;
    static float minDiff;
    float[] subDistances;
    float maxLength = 0f;
    float totalDist = 0f; // weight


    boolean isLast = true;

    TreeNode(String t) {

	mpTree = new String();
	minDiff = 100f;

	ProAlign.log("TreeNode");

	tree = t;
	TreeReader tr = new TreeReader();

	subTrees = tr.divideTree(tree);
	subDistances = tr.distance;
	subDistances[0] = Math.abs(subDistances[0]);
	subDistances[1] = Math.abs(subDistances[1]);

//	System.out.println(subTrees[0]+" "+subTrees[1]);

	float tot = subDistances[0]+subDistances[1];
	revTrees = new String[2];
	revTrees[0] = subTrees[1]+":"+tot;
	revTrees[1] = subTrees[0]+":"+tot;

	child0 = new TreeNode(tr,subTrees[0],TreeNode.this,0);
	child1 = new TreeNode(tr,subTrees[1],TreeNode.this,1);

	totalDist = subDistances[0]+subDistances[1]+
	    child0.totalDist+child1.totalDist;

	float currPair = subDistances[0]+child0.maxLength+
	    subDistances[1]+child1.maxLength;
	if(currPair > TreeNode.maxSpan) {
	    TreeNode.maxSpan = currPair;
//	    System.out.println("ms0 "+child0.tree+":"+subDistances[0]+" | "+
//			       child1.tree+":"+subDistances[1]+" "+currPair);
	}
    }

    TreeNode(TreeReader tr,String t,TreeNode p,int branch) {

	tree = t;
	parent = p;

	if(tree.indexOf(",")>0) {

	    isLast = false;
	    subTrees = tr.divideTree(tree);
	    subDistances = tr.distance;
	    subDistances[0] = Math.abs(subDistances[0]);
	    subDistances[1] = Math.abs(subDistances[1]);
	    
	    revTrees = new String[2];
	    revTrees[0] = "("+parent.revTrees[branch]+","+
		subTrees[1]+":"+subDistances[1]+"):"+subDistances[0];
	    revTrees[1] = "("+parent.revTrees[branch]+","+
		subTrees[0]+":"+subDistances[0]+"):"+subDistances[1];

	    child0 = new TreeNode(tr,subTrees[0],TreeNode.this,0);
	    child1 = new TreeNode(tr,subTrees[1],TreeNode.this,1);

	    totalDist = subDistances[0]+subDistances[1]+
		child0.totalDist+child1.totalDist;

	    float currPair = subDistances[0]+child0.maxLength+
		subDistances[1]+child1.maxLength;
	    if(currPair > TreeNode.maxSpan) {
		TreeNode.maxSpan = currPair;
//		System.out.println("ms1 "+child0.tree+":"+subDistances[0]+" | "+
//				   child1.tree+":"+subDistances[1]+" "+currPair);
	    }
	    if(subDistances[0]+child0.maxLength > subDistances[1]+child1.maxLength) {
		maxLength = subDistances[0]+child0.maxLength;
	    } else {
		maxLength = subDistances[1]+child1.maxLength;
	    }
//	    System.out.println("ml "+maxLength);
	}
    }

    String findMiddlePoint() {

	TreeNode.halfLength = TreeNode.maxSpan/2f;	

	if(TreeNode.halfLength > child0.maxLength && 
	   TreeNode.halfLength < child0.maxLength+subDistances[0]+subDistances[1]) {

	    float b0 = TreeNode.halfLength-child0.maxLength;
	    float b1 = subDistances[0]+subDistances[1]-b0;

	    TreeNode.mpTree = "("+child0.tree+": "+b0+","+child1.tree+":"+b1+");";		    
	}

	child0.findMiddle(1);
	child1.findMiddle(0);
	ProAlign.log("mprooted: "+TreeNode.mpTree);
	return TreeNode.mpTree;
    }

    void findMiddle(int branch) {
	if(!isLast) {

	    if(branch==0) {

		if(TreeNode.halfLength > child0.maxLength && 
		   TreeNode.halfLength < child0.maxLength+subDistances[0]) {

		    float b0 = TreeNode.halfLength-child0.maxLength;
		    float b1 = subDistances[0]-b0;
			
		    TreeNode.mpTree = "("+child0.tree+": "+b0+",("+parent.revTrees[1]+","+
			subTrees[1]+":"+subDistances[1]+"):"+b1+");";		    
			
//		    System.out.println("nt "+branch+": "+child0.tree+" "+child0.maxLength+" "+
//		     child1.tree+" "+child1.maxLength+" "+TreeNode.halfLength+" "+TreeNode.minDiff);
		}

		if(TreeNode.halfLength > child1.maxLength && 
		   TreeNode.halfLength < child1.maxLength+subDistances[1]) {

		    float b0 = TreeNode.halfLength-child1.maxLength;
		    float b1 = subDistances[1]-b0;

		    TreeNode.mpTree = "("+child1.tree+": "+b0+",("+parent.revTrees[1]+","+
			subTrees[0]+":"+subDistances[0]+"):"+b1+");";

//		    System.out.println("nt "+branch+": "+child0.tree+" "+child0.maxLength+" "+
//		      child1.tree+" "+child1.maxLength+" "+TreeNode.halfLength+" "+TreeNode.minDiff);

		}
		child0.findMiddle(1);
		child1.findMiddle(0);

	    } else {

		if(TreeNode.halfLength > child0.maxLength && 
		   TreeNode.halfLength < child0.maxLength+subDistances[0]) {

		    float b0 = TreeNode.halfLength-child0.maxLength;
		    float b1 = subDistances[0]-b0;

		    TreeNode.mpTree = "("+child0.tree+": "+b0+",("+parent.revTrees[0]+
			","+subTrees[1]+":"+subDistances[1]+"):"+b1+");";

//		    System.out.println("nt "+branch+": "+child0.tree+" "+child0.maxLength+" "+
//		      child1.tree+" "+child1.maxLength+" "+TreeNode.halfLength+" "+TreeNode.minDiff);

		}


		if(TreeNode.halfLength > child1.maxLength && 
		   TreeNode.halfLength < child1.maxLength+subDistances[1]) {

		    float b0 = TreeNode.halfLength-child1.maxLength;
		    float b1 = subDistances[1]-b0;

		    TreeNode.mpTree = "("+child1.tree+": "+b0+",("+parent.revTrees[0]+
			","+subTrees[0]+":"+subDistances[0]+"):"+b1+");";

//		    System.out.println("nt "+branch+": "+child0.tree+" "+child0.maxLength+" "+
//		      child1.tree+" "+child1.maxLength+" "+TreeNode.halfLength+" "+TreeNode.minDiff);

		}
		child0.findMiddle(1);
		child1.findMiddle(0);
	    }
	}
    }


/*
This was the first attempt for the tree rooting. Not used anymore.
*/

    String findMiddleWeightPoint() {
	TreeNode.halfLength = totalDist/2f;
	child0.findMiddle(1);
	child1.findMiddle(0);
	ProAlign.log("mprooted: "+TreeNode.mpTree);
	return TreeNode.mpTree;
    }

    void findMiddleWeight(int branch) {
	if(!isLast) {

	    if(branch==0) {
		if(Math.abs(TreeNode.halfLength-child0.totalDist)<TreeNode.minDiff) {
		    
		    TreeNode.minDiff = Math.abs(TreeNode.halfLength-child0.totalDist);
		    float b0,b1;
		    if(TreeNode.halfLength>child0.totalDist) {
			b0 = TreeNode.halfLength-child0.totalDist;
			b1 = subDistances[0]-b0;
		    } else if(subDistances[0]>0.001f) {
			b0 = 0.001f;
			b1 = subDistances[0]-b0;
		    } else {
			b0 = 0f;
			b1 = subDistances[0];
		    }
		    TreeNode.mpTree = "("+child0.tree+": "+b0+",("+parent.revTrees[1]+","+
			subTrees[1]+":"+subDistances[1]+"):"+b1+");";		    
		}

		if(Math.abs(TreeNode.halfLength-child1.totalDist)<TreeNode.minDiff) {
		    
		    TreeNode.minDiff = Math.abs(TreeNode.halfLength-child1.totalDist);
		    float b0,b1;
		    if(TreeNode.halfLength>child1.totalDist) {
			b0 = TreeNode.halfLength-child1.totalDist;
			b1 = subDistances[1]-b0;
		    } else if(subDistances[1]>0.001f) {
			b0 = 0.001f;
			b1 = subDistances[1]-b0;
		    } else {
			b0 = 0f;
			b1 = subDistances[1];
		    }
		    TreeNode.mpTree = "("+child1.tree+": "+b0+",("+parent.revTrees[1]+","+
			subTrees[0]+":"+subDistances[0]+"):"+b1+");";
		}
		child0.findMiddle(1);
		child1.findMiddle(0);

	    } else {
		if(Math.abs(TreeNode.halfLength-child0.totalDist)<TreeNode.minDiff) {

		    TreeNode.minDiff = Math.abs(TreeNode.halfLength-child0.totalDist);
		    float b0,b1;
		    if(TreeNode.halfLength>child0.totalDist) {
			b0 = TreeNode.halfLength-child0.totalDist;
			b1 = subDistances[0]-b0;
		    } else if(subDistances[0]>0.001f) {
			b0 = 0.001f;
			b1 = subDistances[0]-b0;
		    } else {
			b0 = 0f;
			b1 = subDistances[0];	
		    }
		    TreeNode.mpTree = "("+child0.tree+": "+b0+",("+parent.revTrees[0]+
			","+subTrees[1]+":"+subDistances[1]+"):"+b1+");";
		}

		if(Math.abs(TreeNode.halfLength-child1.totalDist)<TreeNode.minDiff) {
		    
		    TreeNode.minDiff = Math.abs(TreeNode.halfLength-child1.totalDist);
		    float b0,b1;
		    if(TreeNode.halfLength>child1.totalDist) {
			b0 = TreeNode.halfLength-child1.totalDist;
			b1 = subDistances[1]-b0;
		    } else if(subDistances[1]>0.001f) {
			b0 = 0.001f;
			b1 = subDistances[1]-b0;
		    } else {
			b0 = 0f;
			b1 = subDistances[1];
		    }
		    TreeNode.mpTree = "("+child1.tree+": "+b0+",("+parent.revTrees[0]+
			","+subTrees[0]+":"+subDistances[0]+"):"+b1+")";
		}
		child0.findMiddle(1);
		child1.findMiddle(0);
	    }
	}
    }

    void printNames() {
	if(isLast) {
	    System.out.println(tree);
	} else {
	    child0.printNames();
	    child1.printNames();
	    System.out.println("node: "+totalDist);
	}
    }
    /*   
    public static void main(String[] args) {

	TreeReader2 tr = new TreeReader2();
	String tree = tr.readFile(args[0]);
	TreeNode root = new TreeNode(tree);
	TreeNode.halfLength = root.totalDist/2f;
	System.out.println(root.findMiddlePoint());
    }
    //*/
}