inprogress
[jalview.git] / forester / java / src / org / forester / application / msa_compactor.java
index 0f1b961..4230ce0 100644 (file)
@@ -9,47 +9,53 @@ import java.util.List;
 import org.forester.io.parsers.FastaParser;
 import org.forester.io.parsers.GeneralMsaParser;
 import org.forester.msa.Msa;
-import org.forester.msa.MsaCompactor;
-import org.forester.msa.MsaMethods;
+import org.forester.msa.MsaInferrer;
+import org.forester.msa_compactor.MsaCompactor;
 import org.forester.util.CommandLineArguments;
 import org.forester.util.ForesterUtil;
 
 public class msa_compactor {
 
-    final static private String HELP_OPTION_1                 = "help";
-    final static private String HELP_OPTION_2                 = "h";
-    final static private String REMOVE_WORST_OFFENDERS_OPTION = "w";
-    final static private String AV_GAPINESS_OPTION            = "a";
-    final static private String LENGTH_OPTION                 = "l";
-    final static private String REALIGN_OPTION                = "r";
-    final static private String PRG_NAME                      = "msa_compactor";
-    final static private String PRG_DESC                      = "multiple sequnce aligment compactor";
-    final static private String PRG_VERSION                   = "0.90";
-    final static private String PRG_DATE                      = "2012.07.11";
-    final static private String E_MAIL                        = "phylosoft@gmail.com";
-    final static private String WWW                           = "www.phylosoft.org/forester/";
+    final static private String HELP_OPTION_1                          = "help";
+    final static private String HELP_OPTION_2                          = "h";
+    final static private String REMOVE_WORST_OFFENDERS_OPTION          = "r";
+    final static private String AV_GAPINESS_OPTION                     = "g";
+    final static private String STEP_OPTION                            = "s";
+    final static private String LENGTH_OPTION                          = "l";
+    final static private String REALIGN_OPTION                         = "a";
+    final static private String PATH_TO_MAFFT_OPTION                   = "mafft";
+    final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
+    final static private String PRG_NAME                               = "msa_compactor";
+    final static private String PRG_DESC                               = "multiple sequnce aligment compactor";
+    final static private String PRG_VERSION                            = "0.01";
+    final static private String PRG_DATE                               = "140314";
+    final static private String E_MAIL                                 = "phylosoft@gmail.com";
+    final static private String WWW                                    = "https://sites.google.com/site/cmzmasek/home/software/forester";
 
     public static void main( final String args[] ) {
         try {
             final CommandLineArguments cla = new CommandLineArguments( args );
-            if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
+            if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( cla.getNumberOfNames() != 2 ) ) {
                 printHelp();
                 System.exit( 0 );
             }
             final File in = cla.getFile( 0 );
+            final File out = cla.getFile( 1 );
             int worst_remove = -1;
             double av = -1;
             int length = -1;
-            final int step = 5;
+            int step = 1;
             boolean realign = false;
-            //            int to = 0;
-            //            int window = 0;
-            //            int step = 0;
+            boolean norm = true;
+            String path_to_mafft = null;
             final List<String> allowed_options = new ArrayList<String>();
             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
             allowed_options.add( AV_GAPINESS_OPTION );
             allowed_options.add( LENGTH_OPTION );
             allowed_options.add( REALIGN_OPTION );
+            allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
+            allowed_options.add( STEP_OPTION );
+            allowed_options.add( PATH_TO_MAFFT_OPTION );
             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
             if ( dissallowed_options.length() > 0 ) {
                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
@@ -63,9 +69,21 @@ public class msa_compactor {
             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
             }
+            if ( cla.isOptionSet( STEP_OPTION ) ) {
+                step = cla.getOptionValueAsInt( STEP_OPTION );
+            }
             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
                 realign = true;
             }
+            if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
+                if ( !realign ) {
+                    ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
+                }
+                path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
+            }
+            if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
+                norm = false;
+            }
             //            else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
             //                step = cla.getOptionValueAsInt( STEP_OPTION );
             //                window = cla.getOptionValueAsInt( WINDOW_OPTION );
