Changeset 88

Show
Ignore:
Timestamp:
10-02-11 15:02:37 (3 years ago)
Author:
kostas.karasavvas@…
Message:

v0.2.4 updated comments plus getting t2 results as zip possible

Location:
galaxy-taverna/workflow-to-galaxy
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • galaxy-taverna/workflow-to-galaxy/bin/workflow_to_galaxy.rb

    r66 r88  
    5353end 
    5454 
    55 # Set taverna server 
    56 if t2_server == "" 
    57   t2_server = "http://localhost:8980/taverna-server" 
    58 end 
     55# Set taverna server if not specified 
     56t2_server = "http://localhost:8980/taverna-server"  if t2_server == "" 
    5957 
    6058 
    6159 
     60# Generate Galaxy tool's files 
    6261generate_xml(me_rest, xml_file) 
    6362generate_script(me_rest, t2_server, script_file) 
  • galaxy-taverna/workflow-to-galaxy/lib/workflow-to-galaxy/generator.rb

    r83 r88  
    1  
    2 # Contains code to generate the Galaxy's tool xml and script files 
     1# 
     2# The Generator module contains two public methods that generate a Galaxy's tool 
     3# XML and script files 
     4# 
    35module Generator 
    4  
    56 
    67  # private methods 
    78  private 
    89 
    9  
     10  # Used to create indentation when generating code 
    1011  def indent(n) 
    1112    ind = "" 
     
    1415  end 
    1516 
    16  
     17  # Galaxy's XML tool tag 
    1718  def tool_b(out, name) 
    1819    out.write("<tool id=\"#{name}_id\" name=\"#{name}\">\n") 
    1920  end 
    2021 
     22  # Galaxy's XML command tag 
    2123  def command_be(out, me_rest, script) 
    2224    out.write indent(1) + "<command interpreter=\"ruby\">\n" 
    2325    out.write indent(2) + script + "\n" 
     26 
     27    # inputs 
    2428    me_rest.workflow.inputs.each do |i| 
    2529      out.write indent(2) + "#if $#{i.name}_source.history_or_textfield == \"textfield\":\n" 
     
    2933      out.write indent(2) + "#end if\n" 
    3034    end 
     35 
     36    # results as zip input 
     37    out.write indent(2) + "$results_as_zip\n" 
     38 
     39    # outputs 
    3140    out.write indent(2) 
    3241    me_rest.workflow.outputs.each do |o| 
    3342      out.write "$#{o.name} " 
    3443    end 
    35     out.write "\n" 
     44 
     45    # result zip output 
     46    out.write "$result_zip\n" 
     47     
    3648    out.write indent(1) + "</command>\n" 
    3749  end 
    3850 
     51 
     52  # Galaxy's XML inputs tag 
    3953  def inputs_be(out, inputs) 
    4054    out.write indent(1) + "<inputs>\n" 
     
    5266        out.write indent(4) + "<param name=\"textfield_#{i.name}\" type=\"text\" area=\"True\" size=\"2x50\" " 
    5367        if i.examples.size >= 1  
    54           # escape double quotes characters and add <BR> for newlines for galaxy's xml file 
     68          # escape double quotes characters for galaxy's xml file 
    5569          ex = i.examples[0].to_s.gsub('"', '&quot;') 
    5670          # convert newlines to HTML newlines to display in textareas inputs 
     
    6478    else 
    6579      out.write indent(2) + "<param name=\"input\" type=\"select\" display=\"radio\" size=\"250\" label=\"This workflow has no inputs\" />\n" 
    66     end   
     80    end 
     81 
     82    # result as zip input 
     83    out.write indent(2) + "<param name=\"results_as_zip\" type=\"select\" label=\"Would you also like the raw results as a zip file\">" 
     84    out.write indent(3) + "<option value=\"yes\">Yes</option>" 
     85    out.write indent(3) + "<option value=\"no\" selected=\"true\">No</option>" 
     86    out.write indent(2) + "</param>" 
     87 
    6788    out.write indent(1) + "</inputs>\n" 
    6889  end 
    6990 
     91  # Galaxy's XML outputs tag 
    7092  def outputs_be(out, outputs) 
    7193    out.write indent(1) + "<outputs>\n" 
     
    7395      out.write indent(2) + "<data format=\"tabular\" name=\"#{o.name}\" label=\"#{o.name}\"/>\n" 
    7496    end 
     97 
     98    # result zip output 
     99    out.write indent(2) + "<data format=\"zip\" name=\"result_zip\" label=\"Compressed Results (zip)\">" 
     100    out.write indent(3) + "<filter>results_as_zip == \"yes\"</filter>" 
     101    out.write indent(2) + "</data>" 
     102 
    75103    out.write indent(1) + "</outputs>\n" 
    76104  end 
    77105 
     106  # Galaxy's XML help tag 
    78107  def help_be(out, me_rest) 
    79108    out.write indent(1) + "<help>\n" 
     
    100129    out.write "\n"        
    101130 
     131    # if at least one input add it to tool's UI help description 
    102132    if me_rest.workflow.inputs.size >= 1 
    103133      out.write "-----\n\n" 
     
    133163    end 
    134164 
    135     # TODO this code is identical to the inputs code above -- method? 
     165    # if at least one output add it to tool's UI help description 
    136166    if me_rest.workflow.outputs.size >= 1 
    137167      out.write "-----\n\n" 
     
    177207  end 
    178208 
     209  # Galaxy's XML tool tag close 
    179210  def tool_e(out) 
    180211    out.write("</tool>\n") 
     
    183214 
    184215 
    185  
     216  # Galaxy's script preample 
    186217  def script_preample(out) 
    187218    out.write("#!/usr/bin/env ruby\n\n") 
    188219    out.write("require 'rubygems'\n") 
    189220    out.write("require 't2-server'\n") 
    190     out.write("require 'open-uri'\n\n") 
    191   end 
    192  
     221    out.write("require 'open-uri'\n") 
     222    out.write("require 'zip/zipfilesystem'\n\n") 
     223  end 
     224 
     225 
     226  # Galaxy's script utility methods 
     227  # TODO: check sanitise's double quote escaping! wkf 74 + 
     228  # TODO: ensure newline works for multi/sinle line strings as example values 
    193229  def script_util_methods(out) 
    194230 
    195     out.write <<UTIL_METHODS 
     231    out.write <<'UTIL_METHODS' 
     232 
     233# sends the zip file to specified output 
     234def output_zip_file(uuid, zip_out) 
     235  File.open("/tmp/#{uuid}.zip") do |zip| 
     236    while data = zip.read(4096) 
     237      zip_out.write data 
     238    end 
     239  end 
     240  File.delete("/tmp/#{uuid}.zip") 
     241end 
     242 
     243 
     244# 
     245# replicates the directory result structure as constructed by the 
     246# taverna server and recreates it in a zip File 
     247# 
     248def add_to_zip_file(output_dir, data_lists, zip_out)  
     249  zip_out.dir.mkdir("#{output_dir}") 
     250  data_lists.each_with_index do |item, index| 
     251    if item.instance_of? Array 
     252      add_to_zip_file("#{output_dir}/#{index+1}", item, zip_out) 
     253    else 
     254      zip_out.file.open("#{output_dir}/#{index+1}", "w") { |f| f.puts item } 
     255    end 
     256  end 
     257end 
     258 
    196259     
    197260# method that flattens the list of list of list ... result of get_output 
     
    207270 
    208271 
    209 # method that acquires all the results of the specified output 
    210 def get_outputs(run, refs, outfile, dir) 
     272# 
     273# Method that acquires all the results of the specified output directory. 
     274# If valid zip File is passed it also accumulates results as a zip file. 
     275# 
     276def get_outputs(run, refs, outfile, dir, zip_out=nil)  
    211277  data_lists = run.get_output(dir, refs) 
    212278  print_flattened_result(outfile, data_lists) 
     279  if zip_out 
     280    add_to_zip_file(dir, data_lists, zip_out) 
     281  end 
    213282end 
    214283 
     
    243312    f = open(file, "rb+") 
    244313    f.seek(-1, File::SEEK_END) 
    245     f.truncate(File.size(file) - 1) if f.read(1) == "\\n" 
     314    f.truncate(File.size(file) - 1) if f.read(1) == "\n" 
    246315    f.close 
    247316  end 
     
    254323  end 
    255324 
    256  
     325  # Galaxy's script taverna 2 run 
    257326  def script_create_t2_run(out, wkf_uri, t2_uri) 
    258327    out.write <<CREATE_T2_RUN 
     
    275344 
    276345 
     346  # Galaxy's script input handling 
    277347  def script_init_inputs(out, me_rest) 
    278     out.write "# get input arguments -- for each input a boolean specifies if it's from history\n" 
     348    out.write "#\n" 
     349    out.write "# Get input arguments -- for each input a boolean specifies if it's from history\n" 
    279350    out.write "# thus, for each me_rest input we have two arguments in the script!\n" 
     351    out.write "#\n" 
    280352    me_rest.workflow.inputs.each_with_index do |input, i| 
    281353      i_name = input.name.to_s 
     
    290362    end 
    291363 
    292   end 
    293  
    294  
     364    # add code to handle results_as_zip input, i.e. create zip file 
     365    out.write "\n# get results_as_zip input and open zip file if appropriate\n" 
     366    # get argument index after workflow inputs 
     367    zip_index = me_rest.workflow.inputs.size * 2 
     368    out.write "zipped = ARGV[#{zip_index}].chomp == \"yes\"\n" 
     369    out.write 'zip_out = Zip::ZipFile.open("/tmp/#{run.uuid}.zip", Zip::ZipFile::CREATE) if zipped' + "\n" 
     370     
     371  end 
     372 
     373 
     374  # Galaxy's script starting taverna run 
    295375  def script_start_run(out) 
    296376    out.write <<START_RUN 
     377 
    297378 
    298379# start run and wait until it is finished 
     
    304385 
    305386 
     387  # Galaxy's script output handling 
    306388  def script_get_outputs(out, me_rest) 
    307     outputs_start_index = me_rest.workflow.inputs.size * 2 
     389    # outputs start after all inputs plus the results_as_zip input 
     390    outputs_start_index = me_rest.workflow.inputs.size * 2 + 1 
    308391    outputs_end_index = outputs_start_index + me_rest.workflow.outputs.size - 1 
    309392    out.write "# get output arguments and associated them with a file\n" 
     
    311394      out.write "output#{o} = File.open(ARGV[#{o}], \"w\")\n" 
    312395      out.write "begin\n" 
    313       out.write "  get_outputs(run, false, output#{o}, '" + me_rest.workflow.outputs[o - outputs_start_index].name.to_s + "')\n" 
     396      out.write "  get_outputs(run, false, output#{o}, '" + me_rest.workflow.outputs[o - outputs_start_index].name.to_s + "', zip_out)\n" 
    314397      out.write "rescue Exception => err\n" 
    315       out.write "  get_outputs(run, false, output#{o}, '" + me_rest.workflow.outputs[o - outputs_start_index].name.to_s + ".error')\n" 
     398      out.write "  get_outputs(run, false, output#{o}, '" + me_rest.workflow.outputs[o - outputs_start_index].name.to_s + ".error', zip_out)\n" 
     399      out.write "ensure\n" 
     400      out.write "  output#{o}.close\n" 
    316401      out.write "end\n" 
    317402    end 
    318403 
    319   end 
    320  
    321  
     404    # close zip file if created when dealing with the inputs 
     405    #-- 
     406    # ideally that would semantically belong to a separate method 
     407    out.write "\n# close zip_out (the newly created zip file) if opened\n" 
     408    out.write "zip_out.close if zip_out\n" 
     409 
     410    # dealing with zip output here -- zip arg index is outputs_end_index (last 
     411    # output) plus 1 
     412    out.write "\n# open galaxy zip output and write zip file\n" 
     413    out.write "if zipped\n" 
     414    out.write "  galaxy_zip_out = File.open(ARGV[#{outputs_end_index+1}], \"w\")\n" 
     415    out.write "  output_zip_file(run.uuid, galaxy_zip_out)\n" 
     416    out.write "  galaxy_zip_out.close\n" 
     417    out.write "end\n" 
     418     
     419  end 
     420 
     421 
     422  # Galaxy's script cleaning taverna run 
    322423  def script_finish_run(out) 
    323424    out.write "\n# delete run\n" 
     
    325426  end 
    326427 
     428 
     429   
    327430 
    328431 
  • galaxy-taverna/workflow-to-galaxy/Rakefile

    r81 r88  
    1313spec = Gem::Specification.new do |s| 
    1414  s.name = 'workflow-to-galaxy' 
    15   s.version = '0.2.2' 
     15  s.version = '0.2.4' 
    1616  s.has_rdoc = true 
    1717  s.extra_rdoc_files = ['README', 'LICENSE']