in progress
[jalview.git] / forester / java / src / org / forester / surfacing / PrintableSpeciesSpecificDomainSimilariyData.java
1 // $Id:
2 // 22:09:42 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.text.DecimalFormat;
31 import java.text.NumberFormat;
32 import java.util.Set;
33 import java.util.SortedMap;
34 import java.util.TreeMap;
35
36 import org.forester.protein.DomainId;
37 import org.forester.util.DescriptiveStatistics;
38
39 class PrintableSpeciesSpecificDomainSimilariyData implements SpeciesSpecificDomainSimilariyData {
40
41     private final static NumberFormat   FORMATTER = new DecimalFormat( "0.0E0" );
42     final SortedMap<DomainId, Integer>  _combinable_domain_id_to_count_map;
43     final private int                   _key_domain_proteins_count;
44     final private int                   _key_domain_domains_count;
45     final private int                   _combinable_domains_count;
46     final private DescriptiveStatistics _key_domain_confidence_descriptive_statistics;
47
48     public PrintableSpeciesSpecificDomainSimilariyData( final int key_domain_proteins_count,
49                                                         final int key_domain_domains_count,
50                                                         final int combinable_domains,
51                                                         final DescriptiveStatistics key_domain_confidence_descriptive_statistics ) {
52         _key_domain_proteins_count = key_domain_proteins_count;
53         _key_domain_domains_count = key_domain_domains_count;
54         _combinable_domains_count = combinable_domains;
55         _key_domain_confidence_descriptive_statistics = key_domain_confidence_descriptive_statistics;
56         _combinable_domain_id_to_count_map = new TreeMap<DomainId, Integer>();
57     }
58
59     @Override
60     public void addProteinsExhibitingCombinationCount( final DomainId domain_id, final int count ) {
61         if ( getCombinableDomainIdToCountsMap().containsKey( domain_id ) ) {
62             throw new IllegalArgumentException( "Domain with id " + domain_id + " already exists" );
63         }
64         getCombinableDomainIdToCountsMap().put( domain_id, count );
65     }
66
67     @Override
68     public SortedMap<DomainId, Integer> getCombinableDomainIdToCountsMap() {
69         return _combinable_domain_id_to_count_map;
70     }
71
72     private int getCombinableDomainsCount() {
73         return _combinable_domains_count;
74     }
75
76     private DescriptiveStatistics getKeyDomainConfidenceDescriptiveStatistics() {
77         return _key_domain_confidence_descriptive_statistics;
78     }
79
80     private int getKeyDomainDomainsCount() {
81         return _key_domain_domains_count;
82     }
83
84     private int getKeyDomainProteinsCount() {
85         return _key_domain_proteins_count;
86     }
87
88     @Override
89     public int getNumberOfProteinsExhibitingCombinationWith( final DomainId domain_id ) {
90         if ( !getCombinableDomainIdToCountsMap().containsKey( domain_id ) ) {
91             throw new IllegalArgumentException( "Domain with id " + domain_id + " not found" );
92         }
93         return getCombinableDomainIdToCountsMap().get( domain_id );
94     }
95
96     @Override
97     public String toString() {
98         return toStringBuffer( DomainSimilarityCalculator.Detailedness.LIST_COMBINING_DOMAIN_FOR_EACH_SPECIES, false )
99                 .toString();
100     }
101
102     @Override
103     public StringBuffer toStringBuffer( final DomainSimilarityCalculator.Detailedness detailedness, final boolean html ) {
104         final StringBuffer sb = new StringBuffer();
105         if ( detailedness == DomainSimilarityCalculator.Detailedness.PUNCTILIOUS ) {
106             sb.append( " " );
107             sb.append( getKeyDomainDomainsCount() );
108             sb.append( ", " );
109             sb.append( getKeyDomainProteinsCount() );
110             sb.append( ", " );
111             sb.append( getCombinableDomainsCount() );
112             sb.append( ", " );
113             if ( html ) {
114                 sb.append( "<i>" );
115             }
116             sb.append( FORMATTER.format( getKeyDomainConfidenceDescriptiveStatistics().arithmeticMean() ) );
117             if ( html ) {
118                 sb.append( "</i>" );
119             }
120             if ( !getCombinableDomainIdToCountsMap().isEmpty() ) {
121                 sb.append( ":" );
122             }
123         }
124         final Set<DomainId> ids = getCombinableDomainIdToCountsMap().keySet();
125         int i = 0;
126         for( final DomainId domain_id : ids ) {
127             ++i;
128             sb.append( " " );
129             if ( html ) {
130                 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_id.getId() + "\">"
131                         + domain_id.getId() + "</a>" );
132             }
133             else {
134                 sb.append( domain_id.getId() );
135             }
136             if ( detailedness == DomainSimilarityCalculator.Detailedness.PUNCTILIOUS ) {
137                 sb.append( ":" );
138                 sb.append( getCombinableDomainIdToCountsMap().get( domain_id ) );
139             }
140             if ( i < ids.size() - 1 ) {
141                 sb.append( "," );
142             }
143         }
144         return sb;
145     }
146 }