Permalink
Browse files

* Changed method names:

  1. reports_in_* -> parse_*
  2. parse_in_* -> parse_*_entry
  3. to_* -> format_*
* Added Report#output(:format_type) method to be the facade pattern of
  format_* methods.
  • Loading branch information...
1 parent 231ce88 commit 439ad836358601d812befe35fc7831658b7e2b12 @nakao nakao committed Jul 18, 2007
Showing with 94 additions and 76 deletions.
  1. +57 −49 lib/bio/appl/iprscan/report.rb
  2. +37 −27 test/unit/bio/appl/iprscan/test_report.rb
@@ -5,7 +5,7 @@
# Mitsuteru C. Nakao <mn@kazusa.or.jp>
# License:: The Ruby License
#
-# $Id: report.rb,v 1.8 2007/07/17 14:16:50 nakao Exp $
+# $Id: report.rb,v 1.9 2007/07/18 11:11:57 nakao Exp $
#
# == Report classes for the iprscan program.
#
@@ -23,7 +23,7 @@ class Iprscan
#
# == USAGE
# # Read a marged.txt and split each entry.
- # Bio::Iprscan::Report.reports_in_txt(File.read("marged.txt")) do |report|
+ # Bio::Iprscan::Report.parse_txt(File.read("marged.txt")) do |report|
# report.query_id
# report.matches.size
# report.matches.each do |match|
@@ -40,7 +40,7 @@ class Iprscan
# # report.to_html
# end
#
- # Bio::Iprscan::Report.reports_in_raw(File.read("marged.raw")) do |report|
+ # Bio::Iprscan::Report.parse_raw(File.read("marged.raw")) do |report|
# report.class #=> Bio::Iprscan::Report
# end
#
@@ -65,28 +65,28 @@ class Report
attr_accessor :matches
# == USAGE
- # Bio::Iprscan::Report.reports_in_raw(File.open("merged.raw")) do |report|
+ # Bio::Iprscan::Report.parse_raw(File.open("merged.raw")) do |report|
# report
# end
#
- def self.reports_in_raw(io)
+ 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_in_raw(entry)
+ yield Bio::Iprscan::Report.parse_raw_entry(entry)
entry = line
else
entry << line
end
end
- yield Bio::Iprscan::Report.parse_in_raw(entry) if entry != ''
+ 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_in_raw(str)
+ def self.parse_raw_entry(str)
report = self.new
str.split(/\n/).each do |line|
line = line.split("\t")
@@ -116,27 +116,26 @@ def self.parse_in_raw(str)
# Parser method for a xml formated entry. Retruns a Bio::Iprscan::Report
# object.
- def self.parse_in_xml(str)
- NotImplementedError
- end
+# def self.parse_xml(str)
+# end
# Splits the entry stream.
#
# == Usage
#
- # Bio::Iprscan::Report.reports_in_txt(File.open("merged.txt")) do |report|
+ # Bio::Iprscan::Report.reports_txt(File.open("merged.txt")) do |report|
# report.class #=> Bio::Iprscan::Report
# end
#
- def self.reports_in_txt(io)
+ 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_in_txt(entry)
+ yield self.parse_txt_entry(entry)
end
end
@@ -145,7 +144,7 @@ def self.reports_in_txt(io)
# Parser method for a txt formated entry. Returns a Bio::Iprscan::Report
# object.
#
- def self.parse_in_txt(str)
+ def self.parse_txt_entry(str)
unless str =~ /^Sequence /
raise ArgumentError, "Invalid format: \n\n#{str}"
end
@@ -189,12 +188,12 @@ def self.parse_in_txt(str)
# Splits entry stream.
#
# == Usage
- # Bio::Iprscan::Report.reports_in_ptxt(File.open("merged.txt")) do |report|
+ # Bio::Iprscan::Report.parse_ptxt(File.open("merged.txt")) do |report|
# report
# end
- def self.reports_in_ptxt(io)
+ def self.parse_ptxt(io)
io.each("\n\/\/\n") do |entry|
- yield self.parse_in_ptxt(entry)
+ yield self.parse_ptxt_entry(entry)
end
end
@@ -204,10 +203,10 @@ def self.reports_in_ptxt(io)
# == Usage
#
# File.read("marged.txt").each(Bio::Iprscan::Report::RS) do |e|
- # report = Bio::Iprscan::Report.parse_in_ptxt(e)
+ # report = Bio::Iprscan::Report.parse_ptxt_entry(e)
# end
#
- def self.parse_in_ptxt(str)
+ def self.parse_ptxt_entry(str)
report = self.new
ipr_line = ''
str.split(/\n/).each do |line|
@@ -241,28 +240,35 @@ def initialize
@matches = []
end
- def to_html
- NotImplementedError
+
+ # 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 to_xml
- NotImplementedError
- end
+# def format_xml
+# end
- def to_ebixml
- NotImplementedError
- end
+# def format_ebixml
+# end
- def to_txt
- NotImplementedError
- end
+# def format_txt
+# end
- def to_raw
+ def format_raw
@matches.map { |match|
[self.query_id,
self.crc64,
self.query_length,
- match.method,
+ match.method_name,
match.accession,
match.description,
match.match_start,
@@ -277,27 +283,29 @@ def to_raw
}.join("\n")
end
- def to_gff3
- NotImplementedError
- end
+# def format_gff3
+# end
- # Returns a Hash (key as an interpro id and value as index key for
- # matches (Array).
+ # Returns a Hash (key as an Interpro ID and value as a Match).
#
- # report.list_of_interpro.each do |ipr_id, indexes|
- # indexes.each do |index|
- # report.matches[index].ipr_id == ipr_id #=> true
+ # report.to_hash.each do |ipr_id, matches|
+ # matches.each do |match|
+ # report.matches.ipr_id == ipr_id #=> true
# end
# end
#
- def list_of_interpro
- @ipr_ids = {} unless @ipr_ids
- @matches.each_with_index do |match, i|
- @ipr_ids[match.ipr_id] = [] unless @ipr_ids[match.ipr_id]
- @ipr_ids[match.ipr_id] << i
+ def to_hash
+ unless @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
- @ipr_ids
end
@@ -332,8 +340,8 @@ 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; @data[:method]; end # Object#metod overrided by Match#method
- # the Gene Ontology description for the InterPro entry, in "Aspect:term (ID)" format.
+ 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
@@ -3,7 +3,7 @@
#
# Copyright (C) 2006 Mitsuteru Nakao <n@bioruby.org>
#
-# $Id: test_report.rb,v 1.6 2007/07/17 14:16:50 nakao Exp $
+# $Id: test_report.rb,v 1.7 2007/07/18 11:11:57 nakao Exp $
#
require 'pathname'
@@ -43,7 +43,7 @@ def setup
ProfileScan\tPS50310\tALA_RICH\t10.224\t805-856
//
END
- @obj = Bio::Iprscan::Report.parse_in_ptxt(test_entry)
+ @obj = Bio::Iprscan::Report.parse_ptxt_entry(test_entry)
end
@@ -68,7 +68,7 @@ def test_match_ipr_description
end
def test_match_method
- assert_equal('BlastProDom', @obj.matches.first.method)
+ assert_equal('BlastProDom', @obj.matches.first.method_name)
end
def test_match_accession
@@ -97,7 +97,7 @@ def test_match_match_end
class TestIprscanTxtEntry < Test::Unit::TestCase
def setup
test_txt = Bio::TestIprscanData.txt_format.read.split(/\n\nSequence/)[0]
- @obj = Bio::Iprscan::Report.parse_in_txt(test_txt)
+ @obj = Bio::Iprscan::Report.parse_txt_entry(test_txt)
end
def test_iprscan_report_class
@@ -125,7 +125,7 @@ def test_match_ipr_description
end
def test_match_method
- assert_equal('FPrintScan', @obj.matches.first.method)
+ assert_equal('FPrintScan', @obj.matches.first.method_name)
end
def test_match_accession
@@ -169,21 +169,22 @@ def test_match_go_terms
class TestIprscanTxtEntryList < Test::Unit::TestCase
def setup
test_txt = Bio::TestIprscanData.txt_format.read.split(/\n\nSequence/)[0]
- @obj = Bio::Iprscan::Report.parse_in_txt(test_txt)
+ @obj = Bio::Iprscan::Report.parse_txt_entry(test_txt)
end
- def test_list_of_interpro
- hsh = {"IPR008994"=>[12, 13, 14],
- "IPR000110"=>[0, 1, 2],
- "IPR003029"=>[3, 4, 5, 6, 7, 8, 9, 10, 11],
- "NULL"=>[15]}
- assert_equal(hsh, @obj.list_of_interpro)
- end
-
- def test_list_of_interpro_match?
- @obj.list_of_interpro.each do |ipr_id, indexes|
- indexes.each do |index|
- assert_equal(ipr_id, @obj.matches[index].ipr_id)
+ def test_to_hash
+ hsh = {"IPR008994" => [12, 13, 14].map {|x| @obj.matches[x] },
+ "IPR000110" => [0, 1, 2].map {|x| @obj.matches[x] },
+ "IPR003029" => [3, 4, 5, 6, 7, 8, 9, 10, 11].map {|x| @obj.matches[x] },
+ "NULL" => [15].map {|x| @obj.matches[x] }}
+ assert_equal(hsh.keys.sort, @obj.to_hash.keys.sort)
+ assert_equal(hsh, @obj.to_hash)
+ end
+
+ def test_to_hash_match?
+ @obj.to_hash.each do |ipr_id, matches|
+ matches.each do |match|
+ assert_equal(ipr_id, match.ipr_id)
end
end
end
@@ -195,8 +196,8 @@ def setup
@test_txt = Bio::TestIprscanData.txt_format
end
- def test_reports_in_txt
- Bio::Iprscan::Report.reports_in_txt(@test_txt) do |report|
+ def test_parse_txt
+ Bio::Iprscan::Report.parse_txt(@test_txt) do |report|
assert_equal(Bio::Iprscan::Report, report.class)
end
end
@@ -215,20 +216,20 @@ def setup
if entry.split("\t").first == line.split("\t").first
entry << line
elsif entry != '' and entry.split("\t").first != line.split("\t").first
- @obj << Bio::Iprscan::Report.parse_in_raw(entry)
+ @obj << Bio::Iprscan::Report.parse_raw_entry(entry)
entry = ''
else
entry << line
end
end
- @obj << Bio::Iprscan::Report.parse_in_raw(entry)
+ @obj << Bio::Iprscan::Report.parse_raw_entry(entry)
end
def test_self_reports_in_raw
io = File.open(File.join(Bio::TestIprscanData::TestDataIprscan,
"merged.raw"))
result = []
- Bio::Iprscan::Report.reports_in_raw(io) {|x| result << x }
+ Bio::Iprscan::Report.parse_raw(io) {|x| result << x }
assert_equal(@obj.size, result.size)
assert_equal(@obj.first.query_id, result.first.query_id)
assert_equal(@obj.first.query_id, result.first.query_id)
@@ -265,7 +266,7 @@ def test_match_query_length
end
def test_match_method
- assert_equal('HMMPfam', @obj.first.matches.first.method)
+ assert_equal('HMMPfam', @obj.first.matches.first.method_name)
end
def test_match_accession
@@ -315,12 +316,21 @@ def test_match_go_terms
class TestIprscanReport < Test::Unit::TestCase
def setup
- test_txt = Bio::TestIprscanData.txt_format.read.split(/\n\nSequence/)[0]
- @obj = Bio::Iprscan::Report.parse_in_txt(test_txt)
+ @test_txt = Bio::TestIprscanData.txt_format.read.split(/\n\nSequence/)[0]
+ @obj = Bio::Iprscan::Report.parse_txt_entry(@test_txt)
+ @test_raw = Bio::TestIprscanData.raw_format.read.split("RS16_ECOLI")[0]
end
def test_to_raw
-# puts @obj.to_raw
+# assert_equal(@test_raw.split("\n").sort,
+# @obj.format_raw.split("\n").sort)
+ end
+
+ def test_output_raw
+# assert_equal(@test_raw.split("\n").sort,
+# @obj.output(:raw).split("\n").sort)
+# assert_equal(@test_raw.split("\n").sort,
+# @obj.output('raw').split("\n").sort)
end
end # TestIprscanReport

0 comments on commit 439ad83

Please sign in to comment.