moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[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: https://sites.google.com/site/cmzmasek/home/software/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     @Override
68     public StringBuffer asSimpleText() {
69         return new StringBuffer( getName() );
70     }
71
72     @Override
73     public StringBuffer asText() {
74         final StringBuffer sb = new StringBuffer( getName() );
75         sb.append( " [" );
76         sb.append( getLength() );
77         if ( !ForesterUtil.isEmpty( getId() ) ) {
78             sb.append( " " );
79             sb.append( getId() );
80         }
81         if ( getConfidence() != CONFIDENCE_DEFAULT ) {
82             sb.append( " " );
83             sb.append( getConfidence() );
84         }
85         sb.append( "]" );
86         return sb;
87     }
88
89     @Override
90     public PhylogenyData copy() {
91         if ( getId() == null ) {
92             return new ProteinDomain( getName(), getFrom(), getTo(), getConfidence() );
93         }
94         return new ProteinDomain( getName(), getFrom(), getTo(), getId(), getConfidence() );
95     }
96
97     public double getConfidence() {
98         return _confidence;
99     }
100
101     public int getFrom() {
102         return _from;
103     }
104
105     public String getId() {
106         return _id;
107     }
108
109     public int getLength() {
110         return ( ( getTo() - getFrom() ) + 1 );
111     }
112
113     public String getName() {
114         return _name;
115     }
116
117     public int getTo() {
118         return _to;
119     }
120
121     @Override
122     public boolean isEqual( final PhylogenyData protein_domain ) {
123         if ( protein_domain == null ) {
124             return false;
125         }
126         if ( !( protein_domain instanceof ProteinDomain ) ) {
127             return false;
128         }
129         else if ( ( ( ProteinDomain ) protein_domain ).getLength() != getLength() ) {
130             return false;
131         }
132         else if ( !( ( ProteinDomain ) protein_domain ).getName().equals( getName() ) ) {
133             return false;
134         }
135         return true;
136     }
137
138     @Override
139     public StringBuffer toNHX() {
140         throw new UnsupportedOperationException();
141     }
142
143     @Override
144     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
145         writer.write( ForesterUtil.LINE_SEPARATOR );
146         writer.write( indentation );
147         if ( getId() != null ) {
148             PhylogenyDataUtil.appendOpen( writer,
149                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN,
150                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_FROM,
151                                           getFrom() + "",
152                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_TO,
153                                           getTo() + "",
154                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_CONFIDENCE,
155                                           getConfidence() + "",
156                                           PhyloXmlMapping.IDENTIFIER,
157                                           getId() );
158         }
159         else {
160             PhylogenyDataUtil.appendOpen( writer,
161                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN,
162                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_FROM,
163                                           getFrom() + "",
164                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_TO,
165                                           getTo() + "",
166                                           PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_CONFIDENCE,
167                                           getConfidence() + "" );
168         }
169         writer.write( getName() );
170         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN );
171     }
172
173     @Override
174     public String toString() {
175         return asText().toString();
176     }
177 }