X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fapplication%2Fmsa_compactor.java;h=1cd94d6c7753951456f86b6e4e92c7ea2f217eac;hb=c2f18ef75d93bcf32df987ba5a817150b73ad93e;hp=bf04e121d1c5c549d4850b43e3b298c062363e6c;hpb=35f27e4a3dec02b6496faa17bd89d880d398747a;p=jalview.git diff --git a/forester/java/src/org/forester/application/msa_compactor.java b/forester/java/src/org/forester/application/msa_compactor.java index bf04e12..1cd94d6 100644 --- a/forester/java/src/org/forester/application/msa_compactor.java +++ b/forester/java/src/org/forester/application/msa_compactor.java @@ -10,8 +10,6 @@ import org.forester.io.parsers.FastaParser; import org.forester.io.parsers.GeneralMsaParser; import org.forester.msa.Msa; import org.forester.msa.MsaInferrer; -import org.forester.msa.Msa.MSA_FORMAT; -import org.forester.msa.MsaMethods; import org.forester.msa_compactor.MsaCompactor; import org.forester.util.CommandLineArguments; import org.forester.util.ForesterUtil; @@ -20,17 +18,17 @@ 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 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 = "r"; + 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_DESC = "multiple sequence aligment compactor"; final static private String PRG_VERSION = "0.01"; - final static private String PRG_DATE = "140314"; + final static private String PRG_DATE = "140316"; final static private String E_MAIL = "phylosoft@gmail.com"; final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"; @@ -44,9 +42,9 @@ public class msa_compactor { final File in = cla.getFile( 0 ); final File out = cla.getFile( 1 ); int worst_remove = -1; - double av = -1; + double av_gap = -1; int length = -1; - int step = 1; + int step = -1; boolean realign = false; boolean norm = true; String path_to_mafft = null; @@ -62,17 +60,47 @@ public class msa_compactor { if ( dissallowed_options.length() > 0 ) { ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options ); } + Msa msa = null; + final FileInputStream is = new FileInputStream( in ); + if ( FastaParser.isLikelyFasta( in ) ) { + msa = FastaParser.parseMsa( is ); + } + else { + msa = GeneralMsaParser.parse( is ); + } if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) { worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION ); + if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) { + ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: " + + worst_remove ); + } } if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) { - av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION ); + if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) { + printHelp(); + System.exit( 0 ); + } + av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION ); + if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) { + ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap ); + } } if ( cla.isOptionSet( LENGTH_OPTION ) ) { + if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) { + printHelp(); + System.exit( 0 ); + } length = cla.getOptionValueAsInt( LENGTH_OPTION ); + if ( ( length < 2 ) || ( length >= msa.getLength() ) ) { + ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length ); + } } if ( cla.isOptionSet( STEP_OPTION ) ) { step = cla.getOptionValueAsInt( STEP_OPTION ); + if ( ( step < 1 ) + || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) { + ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step ); + } } if ( cla.isOptionSet( REALIGN_OPTION ) ) { realign = true; @@ -86,55 +114,22 @@ public class msa_compactor { 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 ); - // } - // else { - // printHelp(); - // System.exit( 0 ); - // } - Msa msa = null; - final FileInputStream is = new FileInputStream( in ); - if ( FastaParser.isLikelyFasta( in ) ) { - msa = FastaParser.parseMsa( is ); - } - else { - msa = GeneralMsaParser.parse( is ); - } - if ( realign ) { if ( ForesterUtil.isEmpty( path_to_mafft ) ) { path_to_mafft = MsaCompactor.guessPathToMafft(); } - 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 + "=\" option" ); - } - else { - ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" ); - } - } + checkPathToMafft( path_to_mafft ); } - - MsaCompactor mc = null; if ( worst_remove > 0 ) { - mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm ); + MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out ); } - else if ( av > 0 ) { - mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50 ); + else if ( av_gap > 0 ) { + MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out ); } else if ( length > 0 ) { - mc = MsaCompactor.reduceLength( msa, length, step, realign ); - } - System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) ); - for( final String id : mc.getRemovedSeqIds() ) { - System.out.println( id ); + // TODO if < shortest seq -> error + MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out ); } - mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" ); } catch ( final Exception e ) { e.printStackTrace(); @@ -142,6 +137,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 + + "=\" option" ); + } + else { + ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" ); + } + } + } + private static void printHelp() { ForesterUtil.printProgramInformation( PRG_NAME, PRG_DESC, @@ -153,20 +163,23 @@ public class msa_compactor { final String path_to_mafft = MsaCompactor.guessPathToMafft(); String mafft_comment; if ( !ForesterUtil.isEmpty( path_to_mafft ) ) { - mafft_comment = " (" + path_to_mafft + ")"; + mafft_comment = " (using " + path_to_mafft + ")"; } else { mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=\" option"; } - System.out.println( "Usage:" ); System.out.println(); - System.out.println( PRG_NAME + " " ); + System.out.println( PRG_NAME + " " ); System.out.println(); System.out.println( " options: " ); System.out.println(); - System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION + "= number of sequences to remove" ); - System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment ); + System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION + + "= number of worst offender sequences to remove" ); + System.out.println( " -" + LENGTH_OPTION + "= target MSA length" ); + System.out.println( " -" + AV_GAPINESS_OPTION + "= target gap-ratio (0.0-1.0)" ); + System.out.println( " -" + STEP_OPTION + "= step (for output and re-aligning)" ); + System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment ); System.out.println(); System.out.println(); System.out.println();