Class: BlastXmlResult

Inherits:
Object
  • Object
show all
Defined in:
lib/scbi_blast/blast_xml_result.rb

Overview

Another XML parser using nokogiri library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ BlastXmlResult

Returns a new instance of BlastXmlResult.



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

def initialize(input)

  @querys = []
  lines=[]

  if input.is_a?(Array)
    lines=input
  else
    if File.exists?(input)
      fich = File.open(input,'r')
      lines = fich.readlines
      fich.close
    end

  end

  # puts "lines length #{lines.length}"
  if !lines.empty?
    data = Nokogiri::XML(lines.join)
    data.root.xpath('//Iteration').each do |iteration|

      # puts JSON::pretty_generate(iteration)
      query_id = iteration.xpath('Iteration_query-ID').text

      full_query_length = iteration.xpath('Iteration_query-len').text
      query_def = iteration.xpath('Iteration_query-def').text

      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.xpath('Iteration_hits/Hit')
      if !hits.nil?
        hits.each do |h|
          #puts JSON::pretty_generate(h)



          subject_id=h.xpath('Hit_id').text
          acc =h.xpath('Hit_accession').text
          full_subject_length = h.xpath('Hit_len').text.to_i
          hit_def=h.xpath('Hit_def').text
          if hit_def=='No definition line'
            hit_def =subject_id
          end

          hsps = h.xpath('Hit_hsps/Hsp')

          hsps.each do |hsp|

            q_beg=hsp.xpath('Hsp_query-from').text.to_i
            q_end=hsp.xpath('Hsp_query-to').text.to_i
            s_beg=hsp.xpath('Hsp_hit-from').text.to_i
            s_end=hsp.xpath('Hsp_hit-to').text.to_i

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

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

            hit.score = hsp.xpath('Hsp_score').text.to_f
            hit.q_frame = hsp.xpath('Hsp_query-frame').text.to_i
            hit.s_frame =hsp.xpath('Hsp_hit-frame').text.to_i

            hit.q_seq = hsp.xpath('Hsp_qseq').text
            hit.s_seq = hsp.xpath('Hsp_hseq').text


            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

Instance Attribute Details

#querysObject

Returns the value of attribute querys.



162
163
164
# File 'lib/scbi_blast/blast_xml_result.rb', line 162

def querys
  @querys
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


153
154
155
156
# File 'lib/scbi_blast/blast_xml_result.rb', line 153

def empty?

  return @querys.empty?
end

#find_query(querys, name_q) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/scbi_blast/blast_xml_result.rb', line 142

def find_query(querys,name_q)
  #  newq = querys.find{|q| ( q.find{|h| (h.subject_id)})}
  new_q=nil

  if !querys.empty?
    new_q=querys.find{|q| (q.query_id==name_q)}
  end

  return new_q
end

#inspectObject



133
134
135
136
137
138
139
140
# File 'lib/scbi_blast/blast_xml_result.rb', line 133

def inspect

  res = "Blast results:\n"
  res+= '-'*20
  res+= "\nQuerys: #{@querys.count}\n"
  @querys.each{|q| res+=q.inspect+"\n"}
  return res
end

#sizeObject



158
159
160
# File 'lib/scbi_blast/blast_xml_result.rb', line 158

def size
  @querys.size
end