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