added infraorder
[jalview.git] / forester / java / src / org / forester / surfacing / 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.surfacing;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
33
34 // Note: when implementing any "equals" method need to keep in mind that
35 // proteins could have the same name and/or id!
36 public class BasicProtein implements Protein {
37
38     private final ProteinId    _id;
39     private final Species      _species;
40     private String             _name;
41     private String             _desc;
42     private String             _accession;
43     private final List<Domain> _protein_domains;
44
45     public BasicProtein( final String id_str, final String species_str ) {
46         _id = new ProteinId( id_str );
47         _species = new BasicSpecies( species_str );
48         _protein_domains = new ArrayList<Domain>();
49         init();
50     }
51
52     @Override
53     public void addProteinDomain( final Domain protein_domain ) {
54         getProteinDomains().add( protein_domain );
55     }
56
57     @Override
58     /**
59      * If in_nc_order is set to true, this returns true only and only if
60      * the order in List 'domains' and this protein (as determined by the start positions
61      * of the domains of this proteins, _not_ by their index) are the same
62      * (interspersing, 'other', domains in this are ignored). 
63      * If in_nc_order is set to false, this returns true only and only if
64      * this contains all domains listed in 'domains' (order and count do not matter).
65      * 
66      * @param domains a list of domain ids in a certain order.
67      * @param in_nc_order to consider order
68      * @return
69      */
70     public boolean contains( final List<DomainId> query_domain_ids, final boolean in_nc_order ) {
71         if ( !in_nc_order ) {
72             for( final DomainId query_domain_id : query_domain_ids ) {
73                 if ( !getProteinDomainIds().contains( query_domain_id ) ) {
74                     return false;
75                 }
76             }
77             return true;
78         }
79         else {
80             int current_start_position = -1;
81             I: for( final DomainId query_domain_id : query_domain_ids ) {
82                 if ( getProteinDomainIds().contains( query_domain_id ) ) {
83                     final List<Domain> found_domains = getProteinDomains( query_domain_id );
84                     final SortedSet<Integer> ordered_start_positions = new TreeSet<Integer>();
85                     for( final Domain found_domain : found_domains ) {
86                         ordered_start_positions.add( found_domain.getFrom() );
87                     }
88                     for( final int start_position : ordered_start_positions ) {
89                         if ( start_position > current_start_position ) {
90                             current_start_position = start_position;
91                             continue I;
92                         }
93                     }
94                     return false;
95                 }
96                 else {
97                     return false;
98                 }
99             }
100             return true;
101         }
102     }
103
104     @Override
105     public String getAccession() {
106         return _accession;
107     }
108
109     @Override
110     public String getDescription() {
111         return _desc;
112     }
113
114     @Override
115     public String getName() {
116         return _name;
117     }
118
119     @Override
120     public int getNumberOfProteinDomains() {
121         return getProteinDomains().size();
122     }
123
124     @Override
125     public Domain getProteinDomain( final int index ) {
126         return _protein_domains.get( index );
127     }
128
129     @Override
130     public int getProteinDomainCount( final DomainId domain_id ) {
131         return getProteinDomains( domain_id ).size();
132     }
133
134     private List<DomainId> getProteinDomainIds() {
135         final List<DomainId> ids = new ArrayList<DomainId>( getProteinDomains().size() );
136         for( final Domain domain : getProteinDomains() ) {
137             ids.add( domain.getDomainId() );
138         }
139         return ids;
140     }
141
142     @Override
143     public List<Domain> getProteinDomains() {
144         return _protein_domains;
145     }
146
147     @Override
148     public List<Domain> getProteinDomains( final DomainId domain_id ) {
149         final List<Domain> domains = new ArrayList<Domain>();
150         for( final Domain domain : getProteinDomains() ) {
151             if ( domain.getDomainId().equals( domain_id ) ) {
152                 domains.add( domain );
153             }
154         }
155         return domains;
156     }
157
158     @Override
159     public ProteinId getProteinId() {
160         return _id;
161     }
162
163     @Override
164     public Species getSpecies() {
165         return _species;
166     }
167
168     private void init() {
169         _desc = "";
170         _accession = "";
171         _name = "";
172     }
173
174     public void setAccession( final String accession ) {
175         _accession = accession;
176     }
177
178     public void setDescription( final String description ) {
179         _desc = description;
180     }
181
182     public void setName( final String name ) {
183         _name = name;
184     }
185 }