2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.evoinference.tools;
28 import java.util.Random;
30 import org.forester.msa.BasicMsa;
31 import org.forester.msa.Msa;
33 public class BootstrapResampler {
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 ) );
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" );
45 if ( msa.getNumberOfSequences() < 1 ) {
46 throw new IllegalArgumentException( "Attempt to bootstrap resample empty multiple sequence alignment" );
49 throw new IllegalArgumentException( "Number of bootstrap resamples cannot be zero or negative" );
53 private static void preconditionCheck( final int length, final int n ) {
55 throw new IllegalArgumentException( "Msa length cannot be smaller than two for bootstrap resampling" );
58 throw new IllegalArgumentException( "Number of bootstrap resamples cannot be zero or negative" );
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() );
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 ) );
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 );