initial commit
[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     public void addCombinableDomain( final DomainId protein_domain ) {
54         if ( getCombiningDomains().containsKey( protein_domain ) ) {
55             getCombiningDomains().put( protein_domain, getCombiningDomains().get( protein_domain ) + 1 );
56         }
57         else {
58             getCombiningDomains().put( protein_domain, 1 );
59         }
60     }
61
62     public List<DomainId> getAllDomains() {
63         final List<DomainId> domains = getCombinableDomains();
64         if ( !domains.contains( getKeyDomain() ) ) {
65             domains.add( getKeyDomain() );
66         }
67         return domains;
68     }
69
70     public List<DomainId> getCombinableDomains() {
71         final List<DomainId> domains = new ArrayList<DomainId>( getNumberOfCombinableDomains() );
72         for( final DomainId domain : getCombiningDomains().keySet() ) {
73             domains.add( domain );
74         }
75         return domains;
76     }
77
78     public SortedMap<DomainId, Integer> getCombinableDomainsIds() {
79         final SortedMap<DomainId, Integer> ids = new TreeMap<DomainId, Integer>();
80         for( final DomainId domain : getCombiningDomains().keySet() ) {
81             final DomainId pd = domain;
82             ids.put( pd, getCombiningDomains().get( pd ) );
83         }
84         return ids;
85     }
86
87     public StringBuilder getCombiningDomainIdsAsStringBuilder() {
88         final StringBuilder sb = new StringBuilder();
89         for( final Iterator<DomainId> iter = getCombiningDomains().keySet().iterator(); iter.hasNext(); ) {
90             final DomainId key = iter.next();
91             sb.append( key.toString() );
92             sb.append( " [" );
93             final int count = getCombiningDomains().get( key );
94             sb.append( count );
95             sb.append( "]" );
96             if ( iter.hasNext() ) {
97                 sb.append( ", " );
98             }
99         }
100         return sb;
101     }
102
103     protected TreeMap<DomainId, Integer> getCombiningDomains() {
104         return _combining_domains;
105     }
106
107     public DomainId getKeyDomain() {
108         return _key_domain;
109     }
110
111     public DescriptiveStatistics getKeyDomainConfidenceDescriptiveStatistics() {
112         return _key_domain_confidence_statistics;
113     }
114
115     public int getKeyDomainCount() {
116         return _key_domain_count;
117     }
118
119     public int getKeyDomainProteinsCount() {
120         return _key_domain_proteins_count;
121     }
122
123     public int getNumberOfCombinableDomains() {
124         return _combining_domains.size();
125     }
126
127     public int getNumberOfProteinsExhibitingCombination( final DomainId protein_domain ) {
128         if ( getCombiningDomains().containsKey( protein_domain ) ) {
129             return getCombiningDomains().get( protein_domain );
130         }
131         else {
132             return 0;
133         }
134     }
135
136     public Species getSpecies() {
137         return _species;
138     }
139
140     private void init() {
141         _key_domain_count = 0;
142         _key_domain_proteins_count = 0;
143         _key_domain_confidence_statistics = null;
144     }
145
146     public boolean isCombinable( final DomainId protein_domain ) {
147         return getCombiningDomains().containsKey( protein_domain );
148     }
149
150     public void setKeyDomainConfidenceDescriptiveStatistics( final DescriptiveStatistics key_domain_confidence_statistics ) {
151         _key_domain_confidence_statistics = key_domain_confidence_statistics;
152     }
153
154     public void setKeyDomainCount( final int key_domain_count ) {
155         _key_domain_count = key_domain_count;
156     }
157
158     public void setKeyDomainProteinsCount( final int key_domain_proteins_count ) {
159         _key_domain_proteins_count = key_domain_proteins_count;
160     }
161
162     @Override
163     public List<BinaryDomainCombination> toBinaryDomainCombinations() {
164         final List<BinaryDomainCombination> binary_combinations = new ArrayList<BinaryDomainCombination>( getNumberOfCombinableDomains() );
165         for( final DomainId domain : getCombiningDomains().keySet() ) {
166             binary_combinations.add( new BasicBinaryDomainCombination( getKeyDomain(), domain ) );
167         }
168         return binary_combinations;
169     }
170
171     @Override
172     public String toString() {
173         final StringBuilder sb = new StringBuilder();
174         sb.append( getKeyDomain() );
175         sb.append( " [" );
176         sb.append( getKeyDomainCount() );
177         sb.append( ", " );
178         sb.append( getKeyDomainProteinsCount() );
179         sb.append( ", " );
180         sb.append( getNumberOfCombinableDomains() );
181         sb.append( "]: " );
182         sb.append( getCombiningDomainIdsAsStringBuilder() );
183         return sb.toString();
184     }
185 }