inprogress
[jalview.git] / forester / java / src / org / forester / msa_compactor / MsaCompactor.java
index ef15161..0b7f8ed 100644 (file)
@@ -24,6 +24,7 @@
 
 package org.forester.msa_compactor;
 
+import java.awt.Color;
 import java.io.File;
 import java.io.IOException;
 import java.io.Writer;
@@ -57,10 +58,15 @@ import org.forester.msa.MsaMethods;
 import org.forester.msa.ResampleableMsa;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.data.NodeVisualData;
+import org.forester.phylogeny.data.NodeVisualData.NodeFill;
+import org.forester.phylogeny.data.NodeVisualData.NodeShape;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.sequence.Sequence;
 import org.forester.tools.ConfidenceAssessor;
+import org.forester.util.BasicDescriptiveStatistics;
 import org.forester.util.ForesterUtil;
 
 public class MsaCompactor {
@@ -108,21 +114,22 @@ public class MsaCompactor {
         for( final GapContribution gap_gontribution : stats ) {
             to_remove_ids.add( gap_gontribution.getId() );
         }
-        if ( _phylogentic_inference ) {
-            System.out.println( "calculating phylegentic tree..." );
-            System.out.println();
-            pi();
-        }
         if ( !_realign ) {
             _step = -1;
         }
-        printTableHeader();
         int x = ForesterUtil.roundToInt( _msa.getNumberOfSequences() / 20.0 );
         if ( x < 1 ) {
             x = 1;
         }
-        MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        MsaProperties msa_prop = new MsaProperties( _msa, "", _report_aln_mean_identity );
         msa_props.add( msa_prop );
+        Phylogeny phy = null;
+        if ( _phylogentic_inference ) {
+            System.out.println( "calculating phylogentic tree..." );
+            System.out.println();
+            phy = calcTree();
+        }
+        printTableHeader();
         printMsaProperties( "", msa_prop );
         System.out.println();
         int i = 0;
@@ -132,7 +139,7 @@ public class MsaCompactor {
             if ( realign && isPrintMsaStatsWriteOutfileAndRealign( i ) ) {
                 removeGapColumns();
                 realignWithMafft();
-                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity );
                 msa_props.add( msa_prop );
                 printMsaProperties( id, msa_prop );
                 System.out.print( "(realigned)" );
@@ -140,13 +147,20 @@ public class MsaCompactor {
             }
             else if ( isPrintMsaStats( i ) ) {
                 removeGapColumns();
-                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity );
                 msa_props.add( msa_prop );
                 printMsaProperties( id, msa_prop );
                 System.out.println();
             }
             ++i;
         }
+        if ( _phylogentic_inference ) {
+            final Phylogeny p2 = phy.copy();
+            decorateTree( p2, to_remove_ids );
+            decorateTree2( phy, msa_props );
+            displayTree( p2 );
+            displayTree( phy );
+        }
         return msa_props;
     }
 
@@ -163,11 +177,11 @@ public class MsaCompactor {
     }
 
     public final void removeSequencesByMinimalLength( final int min_effective_length ) {
-        printMsaProperties( "", new MsaProperties( _msa, _report_aln_mean_identity ) );
+        printMsaProperties( "", new MsaProperties( _msa, "", _report_aln_mean_identity ) );
         System.out.println();
         _msa = DeleteableMsa.createInstance( MsaMethods.removeSequencesByMinimalLength( _msa, min_effective_length ) );
         removeGapColumns();
-        printMsaProperties( "", new MsaProperties( _msa, _report_aln_mean_identity ) );
+        printMsaProperties( "", new MsaProperties( _msa, "", _report_aln_mean_identity ) );
         System.out.println();
     }
 
@@ -180,7 +194,7 @@ public class MsaCompactor {
             to_remove_ids.add( gap_gontribution.getId() );
         }
         printTableHeader();
-        MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        MsaProperties msa_prop = new MsaProperties( _msa, "", _report_aln_mean_identity );
         msa_props.add( msa_prop );
         printMsaProperties( "", msa_prop );
         System.out.println();
@@ -197,7 +211,7 @@ public class MsaCompactor {
                 System.out.println();
             }
             else if ( isPrintMsaStats( i ) ) {
-                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity );
                 msa_props.add( msa_prop );
                 printMsaProperties( id, msa_prop );
                 System.out.println();
@@ -220,7 +234,7 @@ public class MsaCompactor {
             to_remove_ids.add( gap_gontribution.getId() );
         }
         printTableHeader();
-        MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        MsaProperties msa_prop = new MsaProperties( _msa, "", _report_aln_mean_identity );
         msa_props.add( msa_prop );
         printMsaProperties( "", msa_prop );
         System.out.println();
@@ -237,7 +251,7 @@ public class MsaCompactor {
                 System.out.println();
             }
             else if ( isPrintMsaStats( i ) ) {
-                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity );
                 printMsaProperties( id, msa_prop );
                 msa_props.add( msa_prop );
                 System.out.println();
@@ -262,7 +276,7 @@ public class MsaCompactor {
             _removed_seq_ids.add( stats[ j ].getId() );
         }
         printTableHeader();
