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