in progress
[jalview.git] / forester / java / src / org / forester / evoinference / tools / BootstrapResampler.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.evoinference.tools;
27
28 import java.util.Random;
29
30 import org.forester.msa.BasicMsa;
31 import org.forester.msa.Msa;
32
33 public class BootstrapResampler {
34
35     private static void copyIdentifiers( final Msa msa, final Msa new_msa ) {
36         for( int i = 0; i < msa.getNumberOfSequences(); ++i ) {
37             new_msa.setIdentifier( i, msa.getIdentifier( i ) );
38         }
39     }
40
41     private static void preconditionCheck( final Msa msa, final int n ) {
42         if ( msa.getLength() < 2 ) {
43             throw new IllegalArgumentException( "Msa length cannot be smaller than two for bootstrap resampling" );
44         }
45         if ( msa.getNumberOfSequences() < 1 ) {
46             throw new IllegalArgumentException( "Attempt to bootstrap resample empty multiple sequence alignment" );
47         }
48         if ( n < 1 ) {
49             throw new IllegalArgumentException( "Number of bootstrap resamples cannot be zero or negative" );
50         }
51     }
52
53     private static void preconditionCheck( final int length, final int n ) {
54         if ( length < 2 ) {
55             throw new IllegalArgumentException( "Msa length cannot be smaller than two for bootstrap resampling" );
56         }
57         if ( n < 1 ) {
58             throw new IllegalArgumentException( "Number of bootstrap resamples cannot be zero or negative" );
59         }
60     }
61
62     public static Msa[] resample( final Msa msa, final int n, final long seed ) {
63         preconditionCheck( msa, n );
64         final Random random = new Random( seed );
65         final Msa[] msas = new Msa[ n ];
66         for( int i = 0; i < n; ++i ) {
67             final Msa new_msa = new BasicMsa( msa.getNumberOfSequences(), msa.getLength(), msa.getType() );
68             msas[ i ] = new_msa;
69             copyIdentifiers( msa, new_msa );
70             for( int col = 0; col < msa.getLength(); ++col ) {
71                 final int random_col = random.nextInt( msa.getLength() );
72                 for( int row = 0; row < msa.getNumberOfSequences(); ++row ) {
73                     new_msa.setResidueAt( row, col, msa.getResidueAt( row, random_col ) );
74                 }
75             }
76         }
77         return msas;
78     }
79
80     public static int[][] createResampledColumnPositions( final int length, final int n, final long seed ) {
81         preconditionCheck( length, n );
82         final Random random = new Random( seed );
83         final int[][] columns = new int[ n ][ length ];
84         for( int i = 0; i < n; ++i ) {
85             for( int col = 0; col < length; ++col ) {
86                 columns[ i ][ col ] = random.nextInt( length );
87             }
88         }
89         return columns;
90     }
91 }