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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
//******************************************************************************
//
// File: DnaSequenceTree.java
// Package: edu.rit.compbio.phyl
// Unit: Class edu.rit.compbio.phyl.DnaSequenceTree
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.compbio.phyl;
import java.util.ArrayList;
import java.util.Collections;
/**
* Class DnaSequenceTree encapsulates a rooted bifurcating tree of DNA
* sequences. Each node in the tree is designated by an index from 0 to
* <I>N</I>−1, where <I>N</I> is the tree's <I>length.</I> The tree's
* <I>capacity</I> <I>C</I> is the maximum number of nodes (specified when the
* tree was constructed). For a tree to hold <I>M</I> tip nodes, the tree's
* capacity must be <I>C</I> ≥ 2<I>M</I> − 1.
*
* @author Alan Kaminsky
* @version 15-Jul-2008
*/
public class DnaSequenceTree
{
// Hidden constants.
// Amount of extra padding in arrays.
private static final int PAD = 32;
// Hidden helper classes.
// Information about one node in the tree.
private static class Node
implements Comparable<Node>
{
// Parent node index, or -1 for the root node.
public int parent;
// First child node index, or -1 for a tip node.
public int child1;
// Second child node index, or -1 for a tip node.
public int child2;
// DNA sequence associated with this node, or null if none.
public DnaSequence seq;
// Branch length between this node and its parent, or null if none.
public Double brlen;
// Extra padding to avert cache interference.
private long p0, p1, p2, p3, p4, p5, p6, p7;
private long p8, p9, pa, pb, pc, pd, pe, pf;
// Construct a new, empty node.
public Node()
{
clear();
}
// Clear this node.
public void clear()
{
this.parent = -1;
this.child1 = -1;
this.child2 = -1;
this.seq = null;
this.brlen = null;
}
// Set this node to be a copy of the given node.
public void copy
(Node node)
{
this.parent = node.parent;
this.child1 = node.child1;
this.child2 = node.child2;
this.seq = node.seq;
this.brlen = node.brlen;
}
// Compare this node with the given node. The ordering is descending
// order of branch length. A node with no branch length uses a default
// branch length of 0.
public int compareTo
(Node node)
{
double brlen1 = this.brlen == null ? 0.0 : this.brlen;
double brlen2 = node.brlen == null ? 0.0 : node.brlen;
if (brlen1 > brlen2) return -1;
else if (brlen1 < brlen2) return +1;
else return 0;
}
}
// Hidden data members.
// Array of nodes.
private Node[] myNode;
// Tree capacity (maximum number of nodes).
private int myCapacity;
// Tree length (actual number of nodes).
private int myLength;
// Index of root node, or -1 if tree is empty.
private int myRoot;
// Extra padding to avert cache interference.
private long p0, p1, p2, p3, p4, p5, p6, p7;
private long p8, p9, pa, pb, pc, pd, pe, pf;
// Exported constructors.
/**
* Construct a new DNA sequence tree with the given capacity. The new tree
* is initially empty.
*
* @param C Capacity.
*
* @exception IllegalArgumentException
* (unchecked exception) Thrown if <TT>C</TT> < 0.
*/
public DnaSequenceTree
(int C)
{
if (C < 0)
{
throw new IllegalArgumentException
("DnaSequenceTree(): C (= "+C+") < 0, illegal");
}
myNode = new Node [C + PAD];
for (int i = 0; i < C; ++ i)
{
myNode[i] = new Node();
}
myCapacity = C;
myLength = 0;
myRoot = -1;
}
// Exported operations.
/**
* Returns the capacity of this tree.
*
* @return Capacity <I>C</I> (maximum number of nodes).
*/
public int capacity()
{
return myCapacity;
}
/**
* Returns the length of this tree.
*
* @return Length <I>N</I> (number of nodes).
*/
public int length()
{
return myLength;
}
/**
* Returns the root of this tree.
*
* @return Index of the root node, or −1 if this tree is empty.
*/
public int root()
{
return myRoot;
}
/**
* Returns the parent of the given node in this tree.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
*
* @return Index of the parent of node <TT>i</TT>, or −1 if node
* <TT>i</TT> is the root node.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public int parent
(int i)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.parent(): Index (= "+i+") out of bounds");
}
return myNode[i].parent;
}
/**
* Returns the first child of the given node in this tree.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
*
* @return Index of the first child of node <TT>i</TT>, or −1 if node
* <TT>i</TT> is a tip node.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public int child1
(int i)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.child1(): Index (= "+i+") out of bounds");
}
return myNode[i].child1;
}
/**
* Returns the second child of the given node in this tree.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
*
* @return Index of the second child of node <TT>i</TT>, or −1 if
* node <TT>i</TT> is a tip node.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public int child2
(int i)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.child2(): Index (= "+i+") out of bounds");
}
return myNode[i].child2;
}
/**
* Returns the DNA sequence associated with the given node in this tree.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
*
* @return DNA sequence associated with node <TT>i</TT>, or null if no DNA
* sequence is associated.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public DnaSequence seq
(int i)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.seq(): Index (= "+i+") out of bounds");
}
return myNode[i].seq;
}
/**
* Set the DNA sequence associated with the given node in this tree.
* <P>
* <I>Note:</I> The tree contains a reference to (not a copy of)
* <TT>seq</TT>.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
* @param seq DNA sequence associated with node <TT>i</TT>, or null if no
* DNA sequence is associated.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public void seq
(int i,
DnaSequence seq)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.seq(): Index (= "+i+") out of bounds");
}
myNode[i].seq = seq;
}
/**
* Returns the branch length associated with the given node in this tree.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
*
* @return Branch length between node <TT>i</TT> and its parent, or null if
* no branch length is associated.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public Double branchLength
(int i)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.branchLength(): Index (= "+i+
") out of bounds");
}
return myNode[i].brlen;
}
/**
* Set the branch length associated with the given node in this tree.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
* @param brlen Branch length between node <TT>i</TT> and its parent, or
* null if no branch length is associated.
*
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public void branchLength
(int i,
Double brlen)
{
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.branchLength(): Index (= "+i+
") out of bounds");
}
myNode[i].brlen = brlen;
}
/**
* Clear this DNA sequence tree.
*/
public void clear()
{
int C = myCapacity;
for (int i = 0; i < C; ++ i)
{
myNode[i].clear();
}
myLength = 0;
myRoot = -1;
}
/**
* Set this DNA sequence tree to be a copy of the given tree. This tree's
* capacity is unchanged and must be greater than or equal to the given
* tree's length. This tree's length, DNA sequences, and branch lengths
* become the same as <TT>tree</TT>.
* <P>
* <I>Note:</I> This tree contains references to (not copies of) the DNA
* sequences in <TT>tree</TT>.
*
* @param tree DNA sequence tree.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>tree</TT> is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if this tree's capacity is less than
* <TT>tree</TT>'s length.
*/
public void copy
(DnaSequenceTree tree)
{
// Verify preconditions.
if (this.myCapacity < tree.myLength)
{
throw new IllegalArgumentException
("DnaSequenceTree.copy(): Capacity (= "+this.myCapacity+
") too small");
}
int C = this.myCapacity;
int N = tree.myLength;
// Copy <tree>'s nodes to this tree.
for (int i = 0; i < N; ++ i)
{
this.myNode[i].copy (tree.myNode[i]);
}
// Clear any unused nodes in this tree.
for (int i = N; i < C; ++ i)
{
this.myNode[i].clear();
}
// Update this tree's length and root.
this.myLength = N;
this.myRoot = tree.myRoot;
}
/**
* Set this DNA sequence tree to be the join of the two given trees. This
* tree's capacity is unchanged and must be greater than or equal to
* <I>N</I><SUB>1</SUB> + <I>N</I><SUB>2</SUB> + 1,
* where <I>N</I><SUB>1</SUB> and <I>N</I><SUB>2</SUB> are the two given
* trees' lengths. This tree's root node's first child becomes
* <TT>tree1</TT>. This tree's root node's second child becomes
* <TT>tree2</TT>. This tree's root node has no associated DNA sequence.
* <P>
* <I>Note:</I> This tree contains references to (not copies of) the DNA
* sequences in <TT>tree1</TT> and <TT>tree2</TT>.
* <P>
* <I>Note:</I> Both <TT>tree1</TT> and <TT>tree2</TT> must be different
* objects from this tree.
* <P>
* <I>Note:</I> This method may alter the index of this tree's root node.
*
* @param tree1 First DNA sequence tree.
* @param tree2 Second DNA sequence tree.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>tree1</TT> is null. Thrown if
* <TT>tree2</TT> is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if this tree's capacity is less than
* <I>N</I><SUB>1</SUB> + <I>N</I><SUB>2</SUB> + 1.
*/
public void join
(DnaSequenceTree tree1,
DnaSequenceTree tree2)
{
// Verify preconditions.
int N1 = tree1.myLength;
int N2 = tree2.myLength;
if (this.myCapacity < N1+N2+1)
{
throw new IllegalArgumentException
("DnaSequenceTree.join(): Capacity (= "+this.myCapacity+
") too small");
}
int C = this.myCapacity;
// Set up this tree's root node at index 0.
Node root = this.myNode[0];
root.parent = -1;
root.child1 = tree1.myRoot + 1;
root.child2 = tree2.myRoot + N1 + 1;
root.seq = null;
root.brlen = null;
// Copy <tree1>'s nodes to this tree. Must offset <tree1>'s node
// indexes by 1.
for (int i = 0; i < N1; ++ i)
{
Node thisnode = this.myNode[i+1];
Node treenode = tree1.myNode[i];
thisnode.parent =
treenode.parent == -1 ? 0 : treenode.parent + 1;
thisnode.child1 =
treenode.child1 == -1 ? -1 : treenode.child1 + 1;
thisnode.child2 =
treenode.child2 == -1 ? -1 : treenode.child2 + 1;
thisnode.seq = treenode.seq;
thisnode.brlen = treenode.brlen;
}
// Copy <tree2>'s nodes to this tree. Must offset <tree2>'s node
// indexes by N1 + 1.
for (int i = 0; i < N2; ++ i)
{
Node thisnode = this.myNode[i+N1+1];
Node treenode = tree2.myNode[i];
thisnode.parent =
treenode.parent == -1 ? 0 : treenode.parent + N1 + 1;
thisnode.child1 =
treenode.child1 == -1 ? -1 : treenode.child1 + N1 + 1;
thisnode.child2 =
treenode.child2 == -1 ? -1 : treenode.child2 + N1 + 1;
thisnode.seq = treenode.seq;
thisnode.brlen = treenode.brlen;
}
// Clear any unused nodes in this tree.
for (int i = N1+N2+1; i < C; ++ i)
{
this.myNode[i].clear();
}
// Update this tree's length and root.
this.myLength = N1+N2+1;
this.myRoot = 0;
}
/**
* Add the given DNA sequence to this DNA sequence tree. This tree's
* capacity is unchanged and must be greater than or equal to this tree's
* length + 2. A new node is added to this tree. No DNA sequence is
* associated with the new node. The new node's parent is the parent of the
* node at index <TT>i</TT>; if the node at index <TT>i</TT> was the root
* node, the new node becomes the root node. The new node's first child is
* the node at index <TT>i</TT>. The new node's second child is a new tip
* node associated with the given DNA sequence.
* <P>
* Alternatively, if this tree is empty, the <TT>add()</TT> method sets this
* tree to have one root node associated with the given DNA sequence. In
* this case <TT>i</TT> is ignored.
* <P>
* <I>Note:</I> This tree contains a reference to (not a copy of)
* <TT>seq</TT>.
* <P>
* <I>Note:</I> This method may alter the index of this tree's root node.
*
* @param i Node index, 0 ≤ <TT>i</TT> ≤ <I>N</I>−1.
* @param seq DNA sequence associated with new tip node, or null if no DNA
* sequence is associated.
*
* @return Index of new tip node.
*
* @exception IllegalArgumentException
* (unchecked exception) Thrown if this tree's capacity is less than
* this tree's length + 2.
* @exception IndexOutOfBoundsException
* (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
*/
public int add
(int i,
DnaSequence seq)
{
// Case 1: This tree is empty.
if (myLength == 0)
{
// Verify preconditions.
if (myCapacity < 1)
{
throw new IllegalArgumentException
("DnaSequenceTree.add(): Capacity (= "+myCapacity+
") < 1, illegal");
}
// Set up root node with <seq>.
Node root = this.myNode[0];
root.parent = -1;
root.child1 = -1;
root.child2 = -1;
root.seq = seq;
root.brlen = null;
// Update this tree's length and root.
myLength = 1;
myRoot = 0;
return 0;
}
// Case 2: This tree is not empty.
else
{
// Verify preconditions.
if (myCapacity < myLength+2)
{
throw new IllegalArgumentException
("DnaSequenceTree.add(): Capacity (= "+myCapacity+
") too small");
}
if (0 > i || i >= myLength)
{
throw new IndexOutOfBoundsException
("DnaSequenceTree.add(): Index (= "+i+") out of bounds");
}
// Set up new interior node.
int parent_i = myNode[i].parent;
Node newnode = myNode[myLength];
newnode.parent = parent_i;
newnode.child1 = i;
newnode.child2 = myLength+1;
newnode.seq = null;
newnode.brlen = null;
// Splice new interior node between node <i> and node <i>'s parent.
// Update this tree's root if necessary.
if (parent_i == -1)
{
myRoot = myLength;
}
else if (myNode[parent_i].child1 == i)
{
myNode[parent_i].child1 = myLength;
}
else
{
myNode[parent_i].child2 = myLength;
}
myNode[i].parent = myLength;
// Set up new tip node with <seq>.
Node newtip = myNode[myLength+1];
newtip.parent = myLength;
newtip.child1 = -1;
newtip.child2 = -1;
newtip.seq = seq;
newtip.brlen = null;
// Update this tree's length.
myLength += 2;
return myLength - 1;
}
}
/**
* Create a {@linkplain DnaSequenceList} consisting of the DNA sequences
* associated with the tip nodes in this tree. The DNA sequences appear in
* descending order of the tip nodes' branch lengths. A tip node with no
* associated branch length uses a default branch length of 0.
* <P>
* <I>Note:</I> The returned list contains references to (not copies of)
* the DNA sequences in this tree.
*
* @return DNA sequence list.
*/
public DnaSequenceList toList()
{
// Make a list of just the tip nodes.
ArrayList<Node> nodelist = new ArrayList<Node>();
int N = myLength;
for (int i = 0; i < N; ++ i)
{
Node node = myNode[i];
if (node.child1 == -1) nodelist.add (node);
}
// Sort the node list.
Collections.sort (nodelist);
int M = nodelist.size();
// Set up DNA sequence list.
DnaSequenceList seqlist = new DnaSequenceList();
seqlist.mySequence = new DnaSequence [M];
int i = 0;
for (Node node : nodelist)
{
seqlist.mySequence[i++] = node.seq;
}
return seqlist;
}
/**
* Returns a string version of this DNA sequence tree. The returned string
* is in Newick Standard format, including the branch lengths if any and the
* tip nodes' DNA sequence names. For further information about Newick
* Standard format, see:
* <UL>
* <LI>
* <A HREF="http://evolution.gs.washington.edu/phylip/newicktree.html" TARGET="_top">http://evolution.gs.washington.edu/phylip/newicktree.html</A>
* </UL>
*
* @return String version of this tree.
*/
public String toString()
{
StringBuilder buf = new StringBuilder();
if (myLength == 0)
{
buf.append ('(');
buf.append (')');
}
else if (myLength == 1)
{
buf.append ('(');
toString (buf, 0);
buf.append (')');
}
else
{
toString (buf, myRoot);
}
buf.append (';');
return buf.toString();
}
private void toString
(StringBuilder buf,
int index)
{
Node node = myNode[index];
if (node.child1 != -1)
{
buf.append ('(');
toString (buf, node.child1);
buf.append (',');
toString (buf, node.child2);
buf.append (')');
}
buf.append (nodeName (node));
if (node.brlen != null)
{
buf.append (':');
buf.append (node.brlen);
}
}
private String nodeName
(Node node)
{
return
node.seq == null || node.seq.myName == null ?
"" :
node.seq.myName.replaceAll ("\\s+", "_");
}
}
|