-        MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        MsaProperties msa_prop = new MsaProperties( _msa, "", _report_aln_mean_identity );
         msa_props.add( msa_prop );
         printMsaProperties( "", msa_prop );
         System.out.println();
@@ -278,7 +292,7 @@ public class MsaCompactor {
                 System.out.println();
             }
             else if ( isPrintMsaStats( i ) ) {
-                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity );
                 msa_props.add( msa_prop );
                 printMsaProperties( id, msa_prop );
                 System.out.println();
@@ -498,9 +512,10 @@ public class MsaCompactor {
         return master_phy;
     }
 
-    private final Phylogeny pi() {
+    public final Phylogeny calcTree() {
         final Phylogeny phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, false, "" );
         PhylogenyMethods.midpointRoot( phy );
+        PhylogenyMethods.orderAppearance( phy.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.NODE_NAME );
         final boolean x = PhylogenyMethods.extractFastaInformation( phy );
         if ( !x ) {
             final PhylogenyNodeIterator it = phy.iteratorExternalForward();
@@ -517,10 +532,81 @@ public class MsaCompactor {
                 }
             }
         }
+        return phy;
+    }
+
+    public final void decorateTree( final Phylogeny phy, final List<String> to_remove_ids ) {
+        for( int i = 0; i < to_remove_ids.size(); ++i ) {
+            final String id = to_remove_ids.get( i );
+            final PhylogenyNode n = phy.getNode( id );
+            n.setName( n.getName() + " [" + ( i + 1 ) + "]" );
+            final NodeVisualData vis = new NodeVisualData();
+            vis.setFillType( NodeFill.SOLID );
+            vis.setShape( NodeShape.RECTANGLE );
+            vis.setSize( 6 );
+            vis.setNodeColor( new Color( i > 255 ? 0 : 255 - i, 0, 0 ) );
+            n.getNodeData().setNodeVisualData( vis );
+        }
+    }
+
+    public final void decorateTree2( final Phylogeny phy, final List<MsaProperties> msa_props ) {
+        final BasicDescriptiveStatistics length_stats = new BasicDescriptiveStatistics();
+        for( int i = 0; i < msa_props.size(); ++i ) {
+            final MsaProperties msa_prop = msa_props.get( i );
+            final String id = msa_prop.getRemovedSeq();
+            if ( !ForesterUtil.isEmpty( id ) ) {
+                length_stats.addValue( msa_prop.getLength() );
+            }
+        }
+        final double mean = length_stats.arithmeticMean();
+        final double min = length_stats.getMin();
+        final double max = length_stats.getMax();
+        final Color min_color = new Color( 0, 255, 0 );
+        final Color max_color = new Color( 255, 0, 0 );
+        final Color mean_color = new Color( 255, 255, 0 );
+        final PhylogenyNodeIterator it = phy.iteratorExternalForward();
+        while ( it.hasNext() ) {
+            final NodeVisualData vis = new NodeVisualData();
+            vis.setFillType( NodeFill.SOLID );
+            vis.setShape( NodeShape.RECTANGLE );
+            vis.setSize( 6 );
+            vis.setNodeColor( min_color );
+            it.next().getNodeData().setNodeVisualData( vis );
+        }
+        for( int i = 0; i < msa_props.size(); ++i ) {
+            final MsaProperties msa_prop = msa_props.get( i );
+            final String id = msa_prop.getRemovedSeq();
+            if ( !ForesterUtil.isEmpty( id ) ) {
+                final PhylogenyNode n = phy.getNode( id );
+                n.setName( n.getName() + " [" + i + "]" );
+                n.getNodeData()
+                        .getNodeVisualData()
+                        .setNodeColor( ForesterUtil.calcColor( msa_prop.getLength(),
+                                                               min,
+                                                               max,
+                                                               mean,
+                                                               min_color,
+                                                               max_color,
+                                                               mean_color ) );
+            }
+        }
+    }
+
+    public final void displayTree( final Phylogeny phy ) {
         final Configuration config = new Configuration();
         config.setDisplayAsPhylogram( true );
+        config.setUseStyle( true );
+        config.setDisplayTaxonomyCode( false );
+        config.setDisplayTaxonomyCommonNames( false );
+        config.setDisplayTaxonomyScientificNames( false );
+        config.setDisplaySequenceNames( false );
+        config.setDisplaySequenceSymbols( false );
+        config.setDisplayGeneNames( false );
+        config.setShowScale( true );
+        config.setAddTaxonomyImagesCB( false );
+        config.setBaseFontSize( 9 );
+        config.setBaseFontFamilyName( "Arial" );
         Archaeopteryx.createApplication( phy, config, _infile_name );
-        return phy;
     }
 
     private final void printMsaProperties( final String id, final MsaProperties msa_properties ) {
@@ -537,7 +623,7 @@ public class MsaCompactor {
         if ( realign ) {
             realignWithMafft();
         }
-        final MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        final MsaProperties msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity );
         printMsaProperties( id, msa_prop );
         final String s = writeOutfile();
         System.out.print( "-> " + s + ( realign ? "\t(realigned)" : "" ) );