Class: SequenceAnnotation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gbk_file, outdir) ⇒ SequenceAnnotation

Initialize then genbank file



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

def initialize gbk_file, outdir

  @gbk_file = gbk_file
  if ! File.exists? @gbk_file
    fetch_ncbi_genome(@gbk_file, outdir)
    @gbk_file = "#{outdir}/#{gbk_file}.gbk"
    # @gbk_file += ".gbk"
  end

  flat_gbk = Bio::FlatFile.auto(@gbk_file)

  # Check if gbk is valid
  if flat_gbk.dbclass != Bio::GenBank
    abort "Aborting : The input #{@gbk_file} is not a valid genbank file !"
  else
    @gbk = flat_gbk.next_entry
  end

  @bioseq = @gbk.to_biosequence

end

Instance Attribute Details

#cds_fileObject

Returns the value of attribute cds_file.



12
13
14
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 12

def cds_file
  @cds_file
end

#coding_seqObject

Returns the value of attribute coding_seq.



12
13
14
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 12

def coding_seq
  @coding_seq
end

#gbkObject

Returns the value of attribute gbk.



12
13
14
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 12

def gbk
  @gbk
end

#rna_fileObject

Returns the value of attribute rna_file.



12
13
14
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 12

def rna_file
  @rna_file
end

Instance Method Details

#add_annotation_ref_synteny_prot(synteny_prot, annotations, ref_genome = nil) ⇒ Object

add annotation from reference prot synteny



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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 182

def add_annotation_ref_synteny_prot synteny_prot, annotations, ref_genome=nil

  contig = @gbk.definition

  prot_iterator = 0
  @gbk.features.each_with_index do |cds, ft_index|

    next if cds.feature != "CDS"

    prot_iterator+=1
    prot_id = contig+"_"+prot_iterator.to_s

    ftArray = []
    cds.qualifiers = []

    hit = nil

    if ! synteny_prot.has_key? prot_id or
       ! synteny_prot[prot_id].has_key? :homology or
       ! annotations.has_key? synteny_prot[prot_id][:homology][:hits][0] or
       synteny_prot[prot_id][:homology][:assert_cutoff].inject(:+) < 3

      qLocusTag = Bio::Feature::Qualifier.new('locus_tag', "#{prot_id}")
      qProd = Bio::Feature::Qualifier.new('product', "hypothetical protein")

      ftArray.push(qLocusTag)
      ftArray.push(qProd)

      if synteny_prot.has_key? prot_id and
        synteny_prot[prot_id].has_key? :homology and
        synteny_prot[prot_id][:homology][:assert_cutoff] == [1,1,0]
        hit = annotations[synteny_prot[prot_id][:homology][:hits][0]]
        qNote = Bio::Feature::Qualifier.new('note', "possible pseudo gene of #{hit[:locustag]} from #{ref_genome}")
        ftArray.push(qNote)
      end

    else

      hit = annotations[synteny_prot[prot_id][:homology][:hits][0]]

      if synteny_prot.has_key? prot_id

        locus, gene, product, note, inference = nil
        locus = hit[:locustag]
        gene = hit[:gene]
        product = hit[:product]
        note = hit[:note]
        inference = hit[:inference]
        pId = synteny_prot[prot_id][:homology][:pId]
        cov_query = (synteny_prot[prot_id][:homology][:cov_query]*100).round(2)
        cov_subject = (synteny_prot[prot_id][:homology][:cov_subject]*100).round(2)
        reference_prot_id = synteny_prot[prot_id][:homology][:hits][0]

        qLocusTag = Bio::Feature::Qualifier.new('locus_tag', "#{prot_id}")
        ftArray.push(qLocusTag)

        if gene != nil
          qGene = Bio::Feature::Qualifier.new('gene', gene)
          ftArray.push(qGene)
        end

        if product != nil
          qProd = Bio::Feature::Qualifier.new('product', product)
          ftArray.push(qProd)
        end

        # check if there is a reference genome.. reference_locus shouldn't be nil in that case
        if locus != nil
          qNote = Bio::Feature::Qualifier.new('note', "corresponds to #{locus} locus (AA identity: #{pId}%; coverage(q,s): #{cov_query}%,#{cov_subject}%) from #{ref_genome}")
          ftArray.push(qNote)

          db_source = "[DBSource]"
          if reference_prot_id.include? "_"
            db_source = "RefSeq"
          else
            db_source = "INSD"
          end
          qInference = Bio::Feature::Qualifier.new('inference', "similar to AA sequence:#{db_source}:#{reference_prot_id}")
          ftArray.push(qInference)

        end

        if note != nil
          qNote = Bio::Feature::Qualifier.new('note', note)
          ftArray.push(qNote)
        end

        if inference != nil
          qInference = Bio::Feature::Qualifier.new('inference', inference)
          ftArray.push(qInference)
        end

      end

    end

    cds.qualifiers = ftArray

  end


