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