inprogress
[jalview.git] / forester / java / src / org / forester / msa_compactor / MsaCompactor.java
index 30b66d9..366d5bd 100644 (file)
@@ -41,7 +41,8 @@ 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.io.writers.SequenceWriter;
+import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
 import org.forester.msa.DeleteableMsa;
 import org.forester.msa.Mafft;
 import org.forester.msa.Msa;
@@ -60,8 +61,9 @@ public class MsaCompactor {
     final private static NumberFormat NF_3                      = new DecimalFormat( "#.###" );
     final private static NumberFormat NF_4                      = new DecimalFormat( "#.####" );
     private double                    _gap_ratio                = -1;
+    private final short               _longest_id_length;
     //
-    private final String              _maffts_opts              = "--auto";
+    private String                    _maffts_opts              = "--auto";
     private int                       _min_length               = -1;
     //
     private DeleteableMsa             _msa                      = null;
@@ -72,12 +74,11 @@ public class MsaCompactor {
     //
     private boolean                   _realign                  = false;
     private final SortedSet<String>   _removed_seq_ids;
-    private final File                _removed_seqs_out_base    = null;
+    private final ArrayList<Sequence> _removed_seqs;
+    private File                      _removed_seqs_out_base    = null;
     private boolean                   _report_aln_mean_identity = false;
     private int                       _step                     = -1;
     private int                       _step_for_diagnostics     = -1;
-    private final short               _longest_id_length;
-    private final ArrayList<Sequence> _removed_seqs;
     static {
         NF_4.setRoundingMode( RoundingMode.HALF_UP );
         NF_3.setRoundingMode( RoundingMode.HALF_UP );
@@ -90,6 +91,55 @@ public class MsaCompactor {
         _removed_seqs = new ArrayList<Sequence>();
     }
 
+    public final List<MsaProperties> chart( final int step, final boolean realign, final boolean norm )
+            throws IOException, InterruptedException {
+        final GapContribution stats[] = calcGapContribtionsStats( norm );
+        final List<String> to_remove_ids = new ArrayList<String>();
+        final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
+        for( final GapContribution gap_gontribution : stats ) {
+            to_remove_ids.add( gap_gontribution.getId() );
+        }
+        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 );
+        msa_props.add( msa_prop );
+        printMsaProperties( "", msa_prop );
+        System.out.println();
+        int i = 0;
+        while ( _msa.getNumberOfSequences() > x ) {
+            final String id = to_remove_ids.get( i );
+            _msa.deleteRow( id, false );
+            if ( realign && isPrintMsaStatsWriteOutfileAndRealign( i ) ) {
+                removeGapColumns();
+                realignWithMafft();
+                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_props.add( msa_prop );
+                printMsaProperties( id, msa_prop );
+                System.out.print( "(realigned)" );
+                System.out.println();
+            }
+            else if ( isPrintMsaStats( i ) ) {
+                removeGapColumns();
+                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_props.add( msa_prop );
+                printMsaProperties( id, msa_prop );
+                System.out.println();
+            }
+            ++i;
+        }
+        return msa_props;
+    }
+
+    final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
+        _msa.deleteGapColumns( max_allowed_gap_ratio );
+    }
+
     final public Msa getMsa() {
         return _msa;
     }
@@ -98,140 +148,144 @@ public class MsaCompactor {
         return _removed_seq_ids;
     }
 
-    public final void removeViaGapAverage( final double mean_gapiness ) throws IOException, InterruptedException {
+    public final void removeSequencesByMinimalLength( final int min_effective_length ) {
+        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 ) );
+        System.out.println();
+    }
+
+    public final List<MsaProperties> removeViaGapAverage( final double mean_gapiness ) throws IOException,
+            InterruptedException {
         final GapContribution stats[] = calcGapContribtionsStats( _norm );
         final List<String> to_remove_ids = new ArrayList<String>();
+        final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
         for( final GapContribution gap_gontribution : stats ) {
             to_remove_ids.add( gap_gontribution.getId() );
         }
         printTableHeader();
+        MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        msa_props.add( msa_prop );
+        printMsaProperties( "", msa_prop );
+        System.out.println();
         int i = 0;
         while ( MsaMethods.calcGapRatio( _msa ) > mean_gapiness ) {
             final String id = to_remove_ids.get( i );
             _removed_seq_ids.add( id );
-            final Sequence deleted = _msa.deleteRow( id );
+            final Sequence deleted = _msa.deleteRow( id, true );
             _removed_seqs.add( deleted );
             removeGapColumns();
-            if ( ( ( _step > 0 ) && ( ( ( i + 1 ) % _step ) == 0 ) )
-                    || ( MsaMethods.calcGapRatio( _msa ) <= mean_gapiness ) ) {
-                printMsaStatsWriteOutfileAndRealign( _realign, id );
+            if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( MsaMethods.calcGapRatio( _msa ) <= mean_gapiness ) ) {
+                msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id );
+                msa_props.add( msa_prop );
+                System.out.println();
             }
-            else {
-                final MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+            else if ( isPrintMsaStats( i ) ) {
+                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_props.add( msa_prop );
                 printMsaProperties( id, msa_prop );
+                System.out.println();
             }
-            System.out.println();
             ++i;
         }
-        final String msg = writeAndAlignRemovedSeqs();
-        System.out.println( msg );
+        if ( _removed_seqs_out_base != null ) {
+            final String msg = writeAndAlignRemovedSeqs();
+            System.out.println();
+            System.out.println( msg );
+        }
+        return msa_props;
     }
 
