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