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 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 );
59 for( final Domain domain : protein.getProteinDomains() ) {
60 addLength( domain.getDomainId(), species, ( domain.getTo() - domain.getFrom() ) + 1 );
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() );
74 public DescriptiveStatistics calculateMeanBasedStatisticsForDomain( final String domain_id ) {
75 return getDomainLengths( domain_id ).calculateMeanBasedStatistics();
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() );
88 public StringBuilder createMeanBasedStatisticsPerSpeciesTable() {
89 final StringBuilder sb = new StringBuilder();
90 sb.append( "SPECIES" );
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 );
106 sb.append( DF.format( stats.arithmeticMean() ) );
109 sb.append( DF.format( stats.sampleStandardDeviation() ) );
111 catch ( final ArithmeticException e ) {
115 sb.append( DF.format( stats.getMin() ) );
117 sb.append( DF.format( stats.getMax() ) );
120 sb.append( DF.format( stats.median() ) );
122 catch ( final ArithmeticException e ) {
125 sb.append( ForesterUtil.LINE_SEPARATOR );
130 public DomainLengths getDomainLengths( final String domain_id ) {
131 return getDomainLengths().get( domain_id );
134 public List<DomainLengths> getDomainLengthsList() {
135 final List<DomainLengths> list = new ArrayList<DomainLengths>();
136 for( final DomainLengths l : getDomainLengths().values() ) {
142 public DescriptiveStatistics getLengthStatistic( final String domain_id, final Species species ) {
143 return getDomainLengths( domain_id ).getLengthStatistic( species );
146 public List<Species> getSpecies() {
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" );
155 getDomainLengths().put( domain_lengths.getDomainId(), domain_lengths );
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 ) );
162 getDomainLengths().get( domain_id ).addLength( species, domain_length );
165 private SortedMap<String, DomainLengths> getDomainLengths() {
166 return _domain_lengths;