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: https://sites.google.com/site/cmzmasek/home/software/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.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;
42 public class DomainLengthsTable {
44 private final static DecimalFormat DF = new DecimalFormat( "#.0" );
45 final SortedMap<String, DomainLengths> _domain_lengths;
46 final List<Species> _species;
48 public DomainLengthsTable() {
49 _domain_lengths = new TreeMap<String, DomainLengths>();
50 _species = new ArrayList<Species>();
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" );
58 getDomainLengths().put( domain_lengths.getDomainId(), domain_lengths );
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 ) );
65 getDomainLengths().get( domain_id ).addLength( species, domain_length );
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 );
74 for( final Domain domain : protein.getProteinDomains() ) {
75 addLength( domain.getDomainId(), species, ( domain.getTo() - domain.getFrom() ) + 1 );
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() );
89 public DescriptiveStatistics calculateMeanBasedStatisticsForDomain( final String domain_id ) {
90 return getDomainLengths( domain_id ).calculateMeanBasedStatistics();
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() );
103 public StringBuilder createMeanBasedStatisticsPerSpeciesTable() {
104 final StringBuilder sb = new StringBuilder();
105 sb.append( "SPECIES" );
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 );
121 sb.append( DF.format( stats.arithmeticMean() ) );
124 sb.append( DF.format( stats.sampleStandardDeviation() ) );
126 catch ( final ArithmeticException e ) {
130 sb.append( DF.format( stats.getMin() ) );
132 sb.append( DF.format( stats.getMax() ) );
135 sb.append( DF.format( stats.median() ) );
137 catch ( final ArithmeticException e ) {
140 sb.append( ForesterUtil.LINE_SEPARATOR );
145 private SortedMap<String, DomainLengths> getDomainLengths() {
146 return _domain_lengths;
149 public DomainLengths getDomainLengths( final String domain_id ) {
150 return getDomainLengths().get( domain_id );
153 public List<DomainLengths> getDomainLengthsList() {
154 final List<DomainLengths> list = new ArrayList<DomainLengths>();
155 for( final DomainLengths l : getDomainLengths().values() ) {
161 public DescriptiveStatistics getLengthStatistic( final String domain_id, final Species species ) {
162 return getDomainLengths( domain_id ).getLengthStatistic( species );
165 public List<Species> getSpecies() {