regex search
[jalview.git] / forester / java / src / org / forester / phylogeny / data / BranchData.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27
28 package org.forester.phylogeny.data;
29
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 public class BranchData implements PhylogenyData {
36
37     private BranchColor      _branch_color;
38     private List<Confidence> _confidences;
39     private BranchWidth      _branch_width;
40
41     public BranchData() {
42         // Doing nothing. 
43     }
44
45     public void addConfidence( final Confidence confidence ) {
46         getConfidences().add( confidence );
47     }
48
49     @Override
50     public StringBuffer asSimpleText() {
51         throw new UnsupportedOperationException();
52     }
53
54     @Override
55     public StringBuffer asText() {
56         throw new UnsupportedOperationException();
57     }
58
59     @Override
60     public PhylogenyData copy() {
61         final BranchData new_bd = new BranchData();
62         if ( isHasBranchColor() ) {
63             new_bd.setBranchColor( ( BranchColor ) getBranchColor().copy() );
64         }
65         if ( isHasBranchWidth() ) {
66             new_bd.setBranchWidth( ( BranchWidth ) getBranchWidth().copy() );
67         }
68         if ( isHasConfidences() ) {
69             for( final Confidence confidence : getConfidences() ) {
70                 new_bd.addConfidence( ( Confidence ) confidence.copy() );
71             }
72         }
73         return new_bd;
74     }
75
76     public BranchColor getBranchColor() {
77         return _branch_color;
78     }
79
80     public BranchWidth getBranchWidth() {
81         return _branch_width;
82     }
83
84     public Confidence getConfidence( final int index ) {
85         return getConfidences().get( index );
86     }
87
88     public List<Confidence> getConfidences() {
89         if ( _confidences == null ) {
90             _confidences = new ArrayList<Confidence>();
91         }
92         return _confidences;
93     }
94
95     public int getNumberOfConfidences() {
96         return getConfidences().size();
97     }
98
99     @Override
100     public boolean isEqual( final PhylogenyData data ) {
101         throw new UnsupportedOperationException();
102     }
103
104     public boolean isHasBranchColor() {
105         return getBranchColor() != null;
106     }
107
108     public boolean isHasBranchWidth() {
109         return getBranchWidth() != null;
110     }
111
112     public boolean isHasConfidences() {
113         return getNumberOfConfidences() > 0;
114     }
115
116     public void setBranchColor( final BranchColor branch_color ) {
117         _branch_color = branch_color;
118     }
119
120     public void setBranchWidth( final BranchWidth branch_width ) {
121         _branch_width = branch_width;
122     }
123
124     @Override
125     public StringBuffer toNHX() {
126         final StringBuffer sb = new StringBuffer();
127         if ( isHasConfidences() && ( getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
128             sb.append( ":" );
129             sb.append( getConfidence( 0 ).toNHX() );
130         }
131         return sb;
132     }
133
134     @Override
135     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
136         if ( isHasConfidences() ) {
137             for( final Confidence confidence : getConfidences() ) {
138                 confidence.toPhyloXML( writer, level, indentation );
139             }
140         }
141         if ( isHasBranchWidth() ) {
142             getBranchWidth().toPhyloXML( writer, level, indentation );
143         }
144         if ( isHasBranchColor() ) {
145             getBranchColor().toPhyloXML( writer, level, indentation );
146         }
147     }
148 }