@@ -74,6 +92,12 @@ public class msa_compactor {
             //                printHelp();
             //                System.exit( 0 );
             //            }
+            if ( realign ) {
+                if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
+                    path_to_mafft = MsaCompactor.guessPathToMafft();
+                }
+                checkPathToMafft( path_to_mafft );
+            }
             Msa msa = null;
             final FileInputStream is = new FileInputStream( in );
             if ( FastaParser.isLikelyFasta( in ) ) {
@@ -82,23 +106,26 @@ public class msa_compactor {
             else {
                 msa = GeneralMsaParser.parse( is );
             }
-            System.out.println( msa.toString() );
-            System.out.println( MsaMethods.calcBasicGapinessStatistics( msa ).arithmeticMean() );
             MsaCompactor mc = null;
             if ( worst_remove > 0 ) {
-                mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign );
+                mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
             }
             else if ( av > 0 ) {
-                mc = MsaCompactor.reduceGapAverage( msa, av, step, realign );
+                mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, norm, path_to_mafft, out );
             }
             else if ( length > 0 ) {
-                mc = MsaCompactor.reduceLength( msa, length, step, realign );
-            }
-            System.out.println( mc.getMsa().toString() );
-            System.out.println( MsaMethods.calcBasicGapinessStatistics( mc.getMsa() ).arithmeticMean() );
-            for( final String id : mc.getRemovedSeqIds() ) {
-                System.out.println( id );
+                if ( length >= msa.getLength() ) {
+                    ForesterUtil.fatalError( PRG_NAME, "target MSA length (" + length
+                            + ") is greater than or equal to MSA original length (" + msa.getLength() + ")" );
+                }
+                // TODO if < shortest seq -> error
+                mc = MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
             }
+            //System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
+            // for( final String id : mc.getRemovedSeqIds() ) {
+            //     System.out.println( id );
+            //}
+            //mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
         }
         catch ( final Exception e ) {
             e.printStackTrace();
@@ -106,6 +133,21 @@ public class msa_compactor {
         }
     }
 
+    private static void checkPathToMafft( final String path_to_mafft ) {
+        if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
+            ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
+        }
+        else {
+            if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
+                ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
+                        + "=<path to MAFFT>\" option" );
+            }
+            else {
+                ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
+            }
+        }
+    }
+
     private static void printHelp() {
         ForesterUtil.printProgramInformation( PRG_NAME,
                                               PRG_DESC,
@@ -114,17 +156,26 @@ public class msa_compactor {
                                               E_MAIL,
                                               WWW,
                                               ForesterUtil.getForesterLibraryInformation() );
+        final String path_to_mafft = MsaCompactor.guessPathToMafft();
+        String mafft_comment;
+        if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
+            mafft_comment = " (using " + path_to_mafft + ")";
+        }
+        else {
+            mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
+        }
         System.out.println( "Usage:" );
         System.out.println();
-        System.out.println( PRG_NAME + " <options> <msa input file>" );
+        System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
         System.out.println();
         System.out.println( " options: " );
         System.out.println();
-        //        System.out.println( "   -" + FROM_OPTION + "=<integer>: from (msa column)" );
-        //        System.out.println( "   -" + TO_OPTION + "=<integer>: to (msa column)" );
-        //        System.out.println( "    or" );
-        //        System.out.println( "   -" + WINDOW_OPTION + "=<integer>: window size (msa columns)" );
-        System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION + "=<integer>: step size (msa columns)" );
+        System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
+                + "=<integer>  number of worst offender sequences to remove" );
+        System.out.println( "   -" + LENGTH_OPTION + "=<integer>  target MSA length" );
+        System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>  gap %" );
+        System.out.println( "   -" + STEP_OPTION + "=<decimal>  step" );
+        System.out.println( "   -" + REALIGN_OPTION + "            to realign using MAFFT" + mafft_comment );
         System.out.println();
         System.out.println();
         System.out.println();