+ _removed_seqs = new ArrayList<MolecularSequence>();
+ }
+
+ 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.
+ }
+ }
+ }
+ }
+ return phy;
+ }
+
+ public final List<MsaProperties> chart( final int step, final boolean realign, final boolean normalize_for_effective_seq_length )
+ throws IOException, InterruptedException {
+ final GapContribution stats[] = calcGapContribtionsStats( normalize_for_effective_seq_length );
+ final List<String> to_remove_ids = new ArrayList<String>();
+ final List<MsaProperties> msa_props = new ArrayList<MsaProperties>();
+ for( final GapContribution gap_gontribution : stats ) {
+ to_remove_ids.add( gap_gontribution.getId() );
+ }
+ Phylogeny phy = null;
+ if ( _phylogentic_inference ) {
+ System.out.println( "calculating phylogentic tree..." );
+ System.out.println();
+ phy = calcTree();
+ addSeqs2Tree( _msa, phy );
+ }
+ if ( !_realign ) {
+ _step = -1;
+ }
+ int x = ForesterUtil.roundToInt( _msa.getNumberOfSequences() / 10.0 );
+ if ( x < 2 ) {
+ x = 2;
+ }
+ MsaProperties msa_prop = new MsaProperties( _msa, "", _calculate_shannon_entropy );
+ msa_props.add( msa_prop );
+ 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, _calculate_shannon_entropy );
+ msa_props.add( msa_prop );
+ printMsaProperties( msa_prop );
+ System.out.print( "(realigned)" );
+ System.out.println();
+ }
+ else if ( isPrintMsaStats( i ) ) {
+ removeGapColumns();
+ msa_prop = new MsaProperties( _msa, id, _calculate_shannon_entropy );
+ msa_props.add( msa_prop );
+ printMsaProperties( msa_prop );
+ System.out.println();
+ }
+ ++i;
+ }
+
+ if ( _phylogentic_inference ) {
+ decorateTree( phy, msa_props, true );
+ displayTree( phy );
+ }
+ return msa_props;
+ }
+
+ private final static void addSeqs2Tree( final Msa msa, final Phylogeny phy ) {
+ for( int i = 0; i < msa.getNumberOfSequences(); ++i ) {
+ final MolecularSequence seq = msa.getSequence( i );
+ final String seq_name = seq.getIdentifier();
+ final PhylogenyNode n = phy.getNode( seq_name );
+ if ( !n.getNodeData().isHasSequence() ) {
+ n.getNodeData().addSequence( new org.forester.phylogeny.data.Sequence() );
+ }
+ else {
+ throw new IllegalArgumentException( "this should not have happened" );
+ }
+ n.getNodeData().getSequence().setMolecularSequence( seq.getMolecularSequenceAsString() );
+ n.getNodeData().getSequence().setMolecularSequenceAligned( true );
+ n.getNodeData().getSequence().setName( seq_name );
+ }
+ }
+
+ private final static void decorateTree( final Phylogeny phy,
+ final List<MsaProperties> msa_props,
+ final boolean chart_only ) {
+ 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() );
+ }
+ }
+ 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();
+ if ( chart_only ) {
+ while ( it.hasNext() ) {
+ final NodeVisualData vis = new NodeVisualData();
+ vis.setFillType( NodeFill.SOLID );
+ vis.setShape( NodeShape.RECTANGLE );
+ vis.setNodeColor( min_color );
+ it.next().getNodeData().setNodeVisualData( vis );
+ }
+ }
+ 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 + "]" );
+ if ( !chart_only ) {
+ final NodeVisualData vis = new NodeVisualData();
+ vis.setFillType( NodeFill.SOLID );
+ vis.setShape( NodeShape.RECTANGLE );
+ vis.setNodeColor( ForesterUtil.calcColor( msa_prop.getLength(), min, max, mean_color, max_color ) );
+ n.getNodeData().setNodeVisualData( vis );
+ }
+ else {
+ n.getNodeData()
+ .getNodeVisualData()
+ .setNodeColor( ForesterUtil.calcColor( msa_prop.getLength(),
+ min,
+ max,
+ mean,
+ min_color,
+ max_color,
+ mean_color ) );
+ }
+ }
+ }
+ }
+
+ final public void deleteGapColumns( final double max_allowed_gap_ratio ) {
+ _msa.deleteGapColumns( max_allowed_gap_ratio );
+ }
+
+ 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.setDisplayMultipleSequenceAlignment( true );
+ config.setShowScale( true );
+ config.setAddTaxonomyImagesCB( false );
+ config.setBaseFontSize( 9 );
+ config.setBaseFontFamilyName( "Arial" );
+ Archaeopteryx.createApplication( phy, config, _infile_name );