File: sim4.rb

package info (click to toggle)
ruby-bio 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,480 kB
  • ctags: 9,428
  • sloc: ruby: 74,117; xml: 3,383; makefile: 17; perl: 13; sh: 1
file content (124 lines) | stat: -rw-r--r-- 3,375 bytes parent folder | download | duplicates (10)
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
#
# = bio/appl/sim4.rb - sim4 wrapper class
#
# Copyright:: Copyright (C) 2004 GOTO Naohisa <ng@bioruby.org>
# License::   The Ruby License
#
#  $Id: sim4.rb,v 1.10 2007/04/05 23:35:39 trevor Exp $
#
# The sim4 execution wrapper class.
#
# == References
#
# * Florea, L., et al., A Computer program for aligning a cDNA sequence
#   with a genomic DNA sequence, Genome Research, 8, 967--974, 1998.
#   http://www.genome.org/cgi/content/abstract/8/9/967
#

require 'tempfile'
require 'bio/command'

module Bio

  # The sim4 execution wrapper class.
  class Sim4

    autoload :Report,       'bio/appl/sim4/report'

    # Creates a new sim4 execution wrapper object.
    # [+program+]  Program name. Usually 'sim4' in UNIX.
    # [+database+] Default file name of database('seq2').
    # [+option+]   Options (array of strings).
    def initialize(program = 'sim4', database = nil, opt = [])
      @program = program
      @options = opt
      @database = database #seq2
      @command = nil
      @output = nil
      @report = nil
    end

    # default file name of database('seq2')
    attr_accessor :database

    # name of the program (usually 'sim4' in UNIX)
    attr_reader :program

    # options
    attr_accessor :options

    # option is deprecated. Instead, please use options.
    def option
      warn "option is deprecated. Please use options."
      options
    end

    # last command-line strings executed by the object
    attr_reader :command

    #---
    # last messages of program reported to the STDERR
    #attr_reader :log
    #+++

    #log is deprecated (no replacement) and returns empty string.
    def log
      warn "log is deprecated (no replacement) and returns empty string."
      ''
    end

    # last result text (String)
    attr_reader :output

    # last result. Returns a Bio::Sim4::Report object.
    attr_reader :report

    # Executes the sim4 program.
    # <tt>seq1</tt> shall be a Bio::Sequence object.
    # Returns a Bio::Sim4::Report object.
    def query(seq1)
      tf = Tempfile.open('sim4')
      tf.print seq1.to_fasta('seq1', 70)
      tf.close(false)
      r = exec_local(tf.path)
      tf.close(true)
      r
    end

    # Executes the sim4 program.
    # Perform mRNA-genome alignment between given sequences.
    # <tt>seq1</tt> and <tt>seq2</tt> should be Bio::Sequence objects.
    # Returns a Bio::Sim4::Report object.
    def query_pairwise(seq1, seq2)
      tf = Tempfile.open('sim4')
      tf.print seq1.to_fasta('seq1', 70)
      tf.close(false)
      tf2 = Tempfile.open('seq2')
      tf2.print seq1.to_fasta('seq2', 70)
      tf2.close(false)
      r = exec_local(tf.path, tf2.path)
      tf.close(true)
      tf2.close(true)
      r
    end

    # Executes the sim4 program.
    # Perform mRNA-genome alignment between sequences in given files.
    # <tt>filename1</tt> and <tt>filename2</tt> should be file name strings.
    # If <tt>filename2</tt> is not specified, using <tt>self.database</tt>.
    def exec_local(filename1, filename2 = nil)
      @command = [ @program, filename1, (filename2 or @database), *@options ]
      @output = nil
      @report = nil
      Bio::Command.call_command(@command) do |io|
        io.close_write
        @output = io.read
        @report = Bio::Sim4::Report.new(@output)
      end
      @report
    end
    alias exec exec_local

  end #class Sim4
end #module Bio