-    public void removeViaLength( final int length ) throws IOException, InterruptedException {
+    public List<MsaProperties> removeViaLength( final int length ) throws IOException, InterruptedException {
         final GapContribution stats[] = calcGapContribtionsStats( _norm );
         final List<String> to_remove_ids = new ArrayList<String>();
+        final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
         for( final GapContribution gap_gontribution : stats ) {
             to_remove_ids.add( gap_gontribution.getId() );
         }
         printTableHeader();
+        MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+        msa_props.add( msa_prop );
+        printMsaProperties( "", msa_prop );
+        System.out.println();
         int i = 0;
         while ( _msa.getLength() > length ) {
             final String id = to_remove_ids.get( i );
             _removed_seq_ids.add( id );
-            final Sequence deleted = _msa.deleteRow( id );
-            _removed_seqs.add( deleted );
-            removeGapColumns();
-            if ( ( ( _step > 0 ) && ( ( ( i + 1 ) % _step ) == 0 ) ) || ( _msa.getLength() <= length ) ) {
-                printMsaStatsWriteOutfileAndRealign( _realign, id );
-            }
-            final MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
-            printMsaProperties( id, msa_prop );
-            System.out.println();
-            ++i;
-        }
-        final String msg = writeAndAlignRemovedSeqs();
-        System.out.println( msg );
-    }
-
-    public final void removeWorstOffenders( final int to_remove ) throws IOException, InterruptedException {
-        final GapContribution stats[] = calcGapContribtionsStats( _norm );
-        final List<String> to_remove_ids = new ArrayList<String>();
-        for( int j = 0; j < to_remove; ++j ) {
-            to_remove_ids.add( stats[ j ].getId() );
-            _removed_seq_ids.add( stats[ j ].getId() );
-        }
-        printTableHeader();
-        for( int i = 0; i < to_remove_ids.size(); ++i ) {
-            final String id = to_remove_ids.get( i );
-            _removed_seq_ids.add( id );
-            final Sequence deleted = _msa.deleteRow( id );
+            final Sequence deleted = _msa.deleteRow( id, true );
             _removed_seqs.add( deleted );
             removeGapColumns();
-            if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( i == ( to_remove_ids.size() - 1 ) ) ) {
-                printMsaStatsWriteOutfileAndRealign( _realign, id );
+            if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( _msa.getLength() <= length ) ) {
+                msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id );
+                msa_props.add( msa_prop );
                 System.out.println();
             }
             else if ( isPrintMsaStats( i ) ) {
-                final MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
                 printMsaProperties( id, msa_prop );
+                msa_props.add( msa_prop );
                 System.out.println();
             }
+            ++i;
         }
-        final String msg = writeAndAlignRemovedSeqs();
-        System.out.println( msg );
+        if ( _removed_seqs_out_base != null ) {
+            final String msg = writeAndAlignRemovedSeqs();
+            System.out.println();
+            System.out.println( msg );
+        }
+        return msa_props;
     }
 
