initial commit
[jalview.git] / forester / java / src / org / forester / phylogeny / data / ProteinDomain.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 // All rights reserved
8 // 
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 // 
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.phylogeny.data;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
32 import org.forester.util.ForesterUtil;
33
34 public class ProteinDomain implements PhylogenyData {
35
36     final public static double CONFIDENCE_DEFAULT = 0.0;
37     final public static String IDENTIFIER_DEFAULT = "";
38     final private String       _name;
39     final private int          _from;
40     final private int          _to;
41     final private String       _id;
42     final private double       _confidence;
43
44     public ProteinDomain( final String name, final int from, final int to ) {
45         this( name, from, to, ProteinDomain.IDENTIFIER_DEFAULT, ProteinDomain.CONFIDENCE_DEFAULT );
46     }
47
48     public ProteinDomain( final String name, final int from, final int to, final double confidence ) {
49         this( name, from, to, ProteinDomain.IDENTIFIER_DEFAULT, confidence );
50     }
51
52     public ProteinDomain( final String name, final int from, final int to, final String id ) {
53         this( name, from, to, id, ProteinDomain.CONFIDENCE_DEFAULT );
54     }
55
56     public ProteinDomain( final String name, final int from, final int to, final String id, final double confidence ) {
57         if ( ( from >= to ) || ( to < 0 ) ) {
58             throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
59         }
60         _name = name;
61         _from = from;
62         _to = to;
63         _id = id;
64         _confidence = confidence;
65     }
66
67     public StringBuffer asSimpleText() {
68         return new StringBuffer( getName() );
69     }
70
71     public StringBuffer asText() {
72         final StringBuffer sb = new StringBuffer( getName() );
73         sb.append( " [" );
74         sb.append( getLength() );
75         if ( !ForesterUtil.isEmpty( getId() ) ) {
76             sb.append( " " );
77             sb.append( getId() );
78         }
79         if ( getConfidence() != CONFIDENCE_DEFAULT ) {
80             sb.append( " " );
81             sb.append( getConfidence() );
82         }
83         sb.append( "]" );
84         return sb;
85     }
86
87     public PhylogenyData copy() {
88         if ( getId() == null ) {
89             return new ProteinDomain( getName(), getFrom(), getTo(), getConfidence() );
90         }
91         return new ProteinDomain( getName(), getFrom(), getTo(), getId(), getConfidence() );
92     }
93
94     public double getConfidence() {
95         return _confidence;
96     }
97
98     public int getFrom() {
99         return _from;
100     }
101
102     public String getId() {
103         return _id;
104     }
105
106     public int getLength() {
107         return ( getTo() - getFrom() + 1 );
108     }
109
110     public String getName() {
111         return _name;
112     }
113
114     public int getTo() {
115         return _to;
116     }
117
118     public boolean isEqual( final PhylogenyData protein_domain ) {
119         if ( protein_domain == null ) {
120             return false;
121         }
122         if ( !( protein_domain instanceof ProteinDomain ) ) {
123             return false;
124         }
125         else if ( ( ( ProteinDomain ) protein_domain ).getLength() != getLength() ) {
126             return false;
127         }
128         else if ( !( ( ProteinDomain ) protein_domain ).getName().equals( getName() ) ) {
129             return false;
130         }
131         return true;
132     }
133
134     public StringBuffer toNHX() {
135         throw new UnsupportedOperationException();
136     }
137
138     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
139         writer.write( ForesterUtil.LINE_SEPARATOR );
140         writer.write( indentation );
141         if ( getId() != null ) {
142             PhylogenyDataUtil.appendOpen( writer,
143                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN,
144                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_FROM,
145                                           getFrom() + "",
146                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_TO,
147                                           getTo() + "",
148                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_CONFIDENCE,
149                                           getConfidence() + "",
150                                           PhyloXmlMapping.IDENTIFIER,
151                                           getId() );
152         }
153         else {
154             PhylogenyDataUtil.appendOpen( writer,
155                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN,
156                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_FROM,
157                                           getFrom() + "",
158                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_TO,
159                                           getTo() + "",
160                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_CONFIDENCE,
161                                           getConfidence() + "" );
162         }
163         writer.write( getName() );
164         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN );
165     }
166
167     @Override
168     public String toString() {
169         return asText().toString();
170     }
171 }