2 // 19:38:35 cmzmasek Exp $
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.surfacing;
29 import java.util.HashSet;
32 import org.forester.protein.BinaryDomainCombination;
33 import org.forester.protein.DomainId;
35 public class DomainArchitectureBasedGenomeSimilarityCalculator {
37 public static final double MAX_SIMILARITY_SCORE = 1.0;
38 public static final double MIN_SIMILARITY_SCORE = 0.0;
39 final private GenomeWideCombinableDomains _combinable_domains_genome_0;
40 final private GenomeWideCombinableDomains _combinable_domains_genome_1;
41 private Set<DomainId> _domain_ids_to_ignore;
42 private boolean _allow_domains_to_be_ignored;
43 private Set<DomainId> _all_domains;
44 private Set<DomainId> _shared_domains;
45 private Set<DomainId> _domains_specific_to_0;
46 private Set<DomainId> _domains_specific_to_1;
47 private Set<BinaryDomainCombination> _all_binary_domain_combinations;
48 private Set<BinaryDomainCombination> _shared_binary_domain_combinations;
49 private Set<BinaryDomainCombination> _binary_domain_combinations_specific_to_0;
50 private Set<BinaryDomainCombination> _binary_domain_combinations_specific_to_1;
52 public DomainArchitectureBasedGenomeSimilarityCalculator( final GenomeWideCombinableDomains combinable_domains_genome_0,
53 final GenomeWideCombinableDomains combinable_domains_genome_1 ) {
54 if ( ( combinable_domains_genome_0 == null ) || ( combinable_domains_genome_0.getSize() < 1 )
55 || ( combinable_domains_genome_1 == null ) || ( combinable_domains_genome_1.getSize() < 1 ) ) {
56 throw new IllegalArgumentException( "attempt to compare null or empty combinable domains collection" );
58 if ( combinable_domains_genome_0.getSpecies().equals( combinable_domains_genome_1.getSpecies() ) ) {
59 throw new IllegalArgumentException( "attempt to compare combinable domains collection from the same species" );
61 _combinable_domains_genome_0 = combinable_domains_genome_0;
62 _combinable_domains_genome_1 = combinable_domains_genome_1;
67 public void addDomainIdToIgnore( final DomainId domain_id_to_ignore ) {
69 getDomainIdsToIgnore().add( domain_id_to_ignore );
73 * This returns a score between 0.0 (no binary domain combination in common)
74 * and 1.0 (all binary domain combinations in common) measuring the similarity between two
75 * genomes based on the number of shared binary domain combinations:
77 * t: sum of (distinct) binary domain combinations
78 * s: sum of shared (distinct) binary domain combinations
80 * 1 - ( ( t - s ) / t )
82 * @return shared binary domain combinations based similarity score
84 public double calculateSharedBinaryDomainCombinationBasedGenomeSimilarityScore() {
85 final double t = getAllBinaryDomainCombinations().size();
86 final double s = getSharedBinaryDomainCombinations().size();
88 return MIN_SIMILARITY_SCORE;
90 return ( MAX_SIMILARITY_SCORE - ( ( t - s ) / t ) );
94 * This returns a score between 0.0 (no domains in common)
95 * and 1.0 (all domains in common) measuring the similarity between two
96 * genomes based on the number of shared domains:
98 * t: sum of (distinct) domains
99 * s: sum of shared (distinct) domains
101 * 1 - ( ( t - s ) / t )
103 * @return shared domains based similarity score
105 public double calculateSharedDomainsBasedGenomeSimilarityScore() {
106 final double t = getAllDomains().size();
107 final double s = getSharedDomains().size();
109 return MIN_SIMILARITY_SCORE;
111 return ( MAX_SIMILARITY_SCORE - ( ( t - s ) / t ) );
114 public void deleteAllDomainIdsToIgnore() {
115 forceRecalculation();
116 setDomainIdsToIgnore( new HashSet<DomainId>() );
119 private void forceRecalculation() {
121 _shared_domains = null;
122 _domains_specific_to_0 = null;
123 _domains_specific_to_1 = null;
124 _all_binary_domain_combinations = null;
125 _shared_binary_domain_combinations = null;
126 _binary_domain_combinations_specific_to_0 = null;
127 _binary_domain_combinations_specific_to_1 = null;
131 * Does not return binary combinations which contain one or two domains
132 * to be ignored -- if ignoring is allowed.
134 * @return SortedSet<BinaryDomainCombination>
136 public Set<BinaryDomainCombination> getAllBinaryDomainCombinations() {
137 if ( _all_binary_domain_combinations == null ) {
138 final Set<BinaryDomainCombination> all = new HashSet<BinaryDomainCombination>();
139 all.addAll( getCombinableDomainsGenome0().toBinaryDomainCombinations() );
140 all.addAll( getCombinableDomainsGenome1().toBinaryDomainCombinations() );
141 if ( isAllowDomainsToBeIgnored() && !getDomainIdsToIgnore().isEmpty() ) {
142 _all_binary_domain_combinations = pruneBinaryCombinations( all );
145 _all_binary_domain_combinations = all;
148 return _all_binary_domain_combinations;
152 * Does not return domains which are to be
153 * ignored -- if ignoring is allowed.
158 public Set<DomainId> getAllDomains() {
159 if ( _all_domains == null ) {
160 final Set<DomainId> all = new HashSet<DomainId>();
161 all.addAll( getCombinableDomainsGenome0().getAllDomainIds() );
162 all.addAll( getCombinableDomainsGenome1().getAllDomainIds() );
163 if ( isAllowDomainsToBeIgnored() && !getDomainIdsToIgnore().isEmpty() ) {
164 _all_domains = pruneDomains( all );
173 private Set<BinaryDomainCombination> getBinaryDomainCombinationsSpecificToGenome( final boolean specific_to_genome_0 ) {
174 final Set<BinaryDomainCombination> specific = new HashSet<BinaryDomainCombination>();
175 final Set<BinaryDomainCombination> bc0 = getCombinableDomainsGenome0().toBinaryDomainCombinations();
176 final Set<BinaryDomainCombination> bc1 = getCombinableDomainsGenome1().toBinaryDomainCombinations();
177 if ( specific_to_genome_0 ) {
178 for( final BinaryDomainCombination binary_domain_combination0 : bc0 ) {
179 if ( !bc1.contains( binary_domain_combination0 ) ) {
180 specific.add( binary_domain_combination0 );
185 for( final BinaryDomainCombination binary_domain_combination1 : bc1 ) {
186 if ( !bc0.contains( binary_domain_combination1 ) ) {
187 specific.add( binary_domain_combination1 );
191 if ( isAllowDomainsToBeIgnored() && !getDomainIdsToIgnore().isEmpty() ) {
192 return pruneBinaryCombinations( specific );
197 public Set<BinaryDomainCombination> getBinaryDomainCombinationsSpecificToGenome0() {
198 if ( _binary_domain_combinations_specific_to_0 == null ) {
199 _binary_domain_combinations_specific_to_0 = getBinaryDomainCombinationsSpecificToGenome( true );
201 return _binary_domain_combinations_specific_to_0;
204 public Set<BinaryDomainCombination> getBinaryDomainCombinationsSpecificToGenome1() {
205 if ( _binary_domain_combinations_specific_to_1 == null ) {
206 _binary_domain_combinations_specific_to_1 = getBinaryDomainCombinationsSpecificToGenome( false );
208 return _binary_domain_combinations_specific_to_1;
211 private GenomeWideCombinableDomains getCombinableDomainsGenome0() {
212 return _combinable_domains_genome_0;
215 private GenomeWideCombinableDomains getCombinableDomainsGenome1() {
216 return _combinable_domains_genome_1;
219 private Set<DomainId> getDomainIdsToIgnore() {
220 return _domain_ids_to_ignore;
223 private Set<DomainId> getDomainsSpecificToGenome( final boolean specific_to_genome_0 ) {
224 final Set<DomainId> specific = new HashSet<DomainId>();
225 final Set<DomainId> d0 = getCombinableDomainsGenome0().getAllDomainIds();
226 final Set<DomainId> d1 = getCombinableDomainsGenome1().getAllDomainIds();
227 if ( specific_to_genome_0 ) {
228 for( final DomainId domain0 : d0 ) {
229 if ( !d1.contains( domain0 ) ) {
230 specific.add( domain0 );
235 for( final DomainId domain1 : d1 ) {
236 if ( !d0.contains( domain1 ) ) {
237 specific.add( domain1 );
241 if ( isAllowDomainsToBeIgnored() && !getDomainIdsToIgnore().isEmpty() ) {
242 return pruneDomains( specific );
247 public Set<DomainId> getDomainsSpecificToGenome0() {
248 if ( _domains_specific_to_0 == null ) {
249 _domains_specific_to_0 = getDomainsSpecificToGenome( true );
251 return _domains_specific_to_0;
254 public Set<DomainId> getDomainsSpecificToGenome1() {
255 if ( _domains_specific_to_1 == null ) {
256 _domains_specific_to_1 = getDomainsSpecificToGenome( false );
258 return _domains_specific_to_1;
261 public Set<BinaryDomainCombination> getSharedBinaryDomainCombinations() {
262 if ( _shared_binary_domain_combinations == null ) {
263 final Set<BinaryDomainCombination> shared = new HashSet<BinaryDomainCombination>();
264 final Set<BinaryDomainCombination> bc0 = getCombinableDomainsGenome0().toBinaryDomainCombinations();
265 final Set<BinaryDomainCombination> bc1 = getCombinableDomainsGenome1().toBinaryDomainCombinations();
266 for( final BinaryDomainCombination binary_domain_combination0 : bc0 ) {
267 if ( bc1.contains( binary_domain_combination0 ) ) {
268 shared.add( binary_domain_combination0 );
271 _shared_binary_domain_combinations = shared;
272 if ( isAllowDomainsToBeIgnored() && !getDomainIdsToIgnore().isEmpty() ) {
273 _shared_binary_domain_combinations = pruneBinaryCombinations( shared );
276 return _shared_binary_domain_combinations;
279 public Set<DomainId> getSharedDomains() {
280 if ( _shared_domains == null ) {
281 final Set<DomainId> shared = new HashSet<DomainId>();
282 final Set<DomainId> d0 = getCombinableDomainsGenome0().getAllDomainIds();
283 final Set<DomainId> d1 = getCombinableDomainsGenome1().getAllDomainIds();
284 for( final DomainId domain0 : d0 ) {
285 if ( d1.contains( domain0 ) ) {
286 shared.add( domain0 );
289 _shared_domains = shared;
290 if ( isAllowDomainsToBeIgnored() && !getDomainIdsToIgnore().isEmpty() ) {
291 _shared_domains = pruneDomains( shared );
294 return _shared_domains;
297 private void init() {
298 deleteAllDomainIdsToIgnore();
299 setAllowDomainsToBeIgnored( false );
302 private boolean isAllowDomainsToBeIgnored() {
303 return _allow_domains_to_be_ignored;
306 private Set<BinaryDomainCombination> pruneBinaryCombinations( final Set<BinaryDomainCombination> all ) {
307 final Set<BinaryDomainCombination> pruned = new HashSet<BinaryDomainCombination>();
308 for( final BinaryDomainCombination bc : all ) {
309 if ( ( !getDomainIdsToIgnore().contains( bc.getId0() ) )
310 && ( !getDomainIdsToIgnore().contains( bc.getId1() ) ) ) {
317 private Set<DomainId> pruneDomains( final Set<DomainId> all ) {
318 final Set<DomainId> pruned = new HashSet<DomainId>();
319 for( final DomainId d : all ) {
320 if ( !getDomainIdsToIgnore().contains( d ) ) {
327 public void setAllowDomainsToBeIgnored( final boolean allow_domains_to_be_ignored ) {
328 forceRecalculation();
329 _allow_domains_to_be_ignored = allow_domains_to_be_ignored;
332 void setDomainIdsToIgnore( final Set<DomainId> domain_ids_to_ignore ) {
333 forceRecalculation();
334 _domain_ids_to_ignore = domain_ids_to_ignore;