rio
[jalview.git] / forester / java / src / org / forester / protein / BasicProtein.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.protein;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
33
34 import org.forester.species.BasicSpecies;
35 import org.forester.species.Species;
36 import org.forester.util.ForesterUtil;
37
38 // Note: when implementing any "equals" method need to keep in mind that
39 // proteins could have the same name and/or id!
40 public class BasicProtein implements Protein {
41
42     private final ProteinId    _id;
43     private final int          _length;
44     private final Species      _species;
45     private String             _name;
46     private String             _desc;
47     private String             _accession;
48     private final List<Domain> _protein_domains;
49
50     public BasicProtein( final String id_str, final String species_str, final int length ) {
51         if ( length < 0 ) {
52             throw new IllegalArgumentException( "attempt to create protein of length " + length );
53         }
54         if ( ForesterUtil.isEmpty( id_str ) ) {
55             throw new IllegalArgumentException( "attempt to create protein with null or empty identifier" );
56         }
57         if ( ForesterUtil.isEmpty( species_str ) ) {
58             throw new IllegalArgumentException( "attempt to create protein with null or empty species" );
59         }
60         _id = new ProteinId( id_str );
61         _species = new BasicSpecies( species_str );
62         _length = length;
63         _protein_domains = new ArrayList<Domain>();
64         init();
65     }
66
67     @Override
68     public void addProteinDomain( final Domain protein_domain ) {
69         getProteinDomains().add( protein_domain );
70     }
71
72     @Override
73     /**
74      * If in_nc_order is set to true, this returns true only and only if
75      * the order in List 'domains' and this protein (as determined by the start positions
76      * of the domains of this proteins, _not_ by their index) are the same
77      * (interspersing, 'other', domains in this are ignored). 
78      * If in_nc_order is set to false, this returns true only and only if
79      * this contains all domains listed in 'domains' (order and count do not matter).
80      * 
81      * @param domains a list of domain ids in a certain order.
82      * @param in_nc_order to consider order
83      * @return
84      */
85     public boolean contains( final List<DomainId> query_domain_ids, final boolean in_nc_order ) {
86         if ( !in_nc_order ) {
87             for( final DomainId query_domain_id : query_domain_ids ) {
88                 if ( !getProteinDomainIds().contains( query_domain_id ) ) {
89                     return false;
90                 }
91             }
92             return true;
93         }
94         else {
95             int current_start_position = -1;
96             I: for( final DomainId query_domain_id : query_domain_ids ) {
97                 if ( getProteinDomainIds().contains( query_domain_id ) ) {
98                     final List<Domain> found_domains = getProteinDomains( query_domain_id );
99                     final SortedSet<Integer> ordered_start_positions = new TreeSet<Integer>();
100                     for( final Domain found_domain : found_domains ) {
101                         ordered_start_positions.add( found_domain.getFrom() );
102                     }
103                     for( final int start_position : ordered_start_positions ) {
104                         if ( start_position > current_start_position ) {
105                             current_start_position = start_position;
106                             continue I;
107                         }
108                     }
109                     return false;
110                 }
111                 else {
112                     return false;
113                 }
114             }
115             return true;
116         }
117     }
118
119     @Override
120     public String getAccession() {
121         return _accession;
122     }
123
124     @Override
125     public String getDescription() {
126         return _desc;
127     }
128
129     @Override
130     public String getName() {
131         return _name;
132     }
133
134     @Override
135     public int getNumberOfProteinDomains() {
136         return getProteinDomains().size();
137     }
138
139     @Override
140     public Domain getProteinDomain( final int index ) {
141         return _protein_domains.get( index );
142     }
143
144     @Override
145     public int getProteinDomainCount( final DomainId domain_id ) {
146         return getProteinDomains( domain_id ).size();
147     }
148
149     private List<DomainId> getProteinDomainIds() {
150         final List<DomainId> ids = new ArrayList<DomainId>( getProteinDomains().size() );
151         for( final Domain domain : getProteinDomains() ) {
152             ids.add( domain.getDomainId() );
153         }
154         return ids;
155     }
156
157     @Override
158     public List<Domain> getProteinDomains() {
159         return _protein_domains;
160     }
161
162     @Override
163     public List<Domain> getProteinDomains( final DomainId domain_id ) {
164         final List<Domain> domains = new ArrayList<Domain>();
165         for( final Domain domain : getProteinDomains() ) {
166             if ( domain.getDomainId().equals( domain_id ) ) {
167                 domains.add( domain );
168             }
169         }
170         return domains;
171     }
172
173     @Override
174     public ProteinId getProteinId() {
175         return _id;
176     }
177
178     @Override
179     public Species getSpecies() {
180         return _species;
181     }
182
183     private void init() {
184         _desc = "";
185         _accession = "";
186         _name = "";
187     }
188
189     public void setAccession( final String accession ) {
190         _accession = accession;
191     }
192
193     public void setDescription( final String description ) {
194         _desc = description;
195     }
196
197     public void setName( final String name ) {
198         _name = name;
199     }
200
201     @Override
202     public int getLength() {
203         return _length;
204     }
205 }