end

#add_annotations(annotations, mode, reference_locus = nil) ⇒ Object

add annotation to a genbank file produced by prodigal



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
334
335
336
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
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 287

def add_annotations annotations, mode, reference_locus=nil

  # nb_of_added_ft = 0
  i = 0

  contig = @gbk.definition

  if mode == "inplace"

    # iterate through
    @gbk.features.each_with_index do |cds, ft_index|

      next if cds.feature != "CDS"

      ftArray = []
      cds.qualifiers = []

      i += 1
      prot_id = contig+"_"+i.to_s
      hit = nil

      if annotations.has_key? prot_id
        hit = annotations[prot_id]
      else
        puts "no hit for #{prot_id}"
        next
      end

      if hit != nil

        locus, gene, product, note = nil
        locus = hit[:locustag]
        gene = hit[:gene]
        product = hit[:product]
        note = hit[:note]
        pId = hit[:pId]

        if gene != nil
          qGene = Bio::Feature::Qualifier.new('gene', gene)
          ftArray.push(qGene)
        end

        if product != nil
          qProd = Bio::Feature::Qualifier.new('product', product)
          ftArray.push(qProd)
        end

        # check if there is a reference genome.. reference_locus shouldn't be nil in that case
        if locus != nil
          qNote = Bio::Feature::Qualifier.new('note', "corresponds to #{locus} locus (#{pId}% identity) from #{reference_locus.entry_id}")
          ftArray.push(qNote)
        end

        if note != nil
          qNote = Bio::Feature::Qualifier.new('note', note)
          ftArray.push(qNote)
        end

      end
      cds.qualifiers = ftArray

    end


  elsif mode == "new"

    sorted_annotations = annotations.sort_by { |k, v| v[:query_location][0][0] }

    new_features = {}
    annotations_done = {}

    @gbk.features.each_with_index do |ft, ft_index|

      sorted_annotations.each do |k,v|

        next if annotations_done.has_key? k

        if v[:query_location][0][0] < ft.locations[0].from

          if v[:subject_location][0][0] > v[:subject_location][0][1]
            location = "complement(#{v[:query_location][0][0]}..#{v[:query_location][0][1]})"
          else
            location = "#{v[:query_location][0][0]}..#{v[:query_location][0][1]}"
          end

          feature = Bio::Feature.new(v[:feature][0],location)
          feature.qualifiers.push(Bio::Feature::Qualifier.new('product',v[:product][0])) if ! v[:product][0].nil? or v[:product][0] != ""
          new_features[ft_index] = feature
          annotations_done[k] = 1
          break

        end

      end

    end

    new_features.each do |k,v|
      @gbk.features.insert(k,v)
    end

  end

end

#get_cdsObject

Prepare CDS/proteins



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

