1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
#
# = bio/appl/iprscan/report.rb - a class for iprscan output.
#
# Copyright:: Copyright (C) 2006
# Mitsuteru C. Nakao <mn@kazusa.or.jp>
# License:: The Ruby License
#
#
# == Report classes for the iprscan program.
#
module Bio
class Iprscan
# = DESCRIPTION
# Class for InterProScan report. It is used to parse results and reformat
# results from (raw|xml|txt) into (html, xml, ebihtml, txt, gff3) format.
#
# See ftp://ftp.ebi.ac.uk/pub/software/unix/iprscan/README.html
#
# == USAGE
# # Read a marged.txt and split each entry.
# Bio::Iprscan::Report.parse_txt(File.read("marged.txt")) do |report|
# report.query_id
# report.matches.size
# report.matches.each do |match|
# match.ipr_id #=> 'IPR...'
# match.ipr_description
# match.method
# match.accession
# match.description
# match.match_start
# match.match_end
# match.evalue
# end
# # report.to_gff3
# # report.to_html
# end
#
# Bio::Iprscan::Report.parse_raw(File.read("marged.raw")) do |report|
# report.class #=> Bio::Iprscan::Report
# end
#
class Report
# Entry delimiter pattern.
RS = DELIMITER = "\n\/\/\n"
# Qeury sequence name (entry_id).
attr_accessor :query_id
alias :entry_id :query_id
# Qeury sequence length.
attr_accessor :query_length
# CRC64 checksum of query sequence.
attr_accessor :crc64
# Matched InterPro motifs in Hash. Each InterPro motif have :name,
# :definition, :accession and :motifs keys. And :motifs key contains
# motifs in Array. Each motif have :method, :accession, :definition,
# :score, :location_from and :location_to keys.
attr_accessor :matches
# == USAGE
# Bio::Iprscan::Report.parse_raw(File.open("merged.raw")) do |report|
# report
# end
#
def self.parse_raw(io)
entry = ''
while line = io.gets
if entry != '' and entry.split("\t").first == line.split("\t").first
entry << line
elsif entry != ''
yield Bio::Iprscan::Report.parse_raw_entry(entry)
entry = line
else
entry << line
end
end
yield Bio::Iprscan::Report.parse_raw_entry(entry) if entry != ''
end
# Parser method for a raw formated entry. Retruns a Bio::Iprscan::Report
# object.
def self.parse_raw_entry(str)
report = self.new
str.split(/\n/).each do |line|
line = line.split("\t")
report.matches << Match.new(:query_id => line[0],
:crc64 => line[1],
:query_length => line[2].to_i,
:method => line[3],
:accession => line[4],
:description => line[5],
:match_start => line[6].to_i,
:match_end => line[7].to_i,
:evalue => line[8],
:status => line[9],
:date => line[10])
if line[11]
report.matches.last.ipr_id = line[11]
report.matches.last.ipr_description = line[12]
end
report.matches.last.go_terms = line[13].scan(/(\w+ \w+\:.+? \(GO:\d+\))/).flatten if line[13]
end
report.query_id = report.matches.first.query_id
report.query_length = report.matches.first.query_length
report
end
# Parser method for a xml formated entry. Retruns a Bio::Iprscan::Report
# object.
# def self.parse_xml(str)
# end
# Splits the entry stream.
#
# == Usage
#
# Bio::Iprscan::Report.reports_txt(File.open("merged.txt")) do |report|
# report.class #=> Bio::Iprscan::Report
# end
#
def self.parse_txt(io)
io.each("\n\nSequence") do |entry|
if entry =~ /Sequence$/
entry = entry.sub(/Sequence$/, '')
end
unless entry =~ /^Sequence/
entry = 'Sequence' + entry
end
yield self.parse_txt_entry(entry)
end
end
# Parser method for a txt formated entry. Returns a Bio::Iprscan::Report
# object.
#
def self.parse_txt_entry(str)
unless str =~ /^Sequence /
raise ArgumentError, "Invalid format: \n\n#{str}"
end
header, *matches = str.split(/\n\n/)
report = self.new
report.query_id = if header =~ /Sequence \"(.+)\" / then $1 else '' end
report.query_length = if header =~ /length: (\d+) aa./ then $1.to_i else nil end
report.crc64 = if header =~ /crc64 checksum: (\S+) / then $1 else nil end
ipr_line = ''
go_annotation = ''
matches.each do |m|
m = m.split(/\n/).map {|x| x.split(/ +/) }
m.each do |match|
case match[0]
when 'method'
when /(Molecular Function|Cellular Component|Biological Process):/
go_annotation = match[0].scan(/([MCB]\w+ \w+): (\S.+?\S) \((GO:\d+)\),*/)
when 'InterPro'
ipr_line = match
else
pos_scores = match[3].scan(/(\S)\[(\d+)-(\d+)\] (\S+) */)
pos_scores.each do |pos_score|
report.matches << Match.new(:ipr_id => ipr_line[1],
:ipr_description => ipr_line[2],
:method => match[0],
:accession => match[1],
:description => match[2],
:evalue => pos_score[3],
:status => pos_score[0],
:match_start => pos_score[1].to_i,
:match_end => pos_score[2].to_i,
:go_terms => go_annotation)
end
end
end
end
return report
end
# Splits entry stream.
#
# == Usage
# Bio::Iprscan::Report.parse_ptxt(File.open("merged.txt")) do |report|
# report
# end
def self.parse_ptxt(io)
io.each("\n\/\/\n") do |entry|
yield self.parse_ptxt_entry(entry)
end
end
# Parser method for a pseudo-txt formated entry. Retruns a Bio::Iprscan::Report
# object.
#
# == Usage
#
# File.read("marged.txt").each(Bio::Iprscan::Report::RS) do |e|
# report = Bio::Iprscan::Report.parse_ptxt_entry(e)
# end
#
def self.parse_ptxt_entry(str)
report = self.new
ipr_line = ''
str.split(/\n/).each do |line|
line = line.split("\t")
if line.size == 2
report.query_id = line[0]
report.query_length = line[1].to_i
elsif line.first == '//'
elsif line.first == 'InterPro'
ipr_line = line
else
startp, endp = line[4].split("-")
report.matches << Match.new(:ipr_id => ipr_line[1],
:ipr_description => ipr_line[2],
:method => line[0],
:accession => line[1],
:description => line[2],
:evalue => line[3],
:match_start => startp.to_i,
:match_end => endp.to_i)
end
end
report
end
#
def initialize
@query_id = nil
@query_length = nil
@crc64 = nil
@matches = []
end
# Output interpro matches in the format_type.
def output(format_type)
case format_type
when 'raw', :raw
format_raw
else
raise NameError, "Invalid format_type."
end
end
# def format_html
# end
# def format_xml
# end
# def format_ebixml
# end
# def format_txt
# end
def format_raw
@matches.map { |match|
[self.query_id,
self.crc64,
self.query_length,
match.method_name,
match.accession,
match.description,
match.match_start,
match.match_end,
match.evalue,
match.status,
match.date,
match.ipr_id,
match.ipr_description,
match.go_terms.map {|x| x[0] + ': ' + x[1] + ' (' + x[2] + ')' }.join(', ')
].join("\t")
}.join("\n")
end
# def format_gff3
# end
# Returns a Hash (key as an Interpro ID and value as a Match).
#
# report.to_hash.each do |ipr_id, matches|
# matches.each do |match|
# report.matches.ipr_id == ipr_id #=> true
# end
# end
#
def to_hash
unless (defined? @ipr_ids) && @ipr_ids
@ipr_ids = {}
@matches.each_with_index do |match, i|
@ipr_ids[match.ipr_id] ||= []
@ipr_ids[match.ipr_id] << match
end
return @ipr_ids
else
return @ipr_ids
end
end
# == Description
# Container class for InterProScan matches.
#
# == Usage
# match = Match.new(:query_id => ...)
#
# match.ipr_id = 'IPR001234'
# match.ipr_id #=> 'IPR001234'
#
class Match
def initialize(hash)
@data = Hash.new
hash.each do |key, value|
@data[key.to_sym] = value
end
end
# Date for computation.
def date; @data[:date]; end
# CRC64 checksum of query sequence.
def crc64; @data[:crc64]; end
# E-value of the match
def evalue; @data[:evalue]; end
# Status of the match (T for true / M for marginal).
def status; @data[:status]; end
# the corresponding InterPro entry (if any).
def ipr_id; @data[:ipr_id]; end
# the length of the sequence in AA.
def length; @data[:length]; end
# the analysis method launched.
def method_name; @data[:method]; end
# the Gene Ontology description for the InterPro entry, in "Aspect :term (ID)" format.
def go_terms; @data[:go_terms]; end
# Id of the input sequence.
def query_id; @data[:query_id]; end
# the end of the domain match.
def match_end; @data[:match_end]; end
# the database members entry for this match.
def accession; @data[:accession]; end
# the database mambers description for this match.
def description; @data[:description]; end
# the start of the domain match.
def match_start; @data[:match_start]; end
# the descriotion of the InterPro entry.
def ipr_odescription; @data[:ipr_description]; end
def method_missing(name, arg = nil)
if arg
name = name.to_s.sub(/=$/, '')
@data[name.to_sym] = arg
else
@data[name.to_sym]
end
end
end # class Match
end # class Report
end # class Iprscan
end # module Bio
|