From 6479c35c4734850f517a6ef8de0fce500fdd6693 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Thu, 3 Apr 2014 21:51:35 +0000 Subject: [PATCH] inprogress --- .../org/forester/application/msa_compactor.java | 42 +++++------ forester/java/src/org/forester/msa/MsaMethods.java | 12 ++-- .../org/forester/msa_compactor/MsaCompactor.java | 76 +++++++++++--------- forester/java/src/org/forester/test/Test.java | 17 +++-- 4 files changed, 79 insertions(+), 68 deletions(-) diff --git a/forester/java/src/org/forester/application/msa_compactor.java b/forester/java/src/org/forester/application/msa_compactor.java index b030d74..765648f 100644 --- a/forester/java/src/org/forester/application/msa_compactor.java +++ b/forester/java/src/org/forester/application/msa_compactor.java @@ -9,9 +9,8 @@ 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.MsaInferrer; import org.forester.msa.Msa.MSA_FORMAT; -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; @@ -20,11 +19,11 @@ 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"; @@ -94,15 +93,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 ) ) { @@ -111,9 +107,6 @@ public class msa_compactor { else { msa = GeneralMsaParser.parse( is ); } - - - MsaCompactor mc = null; if ( worst_remove > 0 ) { mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm, path_to_mafft ); @@ -124,10 +117,10 @@ public class msa_compactor { else if ( length > 0 ) { mc = MsaCompactor.reduceLength( msa, length, step, realign, path_to_mafft ); } - System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) ); - for( final String id : mc.getRemovedSeqIds() ) { - System.out.println( id ); - } + //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 ) { @@ -136,16 +129,17 @@ public class msa_compactor { } } - private static void checkPathToMafft( String path_to_mafft ) { + 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 + "\"" ); + 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" ); + 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 + "\"" ); + ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" ); } } } @@ -161,20 +155,20 @@ 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( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment ); System.out.println(); System.out.println(); System.out.println(); diff --git a/forester/java/src/org/forester/msa/MsaMethods.java b/forester/java/src/org/forester/msa/MsaMethods.java index b05e837..ac6cb1f 100644 --- a/forester/java/src/org/forester/msa/MsaMethods.java +++ b/forester/java/src/org/forester/msa/MsaMethods.java @@ -175,11 +175,13 @@ public final class MsaMethods { public static SortedMap calculateResidueDestributionPerColumn( final Msa msa, final int column ) { final SortedMap map = new TreeMap(); for( final Character r : msa.getColumnAt( column ) ) { - if ( !map.containsKey( r ) ) { - map.put( r, 1 ); - } - else { - map.put( r, map.get( r ) + 1 ); + if ( r != Sequence.GAP ) { + if ( !map.containsKey( r ) ) { + map.put( r, 1 ); + } + else { + map.put( r, map.get( r ) + 1 ); + } } } return map; diff --git a/forester/java/src/org/forester/msa_compactor/MsaCompactor.java b/forester/java/src/org/forester/msa_compactor/MsaCompactor.java index d6ece5d..5d3a55c 100644 --- a/forester/java/src/org/forester/msa_compactor/MsaCompactor.java +++ b/forester/java/src/org/forester/msa_compactor/MsaCompactor.java @@ -13,7 +13,6 @@ 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; @@ -30,6 +29,8 @@ import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyMethods; import org.forester.sequence.Sequence; import org.forester.tools.ConfidenceAssessor; +import org.forester.util.BasicDescriptiveStatistics; +import org.forester.util.DescriptiveStatistics; import org.forester.util.ForesterUtil; public class MsaCompactor { @@ -39,7 +40,7 @@ public class MsaCompactor { private static final boolean VERBOSE = true; private Msa _msa; private final SortedSet _removed_seq_ids; - private String _path_to_mafft; + private String _path_to_mafft; static { NF_4.setRoundingMode( RoundingMode.HALF_UP ); NF_3.setRoundingMode( RoundingMode.HALF_UP ); @@ -98,19 +99,27 @@ public class MsaCompactor { final private GapContribution[] calcGapContribtionsStats( final boolean norm ) { final GapContribution stats[] = calcGapContribtions( norm ); Arrays.sort( stats ); - for( final GapContribution stat : stats ) { - final StringBuilder sb = new StringBuilder(); - sb.append( stat.getId() ); - sb.append( "\t" ); - sb.append( NF_4.format( stat.getValue() ) ); - sb.append( "\t" ); - // sb.append( NF_4.format( stat.median() ) ); - // sb.append( "\t" ); - // sb.append( NF_4.format( stat.getMin() ) ); - // sb.append( "\t" ); - // sb.append( NF_4.format( stat.getMax() ) ); - //sb.append( "\t" ); - System.out.println( sb ); + // for( final GapContribution stat : stats ) { + // final StringBuilder sb = new StringBuilder(); + // sb.append( stat.getId() ); + // sb.append( "\t" ); + // sb.append( NF_4.format( stat.getValue() ) ); + // sb.append( "\t" ); + // sb.append( NF_4.format( stat.median() ) ); + // sb.append( "\t" ); + // sb.append( NF_4.format( stat.getMin() ) ); + // sb.append( "\t" ); + // sb.append( NF_4.format( stat.getMax() ) ); + //sb.append( "\t" ); + //System.out.println( sb ); + // } + return stats; + } + + private static DescriptiveStatistics calculateIdentityRatio( final int from, final int to, final Msa msa ) { + final DescriptiveStatistics stats = new BasicDescriptiveStatistics(); + for( int c = from; c <= to; ++c ) { + stats.addValue( MsaMethods.calculateIdentityRatio( msa, c ) ); } return stats; } @@ -124,7 +133,7 @@ public class MsaCompactor { } return gappiness; } - + // Returns null if not path found. final public static String guessPathToMafft() { String path; @@ -133,7 +142,6 @@ public class MsaCompactor { if ( MsaInferrer.isInstalled( path ) ) { return path; } - } path = "/usr/local/bin/mafft"; if ( MsaInferrer.isInstalled( path ) ) { @@ -153,16 +161,11 @@ public class MsaCompactor { } return null; } - final private void mafft() throws IOException, InterruptedException { - // final MsaInferrer mafft = Mafft + // final MsaInferrer mafft = Mafft // .createInstance( "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-7.130-without-extensions/scripts/mafft" ); - - final MsaInferrer mafft = Mafft - .createInstance( _path_to_mafft ); - - + final MsaInferrer mafft = Mafft.createInstance( _path_to_mafft ); final List opts = new ArrayList(); opts.add( "--maxiterate" ); opts.add( "1000" ); @@ -178,6 +181,8 @@ public class MsaCompactor { sb.append( _msa.getLength() ); sb.append( "\t" ); sb.append( NF_3.format( MsaMethods.calcGapRatio( _msa ) ) ); + sb.append( "\t" ); + sb.append( NF_3.format( calculateIdentityRatio( 0, _msa.getLength() - 1, _msa ).arithmeticMean() ) ); return sb; } @@ -303,19 +308,20 @@ public class MsaCompactor { final int step, final boolean realign, final boolean norm ) throws IOException, InterruptedException { - final Phylogeny a = pi( "a.pwd" ); - Archaeopteryx.createApplication( a ); + //final Phylogeny a = pi( "a.pwd" ); + //Archaeopteryx.createApplication( a ); final GapContribution stats[] = calcGapContribtionsStats( norm ); final List to_remove_ids = new ArrayList(); for( int j = 0; j < to_remove; ++j ) { to_remove_ids.add( stats[ j ].getId() ); _removed_seq_ids.add( stats[ j ].getId() ); } - //TODO if verbose/interactve + //TODO if verbose/interactive for( final String id : to_remove_ids ) { _msa = MsaMethods.removeSequence( _msa, id ); removeGapColumns(); - System.out.print( id ); + //System.out.print( id ); + System.out.print( ForesterUtil.pad( id, 20, ' ', false ) ); System.out.print( "\t" ); final StringBuilder sb = msaStatsAsSB(); System.out.println( sb ); @@ -326,8 +332,8 @@ public class MsaCompactor { if ( realign ) { mafft(); } - final Phylogeny b = pi( "b.pwd" ); - Archaeopteryx.createApplication( b ); + //final Phylogeny b = pi( "b.pwd" ); + //Archaeopteryx.createApplication( b ); } final private void writeMsa( final String outfile, final MSA_FORMAT format ) throws IOException { @@ -345,7 +351,7 @@ public class MsaCompactor { final String path_to_mafft ) throws IOException, InterruptedException { final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign) { + if ( realign ) { mc.setPathToMafft( path_to_mafft ); } mc.removeViaGapAverage( max_gap_average, step, realign, out, minimal_effective_length ); @@ -356,9 +362,10 @@ public class MsaCompactor { final int length, final int step, final boolean realign, - final String path_to_mafft ) throws IOException, InterruptedException { + final String path_to_mafft ) throws IOException, + InterruptedException { final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign) { + if ( realign ) { mc.setPathToMafft( path_to_mafft ); } mc.removeViaLength( length, step, realign ); @@ -372,7 +379,7 @@ public class MsaCompactor { final String path_to_mafft ) throws IOException, InterruptedException { final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign) { + if ( realign ) { mc.setPathToMafft( path_to_mafft ); } mc.removeWorstOffenders( worst_offenders_to_remove, 1, realign, norm ); @@ -381,6 +388,5 @@ public class MsaCompactor { private void setPathToMafft( final String path_to_mafft ) { _path_to_mafft = path_to_mafft; - } } diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index 1fcf900..80a20b6 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -5698,10 +5698,10 @@ public final class Test { private static boolean testMsaQualityMethod() { try { - final Sequence s0 = BasicSequence.createAaSequence( "a", "ABAXEFGHIJ" ); - final Sequence s1 = BasicSequence.createAaSequence( "b", "ABBXEFGHIJ" ); - final Sequence s2 = BasicSequence.createAaSequence( "c", "AXCXEFGHIJ" ); - final Sequence s3 = BasicSequence.createAaSequence( "d", "AXDDEFGHIJ" ); + final Sequence s0 = BasicSequence.createAaSequence( "a", "ABAXEFGHIJJE-" ); + final Sequence s1 = BasicSequence.createAaSequence( "b", "ABBXEFGHIJJBB" ); + final Sequence s2 = BasicSequence.createAaSequence( "c", "AXCXEFGHIJJ--" ); + final Sequence s3 = BasicSequence.createAaSequence( "d", "AXDDEFGHIJ---" ); final List l = new ArrayList(); l.add( s0 ); l.add( s1 ); @@ -5720,6 +5720,15 @@ public final class Test { if ( !isEqual( 0.75, MsaMethods.calculateIdentityRatio( msa, 3 ) ) ) { return false; } + if ( !isEqual( 0.75, MsaMethods.calculateIdentityRatio( msa, 10 ) ) ) { + return false; + } + if ( !isEqual( 0.25, MsaMethods.calculateIdentityRatio( msa, 11 ) ) ) { + return false; + } + if ( !isEqual( 0.25, MsaMethods.calculateIdentityRatio( msa, 12 ) ) ) { + return false; + } } catch ( final Exception e ) { e.printStackTrace( System.out ); -- 1.7.10.2