src/org/gel/mauve/contigs/ContigOrderer.java

Go to the documentation of this file.
00001 package org.gel.mauve.contigs;
00002 
00003 import java.io.File;
00004 import java.io.IOException;
00005 import java.util.Hashtable;
00006 import java.util.Iterator;
00007 import java.util.LinkedList;
00008 import java.util.Vector;
00009 
00010 import javax.swing.JFileChooser;
00011 import javax.swing.JOptionPane;
00012 import javax.swing.event.EventListenerList;
00013 
00014 import org.gel.air.util.IOUtils;
00015 import org.gel.mauve.MauveConstants;
00016 import org.gel.mauve.MauveHelperFunctions;
00017 import org.gel.mauve.ModelListener;
00018 import org.gel.mauve.MyConsole;
00019 import org.gel.mauve.XMFAAlignment;
00020 import org.gel.mauve.XmfaViewerModel;
00021 import org.gel.mauve.gui.AlignmentProcessListener;
00022 import org.gel.mauve.gui.MauveFrame;
00023 
00024 public class ContigOrderer implements MauveConstants {
00025         
00026         protected File directory;
00027         protected File unordered;
00028         protected File reference;
00029         protected File align_dir;
00030         public static final String DIR_STUB = "alignment";
00031         protected int count = 1;
00032         protected int start = 0;
00033         protected ContigReorderer reorderer;
00034         protected MauveFrame parent;
00035         protected ContigMauveAlignFrame align;
00036         public static final int DEFAULT_ITERATIONS = 15;
00037         protected int iterations = DEFAULT_ITERATIONS;
00038         public static final String ALIGN_START = "Start from alignment file.";
00039         public static final String SEQ_START = "Start from sequence files.";
00040         protected boolean align_start;
00041         protected Vector past_orders;
00042         protected boolean gui;
00043         protected static final String OUTPUT_DIR = "-output";
00044         protected static final String REF_FILE = "-ref";
00045         protected static final String DRAFT_FILE = "-draft";
00046         private EventListenerList alnListeners;
00047         private File alnmtFile;
00048         
00049         public ContigOrderer (String [] args, Vector frames, boolean gui) {
00050                 init (args, frames, gui);
00051                 if (gui)
00052                         initGUI ();
00053                 else
00054                         initParams (args);
00055         }
00056         
00057         public ContigOrderer (String [] args, Vector frames) {
00058                 this (args, frames, true);
00059         }
00060         
00061         public void initParams (String [] args) {
00062                 Hashtable <String, String> pairs = IOUtils.parseDashPairedArgs(args);
00063                 String error = null;
00064                 try {
00065                         if (pairs.containsKey(OUTPUT_DIR)) {
00066                                 directory = new File (pairs.get(OUTPUT_DIR));
00067                                 if (!directory.exists()) {
00068                                         if (!directory.mkdirs())
00069                                                 error = "Couldn't create output directory";
00070                                 }
00071                                 else if (getAlignDir ().exists())
00072                                         error = "Directory already contains reorder";
00073                         }
00074                         else
00075                                 error = "Output dir not given";
00076                         if (pairs.containsKey(REF_FILE)) {
00077                                 System.out.println ("ref file: " + align);
00078                                 align.addSequence(pairs.get(REF_FILE));
00079                         }
00080                         else
00081                                 error = "no reference file given";
00082                         if (pairs.containsKey(DRAFT_FILE)) {
00083                                 align.addSequence(pairs.get(DRAFT_FILE));
00084                         }
00085                         else
00086                                 error = "no draft file given";
00087                 } catch (Exception e) {
00088                         e.printStackTrace();
00089                         error = e.getMessage();
00090                 }
00091                 if (error != null) {
00092                         if(gui){
00093                                 JOptionPane.showMessageDialog(null, error);
00094                         }else{
00095                                 System.err.println(error);
00096                         }
00097                         System.exit(0);
00098                 }
00099                 else {
00100                         align.setArgs (pairs);
00101                         startAlignment (false);
00102                 }
00103         }
00104         
00105         public void initGUI () {
00106                 reorderer.init();
00107                 if (getFiles ()) {
00108                         startAlignment (true);
00109                 }
00110                 else
00111                         iterations = 0;
00112         }
00113         
00114         public void init (String [] args, Vector frames, boolean gui) {
00115                 this.gui = gui;
00116                 alnListeners = new EventListenerList();
00117                 past_orders = new Vector ();
00118                 iterations = DEFAULT_ITERATIONS;
00119                 if (args != null && args.length > 0) {
00120                         try {
00121                                 iterations = Integer.parseInt (args [0]);
00122                         } catch (NumberFormatException e) {
00123                         }
00124                 }
00125                 reorderer = new ContigReorderer (this, frames);
00126                 MyConsole.setUseSwing (gui);
00127                 MyConsole.showConsole ();
00128                 reorderer.ref_ind = 0;
00129                 reorderer.reorder_ind = 1;
00130                 align = new ContigMauveAlignFrame (
00131                                 reorderer, this, gui);
00132         }
00133         
00134         protected void startAlignment (boolean show_message) {
00135                 align.displayFileInput ();
00136                 align.setVisible(gui);
00137                 if (show_message) {
00138                         JOptionPane.showMessageDialog (null, 
00139                                         "The reordering will begin when the start button is pressed.  " +
00140                                         "It is an iterative process,\nand may take anywhere " +
00141                                         "from a half hour to several hours.  It may\nbe cancelled " +
00142                                         "at any point (intermediary results will be viewable).\n" +
00143                                         "If it is cancelled after the first reorder "
00144                                         + "the data will be\navailable in fasta files in the " +
00145                                         "corresponding output directory, although\nan alignment of" +
00146                                         " the last order will not be produced.  If the ordering" +
00147                                         " process\n is not manually ended, it will terminate when it "
00148                                         + "finds an order has repeated.\nSometimes the order will" + 
00149                                         " cycle through several possibilities; this\nindicates it" +
00150                                         " cannot determine which of them is most likely.\n " +
00151                                         "Alignment parameters may be changed before reorder starts or " +
00152                                         "any time between alignments.");
00153                 }
00154                 else
00155                         align.alignButtonActionPerformed (null);
00156         }
00157         
00158         public boolean getFiles () {
00159                 JFileChooser chooser = new JFileChooser ();
00160                 chooser.setDialogTitle("Choose location to keep output files and folders.");
00161                 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
00162                 chooser.setMultiSelectionEnabled (false);
00163                 boolean active = true;
00164                 while (active) {
00165                         int choice = chooser.showDialog(parent, "OK");
00166                         if (choice == JFileChooser.APPROVE_OPTION) {
00167                                 directory = chooser.getSelectedFile();
00168                                 if (getAlignDir ().exists()) {
00169                                         JOptionPane.showMessageDialog (parent, "Directory already " +
00170                                                         "contains reorder; please choose new directory", 
00171                                                         "Chooose new directory", 
00172                                                         JOptionPane.INFORMATION_MESSAGE);
00173                                 }
00174                                 else {
00175                                         /*Object val = JOptionPane.showInputDialog (parent, "Choose input type:", 
00176                                         "Start from:", JOptionPane.QUESTION_MESSAGE, null, new String [] {
00177                                         SEQ_START, ALIGN_START}, SEQ_START);
00178                         if (val == null)
00179                                 return false;
00180                         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
00181                         if (val == SEQ_START) {
00182                                 return true;
00183                         }
00184                         else {
00185                                 JOptionPane.showMessageDialog (parent, "Alignment should contain only the " +
00186                                                 "source and reference genome;\n the reference genome should be displayed " +
00187                                                 "above the genome to reorder.\n  Select the alignment file and not the " +
00188                                                 "directory containing the alignment.\n  For alignments generated from the " +
00189                                                 "contig reordering program,\n this is the file with the identical name to " +
00190                                                 "the containing directory\n (alignment followed by a number).",
00191                                                 "Start from alignment", JOptionPane.INFORMATION_MESSAGE);
00192                                 chooser.setDialogTitle ("Select alignment file");
00193                                 choice = chooser.showDialog (parent, "OK");
00194                                 if (choice == JFileChooser.APPROVE_OPTION) {
00195                                         align_start = true;
00196                                         align_dir = chooser.getSelectedFile ();
00197                                         reorderer.loadFile (align_dir);
00198                                 }
00199                         }*/
00200                                         return true;
00201                                 }
00202                         }
00203                         else
00204                                 return false;
00205                 }
00206                 return false;
00207         }
00208         
00209         protected void setFilesFromAlignStart () {
00210                 align_dir = align_dir.getParentFile ();
00211                 XMFAAlignment xmfa = ((XmfaViewerModel) parent.getModel ()).getXmfa ();
00212                 reference = new File (align_dir, new File (xmfa.getName (0)).getName ());
00213                 String name = new File (xmfa.getName (1)).getName ();
00214                 unordered = new File (align_dir, name);
00215                 File file = new File (align_dir,
00216                                 reorderer.file + ContigReorderer.FEATURE_EXT);
00217                 if (file.exists ())
00218                         parent.getFeatureImporter ().importAnnotationFile (file, reorderer.fix);
00219                 //iterations = 0;
00220         }
00221         
00222         public boolean shouldReorder () {
00223                 boolean ok = count <= iterations;
00224                 if (align_start)
00225                         setFilesFromAlignStart ();
00226                 /*if (!ok)
00227                         renameLastAlignment ();*/
00228                 return ok;
00229         }
00230         
00231         public void renameLastAlignment () {
00232                 File from = getAlignDir ();
00233                 File to = new File (directory, "final_" + DIR_STUB);
00234                 from.renameTo (to);
00235         }
00236         
00237         public void reorderDone () {
00238                 try {
00239                         System.out.println ("C: " + count);
00240                         File temp = null;
00241                         if (!align_start)
00242                                 temp = new File (getAlignDir (), CONTIG_OUTPUT);
00243                         else {
00244                                 temp = new File (align_dir, CONTIG_OUTPUT);
00245                         }
00246                         // ugh, this is really ugly hardcoding.
00247                         String alnmtFilename = directory + "/alignment" + count;
00248                         alnmtFilename += "/alignment" + count;
00249                         alnmtFile = new File( alnmtFilename );
00250                         count++;
00251                         if (orderRepeated () || count > iterations) {
00252                                 iterations = 0;
00253                                 IOUtils.deleteDir (temp);
00254                                 reorderer.active = false;
00255                                 if (gui) {
00256                                         JOptionPane.showMessageDialog(parent, "The reordering process is done.\n" +
00257                                                         "Results are displayed, and data is in output directory.", 
00258                                                         "Reorder Done", JOptionPane.INFORMATION_MESSAGE);
00259                                         reorderer.inverted_from_start.clear ();
00260                                 }
00261                                 else if (alnListeners != null){
00262                                         fireAlignmentEvent();
00263                                 }else {
00264                                         System.exit(0);
00265                                 }
00266                         }
00267                         else {
00268                                 past_orders.add(reorderer.ordered);
00269                                 File to = makeAlignDir ();
00270                                 temp.renameTo (to);
00271                                 temp = new File (to, reference.getName ());
00272                                 IOUtils.copyFile (reference, temp);
00273                                 reference = temp;
00274                                 unordered = new File (to, MauveHelperFunctions.genomeNameToFasta (
00275                                                 reorderer.fix));
00276                                 reorderer.feature_file = null;
00277                                 startAlignment (align_start);
00278                                 align_start = false;
00279                         }
00280                 } catch (Exception e) {
00281                         e.printStackTrace();
00282                 }
00283         }
00284         
00285         protected boolean orderRepeated () {
00286                 for (int i = 0; i < past_orders.size(); i++) {
00287                         if (reorderer.ordered.equals(past_orders.get(i)))
00288                                 return true;
00289                 }
00290                 return false;
00291         }
00292 
00293         public void addAlignmentProcessListener(AlignmentProcessListener listener){
00294                 alnListeners.add(AlignmentProcessListener.class, listener);
00295         }
00296 
00297         private void fireAlignmentEvent(){
00298                 Object [] listeners = alnListeners.getListenerList ();
00299                 for (int i = listeners.length - 2; i >= 0; i -= 2) {
00300                         if (listeners[i] == AlignmentProcessListener.class) {
00301                                 ((AlignmentProcessListener) listeners[i + 1]).completeAlignment (0);
00302                         }
00303                 }
00304         }
00305 
00306         public File getAlignmentFile(){
00307                 return alnmtFile;
00308         }               
00309         public void copyInputFiles () {
00310                 try {
00311                         File dir = makeAlignDir ();
00312                         dir.mkdirs ();
00313                         File file = new File (dir, reference.getName ());
00314                         IOUtils.copyFile (reference, file);
00315                         reference = file;
00316                         file = new File (dir, unordered.getName ());
00317                         IOUtils.copyFile (unordered, file);
00318                         unordered = file;
00319                 } catch (IOException e) {
00320                         e.printStackTrace();
00321                 }
00322         }
00323         
00324         protected File makeAlignDir () {
00325                 File dir = getAlignDir ();
00326                 while (dir.exists ()) {
00327                         count++;
00328                         start++;
00329                         iterations++;
00330                         dir = getAlignDir ();
00331                 }
00332                 //dir.mkdirs ();
00333                 return dir;
00334         }
00335         
00336         public File getAlignDir () {
00337                 return new File (directory.getAbsolutePath(), DIR_STUB + count);
00338         }
00339         
00340         
00341         public static void main (String [] args) {      
00342                 new ContigOrderer (args, null, args.length == 0);
00343         }
00344 
00345 }

Generated on Mon Aug 19 06:03:41 2013 for Mauve by doxygen 1.3.6