Class: SequenceSynteny

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, outdir, query_file, subject_file, name, pidentity, min_coverage, type) ⇒ SequenceSynteny

Returns a new instance of SequenceSynteny.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 16

def initialize root, outdir, query_file, subject_file, name, pidentity, min_coverage, type

  @root = root
  @outdir = outdir
  @query_file = query_file
  @subject_file = subject_file
  @query_sequences = get_sequences(query_file)
  @subject_sequences = get_sequences(subject_file)

  @name = name
  @pidentity = pidentity
  @min_coverage = min_coverage
  @aln_file = nil
  @type = type

end

Instance Attribute Details

#aln_hitsObject (readonly)

Returns the value of attribute aln_hits.



14
15
16
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 14

def aln_hits
  @aln_hits
end

#query_fileObject (readonly)

Returns the value of attribute query_file.



14
15
16
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 14

def query_file
  @query_file
end

#query_sequencesObject (readonly)

Returns the value of attribute query_sequences.



14
15
16
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 14

def query_sequences
  @query_sequences
end

#subject_fileObject (readonly)

Returns the value of attribute subject_file.



14
15
16
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 14

def subject_file
  @subject_file
end

#subject_sequencesObject (readonly)

Returns the value of attribute subject_sequences.



14
15
16
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 14

def subject_sequences
  @subject_sequences
end

Instance Method Details

#choose_best_hit(i, prots, ref_cds) ⇒ Object

Choose Best Hit base on neighbor hits



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 337

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



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

