Skip to content

Instantly share code, notes, and snippets.

@cth
Created September 22, 2009 15:47
Show Gist options
  • Save cth/191174 to your computer and use it in GitHub Desktop.
Save cth/191174 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Christian Theil Have, Sep. 2009.
require 'rubygems'
require 'xmlsimple'
class String
def to_prolog
"'" + self.gsub("'",'\\\\\'') + "'"
end
def to_prolog_array
arr = []
self.downcase.each_byte { |b| arr << b.chr }
arr.to_prolog
end
def to_prolog_atom
self.downcase
end
end
class Array
def to_prolog
to_prolog_list
end
def to_prolog_facts
(self.map{ |e| e.to_prolog}).join('\n')
end
def to_prolog_list
"[" + (self.map { |e| e.to_prolog}).join(',') + "]"
end
end
class BlastMatch
def initialize(hit_id, hit_features)
@hit_id, @features = hit_id, hit_features
end
def to_prolog
prolog_str = ""
@features.each do |key,val|
prolog_str << "blast_match(#{@hit_id.to_prolog}, #{key.to_s}, #{val})." << "\n"
end
prolog_str
end
end
class BlastXML2Prolog
def initialize(infile,outfile)
@xml = XmlSimple.xml_in infile
iteration = @xml['BlastOutput_iterations'][0]['Iteration'][0]
hits = []
iteration_hits = iteration['Iteration_hits'][0]['Hit']
iteration_hits.each do |h|
first_hsp = h['Hit_hsps'][0]['Hsp'][0]
hits << BlastMatch.new(
h['Hit_id'].first.to_s,
{
:hit_num => h['Hit_num'][0].to_s.to_prolog,
:organism => h['Hit_def'][0].to_prolog,
:evalue => first_hsp['Hsp_evalue'][0].to_s.to_prolog,
:qseq => first_hsp['Hsp_qseq'][0].to_s.to_prolog_array,
:hseq => first_hsp['Hsp_hseq'][0].to_s.to_prolog_array
}
)
end
File.open(outfile, "w") { |f| hits.each { |h| f << h.to_prolog } }
end
end
if ARGV.length != 2
puts "Usage is blastxml2prolog inputfile outputfile"
exit -1
end
BlastXML2Prolog.new(ARGV[0],ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment