inprogress
[jalview.git] / forester / java / src / org / forester / msa_compactor / MsaCompactor.java
index 0356dec..fb0d0fe 100644 (file)
@@ -9,19 +9,27 @@ import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Comparator;
 import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
+import org.forester.archaeopteryx.Archaeopteryx;
+import org.forester.evoinference.distance.NeighborJoiningF;
+import org.forester.evoinference.distance.PairwiseDistanceCalculator;
+import org.forester.evoinference.distance.PairwiseDistanceCalculator.PWD_DISTANCE_METHOD;
+import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
+import org.forester.evoinference.tools.BootstrapResampler;
+import org.forester.msa.BasicMsa;
 import org.forester.msa.Mafft;
 import org.forester.msa.Msa;
 import org.forester.msa.Msa.MSA_FORMAT;
 import org.forester.msa.MsaInferrer;
 import org.forester.msa.MsaMethods;
+import org.forester.msa.ResampleableMsa;
+import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.sequence.Sequence;
-import org.forester.util.BasicDescriptiveStatistics;
-import org.forester.util.DescriptiveStatistics;
+import org.forester.tools.ConfidenceAssessor;
 import org.forester.util.ForesterUtil;
 
 public class MsaCompactor {
@@ -66,26 +74,6 @@ public class MsaCompactor {
         return ng;
     }
 
-    private final DescriptiveStatistics[] calcGapContribtionsX( final boolean normalize_for_effective_seq_length ) {
-        final double gappiness[] = calcGappiness();
-        final DescriptiveStatistics stats[] = new DescriptiveStatistics[ _msa.getNumberOfSequences() ];
-        for( int row = 0; row < _msa.getNumberOfSequences(); ++row ) {
-            stats[ row ] = new BasicDescriptiveStatistics( _msa.getIdentifier( row ) );
-            final double l = calculateEffectiveLengthRatio( row );
-            for( int col = 0; col < _msa.getLength(); ++col ) {
-                if ( !_msa.isGapAt( row, col ) ) {
-                    if ( normalize_for_effective_seq_length ) {
-                        stats[ row ].addValue( gappiness[ col ] / l );
-                    }
-                    else {
-                        stats[ row ].addValue( gappiness[ col ] );
-                    }
-                }
-            }
-        }
-        return stats;
-    }
-
     private final GapContribution[] calcGapContribtions( final boolean normalize_for_effective_seq_length ) {
         final double gappiness[] = calcGappiness();
         final GapContribution stats[] = new GapContribution[ _msa.getNumberOfSequences() ];
@@ -97,10 +85,10 @@ public class MsaCompactor {
                 }
             }
             if ( normalize_for_effective_seq_length ) {
-                stats[ row ].divideValue( calculateEffectiveLengthRatio( row ) );
+                stats[ row ].divideValue( calcNonGapResidues( _msa.getSequence( row ) ) );
             }
             else {
-                // 
+                stats[ row ].divideValue( _msa.getLength() );
             }
         }
         return stats;
@@ -136,17 +124,13 @@ public class MsaCompactor {
         return gappiness;
     }
 
-    private double calculateEffectiveLengthRatio( final int row ) {
-        return ( double ) calcNonGapResidues( _msa.getSequence( row ) ) / _msa.getLength();
-    }
-
     final private void mafft() throws IOException, InterruptedException {
         final MsaInferrer mafft = Mafft
                 .createInstance( "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-7.130-without-extensions/scripts/mafft" );
         final List<String> opts = new ArrayList<String>();
-        // opts.add( "--maxiterate" );
-        // opts.add( "1000" );
-        // opts.add( "--localpair" );
+        opts.add( "--maxiterate" );
+        opts.add( "1000" );
+        opts.add( "--localpair" );
         opts.add( "--quiet" );
         _msa = mafft.infer( _msa.asSequenceList(), opts );
     }
@@ -229,10 +213,62 @@ public class MsaCompactor {
         }
     }
 
