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