[BioRuby-cvs] bioruby/lib/bio/io fastacmd.rb,1.1,1.2

Katayama Toshiaki k at pub.open-bio.org
Tue Aug 9 04:55:42 EDT 2005


Update of /home/repository/bioruby/bioruby/lib/bio/io
In directory pub.open-bio.org:/tmp/cvs-serv1425

Modified Files:
	fastacmd.rb 
Log Message:
* the class is renamed to Bio::Fastacmd (from Bio::BlastDB)
* constant FASTACMD is changed to an instance variable
* added accessors for database and fastacmd
* get_by_ids method is integrated into fetch method
* test code modified to accept arguments


Index: fastacmd.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/io/fastacmd.rb,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** fastacmd.rb	9 Aug 2005 07:52:45 -0000	1.1
--- fastacmd.rb	9 Aug 2005 08:55:40 -0000	1.2
***************
*** 3,6 ****
--- 3,7 ----
  #
  #   Copyright (C) 2005 Shuji SHIGENOBU <shige at nibb.ac.jp>
+ #   Copyright (C) 2005 Toshiaki Katayama <k at bioruby.org>
  #
  #  This library is free software; you can redistribute it and/or
***************
*** 27,62 ****
  module Bio
  
!   class BlastDB
  
      include Enumerable
  
-     FASTACMD = 'fastacmd'
- 
      def initialize(db)
        @database = db
      end
  
!     def get_by_id(id)
!       cmd = "#{FASTACMD} -d #{@database} -s #{id}"
!       begin
!         inn, out, err = Open3.popen3(cmd)
!         result = out.read
!         err_msg = err.read
!         fas = Bio::FastaFormat.new(result)
!         return fas
!       rescue
!         raise "[Error] command execution failed : #{cmd}\n#{err_msg}"
!       ensure
!         inn.close; out.close; err.close
!       end
      end
  
!     def get_by_ids(ids) # ids: Array object
!       cmd = "#{FASTACMD} -d #{@database} -s #{ids.join(',')}"
        begin
          inn, out, err = Open3.popen3(cmd)
          err_msg = err.read
!         fas_set = Bio::FlatFile.new(Bio::FastaFormat, out).to_a
!         return fas_set
        rescue
          raise "[Error] command execution failed : #{cmd}\n#{err_msg}"
--- 28,60 ----
  module Bio
  
!   class Fastacmd
  
      include Enumerable
  
      def initialize(db)
        @database = db
+       @fastacmd = 'fastacmd'
      end
+     attr_accessor :database, :fastacmd
  
!     # get an entry_id and returns a Bio::FastaFormat object
!     def get_by_id(entry_id)
!       fetch(entry_id).shift
      end
  
!     # get one or more entry_id and returns an Array of Bio::FastaFormat objects
!     def fetch(list)
!       if list.respond_to?(:join)
!         entry_id = list.join(",")
!       else
!         entry_id = list
!       end
! 
!       cmd = "#{@fastacmd} -d #{@database} -s #{entry_id}"
        begin
          inn, out, err = Open3.popen3(cmd)
+         results = Bio::FlatFile.new(Bio::FastaFormat, out).to_a
          err_msg = err.read
!         return results
        rescue
          raise "[Error] command execution failed : #{cmd}\n#{err_msg}"
***************
*** 67,79 ****
  
      def each_entry
!       cmd = "#{FASTACMD} -d #{@database} -D T"
!       io = IO.popen(cmd)
!       f = Bio::FlatFile.new(Bio::FastaFormat, io)
!       f.each_entry do |e|
!         yield e
        end
-       io.close
      end
- 
      alias :each :each_entry
  
--- 65,76 ----
  
      def each_entry
!       cmd = "#{@fastacmd} -d #{@database} -D T"
!       IO.popen(cmd) do |io|
!         f = Bio::FlatFile.new(Bio::FastaFormat, io)
!         f.each_entry do |e|
!           yield e
!         end
        end
      end
      alias :each :each_entry
  
***************
*** 85,103 ****
  if __FILE__ == $0
  
!   # test code
  
!   bdb = Bio::BlastDB.new("/db/myblastdb")
  
!   # Retrieve one sequence
!   puts bdb.get_by_id("P25724")
  
!   # Retrieve one more sequences
!   bdb.get_by_ids(["P25724", "AAB59189", "AAA28715"]).each do |fas|
!     puts fas
    end
  
!   # Iterate all sequences
!   bdb.each do |fas|
!     p [fas.definition[0..30], fas.seq.size]
    end
  
--- 82,121 ----
  if __FILE__ == $0
  
!   database = ARGV.shift || "/db/myblastdb"
!   entry_id = ARGV.shift || "sp:128U_DROME"
!   ent_list = ["sp:1433_SPIOL", "sp:1432_MAIZE"]
  
!   fastacmd = Bio::Fastacmd.new(database)
  
!   ### Retrieve one sequence
!   entry = fastacmd.get_by_id(entry_id)
  
!   # Bio::Fastacmd#get_by_id(entry_id) returns a Bio::FastaFormat object.
!   p entry
! 
!   # Bio::FastaFormat becomes a fasta format string when printed by puts.
!   puts entry
! 
!   # Bio::Fastacmd#fetch(entry_id) returns an Array of a Bio::FastaFormat
!   # object even when the result is a single entry.
!   p fastacmd.fetch(entry_id)
! 
!   ### Retrieve more sequences
! 
!   # Bio::Fastacmd#fetch method also accepts a list of entry_id and returns
!   # an Array of Bio::FastaFormat objects.
!   p fastacmd.fetch(ent_list)
! 
!   # So, you can iterate on the results.
!   fastacmd.fetch(ent_list).each do |fasta|
!     puts fasta
    end
  
! 
!   ### Iterates on all entries
! 
!   # You can also iterate on all sequences in the database!
!   fastacmd.each do |fasta|
!     p [ fasta.definition[0..30], fasta.seq.size ]
    end
  



More information about the bioruby-cvs mailing list