Skip to content

Instantly share code, notes, and snippets.

@shujishigenobu
Last active May 27, 2016 04:57
Show Gist options
  • Select an option

  • Save shujishigenobu/6edabc947262a1ba59ee163caa5174b4 to your computer and use it in GitHub Desktop.

Select an option

Save shujishigenobu/6edabc947262a1ba59ee163caa5174b4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support/core_ext/hash/conversions'
require 'open-uri'
require 'json'
require 'yaml'
require 'pp'
infile = ARGV[0]
outfmt = (ARGV[1] || 'simple')
def print_usage
puts "Usage: "
puts ""
puts " #{$0} runParameters.xml [outfmt]"
puts ""
puts " runParameters.xml: file or URL(http://~)"
puts " outfmt: json, json_pretty, yaml, ruby, or simple"
puts " (default = simple)"
puts ""
exit
end
def convert(h, format)
case format
when 'json'
json = h.to_json
puts json
when 'json_pretty'
puts JSON.pretty_generate(h)
when 'yaml'
yaml = h['RunParameters']['Setup'].to_yaml
puts yaml
when 'simple'
hh = h['RunParameters']['Setup']
h2 = Hash.new
h2['RunID'] = hh['RunID']
date = Date.parse(hh['RunStartDate']).to_s
h2['RunStartDate'] = date
h2['RunMode'] = hh['RunMode']
h2['SeqType'] = (hh['PairEndFC'] == 'true' ? "Paired-end" : "Single-read")
h2['ReadPattern'] = "Read1:#{hh['Read1']} + Index1:#{hh['IndexRead1']}"
h2['ReadPattern'] += " + Index2:#{hh['IndexRead2']}" unless hh['IndexRead2'] == '0'
h2['ReadPattern'] += " + Read2:#{hh['Read2']}" unless hh['Read2'] == '0'
h2['ClusteringChoice'] = hh['ClusteringChoice']
h2['Flowcell'] = hh['Flowcell']
h2['Sbs'] = hh['Sbs']
h2['Index'] = hh['Index']
puts h2.to_yaml
when 'ruby'
pp h['RunParameters']['Setup']
else
raise "No outfmt option"
end
end
unless infile
print_usage
end
xml = nil
if /^http:.+\.xml/.match(infile)
uri = infile
xml = open(uri).read
else
xml = File.open(infile).read
end
h = Hash.from_xml(xml)
puts "# source: #{infile}"
puts ""
convert(h, outfmt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment