added AGRESSIVE tax extraction ^^
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Distribution.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 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.util.ForesterUtil;
38
39 public class Distribution implements PhylogenyData {
40
41     private final String        _desc;
42     private final List<Point>   _points;
43     private final List<Polygon> _polygons;
44
45     public Distribution( final String desc ) {
46         _desc = desc;
47         _points = null;
48         _polygons = null;
49     }
50
51     public Distribution( final String desc, final List<Point> points ) {
52         _desc = null;
53         _points = points;
54         _polygons = null;
55     }
56
57     public Distribution( final String desc, final List<Point> points, final List<Polygon> polygons ) {
58         _desc = desc;
59         _points = points;
60         _polygons = polygons;
61     }
62
63     public boolean isEmpty() {
64         if ( ForesterUtil.isEmpty( _desc ) && ( ( getPoints() != null ) && ( getPoints().size() == 1 ) )
65                 && ForesterUtil.isEmpty( _polygons ) ) {
66             if ( Point.isSeemsEmpty( getPoints().get( 0 ) ) ) {
67                 return true;
68             }
69         }
70         return ForesterUtil.isEmpty( _desc ) && ForesterUtil.isEmpty( _points ) && ForesterUtil.isEmpty( _polygons );
71     }
72
73     @Override
74     public StringBuffer asSimpleText() {
75         final StringBuffer sb = new StringBuffer();
76         if ( isEmpty() ) {
77             return sb;
78         }
79         sb.append( "Distribution: " );
80         if ( !ForesterUtil.isEmpty( getDesc() ) ) {
81             sb.append( ForesterUtil.LINE_SEPARATOR );
82             sb.append( " Description: " );
83             sb.append( getDesc() );
84         }
85         int i = 0;
86         if ( getPoints() != null ) {
87             for( final Point point : getPoints() ) {
88                 if ( ( point != null ) && !Point.isSeemsEmpty( point ) ) {
89                     sb.append( ForesterUtil.LINE_SEPARATOR );
90                     sb.append( " Point " + i + ": " );
91                     sb.append( point.asSimpleText() );
92                     i++;
93                 }
94             }
95         }
96         i = 0;
97         if ( getPolygons() != null ) {
98             for( final Polygon polygon : getPolygons() ) {
99                 if ( polygon != null ) {
100                     sb.append( ForesterUtil.LINE_SEPARATOR );
101                     sb.append( " Polygon " + i + ":" );
102                     sb.append( ForesterUtil.LINE_SEPARATOR );
103                     sb.append( polygon.asSimpleText() );
104                     i++;
105                 }
106             }
107         }
108         return sb;
109     }
110
111     @Override
112     public StringBuffer asText() {
113         return asSimpleText();
114     }
115
116     @Override
117     public PhylogenyData copy() {
118         List<Point> new_points = null;
119         List<Polygon> new_polygons = null;
120         if ( getPoints() != null ) {
121             new_points = new ArrayList<Point>();
122             for( final Point point : getPoints() ) {
123                 new_points.add( ( Point ) point.copy() );
124             }
125         }
126         if ( getPolygons() != null ) {
127             new_polygons = new ArrayList<Polygon>();
128             for( final Polygon polygon : getPolygons() ) {
129                 new_polygons.add( ( Polygon ) polygon.copy() );
130             }
131         }
132         return new Distribution( getDesc(), new_points, new_polygons );
133     }
134
135     public String getDesc() {
136         return _desc;
137     }
138
139     public List<Point> getPoints() {
140         return _points;
141     }
142
143     public List<Polygon> getPolygons() {
144         return _polygons;
145     }
146
147     @Override
148     public boolean isEqual( final PhylogenyData data ) {
149         throw new UnsupportedOperationException();
150     }
151
152     @Override
153     public StringBuffer toNHX() {
154         throw new UnsupportedOperationException();
155     }
156
157     @Override
158     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
159         if ( isEmpty() ) {
160             return;
161         }
162         writer.write( ForesterUtil.LINE_SEPARATOR );
163         writer.write( indentation );
164         PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.DISTRIBUTION );
165         if ( !ForesterUtil.isEmpty( getDesc() ) ) {
166             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.DISTRIBUTION_DESC, getDesc(), indentation );
167         }
168         final String ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
169         if ( getPoints() != null ) {
170             for( final Point point : getPoints() ) {
171                 if ( ( point != null ) && !Point.isSeemsEmpty( point ) ) {
172                     point.toPhyloXML( writer, level, ind );
173                 }
174             }
175         }
176         if ( getPolygons() != null ) {
177             for( final Polygon polygon : getPolygons() ) {
178                 if ( polygon != null ) {
179                     polygon.toPhyloXML( writer, level, ind );
180                 }
181             }
182         }
183         writer.write( ForesterUtil.LINE_SEPARATOR );
184         writer.write( indentation );
185         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.DISTRIBUTION );
186     }
187
188     @Override
189     public String toString() {
190         return asSimpleText().toString();
191     }
192 }