File: fasta.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 (213 lines) | stat: -rw-r--r-- 5,976 bytes parent folder | download | duplicates (9)
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
#
# = bio/appl/fasta.rb - FASTA wrapper
#
# Copyright::  Copyright (C) 2001, 2002 Toshiaki Katayama <k@bioruby.org>
# License::    The Ruby License
#
# $Id:$
#

require 'net/http'
require 'uri'
require 'bio/command'
require 'shellwords'

module Bio

class Fasta

  autoload :Report, 'bio/appl/fasta/format10'
  #autoload :?????,  'bio/appl/fasta/format6'

  # Returns a FASTA factory object (Bio::Fasta).
  def initialize(program, db, opt = [], server = 'local')
    @format	= 10

    @program	= program
    @db	= db
    @server	= server

    @ktup	= nil
    @matrix	= nil

    @output	= ''

    begin
      a = opt.to_ary
    rescue NameError #NoMethodError
      # backward compatibility
      a = Shellwords.shellwords(opt)
    end
    @options	= [ '-Q', '-H', '-m', @format.to_s, *a ] # need -a ?
  end
  attr_accessor :program, :db, :options, :server, :ktup, :matrix

  # Returns a String containing fasta execution output in as is format.
  attr_reader :output

  def option
    # backward compatibility
    Bio::Command.make_command_line(@options)
  end

  def option=(str)
    # backward compatibility
    @options = Shellwords.shellwords(str)
  end

  # Accessors for the -m option.
  def format=(num)
    @format = num.to_i
    if i = @options.index('-m') then
      @options[i+1, 1] = @format.to_s
    else
      @options << '-m' << @format.to_s
    end
  end
  attr_reader :format

  # OBSOLETE. Does nothing and shows warning messages.
  #
  # Historically, selecting parser to use ('format6' or 'format10' were
  # expected, but only 'format10' was available as a working parser).
  #
  def self.parser(parser)
    warn 'Bio::Fasta.parser is obsoleted and will soon be removed.'
  end

  # Returns a FASTA factory object (Bio::Fasta) to run FASTA search on
  # local computer.
  def self.local(program, db, option = '')
    self.new(program, db, option, 'local')
  end

  # Returns a FASTA factory object (Bio::Fasta) to execute FASTA search on
  # remote server.
  #
  # For the develpper, you can add server 'hoge' by adding
  # exec_hoge(query) method.
  #
  def self.remote(program, db, option = '', server = 'genomenet')
    self.new(program, db, option, server)
  end

  # Execute FASTA search and returns Report object (Bio::Fasta::Report).
  def query(query)
    return self.send("exec_#{@server}", query.to_s)
  end


  private


  def parse_result(data)
    Report.new(data)
  end


  def exec_local(query)
    cmd = [ @program, *@options ]
    cmd.concat([ '@', @db ])
    cmd.push(@ktup) if @ktup

    report = nil

    @output = Bio::Command.query_command(cmd, query)
    report = parse_result(@output)

    return report
  end


  # == Available databases for Fasta.remote(@program, @db, option, 'genomenet')
  #
  # See http://fasta.genome.jp/ideas/ideas.html#fasta for more details.
  #
  #   ----------+-------+---------------------------------------------------
  #    @program | query | @db (supported in GenomeNet)
  #   ----------+-------+---------------------------------------------------
  #    fasta    | AA    | nr-aa, genes, vgenes.pep, swissprot, swissprot-upd,
  #             |       | pir, prf, pdbstr
  #             +-------+---------------------------------------------------
  #             | NA    | nr-nt, genbank-nonst, gbnonst-upd, dbest, dbgss,
  #             |       | htgs, dbsts, embl-nonst, embnonst-upd, epd,
  #             |       | genes-nt, genome, vgenes.nuc
  #   ----------+-------+---------------------------------------------------
  #    tfasta   | AA    | nr-nt, genbank-nonst, gbnonst-upd, dbest, dbgss,
  #             |       | htgs, dbsts, embl-nonst, embnonst-upd,
  #             |       | genes-nt, genome, vgenes.nuc
  #   ----------+-------+---------------------------------------------------
  #
  def exec_genomenet(query)
    host = "fasta.genome.jp"
    #path = "/sit-bin/nph-fasta"
    path = "/sit-bin/fasta"  # 2005.08.12

    form = {
      'style'        => 'raw',
      'prog'         => @program,
      'dbname'       => @db,
      'sequence'     => query,
      'other_param'  => Bio::Command.make_command_line_unix(@options),
      'ktup_value'   => @ktup,
      'matrix'       => @matrix,
    }

    form.keys.each do |k|
      form.delete(k) unless form[k]
    end

    report = nil

    begin
      http = Bio::Command.new_http(host)
      http.open_timeout = 3000
      http.read_timeout = 6000
      result = Bio::Command.http_post_form(http, path, form)
      # workaround 2006.8.1 - fixed for new batch queuing system
      case result.code
      when "302"
        result_location = result.header['location']
        result_uri = URI.parse(result_location)
        result_path = result_uri.path
        done = false
        until done
          result = http.get(result_path)
          if result.body[/Your job ID is/]
            sleep 15
          else
            done = true
          end
        end
      end
      @output = result.body.to_s
      # workaround 2005.08.12
      re = %r{<A HREF="http://#{host}(/tmp/[^"]+)">Show all result</A>}i # "
      if path = @output[re, 1]
        result = http.get(path)
        @output = result.body
        txt = @output.to_s.split(/\<pre\>/)[1]
        raise 'cannot understand response' unless txt
        txt.sub!(/\<\/pre\>.*\z/m, '')
        txt.sub!(/.*^((T?FASTA|SSEARCH) (searches|compares))/m, '\1')
        txt.sub!(/^\<form method\=\"POST\" name\=\"clust_check\"\>.*\n/, '')
        txt.sub!(/^\<select +name\=\"allch\".+\r?\n/i, '') # 2009.11.26
        txt.gsub!(/\<input[^\>]+value\=\"[^\"]*\"[^\>]*\>/i, '')
        txt.gsub!(/\<(a|form|select|input|option|img)\s+[^\>]+\>/i, '')
        txt.gsub!(/\<\/(a|form|select|input|option|img)\>/i, '')
        txt.gsub!(/\&lt\;/, '<')
        txt.gsub!(/\&gt\;/, '>') # 2009.11.26
        @output = txt
        report = parse_result(@output.dup)
      else
        raise 'cannot understand response'
      end
    end

    return report
  end

end # Fasta

end # Bio