3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2010 Christian M. Zmasek
7 // Copyright (C) 2008-2010 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
27 package org.forester.surfacing;
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;
35 import org.forester.util.BasicDescriptiveStatistics;
36 import org.forester.util.DescriptiveStatistics;
37 import org.forester.util.ForesterUtil;
39 public class DomainLengthsTable {
41 private final static DecimalFormat DF = new DecimalFormat( "#.0" );
42 final SortedMap<DomainId, DomainLengths> _domain_lengths;
43 final List<Species> _species;
45 public DomainLengthsTable() {
46 _domain_lengths = new TreeMap<DomainId, DomainLengths>();
47 _species = new ArrayList<Species>();
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" );
55 getDomainLengths().put( domain_lengths.getDomainId(), domain_lengths );
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 ) );
62 getDomainLengths().get( domain_id ).addLength( species, domain_length );
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 );
71 for( final Domain domain : protein.getProteinDomains() ) {
72 addLength( domain.getDomainId(), species, ( domain.getTo() - domain.getFrom() ) + 1 );
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() );
86 public DescriptiveStatistics calculateMeanBasedStatisticsForDomain( final DomainId domain_id ) {
87 return getDomainLengths( domain_id ).calculateMeanBasedStatistics();
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() );
100 public StringBuilder createMeanBasedStatisticsPerSpeciesTable() {
101 final StringBuilder sb = new StringBuilder();
102 sb.append( "SPECIES" );
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 );
118 sb.append( DF.format( stats.arithmeticMean() ) );
121 sb.append( DF.format( stats.sampleStandardDeviation() ) );
123 catch ( final ArithmeticException e ) {
127 sb.append( DF.format( stats.getMin() ) );
129 sb.append( DF.format( stats.getMax() ) );
132 sb.append( DF.format( stats.median() ) );
134 catch ( final ArithmeticException e ) {
137 sb.append( ForesterUtil.LINE_SEPARATOR );
142 private SortedMap<DomainId, DomainLengths> getDomainLengths() {
143 return _domain_lengths;
146 public DomainLengths getDomainLengths( final DomainId domain_id ) {
147 return getDomainLengths().get( domain_id );
150 public List<DomainLengths> getDomainLengthsList() {
151 final List<DomainLengths> list = new ArrayList<DomainLengths>();
152 for( final DomainLengths l : getDomainLengths().values() ) {
158 public DescriptiveStatistics getLengthStatistic( final DomainId domain_id, final Species species ) {
159 return getDomainLengths( domain_id ).getLengthStatistic( species );
162 public List<Species> getSpecies() {