source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/FengDoolittleNode.java @ 1573

Last change on this file since 1573 was 1573, checked in by rkrimmel, 10 years ago

Now really adding PAL Library

File size: 8.5 KB
Line 
1// SimpleNode.java
2//
3// (c) 1999-2001 PAL Development Core Team
4//
5// This package may be distributed under the
6// terms of the Lesser GNU General Public License (LGPL)
7
8
9package de.ugoe.cs.autoquest.tasktrees.alignment.pal.tree;
10
11
12import java.util.ArrayList;
13
14import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence;
15import de.ugoe.cs.autoquest.tasktrees.alignment.pal.misc.Identifier;
16import de.ugoe.cs.autoquest.tasktrees.alignment.pal.misc.LabelMapping;
17
18
19/**
20 * data structure for a node (includes branch) in a binary/non-binary
21 * rooted/unrooted tree
22 *
23 * @version $Id: SimpleNode.java,v 1.20 2002/01/14 04:16:53 matt Exp $
24 *
25 * @author Korbinian Strimmer
26 * @author Alexei Drummond
27 */
28public class FengDoolittleNode  implements Node {
29
30        /** parent node */
31        private Node parent;
32
33        /** number of node as displayed */
34        private int number;
35
36        /** sequences associated with node */
37        private ArrayList<NumberSequence> sequences;
38
39        /** length of branch to parent node */
40        private double length;
41
42        /** standard error of length of branch to parent node */
43        private double lengthSE;
44
45        /** height of this node */
46        private double height;
47
48        /** identifier of node/associated branch */
49        private Identifier identifier;
50
51        private Node[] child;
52
53        // the following constructors should eventually become
54        // "friendly" to prevent anyone calling them directly.
55        // Instead, the NodeFactory should be used!
56
57        /** constructor default node */
58        public FengDoolittleNode()
59        {
60                parent = null;
61                child = null;
62                length = 0.0;
63                lengthSE = 0.0;
64                height = 0.0;
65                identifier = Identifier.ANONYMOUS;
66
67                number = 0;
68                sequences = new ArrayList<NumberSequence>();
69        }
70
71        public FengDoolittleNode(String name, double branchLength) {
72                this();
73                identifier = new Identifier(name);
74                length = branchLength;
75        }
76
77
78        public void reset()
79        {
80                parent = null;
81                child = null;
82                length = 0.0;
83                lengthSE = 0.0;
84                height = 0.0;
85                identifier = Identifier.ANONYMOUS;
86
87                number = 0;
88                sequences = null;
89        }
90
91        public FengDoolittleNode(Node n, boolean keepIds) {
92                init(n, keepIds);
93                for (int i = 0; i < n.getChildCount(); i++) {
94                        addChild(new FengDoolittleNode(n.getChild(i), keepIds));
95                }
96        }
97
98        public FengDoolittleNode(Node n, LabelMapping lm) {
99                init(n, true, lm);
100                for (int i = 0; i < n.getChildCount(); i++) {
101                        addChild(new FengDoolittleNode(n.getChild(i), lm));
102                }
103        }
104
105       
106        /** constructor used to clone a node and all children */
107        public FengDoolittleNode(Node n)
108        {
109                this(n, true);
110        }
111
112       
113
114        protected void init(Node n) {
115                init(n, true);
116        }
117        /**
118         * Initialized node instance variables based on given Node.
119         * children are ignored.
120         */
121        protected void init(Node n, boolean keepId) {
122                init(n,keepId,null);
123        }
124        /**
125         * Initialized node instance variables based on given Node.
126         * children are ignored.
127         * @param lm - may be null
128         */
129        protected void init(Node n, boolean keepId, LabelMapping lm) {
130                parent = null;
131                length = n.getBranchLength();
132                lengthSE = n.getBranchLengthSE();
133                height = n.getNodeHeight();
134                if (keepId) {
135                        if(lm!=null) {
136                                identifier = lm.getLabelIdentifier(n.getIdentifier());
137                        } else {
138                                identifier = n.getIdentifier();
139                        }
140                } else { identifier = Identifier.ANONYMOUS; }
141
142                number = n.getNumber();
143                sequences = n.getSequences();
144                child = null;
145        }
146
147        /**
148         * Returns the parent node of this node.
149         */
150        public final Node getParent() {
151                return parent;
152        }
153
154        /** Set the parent node of this node. */
155        public void setParent(Node node)
156        {
157                parent = node;
158        }
159
160        /**
161         * removes parent.
162         */
163        public final void removeParent() {
164                parent = null;
165        }
166
167        /**
168         * Returns the sequence at this node, in the form of a String.
169         */
170        public String getSequenceString(int index) {
171                return sequences.get(index).getSequence().toString();
172        }
173
174        /**
175         * Returns the sequence at this node, in the form of an array of bytes.
176         */
177        public NumberSequence getSequence(int index) {
178                return sequences.get(index);
179        }
180
181        /**
182         * Sets the sequence at this node, in the form of an array of bytes.
183         */
184        public void setSequence(int index,NumberSequence s) {
185                sequences.set(index, s);
186        }
187
188        /**
189         * Get the length of the branch attaching this node to its parent.
190         */
191        public final double getBranchLength() {
192                return length;
193        }
194
195        /**
196         * Set the length of the branch attaching this node to its parent.
197         */
198        public final void setBranchLength(double value) {
199                length = value;
200        }
201
202        /**
203         * Get the length SE of the branch attaching this node to its parent.
204         */
205        public final double getBranchLengthSE() {
206                return lengthSE;
207        }
208
209        /**
210         * Set the length SE of the branch attaching this node to its parent.
211         */
212        public final void setBranchLengthSE(double value) {
213                lengthSE = value;
214        }
215
216
217        /**
218         * Get the height of this node relative to the most recent node.
219         */
220        public final double getNodeHeight() {
221                return height;
222        }
223
224        /**
225         * Set the height of this node relative to the most recent node.
226         */
227        public final void setNodeHeight(double value) {
228                height = Math.abs(value);
229        }
230
231        /**
232         * Returns the identifier for this node.
233         */
234        public final Identifier getIdentifier() {
235                return identifier;
236        }
237
238        /**
239         * Set identifier for this node.
240         */
241        public final Identifier setIdentifier(Identifier id) {
242                identifier = id;
243                return identifier;
244        }
245
246        public void setNumber(int n) {
247                number = n;
248        }
249
250        public int getNumber() {
251                return number;
252        }
253
254        /**
255         * get child node
256         *
257         * @param n number of child
258         *
259         * @return child node
260         */
261        public Node getChild(int n)
262        {
263                return child[n];
264        }
265
266        /**
267         * set child node
268         *
269         * @param n number
270         * @node node new child node
271         */
272        public void setChild(int n, Node node)
273        {
274                child[n] = node;
275                child[n].setParent(this);
276        }
277
278        /**
279         * check whether this node is an internal node
280         *
281         * @return result (true or false)
282         */
283        public boolean hasChildren()
284        {
285                return !isLeaf();
286        }
287
288        /**
289         * check whether this node is an external node
290         *
291         * @return result (true or false)
292         */
293        public boolean isLeaf() {
294                return (getChildCount() == 0);
295        }
296
297        /**
298         * check whether this node is a root node
299         *
300         * @return result (true or false)
301         */
302        public boolean isRoot()
303        {
304                if (parent == null)
305                {
306                        return true;
307                }
308                else
309                {
310                        return false;
311                }
312        }
313
314
315        /**
316         * add new child node
317         *
318         * @param n new child node
319         */
320        public void addChild(Node n)
321        {
322                insertChild(n, getChildCount());
323        }
324
325        /**
326         * add new child node (insertion at a specific position)
327         *
328         * @param n new child node
329         + @param pos position
330         */
331        public void insertChild(Node n, int pos)
332        {
333                int numChildren = getChildCount();
334
335                Node[] newChild = new Node[numChildren + 1];
336
337                for (int i = 0; i < pos; i++)
338                {
339                        newChild[i] = child[i];
340                }
341                newChild[pos] = n;
342                for (int i = pos; i < numChildren; i++)
343                {
344                        newChild[i+1] = child[i];
345                }
346
347                child = newChild;
348
349                n.setParent(this);
350        }
351
352
353        /**
354         * remove child
355         *
356         * @param n number of child to be removed
357         */
358        public Node removeChild(int n)
359        {
360                int numChildren = getChildCount();
361
362                if (n >= numChildren)
363                {
364                        throw new IllegalArgumentException("Nonexistent child");
365                }
366                Node[] newChild = new Node[numChildren-1];
367
368                for (int i = 0; i < n; i++)
369                {
370                        newChild[i] = child[i];
371                }
372
373                for (int i = n; i < numChildren-1; i++)
374                {
375                        newChild[i] = child[i+1];
376                }
377
378                Node removed = child[n];
379
380                //remove parent link from removed child!
381                removed.setParent(null);
382
383                child = newChild;
384
385                return removed;
386        }
387
388
389
390        /**
391         * join two children, introducing a new node/branch in the tree
392         * that replaces the first child
393         *
394         * @param n1 number of first child
395         * @param n2 number of second child
396         */
397        public void joinChildren( int n1, int n2) {
398
399                if (n1 == n2) {
400                        throw new IllegalArgumentException("CHILDREN MUST BE DIFFERENT");
401                }
402
403                int c1, c2;
404                if (n2 < n1)
405                {
406                        c1 = n2;
407                        c2 = n1;
408                }
409                else
410                {
411                        c1 = n1;
412                        c2 = n2;
413                }
414
415                Node newNode = NodeFactory.createNode();
416
417                Node child1 = this.getChild(c1);
418                Node child2 = this.getChild(c2);
419
420                setChild(c1, newNode);
421                newNode.setParent(this);
422                this.removeChild(c2); // now parent of child2 = null
423
424                newNode.addChild(child1);
425                newNode.addChild(child2);
426               
427                if(newNode instanceof FengDoolittleNode) {
428                        ((FengDoolittleNode) newNode).alignSequences();
429                }
430               
431        }
432       
433
434        /**
435         * Returns the number of children this node has.
436         */
437        public final int getChildCount() {
438                if (child == null) return 0;
439                return child.length;
440        }
441
442
443        public ArrayList<NumberSequence> getSequences() {
444                return sequences;
445        }
446
447
448        private void alignSequences() {
449                if(this.getChildCount()<3) {
450                       
451                        Node node1 = getChild(0);
452                        Node node2 = getChild(1);
453                       
454                        int seqCount1 = node1.getSequences().size();
455                        int seqCount2 = node2.getSequences().size();
456                       
457                       
458                       
459                }
460                else {
461                       
462                }
463               
464        }
465
466       
467}
Note: See TracBrowser for help on using the repository browser.