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