e54142f5f3e47388496496e498520879151162da
[jalview.git] / forester / java / src / org / forester / surfacing / PrintableSpeciesSpecificDcData.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: https://sites.google.com/site/cmzmasek/home/software/forester
27
28 package org.forester.surfacing;
29
30 import java.util.Set;
31 import java.util.SortedMap;
32 import java.util.SortedSet;
33 import java.util.TreeMap;
34 import java.util.TreeSet;
35
36 import org.forester.util.ForesterUtil;
37 import org.forester.util.SequenceIdParser;
38
39 class PrintableSpeciesSpecificDcData implements SpeciesSpecificDcData {
40
41     final SortedMap<String, Integer> _combinable_domain_id_to_count_map;
42     final SortedSet<String>          _key_domain_proteins;
43     final private int                _combinable_domains_count;
44     final private int                _key_domain_domains_count;
45
46     public PrintableSpeciesSpecificDcData( final int key_domain_domains_count, final int combinable_domains ) {
47         _key_domain_proteins = new TreeSet<String>();
48         _key_domain_domains_count = key_domain_domains_count;
49         _combinable_domains_count = combinable_domains;
50         _combinable_domain_id_to_count_map = new TreeMap<String, Integer>();
51     }
52
53     @Override
54     public void addKeyDomainProtein( final String protein ) {
55         if ( ForesterUtil.isEmpty( protein ) ) {
56             throw new IllegalArgumentException( "attempt to add null or empty protein" );
57         }
58         if ( getKeyDomainProteins().contains( protein ) ) {
59             throw new IllegalArgumentException( "protein \"" + protein + "\" is not unique" );
60         }
61         getKeyDomainProteins().add( protein );
62     }
63
64     @Override
65     public void addProteinsExhibitingCombinationCount( final String domain_id, final int count ) {
66         if ( getCombinableDomainIdToCountsMap().containsKey( domain_id ) ) {
67             throw new IllegalArgumentException( "Domain with id " + domain_id + " already exists" );
68         }
69         getCombinableDomainIdToCountsMap().put( domain_id, count );
70     }
71
72     @Override
73     public SortedMap<String, Integer> getCombinableDomainIdToCountsMap() {
74         return _combinable_domain_id_to_count_map;
75     }
76
77     @Override
78     public SortedSet<String> getKeyDomainProteins() {
79         return _key_domain_proteins;
80     }
81
82     @Override
83     public int getNumberOfProteinsExhibitingCombinationWith( final String domain_id ) {
84         if ( !getCombinableDomainIdToCountsMap().containsKey( domain_id ) ) {
85             throw new IllegalArgumentException( "Domain with id " + domain_id + " not found" );
86         }
87         return getCombinableDomainIdToCountsMap().get( domain_id );
88     }
89
90     @Override
91     public String toString() {
92         return toStringBuffer( DomainSimilarityCalculator.Detailedness.LIST_COMBINING_DOMAIN_FOR_EACH_SPECIES, false )
93                 .toString();
94     }
95
96     @Override
97     public StringBuffer toStringBuffer( final DomainSimilarityCalculator.Detailedness detailedness, final boolean html ) {
98         final StringBuffer sb = new StringBuffer();
99         if ( detailedness == DomainSimilarityCalculator.Detailedness.PUNCTILIOUS ) {
100             sb.append( " " );
101             sb.append( getKeyDomainDomainsCount() );
102             sb.append( ", " );
103             sb.append( getKeyDomainProteinsCount() );
104             sb.append( ", " );
105             sb.append( getCombinableDomainsCount() );
106             if ( !getCombinableDomainIdToCountsMap().isEmpty() ) {
107                 sb.append( ":" );
108             }
109         }
110         final Set<String> ids = getCombinableDomainIdToCountsMap().keySet();
111         int i = 0;
112         for( final String domain_id : ids ) {
113             ++i;
114             sb.append( " " );
115             if ( html ) {
116                 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_id + "\">" + domain_id
117                         + "</a>" );
118             }
119             else {
120                 sb.append( domain_id );
121             }
122             if ( detailedness == DomainSimilarityCalculator.Detailedness.PUNCTILIOUS ) {
123                 sb.append( ":" );
124                 sb.append( getCombinableDomainIdToCountsMap().get( domain_id ) );
125             }
126         }
127         sb.append( " [" );
128         boolean first = true;
129         for( final String p : getKeyDomainProteins() ) {
130             final String link = obtainSeqLink( p );
131             if ( first ) {
132                 first = false;
133             }
134             else {
135                 sb.append( ", " );
136             }
137             sb.append( link );
138         }
139         sb.append( "]" );
140         return sb;
141     }
142
143     private int getCombinableDomainsCount() {
144         return _combinable_domains_count;
145     }
146
147     private int getKeyDomainDomainsCount() {
148         return _key_domain_domains_count;
149     }
150
151     private int getKeyDomainProteinsCount() {
152         return _key_domain_proteins.size();
153     }
154
155     private static String obtainSeqLink( final String p ) {
156         String link;
157         final String up_id = ForesterUtil.extractUniProtKbProteinSeqIdentifier( p );
158         if ( !ForesterUtil.isEmpty( up_id ) ) {
159             link = "<a class=\"pl\" href=\"" + ForesterUtil.UNIPROT_KB + up_id + "\" target=\"_up_window\">" + up_id
160                     + "</a>";
161         }
162         else {
163             final String gb_id = SequenceIdParser.parseGenbankProteinAccessor( p );
164             if ( !ForesterUtil.isEmpty( gb_id ) ) {
165                 link = "<a class=\"pl\" href=\"" + ForesterUtil.NCBI_PROTEIN + gb_id + "\" target=\"_up_window\">"
166                         + gb_id + "</a>";
167             }
168             else {
169                 final String gi = SequenceIdParser.parseGInumber( p );
170                 if ( !ForesterUtil.isEmpty( gi ) ) {
171                     link = "<a class=\"pl\" href=\"" + ForesterUtil.NCBI_GI + gi + "\" target=\"_up_window\">gi|" + gi
172                             + "</a>";
173                 }
174                 else {
175                     link = "<a class=\"ps\" href=\"" + "http://www.google.com/search?q=" + p
176                             + "\" target=\"_g_window\">" + p + "</a>";
177                 }
178             }
179         }
180         return link;
181     }
182 }