/** * Helper function to clone a forester symmetrical DistanceMatrix. * * @param distM * forester symmetrical DistanceMatrix * @return identical copy of the forester symmetrical DistanceMatrix */ public static BasicSymmetricalDistanceMatrix cloneDM( BasicSymmetricalDistanceMatrix distM) { int n = distM.getSize(); BasicSymmetricalDistanceMatrix cloneDM = new BasicSymmetricalDistanceMatrix(n); for (int i = 0; i < n; i++) { cloneDM.setIdentifier(i, distM.getIdentifier(i)); for (int j = i + 1; j < n; j++) { cloneDM.setValue(i, j, distM.getValue(i, j)); } } return cloneDM; }
/** * Returns the distance matrix used to construct this guide tree. The scores have been normalized. * * @return the distance matrix used to construct this guide tree */ public double[][] getDistanceMatrix() { double[][] matrix = new double[distances.getSize()][distances.getSize()]; for (int i = 0; i < matrix.length; i++) { for (int j = i+1; j < matrix.length; j++) { matrix[i][j] = matrix[j][i] = distances.getValue(i, j); } } return matrix; }
/** * Creates a guide tree for use during progressive multiple sequence alignment. * * @param sequences the {@link List} of {@link Sequence}s to align * @param scorers list of sequence pair scorers, one for each pair of sequences given */ public GuideTree(List<S> sequences, List<PairwiseSequenceScorer<S, C>> scorers) { this.sequences = Collections.unmodifiableList(sequences); this.scorers = Collections.unmodifiableList(scorers); distances = new BasicSymmetricalDistanceMatrix(sequences.size()); for (int i = 0, n = 0; i < sequences.size(); i++) { AccessionID id = sequences.get(i).getAccession(); String str = (id == null) ? Integer.toString(i + 1) : id.getID(); distances.setIdentifier(i, str); for (int j = i+1; j < sequences.size(); j++) { double dist = scorers.get(n++).getDistance(); distances.setValue(i, j, dist); } } BasicSymmetricalDistanceMatrix distclone = ForesterWrapper.cloneDM(distances); Phylogeny phylogeny = TreeConstructor.distanceTree(distclone, TreeConstructorType.NJ); newick = phylogeny.toString(); root = new Node(phylogeny.getRoot(), null); }
public final void write( final Writer w ) throws IOException { w.write( " " ); w.write( getSize() + "" ); w.write( ForesterUtil.LINE_SEPARATOR ); for( int row = 0; row < getSize(); ++row ) { if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) { w.write( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ).toString() ); w.write( ' ' ); w.write( ' ' ); } else { throw new IllegalFormatUseException( "Phylip format does not allow empty identifiers" ); } for( int col = 0; col < getSize(); ++col ) { w.write( PHYLIP_FORMATTER.format( getValue( col, row ) ) ); if ( col < ( getSize() - 1 ) ) { w.write( ' ' ); w.write( ' ' ); } } if ( row < ( getSize() - 1 ) ) { w.write( ForesterUtil.LINE_SEPARATOR ); } } }
private BasicSymmetricalDistanceMatrix calcKimuraDistances() { final int s = _msa.getNumberOfSequences(); final BasicSymmetricalDistanceMatrix d = new BasicSymmetricalDistanceMatrix( s ); copyIdentifiers( s, d ); calcKimuraDistances( s, d ); return d; }
private static boolean testNeighborJoiningR() { try { final NeighborJoiningR nj0 = NeighborJoiningR.createInstance(); final BasicSymmetricalDistanceMatrix m0 = new BasicSymmetricalDistanceMatrix( 4 ); m0.setIdentifier( 0, "A" ); m0.setIdentifier( 1, "B" ); m0.setIdentifier( 2, "C" ); m0.setIdentifier( 3, "D" ); m0.setRow( "5 ", 1 ); m0.setRow( "3 6 ", 2 ); m0.setRow( "7.5 10.5 5.5", 3 ); final Phylogeny p0 = nj0.execute( m0 ); p0.reRoot( p0.getNode( "D" ) ); final BasicSymmetricalDistanceMatrix m1 = new BasicSymmetricalDistanceMatrix( 6 ); m1.setRow( "5", 1 ); m1.setRow( "4 7", 2 ); m1.setRow( "7 10 7", 3 ); m1.setRow( "6 9 6 5", 4 ); m1.setRow( "8 11 8 9 8", 5 ); m1.setIdentifier( 0, "A" ); m1.setIdentifier( 1, "B" ); m1.setIdentifier( 2, "C" ); m1.setIdentifier( 3, "D" ); m1.setIdentifier( 4, "E" ); m1.setIdentifier( 5, "F" ); final NeighborJoiningR nj1 = NeighborJoiningR.createInstance(); final Phylogeny p1 = nj1.execute( m1 ); final BasicSymmetricalDistanceMatrix m2 = new BasicSymmetricalDistanceMatrix( 7 );
private static boolean testNeighborJoining( final boolean verbose ) { try { NeighborJoining nj = NeighborJoining.createInstance(); final BasicSymmetricalDistanceMatrix m0 = new BasicSymmetricalDistanceMatrix( 4 ); m0.setIdentifier( 0, "A" ); m0.setIdentifier( 1, "B" ); m0.setIdentifier( 2, "C" ); m0.setIdentifier( 3, "D" ); m0.setRow( "5 ", 1 ); m0.setRow( "3 6 ", 2 ); m0.setRow( "7.5 10.5 5.5", 3 ); final Phylogeny p0 = nj.execute( m0 ); p0.reRoot( p0.getNode( "D" ) ); final BasicSymmetricalDistanceMatrix m00 = new BasicSymmetricalDistanceMatrix( 4 ); m00.setIdentifier( 0, "A" ); m00.setIdentifier( 1, "B" ); m00.setIdentifier( 2, "C" ); m00.setIdentifier( 3, "D" ); m00.setRow( "2.01 ", 1 ); m00.setRow( "3 3.01 ", 2 ); m00.setRow( "3.01 3.02 1.01", 3 ); final Phylogeny p00 = nj.execute( m00 ); p00.reRoot( p00.getNode( "D" ) ); BasicSymmetricalDistanceMatrix m = new BasicSymmetricalDistanceMatrix( 6 ); m.setRow( "5", 1 ); m.setRow( "4 7", 2 ); m.setRow( "7 10 7", 3 ); m.setRow( "6 9 6 5", 4 );
public final void setRow( final String s, final int row ) { final StringTokenizer tk = new StringTokenizer( s ); int i = 0; while ( tk.hasMoreElements() ) { setValue( i, row, new Double( tk.nextToken() ).doubleValue() ); i++; } }
@Override public final int getIndex( final String identifier ) { for( int i = 0; i < _identifiers.length; i++ ) { if ( getIdentifier( i ).equals( identifier ) ) { return i; } } throw new IllegalArgumentException( "identifier [" + identifier + "] not found in distance matrix" ); }
private void copyIdentifiers( final int s, final BasicSymmetricalDistanceMatrix d ) { for( int i = 0; i < s; i++ ) { d.setIdentifier( i, _msa.getIdentifier( i ) ); } }
private static void timeNeighborJoining() { final NeighborJoiningR njr = NeighborJoiningR.createInstance(); for( int n = 3; n <= 10; ++n ) { final int x = ( int ) Math.pow( 2, n ); final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x ); mt.randomize( new Date().getTime() ); final long start_time = new Date().getTime(); njr.execute( mt ); System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" ); } final NeighborJoiningF njf = NeighborJoiningF.createInstance(); for( int n = 3; n <= 10; ++n ) { final int x = ( int ) Math.pow( 2, n ); final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x ); mt.randomize( new Date().getTime() ); final long start_time = new Date().getTime(); njf.execute( mt ); System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" ); } final NeighborJoining nj = NeighborJoining.createInstance(); for( int n = 3; n <= 10; ++n ) { final int x = ( int ) Math.pow( 2, n ); final BasicSymmetricalDistanceMatrix mt = new BasicSymmetricalDistanceMatrix( x ); mt.randomize( new Date().getTime() ); final long start_time = new Date().getTime(); nj.execute( mt ); System.out.println( "Size: " + x + " -> " + ( new Date().getTime() - start_time ) + "ms" ); } } }
/** * Creates a guide tree for use during progressive multiple sequence alignment. * * @param sequences the {@link List} of {@link Sequence}s to align * @param scorers list of sequence pair scorers, one for each pair of sequences given */ public GuideTree(List<S> sequences, List<PairwiseSequenceScorer<S, C>> scorers) { this.sequences = Collections.unmodifiableList(sequences); this.scorers = Collections.unmodifiableList(scorers); distances = new BasicSymmetricalDistanceMatrix(sequences.size()); for (int i = 0, n = 0; i < sequences.size(); i++) { AccessionID id = sequences.get(i).getAccession(); String str = (id == null) ? Integer.toString(i + 1) : id.getID(); distances.setIdentifier(i, str); for (int j = i+1; j < sequences.size(); j++) { double dist = scorers.get(n++).getDistance(); distances.setValue(i, j, dist); } } BasicSymmetricalDistanceMatrix distclone = ForesterWrapper.cloneDM(distances); Phylogeny phylogeny = TreeConstructor.distanceTree(distclone, TreeConstructorType.NJ); newick = phylogeny.toString(); root = new Node(phylogeny.getRoot(), null); }
sb.append( ' ' ); sb.append( ' ' ); sb.append( getSize() ); sb.append( ForesterUtil.LINE_SEPARATOR ); for( int row = 0; row < getSize(); ++row ) { if ( !ForesterUtil.isEmpty( getIdentifier( row ) ) ) { sb.append( ForesterUtil.pad( getIdentifier( row ), 10, ' ', false ) ); sb.append( ' ' ); sb.append( ' ' ); for( int col = 0; col < getSize(); ++col ) { sb.append( PHYLIP_FORMATTER.format( getValue( col, row ) ) ); if ( col < ( getSize() - 1 ) ) { sb.append( ' ' ); sb.append( ' ' ); if ( row < ( getSize() - 1 ) ) { sb.append( ForesterUtil.LINE_SEPARATOR );
private BasicSymmetricalDistanceMatrix calcPoissonDistances() { final int s = _msa.getNumberOfSequences(); final BasicSymmetricalDistanceMatrix d = new BasicSymmetricalDistanceMatrix( s ); copyIdentifiers( s, d ); calcPoissonDistances( s, d ); return d; }
/** * Returns the distance matrix used to construct this guide tree. The scores have been normalized. * * @return the distance matrix used to construct this guide tree */ public double[][] getDistanceMatrix() { double[][] matrix = new double[distances.getSize()][distances.getSize()]; for (int i = 0; i < matrix.length; i++) { for (int j = i+1; j < matrix.length; j++) { matrix[i][j] = matrix[j][i] = distances.getValue(i, j); } } return matrix; }
private void calcKimuraDistances( final int s, final BasicSymmetricalDistanceMatrix d ) { for( int i = 1; i < s; i++ ) { for( int j = 0; j < i; j++ ) { d.setValue( i, j, calcKimuraDistance( i, j ) ); } } }
private final void initExternalNodes() { _external_nodes = new PhylogenyNode[ _n ]; String id; for( int i = 0; i < _n; ++i ) { _external_nodes[ i ] = new PhylogenyNode(); id = _d.getIdentifier( i ); if ( id != null ) { _external_nodes[ i ].setName( id ); } else { _external_nodes[ i ].setName( Integer.toString( i ) ); } _mappings[ i ] = i; } }
} else alreadySeen.put(id, 1); rmsdDist.setIdentifier(i, id);