(no commit message)
[jalview.git] / forester / java / src / org / forester / surfacing / BasicCombinableDomains.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.surfacing;
28
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.SortedMap;
35 import java.util.TreeMap;
36
37 import org.forester.protein.BinaryDomainCombination;
38 import org.forester.species.Species;
39 import org.forester.util.ForesterUtil;
40
41 public class BasicCombinableDomains implements CombinableDomains {
42
43     final private TreeMap<String, Integer> _combining_domains;
44     final private String                   _key_domain;
45     private int                            _key_domain_count;
46     final private Set<String>              _proteins_with_key_domain;
47     final private Species                  _species;
48
49     public BasicCombinableDomains( final String key_domain, final Species species ) {
50         _key_domain = key_domain;
51         _species = species;
52         _combining_domains = new TreeMap<String, Integer>();
53         _proteins_with_key_domain = new HashSet<String>();
54         _key_domain_count = 0;
55     }
56
57     @Override
58     public void addCombinableDomain( final String protein_domain ) {
59         if ( getCombiningDomains().containsKey( protein_domain ) ) {
60             getCombiningDomains().put( protein_domain, getCombiningDomains().get( protein_domain ) + 1 );
61         }
62         else {
63             getCombiningDomains().put( protein_domain, 1 );
64         }
65     }
66
67     @Override
68     public void addKeyDomainProtein( final String protein ) {
69         if ( ForesterUtil.isEmpty( protein ) ) {
70             throw new IllegalArgumentException( "attempt to add null or empty protein" );
71         }
72         getKeyDomainProteins().add( protein );
73     }
74
75     @Override
76     public List<String> getAllDomains() {
77         final List<String> domains = getCombinableDomains();
78         if ( !domains.contains( getKeyDomain() ) ) {
79             domains.add( getKeyDomain() );
80         }
81         return domains;
82     }
83
84     @Override
85     public List<String> getCombinableDomains() {
86         final List<String> domains = new ArrayList<String>( getNumberOfCombinableDomains() );
87         for( final String domain : getCombiningDomains().keySet() ) {
88             domains.add( domain );
89         }
90         return domains;
91     }
92
93     @Override
94     public SortedMap<String, Integer> getCombinableDomainsIds() {
95         final SortedMap<String, Integer> ids = new TreeMap<String, Integer>();
96         for( final String domain : getCombiningDomains().keySet() ) {
97             final String pd = domain;
98             ids.put( pd, getCombiningDomains().get( pd ) );
99         }
100         return ids;
101     }
102
103     @Override
104     public StringBuilder getCombiningDomainIdsAsStringBuilder() {
105         final StringBuilder sb = new StringBuilder();
106         for( final Iterator<String> iter = getCombiningDomains().keySet().iterator(); iter.hasNext(); ) {
107             final String key = iter.next();
108             sb.append( key.toString() );
109             sb.append( " [" );
110             final int count = getCombiningDomains().get( key );
111             sb.append( count );
112             sb.append( "]" );
113             if ( iter.hasNext() ) {
114                 sb.append( ", " );
115             }
116         }
117         return sb;
118     }
119
120     @Override
121     public String getKeyDomain() {
122         return _key_domain;
123     }
124
125     @Override
126     public int getKeyDomainCount() {
127         return _key_domain_count;
128     }
129
130     @Override
131     public Set<String> getKeyDomainProteins() {
132         return _proteins_with_key_domain;
133     }
134
135     @Override
136     public int getKeyDomainProteinsCount() {
137         return getKeyDomainProteins().size();
138     }
139
140     @Override
141     public int getNumberOfCombinableDomains() {
142         return _combining_domains.size();
143     }
144
145     @Override
146     public int getNumberOfProteinsExhibitingCombination( final String protein_domain ) {
147         if ( getCombiningDomains().containsKey( protein_domain ) ) {
148             return getCombiningDomains().get( protein_domain );
149         }
150         else {
151             return 0;
152         }
153     }
154
155     @Override
156     public Species getSpecies() {
157         return _species;
158     }
159
160     @Override
161     public boolean isCombinable( final String protein_domain ) {
162         return getCombiningDomains().containsKey( protein_domain );
163     }
164
165     @Override
166     public void setKeyDomainCount( final int key_domain_count ) {
167         _key_domain_count = key_domain_count;
168     }
169
170     @Override
171     public List<BinaryDomainCombination> toBinaryDomainCombinations() {
172         final List<BinaryDomainCombination> binary_combinations = new ArrayList<BinaryDomainCombination>( getNumberOfCombinableDomains() );
173         for( final String domain : getCombiningDomains().keySet() ) {
174             // binary_combinations.add( new BasicBinaryDomainCombination( getKeyDomain(), domain ) );
175             binary_combinations.add( BasicBinaryDomainCombination.obtainInstance( getKeyDomain(), domain ) );
176         }
177         return binary_combinations;
178     }
179
180     @Override
181     public String toString() {
182         final StringBuilder sb = new StringBuilder();
183         sb.append( getKeyDomain() );
184         sb.append( " [" );
185         sb.append( getKeyDomainCount() );
186         sb.append( ", " );
187         sb.append( getKeyDomainProteinsCount() );
188         sb.append( ", " );
189         sb.append( getNumberOfCombinableDomains() );
190         sb.append( "]: " );
191         sb.append( getCombiningDomainIdsAsStringBuilder() );
192         return sb.toString();
193     }
194
195     protected TreeMap<String, Integer> getCombiningDomains() {
196         return _combining_domains;
197     }
198 }