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