clean up
[jalview.git] / forester / java / src / org / forester / surfacing / DomainLengthsTable.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2010 Christian M. Zmasek
7 // Copyright (C) 2008-2010 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.surfacing;
28
29 import java.text.DecimalFormat;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.SortedMap;
33 import java.util.TreeMap;
34
35 import org.forester.util.BasicDescriptiveStatistics;
36 import org.forester.util.DescriptiveStatistics;
37 import org.forester.util.ForesterUtil;
38
39 public class DomainLengthsTable {
40
41     private final static DecimalFormat       DF = new DecimalFormat( "#.0" );
42     final SortedMap<DomainId, DomainLengths> _domain_lengths;
43     final List<Species>                      _species;
44
45     public DomainLengthsTable() {
46         _domain_lengths = new TreeMap<DomainId, DomainLengths>();
47         _species = new ArrayList<Species>();
48     }
49
50     private void addDomainLengths( final DomainLengths domain_lengths ) {
51         if ( getDomainLengths().containsKey( domain_lengths.getDomainId() ) ) {
52             throw new IllegalArgumentException( "domain lengths for [" + domain_lengths.getDomainId()
53                     + "] already added" );
54         }
55         getDomainLengths().put( domain_lengths.getDomainId(), domain_lengths );
56     }
57
58     private void addLength( final DomainId domain_id, final Species species, final int domain_length ) {
59         if ( !getDomainLengths().containsKey( domain_id ) ) {
60             addDomainLengths( new DomainLengths( domain_id ) );
61         }
62         getDomainLengths().get( domain_id ).addLength( species, domain_length );
63     }
64
65     public void addLengths( final List<Protein> protein_list ) {
66         for( final Protein protein : protein_list ) {
67             final Species species = protein.getSpecies();
68             if ( !_species.contains( species ) ) {
69                 _species.add( species );
70             }
71             for( final Domain domain : protein.getProteinDomains() ) {
72                 addLength( domain.getDomainId(), species, ( domain.getTo() - domain.getFrom() ) + 1 );
73             }
74         }
75     }
76
77     public DescriptiveStatistics calculateMeanBasedStatisticsForAllSpecies() {
78         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
79         for( final Species species : getSpecies() ) {
80             final DescriptiveStatistics stats_per_species = calculateMeanBasedStatisticsForSpecies( species );
81             stats.addValue( stats_per_species.arithmeticMean() );
82         }
83         return stats;
84     }
85
86     public DescriptiveStatistics calculateMeanBasedStatisticsForDomain( final DomainId domain_id ) {
87         return getDomainLengths( domain_id ).calculateMeanBasedStatistics();
88     }
89
90     public DescriptiveStatistics calculateMeanBasedStatisticsForSpecies( final Species species ) {
91         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
92         for( final DomainLengths l : getDomainLengths().values() ) {
93             if ( l.isHasLengthStatistic( species ) ) {
94                 stats.addValue( l.getLengthStatistic( species ).arithmeticMean() );
95             }
96         }
97         return stats;
98     }
99
100     public StringBuilder createMeanBasedStatisticsPerSpeciesTable() {
101         final StringBuilder sb = new StringBuilder();
102         sb.append( "SPECIES" );
103         sb.append( "\t" );
104         sb.append( "MEAN" );
105         sb.append( "\t" );
106         sb.append( "SD" );
107         sb.append( "\t" );
108         sb.append( "MIN" );
109         sb.append( "\t" );
110         sb.append( "MAX" );
111         sb.append( "\t" );
112         sb.append( "MEDIAN" );
113         sb.append( ForesterUtil.LINE_SEPARATOR );
114         for( final Species species : getSpecies() ) {
115             final DescriptiveStatistics stats = calculateMeanBasedStatisticsForSpecies( species );
116             sb.append( species );
117             sb.append( "\t" );
118             sb.append( DF.format( stats.arithmeticMean() ) );
119             sb.append( "\t" );
120             try {
121                 sb.append( DF.format( stats.sampleStandardDeviation() ) );
122             }
123             catch ( final ArithmeticException e ) {
124                 sb.append( "" );
125             }
126             sb.append( "\t" );
127             sb.append( DF.format( stats.getMin() ) );
128             sb.append( "\t" );
129             sb.append( DF.format( stats.getMax() ) );
130             sb.append( "\t" );
131             try {
132                 sb.append( DF.format( stats.median() ) );
133             }
134             catch ( final ArithmeticException e ) {
135                 sb.append( "" );
136             }
137             sb.append( ForesterUtil.LINE_SEPARATOR );
138         }
139         return sb;
140     }
141
142     private SortedMap<DomainId, DomainLengths> getDomainLengths() {
143         return _domain_lengths;
144     }
145
146     public DomainLengths getDomainLengths( final DomainId domain_id ) {
147         return getDomainLengths().get( domain_id );
148     }
149
150     public List<DomainLengths> getDomainLengthsList() {
151         final List<DomainLengths> list = new ArrayList<DomainLengths>();
152         for( final DomainLengths l : getDomainLengths().values() ) {
153             list.add( l );
154         }
155         return list;
156     }
157
158     public DescriptiveStatistics getLengthStatistic( final DomainId domain_id, final Species species ) {
159         return getDomainLengths( domain_id ).getLengthStatistic( species );
160     }
161
162     public List<Species> getSpecies() {
163         return _species;
164     }
165 }