in progress
[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.protein.Domain;
36 import org.forester.protein.DomainId;
37 import org.forester.protein.Protein;
38 import org.forester.species.Species;
39 import org.forester.util.BasicDescriptiveStatistics;
40 import org.forester.util.DescriptiveStatistics;
41 import org.forester.util.ForesterUtil;
42
43 public class DomainLengthsTable {
44
45     private final static DecimalFormat       DF = new DecimalFormat( "#.0" );
46     final SortedMap<DomainId, DomainLengths> _domain_lengths;
47     final List<Species>                      _species;
48
49     public DomainLengthsTable() {
50         _domain_lengths = new TreeMap<DomainId, DomainLengths>();
51         _species = new ArrayList<Species>();
52     }
53
54     private void addDomainLengths( final DomainLengths domain_lengths ) {
55         if ( getDomainLengths().containsKey( domain_lengths.getDomainId() ) ) {
56             throw new IllegalArgumentException( "domain lengths for [" + domain_lengths.getDomainId()
57                     + "] already added" );
58         }
59         getDomainLengths().put( domain_lengths.getDomainId(), domain_lengths );
60     }
61
62     private void addLength( final DomainId domain_id, final Species species, final int domain_length ) {
63         if ( !getDomainLengths().containsKey( domain_id ) ) {
64             addDomainLengths( new DomainLengths( domain_id ) );
65         }
66         getDomainLengths().get( domain_id ).addLength( species, domain_length );
67     }
68
69     public void addLengths( final List<Protein> protein_list ) {
70         for( final Protein protein : protein_list ) {
71             final Species species = protein.getSpecies();
72             if ( !_species.contains( species ) ) {
73                 _species.add( species );
74             }
75             for( final Domain domain : protein.getProteinDomains() ) {
76                 addLength( domain.getDomainId(), species, ( domain.getTo() - domain.getFrom() ) + 1 );
77             }
78         }
79     }
80
81     public DescriptiveStatistics calculateMeanBasedStatisticsForAllSpecies() {
82         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
83         for( final Species species : getSpecies() ) {
84             final DescriptiveStatistics stats_per_species = calculateMeanBasedStatisticsForSpecies( species );
85             stats.addValue( stats_per_species.arithmeticMean() );
86         }
87         return stats;
88     }
89
90     public DescriptiveStatistics calculateMeanBasedStatisticsForDomain( final DomainId domain_id ) {
91         return getDomainLengths( domain_id ).calculateMeanBasedStatistics();
92     }
93
94     public DescriptiveStatistics calculateMeanBasedStatisticsForSpecies( final Species species ) {
95         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
96         for( final DomainLengths l : getDomainLengths().values() ) {
97             if ( l.isHasLengthStatistic( species ) ) {
98                 stats.addValue( l.getLengthStatistic( species ).arithmeticMean() );
99             }
100         }
101         return stats;
102     }
103
104     public StringBuilder createMeanBasedStatisticsPerSpeciesTable() {
105         final StringBuilder sb = new StringBuilder();
106         sb.append( "SPECIES" );
107         sb.append( "\t" );
108         sb.append( "MEAN" );
109         sb.append( "\t" );
110         sb.append( "SD" );
111         sb.append( "\t" );
112         sb.append( "MIN" );
113         sb.append( "\t" );
114         sb.append( "MAX" );
115         sb.append( "\t" );
116         sb.append( "MEDIAN" );
117         sb.append( ForesterUtil.LINE_SEPARATOR );
118         for( final Species species : getSpecies() ) {
119             final DescriptiveStatistics stats = calculateMeanBasedStatisticsForSpecies( species );
120             sb.append( species );
121             sb.append( "\t" );
122             sb.append( DF.format( stats.arithmeticMean() ) );
123             sb.append( "\t" );
124             try {
125                 sb.append( DF.format( stats.sampleStandardDeviation() ) );
126             }
127             catch ( final ArithmeticException e ) {
128                 sb.append( "" );
129             }
130             sb.append( "\t" );
131             sb.append( DF.format( stats.getMin() ) );
132             sb.append( "\t" );
133             sb.append( DF.format( stats.getMax() ) );
134             sb.append( "\t" );
135             try {
136                 sb.append( DF.format( stats.median() ) );
137             }
138             catch ( final ArithmeticException e ) {
139                 sb.append( "" );
140             }
141             sb.append( ForesterUtil.LINE_SEPARATOR );
142         }
143         return sb;
144     }
145
146     private SortedMap<DomainId, DomainLengths> getDomainLengths() {
147         return _domain_lengths;
148     }
149
150     public DomainLengths getDomainLengths( final DomainId domain_id ) {
151         return getDomainLengths().get( domain_id );
152     }
153
154     public List<DomainLengths> getDomainLengthsList() {
155         final List<DomainLengths> list = new ArrayList<DomainLengths>();
156         for( final DomainLengths l : getDomainLengths().values() ) {
157             list.add( l );
158         }
159         return list;
160     }
161
162     public DescriptiveStatistics getLengthStatistic( final DomainId domain_id, final Species species ) {
163         return getDomainLengths( domain_id ).getLengthStatistic( species );
164     }
165
166     public List<Species> getSpecies() {
167         return _species;
168     }
169 }