2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 Sanford-Burnham Medical Research Institute
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 package org.forester.msa_compactor;
27 import java.awt.Color;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.math.RoundingMode;
32 import java.text.DecimalFormat;
33 import java.text.NumberFormat;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.SortedSet;
38 import java.util.TreeSet;
40 import org.forester.archaeopteryx.Archaeopteryx;
41 import org.forester.archaeopteryx.Configuration;
42 import org.forester.evoinference.distance.NeighborJoiningF;
43 import org.forester.evoinference.distance.PairwiseDistanceCalculator;
44 import org.forester.evoinference.distance.PairwiseDistanceCalculator.PWD_DISTANCE_METHOD;
45 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
46 import org.forester.evoinference.tools.BootstrapResampler;
47 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
48 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
49 import org.forester.io.parsers.util.ParserUtils;
50 import org.forester.io.writers.SequenceWriter;
51 import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
52 import org.forester.msa.DeleteableMsa;
53 import org.forester.msa.Mafft;
54 import org.forester.msa.Msa;
55 import org.forester.msa.Msa.MSA_FORMAT;
56 import org.forester.msa.MsaInferrer;
57 import org.forester.msa.MsaMethods;
58 import org.forester.msa.ResampleableMsa;
59 import org.forester.phylogeny.Phylogeny;
60 import org.forester.phylogeny.PhylogenyMethods;
61 import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
62 import org.forester.phylogeny.PhylogenyNode;
63 import org.forester.phylogeny.data.NodeVisualData;
64 import org.forester.phylogeny.data.NodeVisualData.NodeFill;
65 import org.forester.phylogeny.data.NodeVisualData.NodeShape;
66 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
67 import org.forester.sequence.MolecularSequence;
68 import org.forester.tools.ConfidenceAssessor;
69 import org.forester.util.BasicDescriptiveStatistics;
70 import org.forester.util.DescriptiveStatistics;
71 import org.forester.util.ForesterUtil;
73 public class MsaCompactor {
75 final private static NumberFormat NF_1 = new DecimalFormat( "0.#" );
76 final private static NumberFormat NF_3 = new DecimalFormat( "0.###" );
77 final private static NumberFormat NF_4 = new DecimalFormat( "0.####" );
78 private boolean _calculate_shannon_entropy = false;
80 private String _infile_name = null;
81 private final short _longest_id_length;
83 private String _maffts_opts = "--auto";
84 private DeleteableMsa _msa = null;
85 private boolean _norm = true;
86 private File _out_file_base = null;
87 private MSA_FORMAT _output_format = MSA_FORMAT.FASTA;
88 private String _path_to_mafft = null;
89 private boolean _phylogentic_inference = false;
91 private boolean _realign = false;
92 private final SortedSet<String> _removed_seq_ids;
93 private final ArrayList<MolecularSequence> _removed_seqs;
94 private File _removed_seqs_out_base = null;
95 private int _step = -1;
96 private int _step_for_diagnostics = -1;
98 NF_1.setRoundingMode( RoundingMode.HALF_UP );
99 NF_4.setRoundingMode( RoundingMode.HALF_UP );
100 NF_3.setRoundingMode( RoundingMode.HALF_UP );
103 public MsaCompactor( final DeleteableMsa msa ) {
105 _removed_seq_ids = new TreeSet<String>();
106 _longest_id_length = _msa.determineMaxIdLength();
107 _removed_seqs = new ArrayList<MolecularSequence>();
110 public final Phylogeny calcTree() {
111 final Phylogeny phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, false, "" );
112 PhylogenyMethods.midpointRoot( phy );
113 PhylogenyMethods.orderAppearance( phy.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.NODE_NAME );
114 final boolean x = PhylogenyMethods.extractFastaInformation( phy );
116 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
117 while ( it.hasNext() ) {
118 final PhylogenyNode n = it.next();
119 final String name = n.getName().trim();
120 if ( !ForesterUtil.isEmpty( name ) ) {
122 ParserUtils.extractTaxonomyDataFromNodeName( n, TAXONOMY_EXTRACTION.AGGRESSIVE );
124 catch ( final PhyloXmlDataFormatException e ) {
133 public final List<MsaProperties> chart( final int step, final boolean realign, final boolean norm )
134 throws IOException, InterruptedException {
135 final GapContribution stats[] = calcGapContribtionsStats( norm );
136 final List<String> to_remove_ids = new ArrayList<String>();
137 final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
138 for( final GapContribution gap_gontribution : stats ) {
139 to_remove_ids.add( gap_gontribution.getId() );
141 Phylogeny phy = null;
142 if ( _phylogentic_inference ) {
143 System.out.println( "calculating phylogentic tree..." );
144 System.out.println();
146 addSeqs2Tree( _msa, phy );
151 int x = ForesterUtil.roundToInt( _msa.getNumberOfSequences() / 10.0 );
155 MsaProperties msa_prop = new MsaProperties( _msa, "", _calculate_shannon_entropy );
156 msa_props.add( msa_prop );
158 printMsaProperties( msa_prop );
159 System.out.println();
161 while ( _msa.getNumberOfSequences() > x ) {
162 final String id = to_remove_ids.get( i );
163 _msa.deleteRow( id, false );
164 if ( realign && isPrintMsaStatsWriteOutfileAndRealign( i ) ) {
167 msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
168 msa_props.add( msa_prop );
169 printMsaProperties( msa_prop );
170 System.out.print( "(realigned)" );
171 System.out.println();
173 else if ( isPrintMsaStats( i ) ) {
175 msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
176 msa_props.add( msa_prop );
177 printMsaProperties( msa_prop );
178 System.out.println();
182 if ( _phylogentic_inference ) {
183 decorateTree( phy, msa_props, true );
189 private final static void addSeqs2Tree( final Msa msa, final Phylogeny phy ) {
190 for( int i = 0; i < msa.getNumberOfSequences(); ++i ) {
191 final MolecularSequence seq = msa.getSequence( i );
192 final String seq_name = seq.getIdentifier();
193 final PhylogenyNode n = phy.getNode( seq_name );
194 if ( !n.getNodeData().isHasSequence() ) {
195 n.getNodeData().addSequence( new org.forester.phylogeny.data.Sequence() );
198 throw new IllegalArgumentException( "this should not have happened" );
200 n.getNodeData().getSequence().setMolecularSequence( seq.getMolecularSequenceAsString() );
201 n.getNodeData().getSequence().setMolecularSequenceAligned( true );
202 n.getNodeData().getSequence().setName( seq_name );
206 private final static void decorateTree( final Phylogeny phy,
207 final List<MsaProperties> msa_props,
208 final boolean chart_only ) {
209 final BasicDescriptiveStatistics length_stats = new BasicDescriptiveStatistics();
210 for( int i = 0; i < msa_props.size(); ++i ) {
211 final MsaProperties msa_prop = msa_props.get( i );
212 final String id = msa_prop.getRemovedSeq();
213 if ( !ForesterUtil.isEmpty( id ) ) {
214 length_stats.addValue( msa_prop.getLength() );
217 final double mean = length_stats.arithmeticMean();
218 final double min = length_stats.getMin();
219 final double max = length_stats.getMax();
220 final Color min_color = new Color( 0, 255, 0 );
221 final Color max_color = new Color( 255, 0, 0 );
222 final Color mean_color = new Color( 255, 255, 0 );
223 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
225 while ( it.hasNext() ) {
226 final NodeVisualData vis = new NodeVisualData();
227 vis.setFillType( NodeFill.SOLID );
228 vis.setShape( NodeShape.RECTANGLE );
229 vis.setNodeColor( min_color );
230 it.next().getNodeData().setNodeVisualData( vis );
234 for( int i = 0; i < msa_props.size(); ++i ) {
235 final MsaProperties msa_prop = msa_props.get( i );
236 final String id = msa_prop.getRemovedSeq();
237 if ( !ForesterUtil.isEmpty( id ) ) {
238 final PhylogenyNode n = phy.getNode( id );
239 n.setName( n.getName() + " [" + i + "]" );
241 final NodeVisualData vis = new NodeVisualData();
242 vis.setFillType( NodeFill.SOLID );
243 vis.setShape( NodeShape.RECTANGLE );
244 vis.setNodeColor( ForesterUtil.calcColor( msa_prop.getLength(), min, max, mean_color, max_color ) );
245 n.getNodeData().setNodeVisualData( vis );
250 .setNodeColor( ForesterUtil.calcColor( msa_prop.getLength(),
263 final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
264 _msa.deleteGapColumns( max_allowed_gap_ratio );
267 public final void displayTree( final Phylogeny phy ) {
268 final Configuration config = new Configuration();
269 config.setDisplayAsPhylogram( true );
270 config.setUseStyle( true );
271 config.setDisplayTaxonomyCode( false );
272 config.setDisplayTaxonomyCommonNames( false );
273 config.setDisplayTaxonomyScientificNames( false );
274 config.setDisplaySequenceNames( false );
275 config.setDisplaySequenceSymbols( false );
276 config.setDisplayGeneNames( false );
277 config.setShowScale( true );
278 config.setAddTaxonomyImagesCB( false );
279 config.setBaseFontSize( 9 );
280 config.setBaseFontFamilyName( "Arial" );
281 Archaeopteryx.createApplication( phy, config, _infile_name );
284 final public Msa getMsa() {
288 public final void removeSequencesByMinimalLength( final int min_effective_length ) throws IOException {
289 _msa = DeleteableMsa.createInstance( MsaMethods.removeSequencesByMinimalLength( _msa, min_effective_length ) );
291 final String s = writeOutfile();
292 final DescriptiveStatistics msa_stats = MsaMethods.calculateEffectiveLengthStatistics( _msa );
293 System.out.println( "Output MSA : " + s );
294 System.out.println( " MSA length : " + _msa.getLength() );
295 System.out.println( " Number of sequences : " + _msa.getNumberOfSequences() );
296 System.out.println( " Median sequence length : " + NF_1.format( msa_stats.median() ) );
297 System.out.println( " Mean sequence length : " + NF_1.format( msa_stats.arithmeticMean() ) );
298 System.out.println( " Max sequence length : " + ( ( int ) msa_stats.getMax() ) );
299 System.out.println( " Min sequence length : " + ( ( int ) msa_stats.getMin() ) );
300 System.out.println( " Gap ratio : " + NF_4.format( MsaMethods.calcGapRatio( _msa ) ) );
301 System.out.println( " Normalized Shannon Entropy (entn21): "
302 + NF_4.format( MsaMethods.calcNormalizedShannonsEntropy( 21, _msa ) ) );
303 System.out.println();
306 public final List<MsaProperties> removeViaGapAverage( final double mean_gapiness ) throws IOException,
307 InterruptedException {
308 final GapContribution stats[] = calcGapContribtionsStats( _norm );
309 final List<String> to_remove_ids = new ArrayList<String>();
310 final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
311 for( final GapContribution gap_gontribution : stats ) {
312 to_remove_ids.add( gap_gontribution.getId() );
314 Phylogeny phy = null;
315 if ( _phylogentic_inference ) {
316 System.out.println( "calculating phylogentic tree..." );
317 System.out.println();
319 addSeqs2Tree( _msa, phy );
322 MsaProperties msa_prop = new MsaProperties( _msa, "", _calculate_shannon_entropy );
323 msa_props.add( msa_prop );
324 printMsaProperties( msa_prop );
325 System.out.println();
327 while ( MsaMethods.calcGapRatio( _msa ) > mean_gapiness ) {
328 final String id = to_remove_ids.get( i );
329 _removed_seq_ids.add( id );
330 final MolecularSequence deleted = _msa.deleteRow( id, true );
331 _removed_seqs.add( deleted );
333 if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( MsaMethods.calcGapRatio( _msa ) <= mean_gapiness ) ) {
334 msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id );
335 msa_props.add( msa_prop );
336 System.out.println();
338 else if ( isPrintMsaStats( i ) ) {
339 msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
340 msa_props.add( msa_prop );
341 printMsaProperties( msa_prop );
342 System.out.println();
346 if ( _removed_seqs_out_base != null ) {
347 final String msg = writeAndAlignRemovedSeqs();
348 System.out.println();
349 System.out.println( msg );
351 if ( _phylogentic_inference ) {
352 decorateTree( phy, msa_props, false );
358 public List<MsaProperties> removeViaLength( final int length ) throws IOException, InterruptedException {
359 final GapContribution stats[] = calcGapContribtionsStats( _norm );
360 final List<String> to_remove_ids = new ArrayList<String>();
361 final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
362 for( final GapContribution gap_gontribution : stats ) {
363 to_remove_ids.add( gap_gontribution.getId() );
365 Phylogeny phy = null;
366 if ( _phylogentic_inference ) {
367 System.out.println( "calculating phylogentic tree..." );
368 System.out.println();
370 addSeqs2Tree( _msa, phy );
373 MsaProperties msa_prop = new MsaProperties( _msa, "", _calculate_shannon_entropy );
374 msa_props.add( msa_prop );
375 printMsaProperties( msa_prop );
376 System.out.println();
378 while ( _msa.getLength() > length ) {
379 final String id = to_remove_ids.get( i );
380 _removed_seq_ids.add( id );
381 final MolecularSequence deleted = _msa.deleteRow( id, true );
382 _removed_seqs.add( deleted );
384 if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( _msa.getLength() <= length ) ) {
385 msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id );
386 msa_props.add( msa_prop );
387 System.out.println();
389 else if ( isPrintMsaStats( i ) ) {
390 msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
391 printMsaProperties( msa_prop );
392 msa_props.add( msa_prop );
393 System.out.println();
397 if ( _removed_seqs_out_base != null ) {
398 final String msg = writeAndAlignRemovedSeqs();
399 System.out.println();
400 System.out.println( msg );
402 if ( _phylogentic_inference ) {
403 decorateTree( phy, msa_props, false );
409 public final List<MsaProperties> removeWorstOffenders( final int to_remove ) throws IOException,
410 InterruptedException {
411 final GapContribution stats[] = calcGapContribtionsStats( _norm );
412 final List<String> to_remove_ids = new ArrayList<String>();
413 final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
414 for( int j = 0; j < to_remove; ++j ) {
415 to_remove_ids.add( stats[ j ].getId() );
417 Phylogeny phy = null;
418 if ( _phylogentic_inference ) {
419 System.out.println( "calculating phylogentic tree..." );
420 System.out.println();
422 addSeqs2Tree( _msa, phy );
425 MsaProperties msa_prop = new MsaProperties( _msa, "", _calculate_shannon_entropy );
426 msa_props.add( msa_prop );
427 printMsaProperties( msa_prop );
428 System.out.println();
429 for( int i = 0; i < to_remove_ids.size(); ++i ) {
430 final String id = to_remove_ids.get( i );
431 _removed_seq_ids.add( id );
432 final MolecularSequence deleted = _msa.deleteRow( id, true );
433 _removed_seqs.add( deleted );
435 if ( isPrintMsaStatsWriteOutfileAndRealign( i ) || ( i == ( to_remove_ids.size() - 1 ) ) ) {
436 msa_prop = printMsaStatsWriteOutfileAndRealign( _realign, id );
437 msa_props.add( msa_prop );
438 System.out.println();
440 else if ( isPrintMsaStats( i ) ) {
441 msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
442 msa_props.add( msa_prop );
443 printMsaProperties( msa_prop );
444 System.out.println();
447 if ( _removed_seqs_out_base != null ) {
448 final String msg = writeAndAlignRemovedSeqs();
449 System.out.println();
450 System.out.println( msg );
452 if ( _phylogentic_inference ) {
453 decorateTree( phy, msa_props, false );
457 System.out.println( "calculating phylogentic tree..." );
458 System.out.println();
459 Phylogeny phy2 = calcTree();
460 addSeqs2Tree( _msa, phy2 );
467 public final void setCalculateNormalizedShannonEntropy( final boolean calculate_shannon_entropy ) {
468 _calculate_shannon_entropy = calculate_shannon_entropy;
471 public void setInfileName( final String infile_name ) {
472 _infile_name = infile_name;
475 public final void setMafftOptions( final String maffts_opts ) {
476 _maffts_opts = maffts_opts;
479 public final void setNorm( final boolean norm ) {
483 final public void setOutFileBase( final File out_file_base ) {
484 _out_file_base = out_file_base;
487 public final void setOutputFormat( final MSA_FORMAT output_format ) {
488 _output_format = output_format;
491 public void setPathToMafft( final String path_to_mafft ) {
492 _path_to_mafft = path_to_mafft;
495 public void setPeformPhylogenticInference( final boolean phylogentic_inference ) {
496 _phylogentic_inference = phylogentic_inference;
499 public final void setRealign( final boolean realign ) {
503 public final void setRemovedSeqsOutBase( final File removed_seqs_out_base ) {
504 _removed_seqs_out_base = removed_seqs_out_base;
507 public final void setStep( final int step ) {
511 public final void setStepForDiagnostics( final int step_for_diagnostics ) {
512 _step_for_diagnostics = step_for_diagnostics;
515 final public String writeAndAlignRemovedSeqs() throws IOException, InterruptedException {
516 final StringBuilder msg = new StringBuilder();
517 final String n = _removed_seqs_out_base + "_" + _removed_seqs.size() + ".fasta";
518 SequenceWriter.writeSeqs( _removed_seqs, new File( n ), SEQ_FORMAT.FASTA, 100 );
519 msg.append( "wrote " + _removed_seqs.size() + " removed sequences to " + "\"" + n + "\"" );
521 final MsaInferrer mafft = Mafft.createInstance( _path_to_mafft );
522 final List<String> opts = new ArrayList<String>();
523 for( final String o : _maffts_opts.split( "\\s" ) ) {
526 final Msa removed_msa = mafft.infer( _removed_seqs, opts );
527 final Double gr = MsaMethods.calcGapRatio( removed_msa );
528 String s = _removed_seqs_out_base + "_" + removed_msa.getNumberOfSequences() + "_"
529 + removed_msa.getLength() + "_" + ForesterUtil.roundToInt( gr * 100 );
530 final String suffix = obtainSuffix();
532 writeMsa( removed_msa, s, _output_format );
533 msg.append( ", and as MSA of length " + removed_msa.getLength() + " to \"" + s + "\"" );
535 return msg.toString();
538 final public String writeMsa( final File outfile ) throws IOException {
539 final Double gr = MsaMethods.calcGapRatio( _msa );
540 final String s = outfile + "_" + _msa.getNumberOfSequences() + "_" + _msa.getLength() + "_"
541 + ForesterUtil.roundToInt( gr * 100 );
542 writeMsa( _msa, s + obtainSuffix(), _output_format );
546 final int calcNonGapResidues( final MolecularSequence seq ) {
548 for( int i = 0; i < seq.getLength(); ++i ) {
549 if ( !seq.isGapAt( i ) ) {
556 private final GapContribution[] calcGapContribtions( final boolean normalize_for_effective_seq_length ) {
557 final double gappiness[] = calcGappiness();
558 final GapContribution stats[] = new GapContribution[ _msa.getNumberOfSequences() ];
559 for( int row = 0; row < _msa.getNumberOfSequences(); ++row ) {
560 stats[ row ] = new GapContribution( _msa.getIdentifier( row ) );
561 for( int col = 0; col < _msa.getLength(); ++col ) {
562 if ( !_msa.isGapAt( row, col ) ) {
563 stats[ row ].addToValue( gappiness[ col ] );
566 if ( normalize_for_effective_seq_length ) {
567 stats[ row ].divideValue( calcNonGapResidues( _msa.getSequence( row ) ) );
570 stats[ row ].divideValue( _msa.getLength() );
576 final private GapContribution[] calcGapContribtionsStats( final boolean norm ) {
577 final GapContribution stats[] = calcGapContribtions( norm );
578 Arrays.sort( stats );
582 private final double[] calcGappiness() {
583 final int l = _msa.getLength();
584 final double gappiness[] = new double[ l ];
585 final int seqs = _msa.getNumberOfSequences();
586 for( int i = 0; i < l; ++i ) {
587 gappiness[ i ] = ( double ) MsaMethods.calcGapSumPerColumn( _msa, i ) / seqs;
592 private final Phylogeny collapse( final Msa msa, final int threshold ) {
593 final BasicSymmetricalDistanceMatrix m = PairwiseDistanceCalculator.calcFractionalDissimilarities( msa );
598 private final Phylogeny inferNJphylogeny( final PWD_DISTANCE_METHOD pwd_distance_method,
600 final boolean write_matrix,
601 final String matrix_name ) {
602 BasicSymmetricalDistanceMatrix m = null;
603 switch ( pwd_distance_method ) {
604 case KIMURA_DISTANCE:
605 m = PairwiseDistanceCalculator.calcKimuraDistances( msa );
607 case POISSON_DISTANCE:
608 m = PairwiseDistanceCalculator.calcPoissonDistances( msa );
610 case FRACTIONAL_DISSIMILARITY:
611 m = PairwiseDistanceCalculator.calcFractionalDissimilarities( msa );
614 throw new IllegalArgumentException( "invalid pwd method" );
616 if ( write_matrix ) {
618 m.write( ForesterUtil.createBufferedWriter( matrix_name ) );
620 catch ( final IOException e ) {
624 final NeighborJoiningF nj = NeighborJoiningF.createInstance( false, 5 );
625 final Phylogeny phy = nj.execute( m );
629 private final boolean isPrintMsaStats( final int i ) {
630 return ( ( ( _step == 1 ) && ( _step_for_diagnostics == 1 ) ) || ( ( _step_for_diagnostics > 0 ) && ( ( ( i + 1 ) % _step_for_diagnostics ) == 0 ) ) );
633 private final boolean isPrintMsaStatsWriteOutfileAndRealign( final int i ) {
634 return ( ( ( _step == 1 ) && ( _step_for_diagnostics == 1 ) ) || ( ( _step > 0 ) && ( ( ( i + 1 ) % _step ) == 0 ) ) );
637 private final StringBuilder msaPropertiesAsSB( final MsaProperties msa_properties ) {
638 final StringBuilder sb = new StringBuilder();
639 sb.append( msa_properties.getNumberOfSequences() );
641 sb.append( msa_properties.getLength() );
643 sb.append( NF_4.format( msa_properties.getGapRatio() ) );
644 if ( _calculate_shannon_entropy ) {
646 sb.append( NF_4.format( msa_properties.getEntropy7() ) );
648 sb.append( NF_4.format( msa_properties.getEntropy21() ) );
653 private String obtainSuffix() {
654 if ( _output_format == MSA_FORMAT.FASTA ) {
657 else if ( _output_format == MSA_FORMAT.PHYLIP ) {
663 private final Phylogeny pi( final String matrix, final int boostrap ) {
664 final Phylogeny master_phy = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, _msa, true, matrix );
667 final ResampleableMsa resampleable_msa = new ResampleableMsa( _msa );
668 final int[][] resampled_column_positions = BootstrapResampler.createResampledColumnPositions( _msa.getLength(),
671 final Phylogeny[] eval_phys = new Phylogeny[ n ];
672 for( int i = 0; i < n; ++i ) {
673 resampleable_msa.resample( resampled_column_positions[ i ] );
674 eval_phys[ i ] = inferNJphylogeny( PWD_DISTANCE_METHOD.KIMURA_DISTANCE, resampleable_msa, false, null );
676 ConfidenceAssessor.evaluate( "bootstrap", eval_phys, master_phy, true, 1 );
677 PhylogenyMethods.extractFastaInformation( master_phy );
681 private final void printMsaProperties( final MsaProperties msa_properties ) {
682 if ( ( _step == 1 ) || ( _step_for_diagnostics == 1 ) ) {
683 System.out.print( ForesterUtil.pad( msa_properties.getRemovedSeq(), _longest_id_length, ' ', false ) );
684 System.out.print( "\t" );
686 System.out.print( msaPropertiesAsSB( msa_properties ) );
687 System.out.print( "\t" );
690 final private MsaProperties printMsaStatsWriteOutfileAndRealign( final boolean realign, final String id )
691 throws IOException, InterruptedException {
695 final MsaProperties msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
696 printMsaProperties( msa_prop );
697 final String s = writeOutfile();
698 System.out.print( "-> " + s + ( realign ? "\t(realigned)" : "" ) );
702 private final void printTableHeader() {
703 if ( ( _step == 1 ) || ( _step_for_diagnostics == 1 ) ) {
704 System.out.print( ForesterUtil.pad( "Id", _longest_id_length, ' ', false ) );
705 System.out.print( "\t" );
707 System.out.print( "Seqs" );
708 System.out.print( "\t" );
709 System.out.print( "Length" );
710 System.out.print( "\t" );
711 System.out.print( "Gaps" );
712 System.out.print( "\t" );
713 if ( _calculate_shannon_entropy ) {
714 System.out.print( "entn7" );
715 System.out.print( "\t" );
716 System.out.print( "entn21" );
717 System.out.print( "\t" );
719 System.out.println();
722 final private void realignWithMafft() throws IOException, InterruptedException {
723 final MsaInferrer mafft = Mafft.createInstance( _path_to_mafft );
724 final List<String> opts = new ArrayList<String>();
725 for( final String o : _maffts_opts.split( "\\s" ) ) {
728 _msa = DeleteableMsa.createInstance( mafft.infer( _msa.asSequenceList(), opts ) );
731 final private void removeGapColumns() {
732 _msa.deleteGapOnlyColumns();
735 private final String writeOutfile() throws IOException {
736 final String s = writeMsa( _out_file_base );
740 // Returns null if not path found.
741 final public static String guessPathToMafft() {
743 if ( ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) >= 0 ) {
744 path = "C:\\Program Files\\mafft-win\\mafft.bat";
745 if ( MsaInferrer.isInstalled( path ) ) {
749 path = "/home/czmasek/SOFTWARE/MSA/MAFFT/mafft-7.130-without-extensions/scripts/mafft";
750 if ( MsaInferrer.isInstalled( path ) ) {
753 path = "/usr/local/bin/mafft";
754 if ( MsaInferrer.isInstalled( path ) ) {
757 path = "/usr/bin/mafft";
758 if ( MsaInferrer.isInstalled( path ) ) {
762 if ( MsaInferrer.isInstalled( path ) ) {
766 if ( MsaInferrer.isInstalled( path ) ) {
772 final private static void writeMsa( final Msa msa, final String outfile, final MSA_FORMAT format )
774 final Writer w = ForesterUtil.createBufferedWriter( outfile );
775 msa.write( w, format );