Ignore:
Timestamp:
Sep 22, 2009, 1:52:00 PM (13 years ago)
Author:
Nicklas Nordborg
Message:

References #242: Implement "Save to BASE" function in the MeV gui

The menu item is in place as well as the file saving/upload functionality. There is need for more error handling. The possibility of creating a new child bioassay set is not yet implemented. The file will always be attached to the same bioassay set as it was loaded from. The functionality has only been tested when the original file is an analysis file. Some slight adjustments are probably needed when starting with TDMS or CGH files.

Location:
extensions/net.sf.basedb.mev/trunk/src/webstart/net/sf/basedb/mev/webstart
Files:
8 added
3 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.mev/trunk/src/webstart/net/sf/basedb/mev/webstart/CmdLine.java

    r1157 r1163  
    11package net.sf.basedb.mev.webstart;
    22
    3 import java.util.ArrayList;
     3import java.io.UnsupportedEncodingException;
     4import java.net.URLDecoder;
     5import java.util.Arrays;
     6import java.util.Iterator;
     7import java.util.LinkedList;
    48import java.util.List;
     9import java.util.regex.Pattern;
    510
    611/**
    712  A very simple command line parser. It assumes that
    813  the command line is of the form: -option1 value1 -option2 value2 ...
     14  Values are expected to be URL-encoded in UTF-8.
    915 
    1016  @author nicklas
     
    1420{
    1521 
    16   private String[] args;
     22  private List<String> args;
    1723 
    1824  /**
     
    2228  public CmdLine(String... args)
    2329  {
    24     this.args = args;
     30    this.args = new LinkedList<String>(Arrays.asList(args));
    2531  }
    2632
     
    3339  public String getOption(String option)
    3440  {
    35     for (int i = 0; i < args.length - 1; ++i)
     41    Iterator<String> it = args.iterator();
     42    String value = null;
     43    while (it.hasNext())
    3644    {
    37       if (option.equals(args[i])) return args[i+1];
     45      if (option.equals(it.next()) && it.hasNext())
     46      {
     47        value = it.next();
     48        break;
     49      }
    3850    }
    39     return null;
     51    try
     52    {
     53      if (value != null) value = URLDecoder.decode(value, "UTF-8");
     54    }
     55    catch (UnsupportedEncodingException ex)
     56    {} // Should never happen
     57    return value;
     58  }
     59 
     60  /**
     61    Get the value for an option as an integer.
     62    @param option The name of the option, including the '-'
     63    @param defaultValue A default value in case the given value is not
     64      an integer
     65  */
     66  public int getIntOption(String option, int defaultValue)
     67  {
     68    int value = defaultValue;
     69    String sValue = getOption(option);
     70    if (sValue != null)
     71    {
     72      try
     73      {
     74        value = Integer.parseInt(sValue);
     75      }
     76      catch (NumberFormatException ex)
     77      {}
     78    }
     79    return value;
     80  }
     81 
     82  /**
     83    Add an option to the command line.
     84    @param option The name of the option
     85    @param value The value of the option
     86  */
     87  public void add(String option, String value)
     88  {
     89    args.add(option);
     90    args.add(value);
     91  }
     92 
     93  /**
     94    Remove all options matching the given pattern.
     95    @param pattern A regular expression pattern
     96  */
     97  public void remove(Pattern pattern)
     98  {
     99    Iterator<String> it = args.iterator();
     100    while (it.hasNext())
     101    {
     102      String option = it.next();
     103      if (pattern.matcher(option).matches())
     104      {
     105        it.remove();
     106        if (it.hasNext())
     107        {
     108          it.next();
     109          it.remove();
     110        }
     111      }
     112    }
    40113  }
    41114 
     
    45118    @return The modified command line with the options/values removed
    46119    */
    47   public String[] remove(String... options)
     120  public void remove(String... options)
    48121  {
    49     List<String> remain = new ArrayList<String>(args.length);
    50     for (int i = 0; i < args.length; ++i)
     122    Iterator<String> it = args.iterator();
     123    while (it.hasNext())
    51124    {
    52       boolean add = true;
     125      String option = it.next();
    53126      for (int j = 0; j < options.length; ++j)
    54127      {
    55         if (args[i].equals(options[j]))
     128        if (option.equals(options[j]))
    56129        {
    57           add = false;
    58           ++i;
     130          it.remove();
     131          if (it.hasNext())
     132          {
     133            it.next();
     134            it.remove();
     135          }
    59136          break;
    60137        }
    61138      }
    62       if (add)
    63       {
    64         remain.add(args[i]);
    65       }
    66139    }
    67     return remain.toArray(new String[0]);
    68140  }
     141 
     142  /**
     143    Create a new string array with the existing options.
     144  */
     145  public String[] toArray()
     146  {
     147    return args.toArray(new String[args.size()]);
     148  }
     149 
     150  @Override
     151  public String toString()
     152  {
     153    return args.toString();
     154  }
     155 
    69156}
    70157
  • extensions/net.sf.basedb.mev/trunk/src/webstart/net/sf/basedb/mev/webstart/MevUtil.java

    r1157 r1163  
    11package net.sf.basedb.mev.webstart;
    22
     3import java.io.BufferedInputStream;
    34import java.io.File;
     5import java.io.FileInputStream;
     6import java.io.IOException;
     7import java.io.InputStream;
     8import java.io.PrintWriter;
     9import java.io.StringWriter;
    410import java.lang.reflect.Method;
     11import java.net.URL;
     12
     13import javax.swing.JMenu;
     14import javax.swing.JMenuBar;
     15import javax.swing.JMenuItem;
     16import javax.swing.JOptionPane;
     17
     18
     19import net.sf.basedb.mev.webstart.action.SaveToBaseActionListener;
    520
    621import org.tigr.microarray.mev.MultipleArrayViewer;
     22import org.tigr.microarray.mev.cluster.gui.impl.dialogs.Progress;
     23import org.tigr.microarray.mev.resources.FileDownloader;
     24import org.tigr.microarray.mev.resources.SupportFileAccessError;
     25
     26import HTTPClient.HTTPConnection;
     27import HTTPClient.HTTPResponse;
     28import HTTPClient.HttpOutputStream;
     29import HTTPClient.ModuleException;
    730
    831/**
     
    3760  }
    3861 
     62  /**
     63    Saves the current analysis to a file. This method calls the
     64    MultipleArrayViewer.saveState() method using reflection.
     65    <p>
     66    NOTE! MeV starts a new thread for the saving and this method
     67    will return immediately.
     68   
     69    @param viewer The array viewer window that that should be saved
     70    @param file The file to save to
     71  */
     72  public static void saveAnalysisToFile(MultipleArrayViewer viewer, File file)
     73  {
     74    try
     75    {
     76      Method saveState = MultipleArrayViewer.class.getDeclaredMethod("saveState", File.class);
     77      saveState.setAccessible(true);
     78      saveState.invoke(viewer, file);
     79    }
     80    catch (Exception ex)
     81    {
     82      throw new RuntimeException(ex);
     83    }
     84  }
     85 
     86  /**
     87    Adds a top-level menu 'BASE' with a 'Save to BASE' menuitem.
     88    @param viewer The viewer window
     89    @param info Session information for the BASE connection
     90  */
     91  public static void addBaseMenus(MultipleArrayViewer viewer, SessionInfo info)
     92  {
     93    JMenuBar menubar = viewer.getMenubar();
     94    JMenu baseMenu = new JMenu("BASE");
     95    JMenuItem save = new JMenuItem("Save to BASE...");
     96    save.addActionListener(new SaveToBaseActionListener(viewer, info));
     97    baseMenu.add(save);
     98    menubar.add(baseMenu);
     99    menubar.validate();
     100  }
     101 
     102  /**
     103    Uploads a file to a web server by posting it to the given URL. The file
     104    data is written as binary bytes to the http request. This method will
     105    block until the upload has been finished. To interrupt the upload,
     106    call {@link Thread#interrupt()} on this thread.
     107   
     108    @param file The file on the local file system
     109    @param url The URL to post the file to
     110    @param progress An optional progress dialog that should receive
     111      notification about the upload progress
     112    @throws IOException If there is any problem with the upload
     113    @throws InterruptedException If the upload is interrupted
     114  */
     115  public static HTTPResponse uploadFile(File file, URL url, Progress progress)
     116    throws IOException, ModuleException, InterruptedException
     117  {
     118    HttpOutputStream out = null;
     119    InputStream in = null;
     120    HTTPResponse response = null;
     121   
     122    // Prepare the progress reporter
     123    int totalBytes = 0;
     124    if (progress != null)
     125    {
     126      progress.setUnits((int)file.length());
     127      if (!progress.isShowing()) progress.show();
     128    }
     129
     130    try
     131    {
     132      // Open a connection to the server and prepare input and output stream
     133      HTTPConnection c = new HTTPConnection(url.getProtocol(), url.getHost(), url.getPort());
     134      out = new HttpOutputStream((int)file.length());
     135      in = new BufferedInputStream(new FileInputStream(file));
     136      response = c.Post(url.getFile(), out);
     137      byte[] buffer = new byte[4096];
     138      for (int bytes = 0; bytes != -1;) // -1 = end of stream
     139      {
     140        bytes = in.read(buffer, 0, buffer.length);
     141        if (Thread.interrupted())
     142        {
     143          c.stop();
     144          throw new InterruptedException();
     145        }
     146        if (bytes > 0)
     147        {
     148          out.write(buffer, 0, bytes);
     149          totalBytes += bytes;
     150          if (progress != null)
     151          {
     152            progress.setValue(totalBytes);
     153            progress.repaint();
     154          }
     155        }
     156        //Thread.sleep(5);
     157      }
     158    }
     159    finally
     160    {
     161      try
     162      {
     163        if (in != null) in.close();
     164        if (out != null) out.close();
     165      }
     166      catch (IOException ex)
     167      {}
     168    }
     169    return response;
     170  }
     171 
     172  /**
     173    Download the contents from the given URL.
     174    @param url The URL to download from
     175    @return The temporary file where the downloaded data has been stored
     176  */
     177  public static File downloadFile(URL url)
     178    throws SupportFileAccessError
     179  {
     180    FileDownloader downloader = FileDownloader.getInstance(url);
     181    File anlFile = downloader.getTempFile(url.getFile());
     182    return anlFile;
     183  }
     184 
     185  /**
     186    Open a dialog window that shows the error message.
     187    @param title The title of the dialog
     188    @param t The error
     189  */
     190  public static void showError(String title, Throwable t)
     191  {
     192    StringWriter writer = new StringWriter();
     193    t.printStackTrace(new PrintWriter(writer));
     194    JOptionPane.showMessageDialog(null,
     195      t.getMessage() + writer.toString(),
     196      title, JOptionPane.ERROR_MESSAGE);
     197  }
     198 
    39199}
  • extensions/net.sf.basedb.mev/trunk/src/webstart/net/sf/basedb/mev/webstart/StartMev.java

    r1157 r1163  
    33import java.io.File;
    44import java.net.URL;
     5import java.util.regex.Pattern;
    56
    6 import javax.swing.JOptionPane;
    77
    88import org.tigr.microarray.mev.Manager;
    99import org.tigr.microarray.mev.MultipleArrayViewer;
    1010import org.tigr.microarray.mev.TMEV;
    11 import org.tigr.microarray.mev.resources.FileDownloader;
    1211
    1312/**
     
    2827  public static void main(String[] args)
    2928  {
    30     // Parse the command line options
     29    // Parse the command line options and store in the session info object
    3130    CmdLine options = new CmdLine(args);
    32     String fileType = options.getOption("-fileType");
    33     String fileUrl = options.getOption("-fileUrl");
    34     boolean analysisFile = "anl".equals(fileType);
     31    SessionInfo info = new SessionInfo();
     32    info.setSessionId(options.getOption("-base:session"));
     33    info.setServletDir(options.getOption("-base:servletdir"));
     34    info.setFileName(options.getOption("-base:file"));
     35    info.setFileType(options.getOption("-fileType"));
     36    info.setBioAssaySet(options.getIntOption("-base:bioAssaySet", 0));
    3537   
    36     // Remove the 'fileType' and 'fileUrl' options if the file
    37     // is an analysis file since MeV can't handle it
     38    // Remove BASE-specific options from the command line
     39    String downloadUrl = info.getDownloadURL();
     40    boolean analysisFile = info.isAnalysisFile();
     41    options.remove(Pattern.compile("-base.*"));
    3842    if (analysisFile)
    3943    {
    40       args = options.remove("-fileType", "-fileUrl");
     44      // Remove the 'fileType' and 'fileUrl' options if the file
     45      // is an analysis file since MeV can't handle it
     46      options.remove("-fileType", "-fileUrl");
    4147    }
     48    else
     49    {
     50      // For other files, add the 'fileUrl' option
     51      options.add("-fileUrl", downloadUrl);
     52    }
     53    args = options.toArray();
    4254   
    4355    // Start MeV and get the main window
    4456    TMEV.main(args);
    4557    final MultipleArrayViewer viewer = (MultipleArrayViewer)Manager.getLastComponent();
     58   
     59    // Add BASE-specific menu items
     60    MevUtil.addBaseMenus(viewer, info);
    4661   
    4762    if (analysisFile)
     
    5065      try
    5166      {
    52         URL url = new URL(fileUrl);
    53         FileDownloader downloader = FileDownloader.getInstance(url);
    54         File anlFile = downloader.getTempFile(url.getPath());
     67        File anlFile = MevUtil.downloadFile(new URL(downloadUrl));
    5568        if (anlFile != null) MevUtil.loadAnalysisFromFile(viewer, anlFile);
    5669      }
    5770      catch (Exception ex)
    5871      {
    59         ex.printStackTrace();
    60         JOptionPane.showMessageDialog(viewer,
    61           ex.getMessage(),
    62           "Load Analysis Error", JOptionPane.ERROR_MESSAGE);
     72        MevUtil.showError("Load Analysis Error", ex);
    6373      }
    6474    }
    65    
    6675  }
    6776
Note: See TracChangeset for help on using the changeset viewer.