Class: SyntenyManip

Inherits:
Object
  • Object
show all
Defined in:
lib/bacterial-annotator/synteny-manip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_file, subject_file, name, pidentity) ⇒ SyntenyManip

Returns a new instance of SyntenyManip.



15
16
17
18
19
20
21
# File 'lib/bacterial-annotator/synteny-manip.rb', line 15

def initialize query_file, subject_file, name, pidentity
  @query_file = query_file
  @subject_file = subject_file
  @name = name
  @pidentity = pidentity
  @aln_file = nil
end

Instance Attribute Details

#aln_hitsObject (readonly)

Returns the value of attribute aln_hits.



13
14
15
# File 'lib/bacterial-annotator/synteny-manip.rb', line 13

def aln_hits
  @aln_hits
end

#query_fileObject (readonly)

Returns the value of attribute query_file.



13
14
15
# File 'lib/bacterial-annotator/synteny-manip.rb', line 13

def query_file
  @query_file
end

#subject_fileObject (readonly)

Returns the value of attribute subject_file.



13
14
15
# File 'lib/bacterial-annotator/synteny-manip.rb', line 13

def subject_file
  @subject_file
end

Instance Method Details

#choose_best_hit(i, prots, ref_cds) ⇒ Object

Choose Best Hit base on neighbor hits



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
# File 'lib/bacterial-annotator/synteny-manip.rb', line 129

def choose_best_hit i, prots, ref_cds

  hit_index = 0
  p = prots[i]
  hit_locus_tags = []

  @aln_hits[p][:hits].each do |h|
    hit_locus_tags << ref_cds[h][:locustag].downcase.split("_")[-1].gsub(/[a-z]/,"").to_i
  end

  continue=true
  offset=1

  while continue
    fwd_end = false
    bcw_end = false
    found = false

    if (i+offset) < (prots.length-1)
      fwd_p = prots[i+offset]
      next_prot_hits = @aln_hits[fwd_p][:hits]
      if next_prot_hits.length < 2
        n = ref_cds[next_prot_hits[0]][:locustag].downcase.split("_")[-1].gsub(/[a-z]/,"").to_i
        closest = 10000
        current_ltag_i = 0
        hit_locus_tags.each_with_index do |ltag,ltag_i|
          if (ltag-n).abs < closest
            current_ltag_i = ltag_i
            closest = (ltag-n).abs
          end
        end
        hit_index = current_ltag_i
        found = true
      end
    else
      fwd_end = true
    end

    if (i-offset) >= 0 and !found
      bcw_p = prots[i-offset]
      next_prot_hits = @aln_hits[bcw_p][:hits]
      if next_prot_hits.length < 2
        n = ref_cds[next_prot_hits[0]][:locustag].downcase.split("_")[-1].gsub(/[a-z]/,"").to_i
        closest = 10000
        current_ltag_i = 0
        hit_locus_tags.each_with_index do |ltag,ltag_i|
          if (ltag-n).abs < closest
            current_ltag_i = ltag_i
            closest = (ltag-n).abs
          end
        end
        hit_index = current_ltag_i
        found = true
      end
    else
      bcw_end = true
    end

    offset += 1
    continue = (!fwd_end and !bcw_end and !found)
  end

  hit_index

end

#extract_hits(mode) ⇒ Object

Extract Hit from blast8 file and save it in hash contig-0_1 ABJ71957.1 96.92 65 2 0 1 65 1 65 9.2e-31 131.0



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
# File 'lib/bacterial-annotator/synteny-manip.rb', line 32

def extract_hits mode

  @aln_hits = {}
  File.open(@aln_file,"r") do |fread|
    while l = fread.gets
      lA = l.chomp!.split("\t")
      key = lA[0]
      if mode == :refgenome
        hit = lA[1]
      elsif mode == :externaldb
        hit = lA[1].chomp.split("|")[1]
      end
      if ! @aln_hits.has_key? key
        next if lA[2].to_f < @pidentity
        @aln_hits[key] = {
          pId: lA[2].to_f.round(2),
          evalue: lA[10],
          score: lA[11].to_f,
          hits: [hit],
          length: [lA[3].to_i],
          query_location: [[lA[6].to_i,lA[7].to_i]],
          subject_location: [[lA[8].to_i,lA[9].to_i]]
        }
      elsif lA[11].to_f > @aln_hits[key][:score]
        @aln_hits[key] = {
          pId: lA[2].to_f.round(2),
          evalue: lA[10],
          score: lA[11].to_f,
          hits: [hit],
          length: [lA[3].to_i],
          query_location: [[lA[6].to_i,lA[7].to_i]],
          subject_location: [[lA[8].to_i,lA[9].to_i]]
        }
      elsif lA[11].to_f == @aln_hits[key][:score]
        @aln_hits[key][:hits] << hit
        @aln_hits[key][:length] << lA[3].to_i
        @aln_hits[key][:query_location] << [lA[6].to_i,lA[7].to_i]
        @aln_hits[key][:subject_location] << [lA[8].to_i,lA[9].to_i]
      end
    end
  end

end

#get_annotation_for_contig(prots_to_annotate, ref_cds) ⇒ Object

Get the annotations for a contig for RerenceGenome



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
# File 'lib/bacterial-annotator/synteny-manip.rb', line 79

def get_annotation_for_contig prots_to_annotate, ref_cds

  return {} if prots_to_annotate == nil

  contig_to_annotate = prots_to_annotate[0].split("_")[0..-2].join("_")
  annotations = {}
  prots = []

  @aln_hits.each_key do |k|
    contig = k.split("_")[0..-2].join("_")
    if contig == contig_to_annotate
      prots << k
    end
  end

  # sorting the prot by their appearance in the contig
  prots.sort! { |a,b| a.split("_")[-1].to_i <=> b.split("_")[-1].to_i }

  i = 0
  prots_to_annotate.each do |p|

    if @aln_hits.has_key? p

      hit_index = 0

      if @aln_hits[p][:hits].length > 1
        hit_index = choose_best_hit i, prots, ref_cds
      end

      h = @aln_hits[p][:hits][hit_index]
      hit = ref_cds[h]
      annotations[p] = hit
      annotations[p][:pId] = @aln_hits[p][:pId]
      annotations[p][:length] = @aln_hits[p][:length][hit_index]
      i+=1

    else

      annotations[p] = nil

    end

  end

  annotations                 # return

end

#run_blat(root, outdir) ⇒ Object

run blat on proteins



24
25
26
27
28
# File 'lib/bacterial-annotator/synteny-manip.rb', line 24

def run_blat root, outdir
  system("#{root}/blat.linux -out=blast8 -minIdentity=#{@pidentity} -prot #{@subject_file} #{@query_file} #{outdir}/#{@name}.blat8.tsv")
  @aln_file = "#{outdir}/#{@name}.blat8.tsv"
  # extract_hits
end