+    Phylogeny pi( final String matrix ) {
+        final Phylogeny master_phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, true, matrix );
+        final int seed = 15;
+        final int n = 100;
+        final ResampleableMsa resampleable_msa = new ResampleableMsa( ( BasicMsa ) _msa );
+        final int[][] resampled_column_positions = BootstrapResampler.createResampledColumnPositions( _msa.getLength(),
+                                                                                                      n,
+                                                                                                      seed );
+        final Phylogeny[] eval_phys = new Phylogeny[ n ];
+        for( int i = 0; i < n; ++i ) {
+            resampleable_msa.resample( resampled_column_positions[ i ] );
+            eval_phys[ i ] = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, resampleable_msa, false, null );
+        }
+        ConfidenceAssessor.evaluate( "bootstrap", eval_phys, master_phy, true, 1 );
+        PhylogenyMethods.extractFastaInformation( master_phy );
+        return master_phy;
+    }
+
+    private Phylogeny inferNJphylogeny( final PWD_DISTANCE_METHOD pwd_distance_method,
+                                        final Msa msa,
+                                        final boolean write_matrix,
+                                        final String matrix_name ) {
+        BasicSymmetricalDistanceMatrix m = null;
+        switch ( pwd_distance_method ) {
+            case KIMURA_DISTANCE:
+                m = PairwiseDistanceCalculator.calcKimuraDistances( msa );
+                break;
+            case POISSON_DISTANCE:
+                m = PairwiseDistanceCalculator.calcPoissonDistances( msa );
+                break;
+            case FRACTIONAL_DISSIMILARITY:
+                m = PairwiseDistanceCalculator.calcFractionalDissimilarities( msa );
+                break;
+            default:
+                throw new IllegalArgumentException( "invalid pwd method" );
+        }
+        if ( write_matrix ) {
+            try {
+                m.write( ForesterUtil.createBufferedWriter( matrix_name ) );
+            }
+            catch ( final IOException e ) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+        final NeighborJoiningF nj = NeighborJoiningF.createInstance( false, 5 );
+        final Phylogeny phy = nj.execute( m );
+        return phy;
+    }
+
     final private void removeWorstOffenders( final int to_remove,
                                              final int step,
                                              final boolean realign,
                                              final boolean norm ) throws IOException, InterruptedException {
+        final Phylogeny a = pi( "a.pwd" );
+        Archaeopteryx.createApplication( a );
         final GapContribution stats[] = calcGapContribtionsStats( norm );
         final List<String> to_remove_ids = new ArrayList<String>();
         for( int j = 0; j < to_remove; ++j ) {
@@ -254,6 +290,8 @@ public class MsaCompactor {
         if ( realign ) {
             mafft();
         }
+        final Phylogeny b = pi( "b.pwd" );
+        Archaeopteryx.createApplication( b );
     }
 
     final private void writeMsa( final String outfile, final MSA_FORMAT format ) throws IOException {
@@ -292,51 +330,4 @@ public class MsaCompactor {
         mc.removeWorstOffenders( worst_offenders_to_remove, 1, realign, norm );
         return mc;
     }
-
-    public static enum SORT_BY {
-        MAX, MEAN, MEDIAN;
-    }
-
-    final static class DescriptiveStatisticsComparator implements Comparator<DescriptiveStatistics> {
-
-        final private boolean _ascending;
-        final private SORT_BY _sort_by;
-
-        public DescriptiveStatisticsComparator( final boolean ascending, final SORT_BY sort_by ) {
-            _ascending = ascending;
-            _sort_by = sort_by;
-        }
-
-        @Override
-        public final int compare( final DescriptiveStatistics s0, final DescriptiveStatistics s1 ) {
-            switch ( _sort_by ) {
-                case MAX:
-                    if ( s0.getMax() < s1.getMax() ) {
-                        return _ascending ? -1 : 1;
-                    }
-                    else if ( s0.getMax() > s1.getMax() ) {
-                        return _ascending ? 1 : -1;
-                    }
-                    return 0;
-                case MEAN:
-                    if ( s0.arithmeticMean() < s1.arithmeticMean() ) {
-                        return _ascending ? -1 : 1;
-                    }
-                    else if ( s0.arithmeticMean() > s1.arithmeticMean() ) {
-                        return _ascending ? 1 : -1;
-                    }
-                    return 0;
-                case MEDIAN:
-                    if ( s0.median() < s1.median() ) {
-                        return _ascending ? -1 : 1;
-                    }
-                    else if ( s0.median() > s1.median() ) {
-                        return _ascending ? 1 : -1;
-                    }
-                    return 0;
-                default:
-                    return 0;
-            }
-        }
-    }
 }