Class: GenbankManip

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gbk_file, outdir) ⇒ GenbankManip

Initialize then genbank file



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

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.



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

def cds_file
  @cds_file
end

#coding_seqObject

Returns the value of attribute coding_seq.



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

def coding_seq
  @coding_seq
end

#gbkObject

Returns the value of attribute gbk.



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

def gbk
  @gbk
end

#rna_fileObject

Returns the value of attribute rna_file.



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

def rna_file
  @rna_file
end

Instance Method Details

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

add annotation to a genbank file produced by prodigal



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

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
      hit = annotations[prot_id] if annotations.has_key? prot_id

      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



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

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}
    end

  end

  @coding_seq

end

#get_rnaObject

Prepare rRNA tRNA



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/genbank-manip.rb', line 86

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



277
278
279
280
281
282
283
# File 'lib/bacterial-annotator/genbank-manip.rb', line 277

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/bacterial-annotator/genbank-manip.rb', line 131

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/bacterial-annotator/genbank-manip.rb', line 157

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