-    public final List<MsaProperties> chart( final int step, final boolean realign, final boolean norm )
-            throws IOException, InterruptedException {
-        final GapContribution stats[] = calcGapContribtionsStats( norm );
+    public final List<MsaProperties> removeWorstOffenders( final int to_remove ) throws IOException,
+            InterruptedException {
+        final GapContribution stats[] = calcGapContribtionsStats( _norm );
         final List<String> to_remove_ids = new ArrayList<String>();
         final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
-        for( final GapContribution gap_gontribution : stats ) {
-            to_remove_ids.add( gap_gontribution.getId() );
+        for( int j = 0; j < to_remove; ++j ) {
+            to_remove_ids.add( stats[ j ].getId() );
+            _removed_seq_ids.add( stats[ j ].getId() );
         }
         printTableHeader();
-        int i = 0;
-        final int x = ForesterUtil.roundToInt( _msa.getNumberOfSequences() / 20.0 );
         MsaProperties msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
         msa_props.add( msa_prop );
         printMsaProperties( "", msa_prop );
         System.out.println();
-        while ( _msa.getNumberOfSequences() > x ) {
+        for( int i = 0; i < to_remove_ids.size(); ++i ) {
             final String id = to_remove_ids.get( i );
-            _msa.deleteRow( id );
-            if ( realign && isPrintMsaStatsWriteOutfileAndRealign( i ) ) {
-                removeGapColumns();
-                realignWithMafft();
-                msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
+            _removed_seq_ids.add( id );
+            final Sequence deleted = _msa.deleteRow( id, true );
+            _removed_seqs.add( deleted );
+            removeGapColumns();
+            if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( i == ( to_remove_ids.size() - 1 ) ) ) {
+                msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id );
                 msa_props.add( msa_prop );
-                printMsaProperties( id, msa_prop );
-                System.out.print( "(realigned)" );
                 System.out.println();
             }
             else if ( isPrintMsaStats( i ) ) {
-                removeGapColumns();
                 msa_prop = new MsaProperties( _msa, _report_aln_mean_identity );
                 msa_props.add( msa_prop );
                 printMsaProperties( id, msa_prop );
                 System.out.println();
             }
-            ++i;
+        }
+        if ( _removed_seqs_out_base != null ) {
+            final String msg = writeAndAlignRemovedSeqs();
+            System.out.println();
+            System.out.println( msg );
         }
         return msa_props;
     }
 
-    private final boolean isPrintMsaStats( final int i ) {
-        return ( ( _step_for_diagnostics < 2 ) || ( ( ( i + 1 ) % _step_for_diagnostics ) == 0 ) );
-    }
-
-    private final boolean isPrintMsaStatsWriteOutfileAndRealign( final int i ) {
-        return ( ( _step < 2 ) || ( ( ( i + 1 ) % _step ) == 0 ) );
-    }
-
     public final void setGapRatio( final double gap_ratio ) {
         _gap_ratio = gap_ratio;
     }
 
+    public final void setMafftOptions( final String maffts_opts ) {
+        _maffts_opts = maffts_opts;
+    }
+
     public final void setMinLength( final int min_length ) {
         _min_length = min_length;
     }
@@ -256,32 +310,27 @@ public class MsaCompactor {
         _realign = realign;
     }
 
-    public final void setStep( final int step ) {
-        _step = step;
-    }
-
-    public final void setStepForDiagnostics( final int step_for_diagnostics ) {
-        _step_for_diagnostics = step_for_diagnostics;
+    public final void setRemovedSeqsOutBase( final File removed_seqs_out_base ) {
+        _removed_seqs_out_base = removed_seqs_out_base;
     }
 
     public final void setReportAlnMeanIdentity( final boolean report_aln_mean_identity ) {
         _report_aln_mean_identity = report_aln_mean_identity;
     }
 
-    final public String writeMsa( final File outfile, final MSA_FORMAT format, final String suffix ) throws IOException {
-        final Double gr = MsaMethods.calcGapRatio( _msa );
-        final String s = outfile + "_" + _msa.getNumberOfSequences() + "_" + _msa.getLength() + "_"
-                + ForesterUtil.roundToInt( gr * 100 );
-        writeMsa( _msa, s + suffix, format );
-        return s;
+    public final void setStep( final int step ) {
+        _step = step;
+    }
+
+    public final void setStepForDiagnostics( final int step_for_diagnostics ) {
+        _step_for_diagnostics = step_for_diagnostics;
     }
 
     final public String writeAndAlignRemovedSeqs() throws IOException, InterruptedException {
         final StringBuilder msg = new StringBuilder();
-        final Msa removed = BasicMsa.createInstance( _removed_seqs );
-        final String n = _removed_seqs_out_base + "_" + removed.getNumberOfSequences() + ".fasta";
-        writeMsa( removed, n, MSA_FORMAT.FASTA );
-        msg.append( "wrote " + removed.getNumberOfSequences() + " removed sequences to " + n );
+        final String n = _removed_seqs_out_base + "_" + _removed_seqs.size() + ".fasta";
+        SequenceWriter.writeSeqs( _removed_seqs, new File( n ), SEQ_FORMAT.FASTA, 100 );
+        msg.append( "wrote " + _removed_seqs.size() + " removed sequences to " + "\"" + n + "\"" );
         if ( _realign ) {
             final MsaInferrer mafft = Mafft.createInstance( _path_to_mafft );
             final List<String> opts = new ArrayList<String>();
@@ -292,20 +341,22 @@ public class MsaCompactor {
             final Double gr = MsaMethods.calcGapRatio( removed_msa );
             String s = _removed_seqs_out_base + "_" + removed_msa.getNumberOfSequences() + "_"
                     + removed_msa.getLength() + "_" + ForesterUtil.roundToInt( gr * 100 );
-            String suffix = "";
-            if ( _output_format == MSA_FORMAT.FASTA ) {
-                suffix = ".fasta";
-            }
-            else if ( _output_format == MSA_FORMAT.PHYLIP ) {
-                suffix = ".aln";
-            }
+            final String suffix = obtainSuffix();
             s += suffix;
             writeMsa( removed_msa, s, _output_format );
-            msg.append( ", and as MSA of length " + removed_msa.getLength() + " to " + s );
+            msg.append( ", and as MSA of length " + removed_msa.getLength() + " to \"" + s + "\"" );
         }
         return msg.toString();
     }
 
+    final public String writeMsa( final File outfile ) throws IOException {
+        final Double gr = MsaMethods.calcGapRatio( _msa );
+        final String s = outfile + "_" + _msa.getNumberOfSequences() + "_" + _msa.getLength() + "_"
+                + ForesterUtil.roundToInt( gr * 100 );
+        writeMsa( _msa, s + obtainSuffix(), _output_format );
+        return s;
+    }
+
     final int calcNonGapResidues( final Sequence seq ) {
         int ng = 0;
         for( int i = 0; i < seq.getLength(); ++i ) {
@@ -383,6 +434,38 @@ public class MsaCompactor {
         return phy;
     }
 
+    private final boolean isPrintMsaStats( final int i ) {
+        return ( ( ( _step == 1 ) && ( _step_for_diagnostics == 1 ) ) || ( ( _step_for_diagnostics > 0 ) && ( ( ( i + 1 ) % _step_for_diagnostics ) == 0 ) ) );
+    }
+
+    private final boolean isPrintMsaStatsWriteOutfileAndRealign( final int i ) {
+        return ( ( ( _step == 1 ) && ( _step_for_diagnostics == 1 ) ) || ( ( _step > 0 ) && ( ( ( i + 1 ) % _step ) == 0 ) ) );
+    }
+
+    private final StringBuilder msaPropertiesAsSB( final MsaProperties msa_properties ) {
+        final StringBuilder sb = new StringBuilder();
+        sb.append( msa_properties.getNumberOfSequences() );
+        sb.append( "\t" );
+        sb.append( msa_properties.getLength() );
+        sb.append( "\t" );
+        sb.append( NF_4.format( msa_properties.getGapRatio() ) );
+        if ( _report_aln_mean_identity ) {
+            sb.append( "\t" );
+            sb.append( NF_4.format( msa_properties.getAverageIdentityRatio() ) );
+        }
+        return sb;
+    }
+
+    private String obtainSuffix() {
+        if ( _output_format == MSA_FORMAT.FASTA ) {
+            return ".fasta";
+        }
+        else if ( _output_format == MSA_FORMAT.PHYLIP ) {
+            return ".aln";
+        }
+        return "";
+    }
+
     private final Phylogeny pi( final String matrix ) {
         final Phylogeny master_phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, true, matrix );
         final int seed = 15;
@@ -402,28 +485,15 @@ public class MsaCompactor {
     }
 
     private final void printMsaProperties( final String id, final MsaProperties msa_properties ) {
-        System.out.print( ForesterUtil.pad( _longest_id_length + 1, 20, ' ', false ) );
-        System.out.print( "\t" );
-        final StringBuilder sb = msaPropertiesAsSB( msa_properties );
-        System.out.print( sb );
-        System.out.print( "\t" );
-    }
-
-    private final StringBuilder msaPropertiesAsSB( final MsaProperties msa_properties ) {
-        final StringBuilder sb = new StringBuilder();
-        sb.append( msa_properties.getNumberOfSequences() );
-        sb.append( "\t" );
-        sb.append( msa_properties.getLength() );
-        sb.append( "\t" );
-        sb.append( NF_4.format( msa_properties.getGapRatio() ) );
-        if ( _report_aln_mean_identity /*msa_properties.getAverageIdentityRatio() >= 0*/) {
-            sb.append( "\t" );
-            sb.append( NF_4.format( msa_properties.getAverageIdentityRatio() ) );
+        if ( ( _step == 1 ) || ( _step_for_diagnostics == 1 ) ) {
+            System.out.print( ForesterUtil.pad( id, _longest_id_length, ' ', false ) );
+            System.out.print( "\t" );
         }
-        return sb;
+        System.out.print( msaPropertiesAsSB( msa_properties ) );
+        System.out.print( "\t" );
     }
 
-    final private void printMsaStatsWriteOutfileAndRealign( final boolean realign, final String id )
+    final private MsaProperties printMsaStatsWriteOutfileAndRealign( final boolean realign, final String id )
             throws IOException, InterruptedException {
         if ( realign ) {
             realignWithMafft();
@@ -432,6 +502,25 @@ public class MsaCompactor {
         printMsaProperties( id, msa_prop );
         final String s = writeOutfile();
         System.out.print( "-> " + s + ( realign ? "\t(realigned)" : "" ) );
+        return msa_prop;
+    }
+
+    private final void printTableHeader() {
+        if ( ( _step == 1 ) || ( _step_for_diagnostics == 1 ) ) {
+            System.out.print( ForesterUtil.pad( "Id", _longest_id_length, ' ', false ) );
+            System.out.print( "\t" );
+        }
+        System.out.print( "Seqs" );
+        System.out.print( "\t" );
+        System.out.print( "Length" );
+        System.out.print( "\t" );
+        System.out.print( "Gaps" );
+        System.out.print( "\t" );
+        if ( _report_aln_mean_identity ) {
+            System.out.print( "MSA qual" );
+            System.out.print( "\t" );
+        }
+        System.out.println();
     }
 
     final private void realignWithMafft() throws IOException, InterruptedException {
@@ -447,16 +536,8 @@ public class MsaCompactor {
         _msa.deleteGapOnlyColumns();
     }
 
-    final private static void writeMsa( final Msa msa, final String outfile, final MSA_FORMAT format )
-            throws IOException {
-        final Writer w = ForesterUtil.createBufferedWriter( outfile );
-        msa.write( w, format );
-        w.close();
-    }
-
     private final String writeOutfile() throws IOException {
-        final String s = writeMsa( _out_file_base, MSA_FORMAT.PHYLIP, ".aln" );
-        //writeMsa( _out_file_base, MSA_FORMAT.FASTA, ".fasta" );
+        final String s = writeMsa( _out_file_base );
         return s;
     }
 
@@ -492,19 +573,10 @@ public class MsaCompactor {
         return null;
     }
 
-    private final void printTableHeader() {
-        System.out.print( ForesterUtil.pad( "Id", _longest_id_length + 1, ' ', false ) );
-        System.out.print( "\t" );
-        System.out.print( "Seqs" );
-        System.out.print( "\t" );
-        System.out.print( "Length" );
-        System.out.print( "\t" );
-        System.out.print( "Gaps" );
-        System.out.print( "\t" );
-        if ( _report_aln_mean_identity ) {
-            System.out.print( "MSA qual" );
-            System.out.print( "\t" );
-        }
-        System.out.println();
+    final private static void writeMsa( final Msa msa, final String outfile, final MSA_FORMAT format )
+            throws IOException {
+        final Writer w = ForesterUtil.createBufferedWriter( outfile );
+        msa.write( w, format );
+        w.close();
     }
 }