in progress....
[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: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.protein;
28
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.List;
33 import java.util.SortedSet;
34 import java.util.TreeSet;
35
36 import org.forester.species.BasicSpecies;
37 import org.forester.species.Species;
38 import org.forester.util.ForesterUtil;
39
40 // Note: when implementing any "equals" method need to keep in mind that
41 // proteins could have the same name and/or id!
42 public class BasicProtein implements Protein {
43
44     private final ProteinId          _id;
45     private final int                _length;
46     private final Species            _species;
47     private String                   _name;
48     private String                   _desc;
49     private String                   _accession;
50     private final List<Domain>       _protein_domains;
51     public static Comparator<Domain> DomainMidPositionComparator = new Comparator<Domain>() {
52
53                                                                      @Override
54                                                                      public int compare( final Domain d1,
55                                                                                          final Domain d2 ) {
56                                                                          final int m1 = ( d1.getTo() + d1.getFrom() );
57                                                                          final int m2 = ( d2.getTo() + d2.getFrom() );
58                                                                          return m1 < m2 ? -1
59                                                                                  : m1 > m2 ? 1
60                                                                                          : d1.getDomainId()
61                                                                                                  .compareTo( d2
62                                                                                                          .getDomainId() );
63                                                                      }
64                                                                  };
65
66     public BasicProtein( final String id_str, final String species_str, final int length ) {
67         if ( length < 0 ) {
68             throw new IllegalArgumentException( "attempt to create protein of length " + length );
69         }
70         if ( ForesterUtil.isEmpty( id_str ) ) {
71             throw new IllegalArgumentException( "attempt to create protein with null or empty identifier" );
72         }
73         if ( ForesterUtil.isEmpty( species_str ) ) {
74             throw new IllegalArgumentException( "attempt to create protein with null or empty species" );
75         }
76         _id = new ProteinId( id_str );
77         _species = new BasicSpecies( species_str );
78         _length = length;
79         _protein_domains = new ArrayList<Domain>();
80         init();
81     }
82
83     @Override
84     public void addProteinDomain( final Domain protein_domain ) {
85         getProteinDomains().add( protein_domain );
86     }
87
88     @Override
89     /**
90      * If in_nc_order is set to true, this returns true only and only if
91      * the order in List 'domains' and this protein (as determined by the start positions
92      * of the domains of this proteins, _not_ by their index) are the same
93      * (interspersing, 'other', domains in this are ignored).
94      * If in_nc_order is set to false, this returns true only and only if
95      * this contains all domains listed in 'domains' (order and count do not matter).
96      *
97      * @param domains a list of domain ids in a certain order.
98      * @param in_nc_order to consider order
99      * @return
100      */
101     public boolean contains( final List<String> query_domain_ids, final boolean in_nc_order ) {
102         if ( !in_nc_order ) {
103             for( final String query_domain_id : query_domain_ids ) {
104                 if ( !getProteinDomainIds().contains( query_domain_id ) ) {
105                     return false;
106                 }
107             }
108             return true;
109         }
110         else {
111             int current_start_position = -1;
112             I: for( final String query_domain_id : query_domain_ids ) {
113                 if ( getProteinDomainIds().contains( query_domain_id ) ) {
114                     final List<Domain> found_domains = getProteinDomains( query_domain_id );
115                     final SortedSet<Integer> ordered_start_positions = new TreeSet<Integer>();
116                     for( final Domain found_domain : found_domains ) {
117                         ordered_start_positions.add( found_domain.getFrom() );
118                     }
119                     for( final int start_position : ordered_start_positions ) {
120                         if ( start_position > current_start_position ) {
121                             current_start_position = start_position;
122                             continue I;
123                         }
124                     }
125                     return false;
126                 }
127                 else {
128                     return false;
129                 }
130             }
131             return true;
132         }
133     }
134
135     @Override
136     public String getAccession() {
137         return _accession;
138     }
139
140     @Override
141     public String getDescription() {
142         return _desc;
143     }
144
145     @Override
146     public List<Domain> getDomainsSortedByPosition() {
147         final List<Domain> domains = new ArrayList<Domain>( getProteinDomains().size() );
148         for( final Domain domain : getProteinDomains() ) {
149             domains.add( domain );
150         }
151         Collections.sort( domains, DomainMidPositionComparator );
152         return domains;
153     }
154
155     @Override
156     public int getLength() {
157         return _length;
158     }
159
160     @Override
161     public String getName() {
162         return _name;
163     }
164
165     @Override
166     public int getNumberOfProteinDomains() {
167         return getProteinDomains().size();
168     }
169
170     @Override
171     public Domain getProteinDomain( final int index ) {
172         return _protein_domains.get( index );
173     }
174
175     @Override
176     public int getProteinDomainCount( final String domain_id ) {
177         return getProteinDomains( domain_id ).size();
178     }
179
180     @Override
181     public List<Domain> getProteinDomains() {
182         return _protein_domains;
183     }
184
185     @Override
186     public List<Domain> getProteinDomains( final String domain_id ) {
187         final List<Domain> domains = new ArrayList<Domain>();
188         for( final Domain domain : getProteinDomains() ) {
189             if ( domain.getDomainId().equals( domain_id ) ) {
190                 domains.add( domain );
191             }
192         }
193         return domains;
194     }
195
196     @Override
197     public ProteinId getProteinId() {
198         return _id;
199     }
200
201     @Override
202     public Species getSpecies() {
203         return _species;
204     }
205
206     public void setAccession( final String accession ) {
207         _accession = accession;
208     }
209
210     public void setDescription( final String description ) {
211         _desc = description;
212     }
213
214     public void setName( final String name ) {
215         _name = name;
216     }
217
218     @Override
219     public final String toDomainArchitectureString( final String separator, final double ie_cutoff ) {
220         final StringBuilder sb = new StringBuilder();
221         boolean first = true;
222         for( final Domain d : getDomainsSortedByPosition() ) {
223             if ( ( ie_cutoff <= -1 ) || ( d.getPerDomainEvalue() <= ie_cutoff ) ) {
224                 if ( first ) {
225                     first = false;
226                 }
227                 else {
228                     sb.append( separator );
229                 }
230                 sb.append( d.getDomainId() );
231             }
232         }
233         return sb.toString();
234     }
235
236     public final String toDomainArchitectureString( final String separator ) {
237         return toDomainArchitectureString( separator, -1 );
238     }
239
240     public String toDomainArchitectureString( final String separator,
241                                               final int repeats_limit,
242                                               final String repeat_separator ) {
243         if ( repeats_limit < 3 ) {
244             throw new IllegalArgumentException( "repeats limit cannot be smaller than 3" );
245         }
246         final StringBuilder sb = new StringBuilder();
247         StringBuilder buffer = new StringBuilder();
248         String prev_id = "";
249         int counter = 1;
250         for( final Domain d : getDomainsSortedByPosition() ) {
251             final String id = d.getDomainId();
252             if ( prev_id.equals( id ) ) {
253                 counter++;
254             }
255             else {
256                 counter = 1;
257                 sb.append( buffer );
258                 buffer = new StringBuilder();
259             }
260             if ( counter < repeats_limit ) {
261                 buffer.append( id );
262                 buffer.append( separator );
263             }
264             else if ( counter == repeats_limit ) {
265                 buffer = new StringBuilder();
266                 buffer.append( id );
267                 buffer.append( repeat_separator );
268                 buffer.append( id );
269                 buffer.append( repeat_separator );
270                 buffer.append( id );
271                 buffer.append( separator );
272             }
273             prev_id = id;
274         }
275         sb.append( buffer.substring( 0, buffer.length() - 1 ) );
276         return sb.toString();
277     }
278
279     @Override
280     public String toString() {
281         return toDomainArchitectureString( "--", 1 );
282     }
283
284     private List<String> getProteinDomainIds() {
285         final List<String> ids = new ArrayList<String>( getProteinDomains().size() );
286         for( final Domain domain : getProteinDomains() ) {
287             ids.add( domain.getDomainId() );
288         }
289         return ids;
290     }
291
292     private void init() {
293         _desc = "";
294         _accession = "";
295         _name = "";
296     }
297 }