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.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;
43 public class DomainLengthsTable {
45 private final static DecimalFormat DF = new DecimalFormat( "#.0" );
46 final SortedMap<DomainId, DomainLengths> _domain_lengths;
47 final List<Species> _species;
49 public DomainLengthsTable() {
50 _domain_lengths = new TreeMap<DomainId, DomainLengths>();
51 _species = new ArrayList<Species>();
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" );
59 getDomainLengths().put( domain_lengths.getDomainId(), domain_lengths );
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 ) );
66 getDomainLengths().get( domain_id ).addLength( species, domain_length );
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 );
75 for( final Domain domain : protein.getProteinDomains() ) {
76 addLength( domain.getDomainId(), species, ( domain.getTo() - domain.getFrom() ) + 1 );
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() );
90 public DescriptiveStatistics calculateMeanBasedStatisticsForDomain( final DomainId domain_id ) {
91 return getDomainLengths( domain_id ).calculateMeanBasedStatistics();
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() );
104 public StringBuilder createMeanBasedStatisticsPerSpeciesTable() {
105 final StringBuilder sb = new StringBuilder();
106 sb.append( "SPECIES" );
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 );
122 sb.append( DF.format( stats.arithmeticMean() ) );
125 sb.append( DF.format( stats.sampleStandardDeviation() ) );
127 catch ( final ArithmeticException e ) {
131 sb.append( DF.format( stats.getMin() ) );
133 sb.append( DF.format( stats.getMax() ) );
136 sb.append( DF.format( stats.median() ) );
138 catch ( final ArithmeticException e ) {
141 sb.append( ForesterUtil.LINE_SEPARATOR );
146 private SortedMap<DomainId, DomainLengths> getDomainLengths() {
147 return _domain_lengths;
150 public DomainLengths getDomainLengths( final DomainId domain_id ) {
151 return getDomainLengths().get( domain_id );
154 public List<DomainLengths> getDomainLengthsList() {
155 final List<DomainLengths> list = new ArrayList<DomainLengths>();
156 for( final DomainLengths l : getDomainLengths().values() ) {
162 public DescriptiveStatistics getLengthStatistic( final DomainId domain_id, final Species species ) {
163 return getDomainLengths( domain_id ).getLengthStatistic( species );
166 public List<Species> getSpecies() {