X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fmsa_compactor%2FMsaCompactor.java;h=0b7f8ed47370fe99c9524da3164074b051c41719;hb=861cf1ba9d6eb76eeb499fa59d7ffc961e5288e1;hp=4eff35e962c8a0d0d3c16817664edb090d682a71;hpb=93f740ca4fbd156ba93b07b8e43112f392645c1d;p=jalview.git diff --git a/forester/java/src/org/forester/msa_compactor/MsaCompactor.java b/forester/java/src/org/forester/msa_compactor/MsaCompactor.java index 4eff35e..0b7f8ed 100644 --- a/forester/java/src/org/forester/msa_compactor/MsaCompactor.java +++ b/forester/java/src/org/forester/msa_compactor/MsaCompactor.java @@ -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; @@ -36,12 +37,19 @@ import java.util.List; import java.util.SortedSet; import java.util.TreeSet; +import org.forester.archaeopteryx.Archaeopteryx; +import org.forester.archaeopteryx.Configuration; import org.forester.evoinference.distance.NeighborJoiningF; 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.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION; +import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException; +import org.forester.io.parsers.util.ParserUtils; +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; import org.forester.msa.Msa.MSA_FORMAT; @@ -50,27 +58,114 @@ 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 { - final private static NumberFormat NF_3 = new DecimalFormat( "#.###" ); - final private static NumberFormat NF_4 = new DecimalFormat( "#.####" ); - private final String _maffts_opts = "--retree 1"; - private Msa _msa; - private File _out_file_base; - private String _path_to_mafft; + 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 String _maffts_opts = "--auto"; + private int _min_length = -1; + // + private String _infile_name = null; + private DeleteableMsa _msa = null; + private boolean _norm = true; + private File _out_file_base = null; + private MSA_FORMAT _output_format = MSA_FORMAT.FASTA; + private String _path_to_mafft = null; + // + private boolean _realign = false; private final SortedSet _removed_seq_ids; + private final ArrayList _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 boolean _phylogentic_inference = false; static { NF_4.setRoundingMode( RoundingMode.HALF_UP ); NF_3.setRoundingMode( RoundingMode.HALF_UP ); } - private MsaCompactor( final Msa msa ) { + public MsaCompactor( final DeleteableMsa msa ) { _msa = msa; _removed_seq_ids = new TreeSet(); + _longest_id_length = _msa.determineMaxIdLength(); + _removed_seqs = new ArrayList(); + } + + public final List chart( final int step, final boolean realign, final boolean norm ) + throws IOException, InterruptedException { + final GapContribution stats[] = calcGapContribtionsStats( norm ); + final List to_remove_ids = new ArrayList(); + final List msa_props = new ArrayList(); + for( final GapContribution gap_gontribution : stats ) { + to_remove_ids.add( gap_gontribution.getId() ); + } + if ( !_realign ) { + _step = -1; + } + 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 ); + 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; + 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, id, _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, 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; + } + + final public void deleteGapColumns( final double max_allowed_gap_ratio ) { + _msa.deleteGapColumns( max_allowed_gap_ratio ); } final public Msa getMsa() { @@ -81,15 +176,212 @@ public class MsaCompactor { return _removed_seq_ids; } + 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 removeViaGapAverage( final double mean_gapiness ) throws IOException, + InterruptedException { + final GapContribution stats[] = calcGapContribtionsStats( _norm ); + final List to_remove_ids = new ArrayList(); + final List msa_props = new ArrayList(); + 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, true ); + _removed_seqs.add( deleted ); + removeGapColumns(); + if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( MsaMethods.calcGapRatio( _msa ) <= mean_gapiness ) ) { + msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id ); + msa_props.add( msa_prop ); + System.out.println(); + } + else if ( isPrintMsaStats( i ) ) { + msa_prop = new MsaProperties( _msa, id, _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; + } + + public List removeViaLength( final int length ) throws IOException, InterruptedException { + final GapContribution stats[] = calcGapContribtionsStats( _norm ); + final List to_remove_ids = new ArrayList(); + final List msa_props = new ArrayList(); + 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, true ); + _removed_seqs.add( deleted ); + removeGapColumns(); + if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( _msa.getLength() <= length ) ) { + msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id ); + msa_props.add( msa_prop ); + System.out.println(); + } + else if ( isPrintMsaStats( i ) ) { + msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity ); + printMsaProperties( id, msa_prop ); + msa_props.add( 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; + } + + public final List removeWorstOffenders( final int to_remove ) throws IOException, + InterruptedException { + final GapContribution stats[] = calcGapContribtionsStats( _norm ); + final List to_remove_ids = new ArrayList(); + final List msa_props = new ArrayList(); + for( int j = 0; j < to_remove; ++j ) { + to_remove_ids.add( stats[ j ].getId() ); + _removed_seq_ids.add( stats[ j ].getId() ); + } + printTableHeader(); + MsaProperties msa_prop = new MsaProperties( _msa, "", _report_aln_mean_identity ); + msa_props.add( msa_prop ); + printMsaProperties( "", msa_prop ); + System.out.println(); + 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, 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 ); + System.out.println(); + } + else if ( isPrintMsaStats( i ) ) { + msa_prop = new MsaProperties( _msa, id, _report_aln_mean_identity ); + msa_props.add( msa_prop ); + printMsaProperties( id, msa_prop ); + System.out.println(); + } + } + if ( _removed_seqs_out_base != null ) { + final String msg = writeAndAlignRemovedSeqs(); + System.out.println(); + System.out.println( msg ); + } + return msa_props; + } + + 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; + } + + public final void setNorm( final boolean norm ) { + _norm = norm; + } + final public void setOutFileBase( final File out_file_base ) { _out_file_base = out_file_base; } - final public String writeMsa( final File outfile, final MSA_FORMAT format, final String suffix ) throws IOException { + public final void setOutputFormat( final MSA_FORMAT output_format ) { + _output_format = output_format; + } + + public void setPathToMafft( final String path_to_mafft ) { + _path_to_mafft = path_to_mafft; + } + + public final void setRealign( final boolean realign ) { + _realign = realign; + } + + 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; + } + + 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 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 opts = new ArrayList(); + for( final String o : _maffts_opts.split( "\\s" ) ) { + opts.add( o ); + } + final Msa removed_msa = mafft.infer( _removed_seqs, opts ); + final Double gr = MsaMethods.calcGapRatio( removed_msa ); + String s = _removed_seqs_out_base + "_" + removed_msa.getNumberOfSequences() + "_" + + removed_msa.getLength() + "_" + ForesterUtil.roundToInt( gr * 100 ); + final String suffix = obtainSuffix(); + s += suffix; + writeMsa( removed_msa, s, _output_format ); + 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( s + suffix, format ); + writeMsa( _msa, s + obtainSuffix(), _output_format ); return s; } @@ -103,24 +395,6 @@ public class MsaCompactor { return ng; } - Phylogeny pi( final String matrix ) { - final Phylogeny master_phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, true, matrix ); - final int seed = 15; - final int n = 100; - final ResampleableMsa resampleable_msa = new ResampleableMsa( ( BasicMsa ) _msa ); - final int[][] resampled_column_positions = BootstrapResampler.createResampledColumnPositions( _msa.getLength(), - n, - seed ); - final Phylogeny[] eval_phys = new Phylogeny[ n ]; - for( int i = 0; i < n; ++i ) { - resampleable_msa.resample( resampled_column_positions[ i ] ); - eval_phys[ i ] = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, resampleable_msa, false, null ); - } - ConfidenceAssessor.evaluate( "bootstrap", eval_phys, master_phy, true, 1 ); - PhylogenyMethods.extractFastaInformation( master_phy ); - return master_phy; - } - private final GapContribution[] calcGapContribtions( final boolean normalize_for_effective_seq_length ) { final double gappiness[] = calcGappiness(); final GapContribution stats[] = new GapContribution[ _msa.getNumberOfSequences() ]; @@ -157,10 +431,10 @@ public class MsaCompactor { return gappiness; } - private Phylogeny inferNJphylogeny( final PWD_DISTANCE_METHOD pwd_distance_method, - final Msa msa, - final boolean write_matrix, - final String matrix_name ) { + private final Phylogeny inferNJphylogeny( final PWD_DISTANCE_METHOD pwd_distance_method, + final Msa msa, + final boolean write_matrix, + final String matrix_name ) { BasicSymmetricalDistanceMatrix m = null; switch ( pwd_distance_method ) { case KIMURA_DISTANCE: @@ -188,201 +462,207 @@ public class MsaCompactor { return phy; } - private StringBuilder msaStatsAsSB() { + 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.getNumberOfSequences() ); + sb.append( msa_properties.getNumberOfSequences() ); sb.append( "\t" ); - sb.append( _msa.getLength() ); + sb.append( msa_properties.getLength() ); sb.append( "\t" ); - sb.append( NF_4.format( MsaMethods.calcGapRatio( _msa ) ) ); - sb.append( "\t" ); - sb.append( NF_4.format( MsaMethods.calculateIdentityRatio( 0, _msa.getLength() - 1, _msa ).arithmeticMean() ) ); + 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 final void printMsaStats( final String id ) { - System.out.print( ForesterUtil.pad( id, 20, ' ', false ) ); - System.out.print( "\t" ); - final StringBuilder sb = msaStatsAsSB(); - System.out.print( sb ); - System.out.print( "\t" ); - } - - final private void realignWithMafft() throws IOException, InterruptedException { - // 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 List opts = new ArrayList(); - for( final String o : _maffts_opts.split( "\\s" ) ) { - opts.add( o ); + private String obtainSuffix() { + if ( _output_format == MSA_FORMAT.FASTA ) { + return ".fasta"; } - //opts.add( "--maxiterate" ); - //opts.add( "1000" ); - //opts.add( "--localpair" ); - //opts.add( "--quiet" ); - _msa = mafft.infer( _msa.asSequenceList(), opts ); + else if ( _output_format == MSA_FORMAT.PHYLIP ) { + return ".aln"; + } + return ""; } - final private void removeGapColumns() { - _msa = MsaMethods.createInstance().removeGapColumns( 1, 0, _msa ); + private final Phylogeny pi( final String matrix, final int boostrap ) { + final Phylogeny master_phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, true, matrix ); + final int seed = 15; + final int n = 100; + final ResampleableMsa resampleable_msa = new ResampleableMsa( _msa ); + final int[][] resampled_column_positions = BootstrapResampler.createResampledColumnPositions( _msa.getLength(), + n, + seed ); + final Phylogeny[] eval_phys = new Phylogeny[ n ]; + for( int i = 0; i < n; ++i ) { + resampleable_msa.resample( resampled_column_positions[ i ] ); + eval_phys[ i ] = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, resampleable_msa, false, null ); + } + ConfidenceAssessor.evaluate( "bootstrap", eval_phys, master_phy, true, 1 ); + PhylogenyMethods.extractFastaInformation( master_phy ); + return master_phy; } - final private void removeViaGapAverage( final double mean_gapiness, - final int step, - final boolean realign, - final boolean norm, - final boolean verbose ) throws IOException, InterruptedException { - final GapContribution stats[] = calcGapContribtionsStats( norm ); - final List to_remove_ids = new ArrayList(); - for( final GapContribution gap_gontribution : stats ) { - to_remove_ids.add( gap_gontribution.getId() ); - } - if ( verbose ) { - printTableHeader(); - } - int i = 0; - while ( MsaMethods.calcGapRatio( _msa ) > mean_gapiness ) { - final String id = to_remove_ids.get( i ); - _msa = MsaMethods.removeSequence( _msa, id ); - removeGapColumns(); - if ( verbose ) { - printMsaStats( id ); - } - if ( ( ( step > 0 ) && ( ( ( i + 1 ) % step ) == 0 ) ) - || ( MsaMethods.calcGapRatio( _msa ) <= mean_gapiness ) ) { - if ( realign ) { - realignWithMafft(); - } - final String s = writeOutfile(); - if ( verbose ) { - System.out.print( "-> " + s ); + 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(); + while ( it.hasNext() ) { + final PhylogenyNode n = it.next(); + final String name = n.getName().trim(); + if ( !ForesterUtil.isEmpty( name ) ) { + try { + ParserUtils.extractTaxonomyDataFromNodeName( n, TAXONOMY_EXTRACTION.AGGRESSIVE ); + } + catch ( final PhyloXmlDataFormatException e ) { + // Ignore. + } } } - if ( verbose ) { - System.out.println(); - } - ++i; } + return phy; } - final private List chart( final boolean realign, final boolean norm, final boolean verbose ) - throws IOException, InterruptedException { - final GapContribution stats[] = calcGapContribtionsStats( norm ); - final List to_remove_ids = new ArrayList(); - final List msa_props = new ArrayList(); - for( final GapContribution gap_gontribution : stats ) { - to_remove_ids.add( gap_gontribution.getId() ); - } - if ( verbose ) { - printTableHeader(); - } - int i = 0; - final int x = ForesterUtil.roundToInt( _msa.getNumberOfSequences() / 20.0 ); - while ( _msa.getNumberOfSequences() > x ) { + public final void decorateTree( final Phylogeny phy, final List to_remove_ids ) { + for( int i = 0; i < to_remove_ids.size(); ++i ) { final String id = to_remove_ids.get( i ); - _msa = MsaMethods.removeSequence( _msa, id ); - removeGapColumns(); - msa_props.add( new MsaProperties( _msa ) ); - if ( verbose ) { - printMsaStats( id ); - } - if ( realign ) { - realignWithMafft(); - } - if ( verbose ) { - System.out.println(); - } - ++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 ); } - return msa_props; } - final private void removeViaLength( final int length, - final int step, - final boolean realign, - final boolean norm, - final boolean verbose ) throws IOException, InterruptedException { - final GapContribution stats[] = calcGapContribtionsStats( norm ); - final List to_remove_ids = new ArrayList(); - for( final GapContribution gap_gontribution : stats ) { - to_remove_ids.add( gap_gontribution.getId() ); + public final void decorateTree2( final Phylogeny phy, final List 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() ); + } } - if ( verbose ) { - printTableHeader(); + 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 ); } - int i = 0; - while ( _msa.getLength() > length ) { - final String id = to_remove_ids.get( i ); - _msa = MsaMethods.removeSequence( _msa, id ); - removeGapColumns(); - if ( verbose ) { - printMsaStats( id ); + 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 ) ); } - if ( ( ( step > 0 ) && ( ( ( i + 1 ) % step ) == 0 ) ) || ( _msa.getLength() <= length ) ) { - if ( realign ) { - realignWithMafft(); - } - final String s = writeOutfile(); - if ( verbose ) { - System.out.print( "-> " + s ); - } - } - if ( verbose ) { - System.out.println(); - } - ++i; } } - final private void removeWorstOffenders( final int to_remove, - final int step, - final boolean realign, - final boolean norm, - final boolean verbose ) throws IOException, InterruptedException { - 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() ); + 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 ); + } + + private final void printMsaProperties( final String id, final MsaProperties msa_properties ) { + if ( ( _step == 1 ) || ( _step_for_diagnostics == 1 ) ) { + System.out.print( ForesterUtil.pad( id, _longest_id_length, ' ', false ) ); + System.out.print( "\t" ); } - if ( verbose ) { - printTableHeader(); + System.out.print( msaPropertiesAsSB( msa_properties ) ); + System.out.print( "\t" ); + } + + final private MsaProperties printMsaStatsWriteOutfileAndRealign( final boolean realign, final String id ) + throws IOException, InterruptedException { + if ( realign ) { + realignWithMafft(); } - for( int i = 0; i < to_remove_ids.size(); ++i ) { - final String id = to_remove_ids.get( i ); - _msa = MsaMethods.removeSequence( _msa, id ); - removeGapColumns(); - if ( verbose ) { - printMsaStats( id ); - } - if ( ( ( step > 0 ) && ( ( ( i + 1 ) % step ) == 0 ) ) || ( i == ( to_remove_ids.size() - 1 ) ) ) { - if ( realign ) { - realignWithMafft(); - } - final String s = writeOutfile(); - if ( verbose ) { - System.out.print( "-> " + s ); - } - } - if ( verbose ) { - System.out.println(); - } + 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)" : "" ) ); + 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(); } - private void setPathToMafft( final String path_to_mafft ) { - _path_to_mafft = path_to_mafft; + final private void realignWithMafft() throws IOException, InterruptedException { + final MsaInferrer mafft = Mafft.createInstance( _path_to_mafft ); + final List opts = new ArrayList(); + for( final String o : _maffts_opts.split( "\\s" ) ) { + opts.add( o ); + } + _msa = DeleteableMsa.createInstance( mafft.infer( _msa.asSequenceList(), opts ) ); } - final private void writeMsa( final String outfile, final MSA_FORMAT format ) throws IOException { - final Writer w = ForesterUtil.createBufferedWriter( outfile ); - _msa.write( w, format ); - w.close(); + final private void removeGapColumns() { + _msa.deleteGapOnlyColumns(); } - private String writeOutfile() throws IOException { - final String s = writeMsa( _out_file_base, MSA_FORMAT.PHYLIP, ".aln" ); - //writeMsa( _out_file_base, MSA_FORMAT.FASTA, ".fasta" ); + private final String writeOutfile() throws IOException { + final String s = writeMsa( _out_file_base ); return s; } @@ -418,78 +698,18 @@ public class MsaCompactor { return null; } - public final static MsaCompactor reduceGapAverage( final Msa msa, - final double max_gap_average, - final int step, - final boolean realign, - final boolean norm, - final String path_to_mafft, - final File out ) throws IOException, InterruptedException { - final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign ) { - mc.setPathToMafft( path_to_mafft ); - } - mc.setOutFileBase( out ); - mc.removeViaGapAverage( max_gap_average, step, realign, norm, true ); - return mc; - } - - public final static MsaCompactor reduceLength( final Msa msa, - final int length, - final int step, - final boolean realign, - final boolean norm, - final String path_to_mafft, - final File out ) throws IOException, InterruptedException { - final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign ) { - mc.setPathToMafft( path_to_mafft ); - } - mc.setOutFileBase( out ); - mc.removeViaLength( length, step, realign, norm, true ); - return mc; - } - - public final static MsaCompactor chart( final Msa msa, - final boolean realign, - final boolean norm, - final String path_to_mafft ) throws IOException, InterruptedException { - final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign ) { - mc.setPathToMafft( path_to_mafft ); - } - final List msa_props = mc.chart( realign, norm, true ); - Chart.display( msa_props ); - return mc; + 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(); } - public final static MsaCompactor removeWorstOffenders( final Msa msa, - final int worst_offenders_to_remove, - final int step, - final boolean realign, - final boolean norm, - final String path_to_mafft, - final File out ) throws IOException, InterruptedException { - final MsaCompactor mc = new MsaCompactor( msa ); - if ( realign ) { - mc.setPathToMafft( path_to_mafft ); - } - mc.setOutFileBase( out ); - mc.removeWorstOffenders( worst_offenders_to_remove, step, realign, norm, true ); - return mc; + public void setPeformPhylogenticInference( final boolean phylogentic_inference ) { + _phylogentic_inference = phylogentic_inference; } - private final static void printTableHeader() { - System.out.print( ForesterUtil.pad( "Id", 20, ' ', 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" ); - System.out.print( "MSA qual" ); - System.out.print( "\t" ); - System.out.println(); + public void setInfileName( final String infile_name ) { + _infile_name = infile_name; } }