Class: BlastSimplexmlResult

Inherits:
BlastResult show all
Defined in:
lib/scbi_blast/blast_simplexml_result.rb

Overview

Extracts results from a blast results in XML format and uses it to create instances of “BlastQuery” and “BlastHit”

Instance Attribute Summary

Attributes inherited from BlastResult

#querys

Instance Method Summary collapse

Methods inherited from BlastResult

#compare?, #empty?, #find_query, #inspect, #size

Constructor Details

#initialize(input) ⇒ BlastSimplexmlResult

Parser initialization



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scbi_blast/blast_simplexml_result.rb', line 33

def initialize(input)
  super(input)

  lines=[]
  if input.is_a?(Array)
    lines=input
  elsif !input.strip.empty?
    if File.exists?(input)
      fich = File.open(input,'r')
      lines = fich.readlines
      fich.close
    else
      raise "File #{input} doesn't exists"
    end
  end
  parse(lines)
end

Instance Method Details

#parse(lines) ⇒ Object



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
# File 'lib/scbi_blast/blast_simplexml_result.rb', line 51

def parse(lines)

  # puts "lines length #{lines.length}"
  if !lines.empty?
    data = XmlSimple.xml_in(lines.join)
    iterations = data['BlastOutput_iterations']
    #require 'json'
    #puts iterations.to_json

    iterations[0]['Iteration'].each do |iteration|

      # puts JSON::pretty_generate(iteration)

      query_id = iteration['Iteration_query-ID'][0]
      full_query_length = iteration['Iteration_query-len'][0].to_i
      query_def = iteration['Iteration_query-def'][0]

      if query_def =~ /^([^\s]+)/
        query_def=$1
      end
      

      #@query_def = iteration['Iteration_query-def'][0]

      query = BlastQuery.new(query_id)
      query.query_def = query_def
      query.full_query_length = full_query_length
      @querys.push query



      hits = iteration['Iteration_hits'][0]['Hit']
      if !hits.nil?
        hits.each do |h|
          #puts JSON::pretty_generate(h)



          subject_id=h['Hit_id'][0]
          acc =h['Hit_accession'][0]
          full_subject_length = h['Hit_len'][0].to_i
          hit_def=h['Hit_def'][0]
          if hit_def=='No definition line'
            hit_def =subject_id
          end

          hsps = h['Hit_hsps'][0]['Hsp']

          hsps.each do |hsp|

            q_beg=hsp['Hsp_query-from'][0].to_i
            q_end=hsp['Hsp_query-to'][0].to_i
            s_beg=hsp['Hsp_hit-from'][0].to_i
            s_end=hsp['Hsp_hit-to'][0].to_i

            # creates the hit
            hit = BlastHit.new(q_beg,q_end,s_beg,s_end)

            hit.align_len=hsp['Hsp_align-len'][0].to_i
            hit.ident=(hsp['Hsp_identity'][0].to_f/hit.align_len)*100
            hit.gaps=hsp['Hsp_gaps'][0].to_i
            hit.mismatches=hsp['Hsp_midline'][0].count(' ').to_i - hit.gaps
            hit.e_val=hsp['Hsp_evalue'][0].to_f
            hit.e_val = (hit.e_val*1000).round/1000.0
            hit.bit_score=hsp['Hsp_bit-score'][0].to_f
            hit.bit_score = (hit.bit_score*100).round/100.0

            hit.score = hsp['Hsp_score'][0].to_f
            hit.q_frame = hsp['Hsp_query-frame'][0].to_i
            hit.s_frame =hsp['Hsp_hit-frame'][0].to_i

            hit.q_seq = hsp['Hsp_qseq'][0]
            hit.s_seq = hsp['Hsp_hseq'][0]


            hit.subject_id= subject_id
            hit.full_subject_length=full_subject_length
            # hit.full_query_length = full_query_length
            hit.definition=hit_def
            hit.acc=acc

            query.add_hit(hit)

          end
        end
      end
    end
  end
  #inspect
end