in progress
[jalview.git] / forester / java / src / org / forester / surfacing / CombinationsBasedPairwiseDomainSimilarityCalculator.java
1 // $Id:
2 // 22:43:35 cmzmasek Exp $
3 //
4 // FORESTER -- software libraries and applications
5 // for evolutionary biology research and applications.
6 //
7 // Copyright (C) 2008-2009 Christian M. Zmasek
8 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
27
28 package org.forester.surfacing;
29
30 import java.util.List;
31
32 import org.forester.protein.DomainId;
33
34 public class CombinationsBasedPairwiseDomainSimilarityCalculator implements PairwiseDomainSimilarityCalculator {
35
36     @Override
37     public PairwiseDomainSimilarity calculateSimilarity( final CombinableDomains domains_1,
38                                                          final CombinableDomains domains_2 ) {
39         if ( !domains_1.getKeyDomain().equals( domains_2.getKeyDomain() ) ) {
40             throw new IllegalArgumentException( "attempt to calculate similarity between domain collection with different keys" );
41         }
42         final List<DomainId> d1 = domains_1.getCombinableDomains();
43         final List<DomainId> d2 = domains_2.getCombinableDomains();
44         int same = 0;
45         int different = 0;
46         for( final DomainId domain : d1 ) {
47             if ( d2.contains( domain ) ) {
48                 same++;
49             }
50             else {
51                 different++;
52             }
53         }
54         for( final DomainId domain : d2 ) {
55             if ( !( d1.contains( domain ) ) ) {
56                 different++;
57             }
58         }
59         final int difference = domains_1.getNumberOfCombinableDomains() - domains_2.getNumberOfCombinableDomains();
60         return new CombinationsBasedPairwiseDomainSimilarity( same, different, difference );
61     }
62 }