root/galaxy-taverna/workflow-to-galaxy/test/Expected_BioAID_ProteinDiscovery_rb @ 89

Revision 89, 4.3 KB (checked in by kostas.karasavvas@…, 3 years ago)

updated test plus produces better looking XML

Line 
1#!/usr/bin/env ruby
2
3require 'rubygems'
4require 't2-server'
5require 'open-uri'
6require 'zip/zipfilesystem'
7
8
9# sends the zip file to specified output
10def output_zip_file(uuid, zip_out)
11  File.open("/tmp/#{uuid}.zip") do |zip|
12    while data = zip.read(4096)
13      zip_out.write data
14    end
15  end
16  File.delete("/tmp/#{uuid}.zip")
17end
18
19
20#
21# replicates the directory result structure as constructed by the
22# taverna server and recreates it in a zip File
23#
24def add_to_zip_file(output_dir, data_lists, zip_out) 
25  zip_out.dir.mkdir("#{output_dir}")
26  data_lists.each_with_index do |item, index|
27    if item.instance_of? Array
28      add_to_zip_file("#{output_dir}/#{index+1}", item, zip_out)
29    else
30      zip_out.file.open("#{output_dir}/#{index+1}", "w") { |f| f.puts item }
31    end
32  end
33end
34
35   
36# method that flattens the list of list of list ... result of get_output
37def print_flattened_result(out, data_lists)
38  data_lists.each do |l|
39    if l.instance_of? Array
40      print_flattened_result(out, l)
41    else
42      out.puts l
43    end
44  end
45end
46
47
48#
49# Method that acquires all the results of the specified output directory.
50# If valid zip File is passed it also accumulates results as a zip file.
51#
52def get_outputs(run, refs, outfile, dir, zip_out=nil) 
53  data_lists = run.get_output(dir, refs)
54  print_flattened_result(outfile, data_lists)
55  if zip_out
56    add_to_zip_file(dir, data_lists, zip_out)
57  end
58end
59
60
61#
62# Sanitize single quotes, double quotes, at symbol and newline in str. E.g. Galaxy
63# substitutes them to __sq__, __dq__, __at__ and __cr____cn__ respectively. This
64# methods turns them back to their original values before using them (i.e.
65# sending them to the taverna server
66#
67def sanitize(string)
68  string.gsub(/(__sq__|__dq__|__at__|__cr____cn__)/) do
69    if $1 == '__sq__'
70      "'"
71    elsif $1 == '__dq__'
72      '\\\"'
73    elsif $1 == '__cr____cn__'
74      '
'
75    else
76      '@'
77    end
78  end
79end
80
81#
82# Deletes last new line of file if it exists! It is needed for t2 workflows that
83# do not sanitize properly, i.e. via a user-provided beanshell script
84#
85def chomp_last_newline(file)
86
87  if File.file?(file) and File.size(file) > 1
88    f = open(file, "rb+")
89    f.seek(-1, File::SEEK_END)
90    f.truncate(File.size(file) - 1) if f.read(1) == "\n"
91    f.close
92  end
93
94end
95
96
97
98# use the uri reference to download the workflow locally
99wkf_file = URI.parse('http://www.myexperiment.org/workflows/74/download/bioaid_proteindiscovery_221429.xml?version=3')
100in_wkf = open(wkf_file)
101wkf = in_wkf.read()
102
103# create run
104begin
105  run = T2Server::Run.create('http://localhost:8980/taverna-server', wkf)
106rescue T2Server::T2ServerError => e
107  exit 1
108end
109
110#
111# Get input arguments -- for each input a boolean specifies if it's from history
112# thus, for each me_rest input we have two arguments in the script!
113#
114Query_from_history = ARGV[0].chomp
115Query_tmp = ARGV[1].chomp
116if Query_from_history == "true"
117  chomp_last_newline(Query_tmp)
118  run.upload_input_file('Query', Query_tmp)
119else
120  run.set_input('Query', sanitize(Query_tmp))
121end
122maxHits_parameter_from_history = ARGV[2].chomp
123maxHits_parameter_tmp = ARGV[3].chomp
124if maxHits_parameter_from_history == "true"
125  chomp_last_newline(maxHits_parameter_tmp)
126  run.upload_input_file('maxHits_parameter', maxHits_parameter_tmp)
127else
128  run.set_input('maxHits_parameter', sanitize(maxHits_parameter_tmp))
129end
130
131# get results_as_zip input and open zip file if appropriate
132zipped = ARGV[4].chomp == "yes"
133zip_out = Zip::ZipFile.open("/tmp/#{run.uuid}.zip", Zip::ZipFile::CREATE) if zipped
134
135
136# start run and wait until it is finished
137run.start
138run.wait(:progress => true)
139
140# get output arguments and associated them with a file
141output5 = File.open(ARGV[5], "w")
142begin
143  get_outputs(run, false, output5, 'ValidatedProtein', zip_out)
144rescue Exception => err
145  get_outputs(run, false, output5, 'ValidatedProtein.error', zip_out)
146ensure
147  output5.close
148end
149output6 = File.open(ARGV[6], "w")
150begin
151  get_outputs(run, false, output6, 'UniProtID', zip_out)
152rescue Exception => err
153  get_outputs(run, false, output6, 'UniProtID.error', zip_out)
154ensure
155  output6.close
156end
157
158# close zip_out (the newly created zip file) if opened
159zip_out.close if zip_out
160
161# open galaxy zip output and write zip file
162if zipped
163  galaxy_zip_out = File.open(ARGV[7], "w")
164  output_zip_file(run.uuid, galaxy_zip_out)
165  galaxy_zip_out.close
166end
167
168# delete run
169run.delete
Note: See TracBrowser for help on using the browser.