cleanup
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / PhylogeneticInferrer.java
index 4f17c69..9e651fb 100644 (file)
@@ -26,7 +26,6 @@
 package org.forester.archaeopteryx.tools;
 
 import java.io.BufferedWriter;
-import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -42,11 +41,11 @@ import org.forester.evoinference.distance.PairwiseDistanceCalculator;
 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
 import org.forester.evoinference.tools.BootstrapResampler;
 import org.forester.io.parsers.FastaParser;
-import org.forester.io.writers.SequenceWriter;
-import org.forester.io.writers.SequenceWriter.SEQ_FORMAT;
 import org.forester.msa.BasicMsa;
+import org.forester.msa.ClustalOmega;
 import org.forester.msa.Mafft;
 import org.forester.msa.Msa;
+import org.forester.msa.Msa.MSA_FORMAT;
 import org.forester.msa.MsaInferrer;
 import org.forester.msa.MsaMethods;
 import org.forester.msa.ResampleableMsa;
@@ -58,7 +57,7 @@ import org.forester.sequence.Sequence;
 import org.forester.tools.ConfidenceAssessor;
 import org.forester.util.ForesterUtil;
 
-public class PhylogeneticInferrer implements Runnable {
+public class PhylogeneticInferrer extends RunnableProcess {
 
     private Msa                                _msa;
     private final MainFrameApplication         _mf;
@@ -86,19 +85,25 @@ public class PhylogeneticInferrer implements Runnable {
         _options = options;
     }
 
-    private Msa inferMsa() throws IOException, InterruptedException {
-        final File temp_seqs_file = File.createTempFile( "__msa__temp__", ".fasta" );
-        if ( DEBUG ) {
-            System.out.println();
-            System.out.println( "temp file: " + temp_seqs_file );
-            System.out.println();
+    private Msa inferMsa( final MSA_PRG msa_prg ) throws IOException, InterruptedException {
+        //        final File temp_seqs_file = File.createTempFile( "__msa__temp__", ".fasta" );
+        //        if ( DEBUG ) {
+        //            System.out.println();
+        //            System.out.println( "temp file: " + temp_seqs_file );
+        //            System.out.println();
+        //        }
+        //        //final File temp_seqs_file = new File( _options.getTempDir() + ForesterUtil.FILE_SEPARATOR + "s.fasta" );
+        //        final BufferedWriter writer = new BufferedWriter( new FileWriter( temp_seqs_file ) );
+        //        SequenceWriter.writeSeqs( _seqs, writer, SEQ_FORMAT.FASTA, 100 );
+        //        writer.close();
+        switch ( msa_prg ) {
+            case MAFFT:
+                return runMAFFT( _seqs, processMafftOptions() );
+            case CLUSTAL_O:
+                return runClustalOmega( _seqs, processMafftOptions() );
+            default:
+                return null;
         }
-        //final File temp_seqs_file = new File( _options.getTempDir() + ForesterUtil.FILE_SEPARATOR + "s.fasta" );
-        final BufferedWriter writer = new BufferedWriter( new FileWriter( temp_seqs_file ) );
-        SequenceWriter.writeSeqs( _seqs, writer, SEQ_FORMAT.FASTA, 100 );
-        writer.close();
-        final List<String> opts = processMafftOptions();
-        return runMAFFT( temp_seqs_file, opts );
     }
 
     private List<String> processMafftOptions() {
@@ -145,7 +150,7 @@ public class PhylogeneticInferrer implements Runnable {
                 e.printStackTrace();
             }
         }
-        final NeighborJoining nj = new NeighborJoining();
+        final NeighborJoining nj = NeighborJoining.createInstance();
         final Phylogeny phy = nj.execute( m );
         PhylogeneticInferrer.extractFastaInformation( phy );
         return phy;
@@ -156,12 +161,14 @@ public class PhylogeneticInferrer implements Runnable {
         if ( ( _msa == null ) && ( _seqs == null ) ) {
             throw new IllegalArgumentException( "cannot run phylogenetic analysis with null msa and seq array" );
         }
+        start( _mf, "phylogenetic inference" );
         if ( _msa == null ) {
             Msa msa = null;
             try {
-                msa = inferMsa();
+                msa = inferMsa( MSA_PRG.MAFFT );
             }
             catch ( final IOException e ) {
+                end( _mf );
                 JOptionPane.showMessageDialog( _mf,
                                                "Could not create multiple sequence alignment with \""
                                                        + _options.getMsaPrg() + "\" and the following parameters:\n\""
@@ -175,6 +182,7 @@ public class PhylogeneticInferrer implements Runnable {
                 return;
             }
             catch ( final Exception e ) {
+                end( _mf );
                 JOptionPane.showMessageDialog( _mf,
                                                "Could not create multiple sequence alignment with \""
                                                        + _options.getMsaPrg() + "\" and the following parameters:\n\""
@@ -188,6 +196,7 @@ public class PhylogeneticInferrer implements Runnable {
                 return;
             }
             if ( msa == null ) {
+                end( _mf );
                 JOptionPane.showMessageDialog( _mf,
                                                "Could not create multiple sequence alignment with "
                                                        + _options.getMsaPrg() + "\nand the following parameters:\n\""
@@ -198,7 +207,7 @@ public class PhylogeneticInferrer implements Runnable {
             }
             if ( DEBUG ) {
                 System.out.println( msa.toString() );
-                System.out.println( MsaMethods.calcBasicGapinessStatistics( msa ).toString() );
+                System.out.println( MsaMethods.calcGapRatio( msa ) );
             }
             final MsaMethods msa_tools = MsaMethods.createInstance();
             if ( _options.isExecuteMsaProcessing() ) {
@@ -206,6 +215,7 @@ public class PhylogeneticInferrer implements Runnable {
                                                   _options.getMsaProcessingMinAllowedLength(),
                                                   msa );
                 if ( msa == null ) {
+                    end( _mf );
                     JOptionPane.showMessageDialog( _mf,
                                                    "Less than two sequences longer than "
                                                            + _options.getMsaProcessingMinAllowedLength()
@@ -218,7 +228,7 @@ public class PhylogeneticInferrer implements Runnable {
             if ( DEBUG ) {
                 System.out.println( msa_tools.getIgnoredSequenceIds() );
                 System.out.println( msa.toString() );
-                System.out.println( MsaMethods.calcBasicGapinessStatistics( msa ).toString() );
+                System.out.println( MsaMethods.calcGapRatio( msa ) );
             }
             _msa = msa;
         }
@@ -237,7 +247,8 @@ public class PhylogeneticInferrer implements Runnable {
             ConfidenceAssessor.evaluate( "bootstrap", eval_phys, master_phy, true, 1 );
         }
         _mf.getMainPanel().addPhylogenyInNewTab( master_phy, _mf.getConfiguration(), "nj", "njpath" );
-        _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
+        //  _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
+        end( _mf );
         JOptionPane.showMessageDialog( _mf,
                                        "Inference successfully completed",
                                        "Inference Completed",
@@ -256,11 +267,12 @@ public class PhylogeneticInferrer implements Runnable {
         }
     }
 
-    private Msa runMAFFT( final File input_seqs, final List<String> opts ) throws IOException, InterruptedException {
+    private Msa runMAFFT( final List<Sequence> seqs, final List<String> opts ) throws IOException, InterruptedException {
         Msa msa = null;
-        final MsaInferrer mafft = Mafft.createInstance();
+        final MsaInferrer mafft = Mafft.createInstance( _mf.getInferenceManager().getPathToLocalMafft()
+                .getCanonicalPath() );
         try {
-            msa = mafft.infer( input_seqs, opts );
+            msa = mafft.infer( seqs, opts );
         }
         catch ( final IOException e ) {
             System.out.println( mafft.getErrorDescription() );
@@ -268,12 +280,26 @@ public class PhylogeneticInferrer implements Runnable {
         return msa;
     }
 
+    private Msa runClustalOmega( final List<Sequence> seqs, final List<String> opts ) throws IOException,
+            InterruptedException {
+        Msa msa = null;
+        final MsaInferrer clustalo = ClustalOmega.createInstance( _mf.getInferenceManager().getPathToLocalClustalo()
+                .getCanonicalPath() );
+        try {
+            msa = clustalo.infer( seqs, opts );
+        }
+        catch ( final IOException e ) {
+            System.out.println( clustalo.getErrorDescription() );
+        }
+        return msa;
+    }
+
     private void writeToFiles( final BasicSymmetricalDistanceMatrix m ) {
         if ( !ForesterUtil.isEmpty( _options.getIntermediateFilesBase() ) ) {
             try {
                 final BufferedWriter msa_writer = new BufferedWriter( new FileWriter( _options.getIntermediateFilesBase()
                         + MSA_FILE_SUFFIX ) );
-                _msa.write( msa_writer );
+                _msa.write( msa_writer, MSA_FORMAT.PHYLIP );
                 msa_writer.close();
                 final BufferedWriter pwd_writer = new BufferedWriter( new FileWriter( _options.getIntermediateFilesBase()
                         + PWD_FILE_SUFFIX ) );
@@ -317,4 +343,8 @@ public class PhylogeneticInferrer implements Runnable {
             }
         }
     }
+
+    public enum MSA_PRG {
+        MAFFT, CLUSTAL_O;
+    }
 }