def get_cds

  if @coding_seq == nil

    @coding_seq = {}

    # Iterate over each CDS
    @gbk.each_cds do |ft|
      ftH = ft.to_hash
      loc = ft.locations
      gene = []
      product = []
      protId = ""
      if ftH.has_key? "pseudo"
        next
      end
      gene = ftH["gene"] if !ftH["gene"].nil?
      product = ftH["product"] if !ftH["product"].nil?
      protId = ftH["protein_id"][0] if !ftH["protein_id"].nil?
      locustag = ftH["locus_tag"][0] if !ftH["locus_tag"].nil?

      dna = get_DNA(ft,@bioseq)
      pep = dna.translate
      pepBioSeq = Bio::Sequence.auto(pep)
      dnaBioSeq = Bio::Sequence.auto(dna)

      if protId.strip == ""
        protId = locustag
      end

      @coding_seq[protId] = {
        protId: protId,
        location: loc,
        locustag: locustag,
        gene: gene[0],
        product: product[0],
        bioseq: pepBioSeq,
        bioseq_gene: dnaBioSeq,
        bioseq_len: pepBioSeq.length
      }

    end

  end

  @coding_seq

end

#get_rnaObject

Prepare rRNA tRNA



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

def get_rna

  if @rna_seq == nil

    @rna_seq = {}
    @gbk.features do |ft|

      next if ! ft.feature.to_s.include? "RNA"

      ftH = ft.to_hash
      loc = ft.locations
      # seqBeg = loc[0].from.to_s
      # seqEnd = loc[0].to.to_s
      # strand = loc[0].strand.to_s
      if ftH.has_key? "pseudo"
        next
      end
      # gene = ftH["gene"] if !ftH["gene"].nil?
      # protId = ftH["protein_id"][0] if !ftH["protein_id"].nil?
      product = ""
      product = ftH["product"][0] if !ftH["product"].nil?
      locustag = ftH["locus_tag"][0] if !ftH["locus_tag"].nil?

      # puts "#{@accession}\t#{seqBeg}\t#{seqEnd}\t#{strand}\t#{protId}\t#{locustag}\t#{gene[0]}\t#{product[0]}"
      dna = get_DNA(ft,@bioseq)
      dnaBioSeq = Bio::Sequence.auto(dna)

      @rna_seq[locustag] = {
        type: ft.feature.to_s,
        location: loc,
        locustag: locustag,
        product: product,
        bioseq_gene: dnaBioSeq
      }

    end

  end

  @rna_seq

end

#save_genbank_to_file(outdir) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 393

def save_genbank_to_file outdir

  File.open("#{outdir}/#{@gbk.definition}.gbk", "w") do |f|
    f.write(@gbk.to_biosequence.output(:genbank))
  end

end

#write_cds_to_file(outdir) ⇒ Object

Print CDS to files RETURN : cds_file path



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 135

def write_cds_to_file outdir

  cds_file = "#{@gbk.accession}.pep"
  dna_file = "#{@gbk.accession}.dna"

  if @coding_seq == nil
    get_cds
  end

  dna_out = File.open("#{outdir}/#{dna_file}", "w")
  File.open("#{outdir}/#{cds_file}", "w") do |fwrite|
    @coding_seq.each_key do |k|
      seqout = @coding_seq[k][:bioseq].output_fasta("#{k}",60)
      seqout_dna = @coding_seq[k][:bioseq_gene].output_fasta("#{k}",60)
      fwrite.write(seqout)
      dna_out.write(seqout_dna)
    end
  end
  dna_out.close

  @cds_file = "#{outdir}/" + cds_file

end

#write_rna_to_file(outdir) ⇒ Object

Print RNA to files RETURN : rna_file path



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/bacterial-annotator/sequence-annotation.rb', line 161

def write_rna_to_file outdir

  rna_file = "#{@gbk.accession}.rna"

  if @rna_seq == nil
    get_rna
  end

  File.open("#{outdir}/#{rna_file}", "w") do |fwrite|
    @rna_seq.each_key do |k|
      seqout_dna = @rna_seq[k][:bioseq_gene].output_fasta("#{k}|#{@rna_seq[k][:type]}|#{@rna_seq[k][:product]}",60)
      fwrite.write(seqout_dna)
    end
  end

  @rna_file = "#{outdir}/" + rna_file

end