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

Instance Method Details

#add_annotation(annotations, outdir, mode, reference_locus) ⇒ Object

add annotation to a genbank file produced by prodigal



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

def add_annotation annotations, outdir, mode, reference_locus

  nb_of_added_ft = 0
  i = 0

  contig = @gbk.definition

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

    next if cds.feature != "CDS"

    if mode == 0
      ftArray = []
      cds.qualifiers = []
    else
      ftArray = cds.qualifiers
    end

    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

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

  # Bioruby doesn't support gff at this point
  # File.open("#{outdir}/#{contig}.gff", "w") do |f| 
  #   f.write(@gbk.to_biosequence.output(:gff))
  # 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
84
85
86
87
88
89
90
# 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?

      # if ftH.has_key? "translation"
      #   pep = ftH["translation"][0] if !ftH["translation"].nil?
      # else
      #   dna = get_DNA(ft,@bioseq)
      #   pep = dna.translate
      # end

      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

#write_cds_to_file(outdir) ⇒ Object

Print CDS to files RETURN : cds_file path



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bacterial-annotator/genbank-manip.rb', line 95

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