Changeset 3984


Ignore:
Timestamp:
Nov 20, 2007, 2:02:22 PM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #827: Add more web services methods

Added example code for downloading raw data files

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/RawBioAssay.java

    r3974 r3984  
    430430  {
    431431    super.toTransferable(info);
    432     info.setArrayDesignId(this.getArrayDesign().getId());
     432    ArrayDesign ad = getArrayDesign();
     433    info.setArrayDesignId(ad == null ? 0 : ad.getId());
    433434    info.setPlatformId(this.getPlatform().getId());
    434435    info.setRawDataTypeId(this.getRawDataType().getId());
  • trunk/src/examples/webservices/README

    r3961 r3984  
    2626  Your password for the BASE web server.
    2727
     28* download.dir
     29  Set this option to a valid directory path to enable download of raw data from
     30  all raw bioassays. NOTE! Be careful with this option if you have a lot of data.
     31  All files from all raw bioassay will be downloaded!
     32
    2833From the command line type:
    2934
     
    3641ant -Dservices.url=http://localhost/base/services
    3742 -Dservices.login=mylogin -Dservices.password=mypassword
     43 -Ddownload.dir=.
    3844
    3945
  • trunk/src/examples/webservices/build.xml

    r3953 r3984  
    1111  <property name="services.login" value="root" />
    1212  <property name="services.password" value="root" />
     13  <!-- set this to a valid directory to enable download of raw data from raw bioassays -->
     14  <property name="download.dir" value="" />
    1315 
    1416  <!-- variables for compiling -->
     
    4951      <arg value="${services.login}" />
    5052      <arg value="${services.password}" />
     53      <arg value="${download.dir}" />
    5154      <classpath>
    5255        <path refid="classpath" />
  • trunk/src/examples/webservices/src/net/sf/basedb/ws/example/Main.java

    r3961 r3984  
    2424package net.sf.basedb.ws.example;
    2525
     26import java.io.File;
     27import java.io.FileOutputStream;
     28import java.io.IOException;
     29import java.io.InputStream;
     30import java.io.OutputStream;
     31
    2632import org.apache.axis2.AxisFault;
    2733
     34import net.sf.basedb.info.DataFileTypeInfo;
    2835import net.sf.basedb.info.ExperimentInfo;
    2936import net.sf.basedb.info.ProjectInfo;
    3037import net.sf.basedb.info.QueryOptions;
     38import net.sf.basedb.info.RawBioAssayInfo;
    3139import net.sf.basedb.info.VersionInfo;
    3240import net.sf.basedb.ws.client.ExperimentClient;
    3341import net.sf.basedb.ws.client.ProjectClient;
     42import net.sf.basedb.ws.client.RawBioAssayClient;
    3443import net.sf.basedb.ws.client.SessionClient;
    3544
     
    4554 
    4655  This class will connect to the BASE server as specified in the
    47   <code>servicesUrl</code>, login, list all projects and experiments
    48   the the user has access to and then logout again.
     56  <code>servicesUrl</code>, login, list all projects, experiments
     57  and raw bioassays the user has access to. It may optionally download
     58  data files associated with a raw bioassay. The final step is to logout
     59  again.
    4960
    5061  @author nicklas
     
    5768  public static void main(String[] args)
    5869  {
    59     if (args.length != 3)
     70    if (args.length < 3 || args.length > 4)
    6071    {
    6172      showUsage();
     
    6576    String username = args[1];
    6677    String password = args[2];
     78    String directory = args.length >= 4 ? args[3] : null;
     79   
    6780
    6881    Main main = new Main();
     
    7891      main.listExperiments();
    7992      System.out.println("------------");
     93      main.listRawBioassays(directory);
     94      System.out.println("------------");
    8095      main.logout();
    8196    }
     
    89104  private static void showUsage()
    90105  {
    91     System.out.println("Usage: Main <url> <login> <password>");
    92     System.out.println("       url      : The URL to the BASE webservices server");
    93     System.out.println("       login    : The login for an existing user account on the BASE server");
    94     System.out.println("       password : The password for the account");
     106    System.out.println("Usage: Main <url> <login> <password> [directory]");
     107    System.out.println("       url       : The URL to the BASE webservices server");
     108    System.out.println("       login     : The login for an existing user account on the BASE server");
     109    System.out.println("       password  : The password for the account");
     110    System.out.println("       directory : Optional; if specified all raw bioassay data files will be downloaded to this directory");
    95111  }
    96112 
     
    196212  }
    197213 
     214  /**
     215    List all raw bioassays that the logged in user is has access to. For
     216    each raw bioassay, list it's datafiles and download those to the specified
     217    directory. If no directory is specified the download is disabled.
     218  */
     219  public void listRawBioassays(String directory)
     220    throws AxisFault
     221  {
     222    RawBioAssayClient rc = new RawBioAssayClient(session);
     223    RawBioAssayInfo[] rawBioAssays = rc.getRawBioAssays(new QueryOptions());
     224   
     225    File downloadDir = directory != null ? new File(directory) : null;
     226    boolean doDownload = downloadDir != null && downloadDir.isDirectory();
     227   
     228    if (rawBioAssays == null || rawBioAssays.length == 0)
     229    {
     230      System.out.println("No raw bioassays found.");
     231    }
     232    else
     233    {
     234      System.out.println(rawBioAssays.length + " raw bioassays found");
     235      int i = 0;
     236      while (i < rawBioAssays.length)
     237      {
     238        RawBioAssayInfo info = rawBioAssays[i];
     239        ++i;
     240        System.out.println( i + ". " + info.getName() + " [id=" + info.getId() + "]");
     241
     242        listDataFiles(rc, info, doDownload ? downloadDir : null);
     243      }
     244    }
     245  }
     246
     247  /**
     248    List all datafiles for a given raw bioassay and download them if a
     249    download directory is given.
     250  */
     251  public void listDataFiles(RawBioAssayClient rc, RawBioAssayInfo info, File downloadDir)
     252    throws AxisFault
     253  {
     254    DataFileTypeInfo[] files = rc.getDataFileTypes(info.getId(), new QueryOptions());
     255    if (files == null || files.length == 0)
     256    {
     257      System.out.println("   No data files found.");
     258    }
     259    else
     260    {
     261      for (DataFileTypeInfo fileType : files)
     262      {
     263        System.out.print("   " + fileType.getName());
     264        if (downloadDir != null)
     265        {
     266          String extension = fileType.getExtension() == null ? "" : "." + fileType.getExtension();
     267          File downloadTo = new File(downloadDir, info.getName() + extension);
     268          try
     269          {
     270            InputStream in = rc.downloadRawDataByType(info.getId(), fileType.getExternalId());
     271            OutputStream out = new FileOutputStream(downloadTo);
     272            copy(in, out);
     273            out.close();
     274            in.close();
     275            System.out.print("; Download --> " + downloadTo);
     276          }
     277          catch (IOException ex)
     278          {
     279            System.out.print("; Download failed: " + ex.getMessage());
     280          }
     281        }
     282        System.out.println("");
     283      }
     284    }
     285  }
     286 
     287  /**
     288    Copy from InputStream to OutputStream
     289  */
     290  public long copy(InputStream in, OutputStream out)
     291    throws IOException
     292  {
     293    int bytes = 0;
     294    long totalBytes = 0;
     295    byte[] buffer = new byte[1024];
     296    while (bytes != -1) // -1 = end of stream
     297    {
     298      bytes = in.read(buffer, 0, buffer.length);
     299      if (bytes > 0)
     300      {
     301        totalBytes += bytes;
     302        out.write(buffer, 0, bytes);
     303      }
     304    }
     305    return totalBytes;
     306  }
     307
    198308 
    199309}
Note: See TracChangeset for help on using the changeset viewer.