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

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

updated generic test

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 all special characters in UI inputs that Galaxy substitutes for
63# security reasons. This methods turns them back to their original values before
64# using them (i.e. sending them to the taverna server
65#
66def sanitize(string)
67  string.gsub(/(__sq__|__dq__|__at__|__cr__|__cn__|__tc__|__gt__|__lt__|__ob__|__cb__|__oc__|__cc__)/) do
68    if $1 == '__sq__'
69      "'"
70    elsif $1 == '__dq__'
71      "'"
72    elsif $1 == '__cr__'
73      "\r"
74    elsif $1 == '__cn__'
75      "\n"
76    elsif $1 == '__tc__'
77      "\t"
78    elsif $1 == '__gt__'
79      '>'
80    elsif $1 == '__lt__'
81      '<'
82    elsif $1 == '__ob__'
83      '['
84    elsif $1 == '__cb__'
85      ']'
86    elsif $1 == '__oc__'
87      '{'
88    elsif $1 == '__cc__'
89      '}'
90    else
91      '@'
92    end
93  end
94end
95
96#
97# Deletes last new line of file if it exists! It is needed for t2 workflows that
98# do not sanitize properly, i.e. via a user-provided beanshell script
99#
100def chomp_last_newline(file)
101
102  if File.file?(file) and File.size(file) > 1
103    f = open(file, "rb+")
104    f.seek(-1, File::SEEK_END)
105    f.truncate(File.size(file) - 1) if f.read(1) == "\n"
106    f.close
107  end
108
109end
110
111
112# use the uri reference to download the workflow locally
113wkf_file = URI.parse('http://www.myexperiment.org/workflows/74/download/bioaid_proteindiscovery_221429.xml')
114in_wkf = open(wkf_file)
115
116wkf = in_wkf.read()
117
118# create run
119begin
120  run = T2Server::Run.create('http://localhost:8980/taverna-server', wkf)
121rescue T2Server::T2ServerError => e
122  exit 1
123end
124
125#
126# Get input arguments -- for each input a boolean specifies if it's from history
127# thus, for each t2_workflow input we have two arguments in the script!
128#
129Query_from_history = ARGV[0].chomp
130Query_tmp = ARGV[1].chomp
131if Query_from_history == "true"
132  chomp_last_newline(Query_tmp)
133  run.upload_input_file('Query', Query_tmp)
134else
135  run.set_input('Query', sanitize(Query_tmp))
136end
137maxHits_parameter_from_history = ARGV[2].chomp
138maxHits_parameter_tmp = ARGV[3].chomp
139if maxHits_parameter_from_history == "true"
140  chomp_last_newline(maxHits_parameter_tmp)
141  run.upload_input_file('maxHits_parameter', maxHits_parameter_tmp)
142else
143  run.set_input('maxHits_parameter', sanitize(maxHits_parameter_tmp))
144end
145
146# get results_as_zip input and open zip file if appropriate
147zipped = ARGV[4].chomp == "yes"
148zip_out = Zip::ZipFile.open("/tmp/#{run.uuid}.zip", Zip::ZipFile::CREATE) if zipped
149
150
151# start run and wait until it is finished
152run.start
153run.wait(:progress => true)
154
155# get output arguments and associated them with a file
156output5 = File.open(ARGV[5], "w")
157begin
158  get_outputs(run, false, output5, 'ValidatedProtein', zip_out)
159rescue Exception => err
160  get_outputs(run, false, output5, 'ValidatedProtein.error', zip_out)
161ensure
162  output5.close
163end
164output6 = File.open(ARGV[6], "w")
165begin
166  get_outputs(run, false, output6, 'UniProtID', zip_out)
167rescue Exception => err
168  get_outputs(run, false, output6, 'UniProtID.error', zip_out)
169ensure
170  output6.close
171end
172
173# close zip_out (the newly created zip file) if opened
174zip_out.close if zip_out
175
176# open galaxy zip output and write zip file
177if zipped
178  galaxy_zip_out = File.open(ARGV[7], "w")
179  output_zip_file(run.uuid, galaxy_zip_out)
180  galaxy_zip_out.close
181end
182
183# delete run
184run.delete
Note: See TracBrowser for help on using the browser.