def extract_hits mode

  feature = ""
  File.open(@aln_file,"r") do |fread|
    while l = fread.gets

      lA = l.chomp!.split("\t")
      key = lA[0]

      # extraction of hit id depends on mode ..
      if mode == :refgenome
        hit = lA[1]
        feature = "cds"
      elsif mode == :externaldb
        # hit = lA[1].chomp.split("|")[3]
        hit = lA[1]
        feature = "cds"
        # decrease min. % identity for foreign proteins
        @pidentity = 60.0
      end

      # compute coverage based on sequences length
      cov_query = (lA[3].to_f/@query_sequences[key][:length]).round(2)
      cov_subject = (lA[3].to_f/@subject_sequences[hit][:length]).round(2)

      # assert cutoff on identity and coverage
      # 1 -> pass cutoff, 0 under cutoff
      assert_cutoff = [1,1,1]
      assert_cutoff[0] = 0 if lA[2].to_f < @pidentity
      assert_cutoff[1] = 0 if cov_query < @min_coverage
      assert_cutoff[2] = 0 if cov_subject < @min_coverage and @query_sequences[key][:partial] == false

      # first hit for query
      if ! @query_sequences[key].has_key? :homology
        @query_sequences[key][:conserved] = true
        # @subject_sequences[key][:conserved] = true
        @query_sequences[key][:homology] = {
          pId: lA[2].to_f.round(2),
          cov_query: cov_query,
          cov_subject: cov_subject,
          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,
          assert_cutoff: assert_cutoff
        }
        @subject_sequences[hit][:hits] = [key]

      # query already got at least 1 hit and new_score > last_score
      elsif lA[11].to_f > @query_sequences[key][:homology][:score]
        @query_sequences[key][:conserved] = true
        # @subject_sequences[key][:conserved] = true
        @query_sequences[key][:homology] = {
          pId: lA[2].to_f.round(2),
          cov_query: cov_query,
          cov_subject: cov_subject,
          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,
          assert_cutoff: assert_cutoff
        }
        @subject_sequences[hit][:hits] =  [key]

      # query already got at least 1 hit and score == last_score
      elsif lA[11].to_f == @query_sequences[key][:homology][:score]
        @query_sequences[key][:homology][:hits] << hit
        @query_sequences[key][:homology][:length] << lA[3].to_i
        @query_sequences[key][:homology][:query_location] << [lA[6].to_i,lA[7].to_i]
        @query_sequences[key][:homology][:subject_location] << [lA[8].to_i,lA[9].to_i]
        if @subject_sequences[hit].has_key? :hits
          @subject_sequences[hit][:hits] << key
        else
          @subject_sequences[hit][:hits] = [key]
        end
      end
    end
  end

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



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

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
      next if lA[2].to_f < @pidentity
      if ! @aln_hits.has_key? key
        @aln_hits[key] = {
          pId: lA[2].to_f.round(2),
          # cov_query: (@query_sequences[key][:length]/lA[3].to_f).round(2),
          # cov_subject: (@subject_sequences[hit][:length]/lA[3].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),
          # cov_query: (@query_sequences[key][:length]/lA[3].to_f).round(2),
          # cov_subject: (@subject_sequences[hit][:length]/lA[3].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

#get_annotation_for_contig(contig_to_annotate, prots_to_annotate = nil, ref_cds = nil) ⇒ Object

Get the annotations for a contig for RerenceGenome



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
330
331
332
333
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 275

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

#get_sequences(raw_file) ⇒ Object

get sequences name with length in hash



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

def get_sequences raw_file

  sequences = {}

  if raw_file.include?(".dmnd")

    seq_info_file = raw_file.gsub(".dmnd",".json.gz")

    json_genes = {}
    Zlib::GzipReader.open(seq_info_file) {|gz|
      json_genes = JSON.parse(gz.read)
    }

    json_genes.each do |gene|

      sequences[gene["cluster_id"]] = {}
      sequences[gene["cluster_id"]][:length] = gene["consensus_length"].to_f
      sequences[gene["cluster_id"]][:conserved] = false
      sequences[gene["cluster_id"]][:contig] = gene["cluster_id"].split("_")[0..-2].join("_") if gene["cluster_id"].include? "_"

    end

  else

    seq_file = raw_file
    flat = Bio::FlatFile.auto("#{seq_file}")
    flat.each_entry do |s|
      s_name = s.definition.chomp.split(" ")[0]
      sequences[s_name] = {}
      properties = s.definition.chomp.split(";")
      partial = false
      if properties.length >= 2 and properties[1].include? "partial"
        partial = (properties[1].gsub("partial=","").include? '1')
      end
      sequences[s_name][:partial] = partial
      sequences[s_name][:length] = s.seq.length
      sequences[s_name][:conserved] = false
      sequences[s_name][:contig] = s_name.split("_")[0..-2].join("_") if s_name.include? "_"

    end

  end

  sequences

end

#prune_aln_hits(aln_hits) ⇒ Object

end of method



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 403

def prune_aln_hits aln_hits

  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_blatObject

run blat on proteins



83
84
85
86
87
88
89
90
91
92
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 83

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

#run_diamondObject

run diamond on proteins



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 106

def run_diamond
  if @type == "prot"
    if subject_file.include? ".dmnd"
      db_file = subject_file
    else
      system("#{@root}/diamond.linux makedb --db #{subject_file} --in #{subject_file} > /dev/null 2>&1")
      db_file = subject_file
    end
    system("#{@root}/diamond.linux blastp --masking none --db #{db_file} -q #{query_file} -o #{@outdir}/#{@name}.diamond.tsv -f 6 > /dev/null 2>&1")
  else
    # system("#{@root}/glsearch36.linux -b 3 -E 1e-25 -m 8 #{@subject_file} #{@query_file} > #{@outdir}/#{@name}.fasta36.tsv")
  end
  @aln_file = "#{@outdir}/#{@name}.diamond.tsv"
  # extract_hits
end

#run_fasta36Object

run fasta36 on proteins



95
96
97
98
99
100
101
102
103
# File 'lib/bacterial-annotator/sequence-synteny.rb', line 95

def run_fasta36
  if @type == "prot"
    system("#{@root}/fasta36.linux -T 1 -b 3 -E 1e-40 -m 8 #{@query_file} #{@subject_file} > #{@outdir}/#{@name}.fasta36.tsv")
  else
    system("#{@root}/glsearch36.linux -T 1 -b 12 -E 1e-40 -m 8 #{@query_file} #{@subject_file} > #{@outdir}/#{@name}.fasta36.tsv")
  end
  @aln_file_fasta36 = "#{@outdir}/#{@name}.fasta36.tsv"
  # extract_hits
end