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, type) ⇒ SyntenyManip

Returns a new instance of SyntenyManip.



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

def initialize query_file, subject_file, name, pidentity, type
  @query_file = query_file
  @subject_file = subject_file
  @name = name
  @pidentity = pidentity
  @aln_file = nil
  @type = type
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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/bacterial-annotator/synteny-manip.rb', line 206

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_dna(mode) ⇒ Object

Extract Hit from blast8 file and save it in hash prpa PA0668.4|rRNA|23S 99.97 2891 1 0 705042 707932 1 2891 0.0e+00 5671.0



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/bacterial-annotator/synteny-manip.rb', line 88

def extract_hits_dna mode

  @aln_hits = {}
  feature = ""
  File.open(@aln_file,"r") do |fread|
    while l = fread.gets
      lA = l.chomp!.split("\t")
      key = lA[0]+"_"+lA[6]+"_"+lA[7]
      if mode == :rna
        hit_split = lA[1].chomp.split("|")
        hit = hit_split[0]
        feature = hit_split[1]
        product = hit_split[2]
      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],
          product: [product],
          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]],
          feature: [feature]
        }
      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],
          product: [product],
          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]],
          feature: [feature]
        }
      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]
        @aln_hits[key][:feature] << feature
        @aln_hits[key][:product] << product
      end
    end
  end

  prune_aln_hits @aln_hits

end

#extract_hits_prodigal(mode, ref_cds = nil) ⇒ 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



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

def extract_hits_prodigal mode, ref_cds=nil

  @aln_hits = {}
  feature = ""
  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]
        feature = "cds"
      elsif mode == :externaldb
        hit = lA[1].chomp.split("|")[3]
        feature = "cds"
      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]],
          feature: feature
        }
      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]],
          feature: feature
        }
      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(contig_to_annotate, prots_to_annotate = nil, ref_cds = nil) ⇒ Object

Get the annotations for a contig for RerenceGenome



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

def get_annotation_for_contig contig_to_annotate, prots_to_annotate=nil, ref_cds=nil

  annotations = {}

  if prots_to_annotate != nil

    # contig_to_annotate = prots_to_annotate[0].split("_")[0..-2].join("_")
    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

  elsif ! @aln_hits.empty?

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

  end

  annotations                 # return

end

#prune_aln_hits(aln_hits) ⇒ Object

end of method



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/bacterial-annotator/synteny-manip.rb', line 272

def prune_aln_hits aln_hits

  # @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]],
  #   feature: [feature]
  # }

  keys_to_delete = []

  aln_hits.each do |key1,val1|

    aln_hits.each do |key2,val2|

      next if key1==key2
      next if keys_to_delete.include? key1
      next if keys_to_delete.include? key2

      if val1[:query_location][0][0] >= val2[:query_location][0][0] and
        val1[:query_location][0][0] < val2[:query_location][0][1]
        overlap_len = val2[:query_location][0][1] - val1[:query_location][0][0]
        val1_len = val1[:query_location][0][1]-val1[:query_location][0][0]
        val2_len = val2[:query_location][0][1]-val2[:query_location][0][0]
        if overlap_len.to_f/val1_len > 0.2 and overlap_len.to_f/val2_len > 0.2
          if val1[:score] < val2[:score]
            keys_to_delete << key1
          else
            keys_to_delete << key2
          end
        end
      elsif val2[:query_location][0][0] >= val1[:query_location][0][0] and
           val2[:query_location][0][0] < val1[:query_location][0][1]
        overlap_len = val1[:query_location][0][1] - val2[:query_location][0][0]
        val1_len = val1[:query_location][0][1]-val1[:query_location][0][0]
        val2_len = val2[:query_location][0][1]-val2[:query_location][0][0]
        if overlap_len.to_f/val1_len > 0.2 and overlap_len.to_f/val2_len > 0.2
          if val1[:score] < val2[:score]
            keys_to_delete << key1
          else
            keys_to_delete << key2
          end
        end
      end

    end

  end

  keys_to_delete.each do |k|
    aln_hits.delete(k)
  end

end

#run_blat(root, outdir) ⇒ Object

run blat on proteins



25
26
27
28
29
30
31
32
33
# File 'lib/bacterial-annotator/synteny-manip.rb', line 25

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