3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.protein;
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;
36 import org.forester.species.BasicSpecies;
37 import org.forester.species.Species;
38 import org.forester.util.ForesterUtil;
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 {
44 private final ProteinId _id;
45 private final int _length;
46 private final Species _species;
49 private String _accession;
50 private final List<Domain> _protein_domains;
51 public static Comparator<Domain> DomainMidPositionComparator = new Comparator<Domain>() {
54 public int compare( final Domain d1,
56 final int m1 = ( d1.getTo() + d1.getFrom() );
57 final int m2 = ( d2.getTo() + d2.getFrom() );
58 return m1 < m2 ? -1 : m1 > m2 ? 1 : d1
60 .compareTo( d2.getDomainId() );
64 public BasicProtein( final String id_str, final String species_str, final int length ) {
66 throw new IllegalArgumentException( "attempt to create protein of length " + length );
68 if ( ForesterUtil.isEmpty( id_str ) ) {
69 throw new IllegalArgumentException( "attempt to create protein with null or empty identifier" );
71 if ( ForesterUtil.isEmpty( species_str ) ) {
72 throw new IllegalArgumentException( "attempt to create protein with null or empty species" );
74 _id = new ProteinId( id_str );
75 _species = new BasicSpecies( species_str );
77 _protein_domains = new ArrayList<Domain>();
82 public void addProteinDomain( final Domain protein_domain ) {
83 getProteinDomains().add( protein_domain );
88 * If in_nc_order is set to true, this returns true only and only if
89 * the order in List 'domains' and this protein (as determined by the start positions
90 * of the domains of this proteins, _not_ by their index) are the same
91 * (interspersing, 'other', domains in this are ignored).
92 * If in_nc_order is set to false, this returns true only and only if
93 * this contains all domains listed in 'domains' (order and count do not matter).
95 * @param domains a list of domain ids in a certain order.
96 * @param in_nc_order to consider order
99 public boolean contains( final List<String> query_domain_ids, final boolean in_nc_order ) {
100 if ( !in_nc_order ) {
101 for( final String query_domain_id : query_domain_ids ) {
102 if ( !getProteinDomainIds().contains( query_domain_id ) ) {
109 int current_start_position = -1;
110 I: for( final String query_domain_id : query_domain_ids ) {
111 if ( getProteinDomainIds().contains( query_domain_id ) ) {
112 final List<Domain> found_domains = getProteinDomains( query_domain_id );
113 final SortedSet<Integer> ordered_start_positions = new TreeSet<Integer>();
114 for( final Domain found_domain : found_domains ) {
115 ordered_start_positions.add( found_domain.getFrom() );
117 for( final int start_position : ordered_start_positions ) {
118 if ( start_position > current_start_position ) {
119 current_start_position = start_position;
134 public String getAccession() {
139 public String getDescription() {
144 public List<Domain> getDomainsSortedByPosition() {
145 final List<Domain> domains = new ArrayList<Domain>( getProteinDomains().size() );
146 for( final Domain domain : getProteinDomains() ) {
147 domains.add( domain );
149 Collections.sort( domains, DomainMidPositionComparator );
154 public int getLength() {
159 public String getName() {
164 public int getNumberOfProteinDomains() {
165 return getProteinDomains().size();
169 public Domain getProteinDomain( final int index ) {
170 return _protein_domains.get( index );
174 public int getProteinDomainCount( final String domain_id ) {
175 return getProteinDomains( domain_id ).size();
179 public List<Domain> getProteinDomains() {
180 return _protein_domains;
184 public List<Domain> getProteinDomains( final String domain_id ) {
185 final List<Domain> domains = new ArrayList<Domain>();
186 for( final Domain domain : getProteinDomains() ) {
187 if ( domain.getDomainId().equals( domain_id ) ) {
188 domains.add( domain );
195 public ProteinId getProteinId() {
200 public Species getSpecies() {
204 public void setAccession( final String accession ) {
205 _accession = accession;
208 public void setDescription( final String description ) {
212 public void setName( final String name ) {
216 public String toDomainArchitectureString( final String separator ) {
217 final StringBuilder sb = new StringBuilder();
218 boolean first = true;
219 for( final Domain d : getDomainsSortedByPosition() ) {
224 sb.append( separator );
226 sb.append( d.getDomainId() );
228 return sb.toString();
231 public String toDomainArchitectureString( final String separator,
232 final int repeats_limit,
233 final String repeat_separator ) {
234 if ( repeats_limit < 3 ) {
235 throw new IllegalArgumentException( "repeats limit cannot be smaller than 3" );
237 final StringBuilder sb = new StringBuilder();
238 StringBuilder buffer = new StringBuilder();
241 for( final Domain d : getDomainsSortedByPosition() ) {
242 final String id = d.getDomainId();
243 if ( prev_id.equals( id ) ) {
249 buffer = new StringBuilder();
251 if ( counter < repeats_limit ) {
253 buffer.append( separator );
255 else if ( counter == repeats_limit ) {
256 buffer = new StringBuilder();
258 buffer.append( repeat_separator );
260 buffer.append( repeat_separator );
262 buffer.append( separator );
266 sb.append( buffer.substring( 0, buffer.length() - 1 ) );
267 return sb.toString();
271 public String toString() {
272 return toDomainArchitectureString( "~" );
275 private List<String> getProteinDomainIds() {
276 final List<String> ids = new ArrayList<String>( getProteinDomains().size() );
277 for( final Domain domain : getProteinDomains() ) {
278 ids.add( domain.getDomainId() );
283 private void init() {