Class: BacterialAnnotator
- Inherits:
-
Object
- Object
- BacterialAnnotator
- Defined in:
- lib/bacterial-annotator.rb
Instance Method Summary collapse
-
#cumulate_annotation_stats_reference(contig) ⇒ Object
cumulate the stats for the synteny return : unannotated cds array def cumulate_annotation_stats_reference contig, contig_prots_ann.
-
#extract_externaldb_prot_info(db) ⇒ Object
extract the information on protein from an externaldb.
-
#finish_annotation(remaining_cds_file) ⇒ Object
Finishing the annotation of the remaining CDS.
-
#initialize(options, root) ⇒ BacterialAnnotator
constructor
Initialize BacterialAnnotator options, ROOT (path).
-
#parse_genbank_files ⇒ Object
parse all genbank files.
-
#prepare_files_for_annotation ⇒ Object
Prepare files for the annotation Will run prodigal on the query and prepare reference genome files.
-
#print_stats(file_dir) ⇒ Object
print statistics to file.
-
#run_annotation ⇒ Object
run_alignment of reference genome proteins and the query.
- #run_mergem_synteny_prot ⇒ Object
- #run_reference_synteny_prot ⇒ Object
Constructor Details
#initialize(options, root) ⇒ BacterialAnnotator
Initialize BacterialAnnotator options, ROOT (path)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bacterial-annotator.rb', line 21 def initialize , root @root = root @options = abort if ! @options.has_key? :input @minlength = @options[:minlength].to_i @options[:minlength] = @options[:minlength].to_i @options[:pidentity] = @options[:pidentity].to_f @options[:pidentity] = @options[:pidentity] * 100 if @options[:pidentity] <= 1.00 @options[:pcoverage] = @options[:pcoverage].to_f @options[:pcoverage] = @options[:pcoverage] / 100 if @options[:pcoverage] > 1.00 if ! @options.has_key? :name @options[:name] = @options[:input].gsub(/.fasta|.fa|.fna/,"") end if File.exists? (@options[:outdir]) if ! .has_key? :force abort "Output directory already exist ! Choose another one or use -f to overwrite" else puts "Overwriting output directory #{@options[:outdir]}" FileUtils.remove_dir(@options[:outdir], :force=>true) end end Dir.mkdir(@options[:outdir]) @query_fasta = SequenceFasta.new(@root, @options[:outdir], @options[:input], @options[:meta]) @with_refence_genome = false @with_db = false if @options.has_key? :refgenome @with_refence_genome = true @ref_genome = SequenceAnnotation.new(@root, @options[:outdir], @options[:refgenome], "refGbk") elsif @options[:mergem] @with_db = true @ref_genome = SequenceAnnotation.new(@root, @options[:outdir], @options[:mergem], "db") end @with_external_db = false @with_external_db = true if @options.has_key? :external_db @prot_synteny = nil @annotation_stats = { by_contigs: {}, annotated_cds: 0, flagged_cds: [], total_cds: 0, foreign_contigs: [], synteny_contigs: [], short_contigs: [] } @contig_foreign_cds = {} @contig_annotations = {} @contig_annotations_externaldb = {} @contig_annotations_cds = {} end |
Instance Method Details
#cumulate_annotation_stats_reference(contig) ⇒ Object
cumulate the stats for the synteny return : unannotated cds array def cumulate_annotation_stats_reference contig, contig_prots_ann
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/bacterial-annotator.rb', line 455 def cumulate_annotation_stats_reference contig remaining_cds = [] contig_prots = @query_fasta.annotation_files[:prot_ids_by_contig][contig] @annotation_stats[:total_cds] += contig_prots.length if contig_prots # count contig as foreign if no cds homolog in reference genome if @contig_annotations_cds.has_key? contig and @contig_annotations_cds[contig].length > 0 @annotation_stats[:synteny_contigs] << contig else @annotation_stats[:foreign_contigs] << contig return end contig_prots.each do |prot| if @contig_annotations_cds[contig].include? prot if @prot_synteny_refgenome.query_sequences[prot].has_key? :homology and @prot_synteny_refgenome.query_sequences[prot][:homology][:hits].length > 0 assert_sum = @prot_synteny_refgenome.query_sequences[prot][:homology][:assert_cutoff].inject(:+) if assert_sum > 2 @annotation_stats[:annotated_cds] += 1 else flag = "#{prot}" flag += "\t#{@prot_synteny_refgenome.query_sequences[prot][:homology][:assert_cutoff].join(',')}" flag += "\t#{@prot_synteny_refgenome.query_sequences[prot][:homology][:pId]}" flag += "\t#{(@prot_synteny_refgenome.query_sequences[prot][:homology][:cov_query]*100).round(2)}" flag += "\t#{(@prot_synteny_refgenome.query_sequences[prot][:homology][:cov_subject]*100).round(2)}" @annotation_stats[:flagged_cds] << flag remaining_cds << prot end else puts "No " + prot end else remaining_cds << prot end end remaining_cds end |
#extract_externaldb_prot_info(db) ⇒ Object
extract the information on protein from an externaldb
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/bacterial-annotator.rb', line 580 def extract_externaldb_prot_info db # NCBI # >gi|103485499|ref|YP_615060.1| chromosomal replication initiation protein [Sphingopyxis alaskensis RB2256] # Swissprot # >sp|C7C422|BLAN1_KLEPN Beta-lactamase NDM-1 OS=Klebsiella pneumoniae GN=blaNDM-1 PE=1 SV=1 # TrEMBL # >tr|E5KIY2|E5KIY2_ECOLX Beta-lactamase NDM-1 OS=Escherichia coli GN=blaNDM-1 PE=1 SV=1 # MERGEM # >Genome_ID|location|Protein_ID|LocusTag|Gene|Protein_Product ref_cds = {} File.open(db, "r") do |dbfile| while l=dbfile.gets if l[0] == ">" lA = l.chomp.split("|") #key_gi = lA[1] key_gi = l.split(" ")[0][1..-1] product_long = lA[-1] organism = "" product = "" db_source = "[DBSource]" if product_long.scan(/|/).count >= 5 # MERGEM product = product_long db_source = "RefSeq" elsif product_long.include? " [" and product_long.include? "]" # NCBI organism = product_long[/\[.*?\]/] product = product_long.split(" [")[0].strip elsif product_long.include? "OS=" # Swissprot / TrEMBL product_tmp = product.split("OS=") organism = product_tmp[1].split(/[A-Z][A-Z]=/)[0].strip product = product_tmp[0].strip elsif product_long.include? "[A-Z][A-Z]=" # NCBI product = product_long.split(/[A-Z][A-Z]=/)[0].strip else product = product_long end org = organism.gsub("[","").gsub("]","") product.lstrip! prot_id = nil if key_gi.count("|") == 4 if lA[2] == "ref" db_source = "RefSeq" end prot_id = lA[3] elsif key_gi.count("|") == 2 if lA[0].include? == "sp" or lA[0].include? == "tr" db_source = "UniProtKB" end prot_id = lA[1] elsif key_gi.count("|") == 5 db_source = "RefSeq" prot_id = lA[2] end ref_cds[key_gi] = {product: product, org: org, prot_id: prot_id, db_source: db_source} end end end # end of file reading ref_cds end |
#finish_annotation(remaining_cds_file) ⇒ Object
Finishing the annotation of the remaining CDS
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 391 392 393 394 395 396 397 398 |
# File 'lib/bacterial-annotator.rb', line 305 def finish_annotation remaining_cds_file # only finish the annotation with an external DB if @options.has_key? :external_db # from an external DB db_file = @options[:external_db] ref_cds = SequenceAnnotation.new(@root, @options[:outdir], db_file, "fasta") # ref_cds = extract_externaldb_prot_info db_file @externaldb_synteny = SequenceSynteny.new(@root, @options[:outdir], remaining_cds_file, db_file, "Prot-ExternalDB", @options[:pidentity], @options[:pcoverage], "prot") # print "# Running BLAT alignment with External Database.." # start_time = Time.now # @externaldb_synteny.run_blat # end_time = Time.now # c_time = Helper.sec2str(end_time-start_time) # print "done (#{c_time})\n" # @externaldb_synteny.extract_hits :externaldb print "# Running alignment with External DB" start_time = Time.now @externaldb_synteny.run_diamond end_time = Time.now c_time = Helper.sec2str(end_time - start_time) print "done (#{c_time})\n" @externaldb_synteny.extract_hits :externaldb @externaldb_synteny.query_sequences.each do |k, v| contig_of_protein = k.split("_")[0..-2].join("_") if ! @contig_annotations_externaldb.has_key? contig_of_protein @contig_annotations_externaldb[contig_of_protein] = {} end next if ! v.has_key? :homology if ! @contig_annotations_cds.has_key? contig_of_protein @contig_annotations_cds[contig_of_protein] = [] end @contig_annotations_cds[contig_of_protein] << k hit_gi = v[:homology][:hits][0] # note = "Protein homology (#{v[:pId]}% identity) with gi:#{hit_gi}" cov_query = (v[:homology][:cov_query]*100).round(2) cov_subject = (v[:homology][:cov_subject]*100).round(2) note = "Protein homology (AA identity: #{v[:homology][:pId]}%; coverage (q,s): #{cov_query}%,#{cov_subject}%) with #{ref_cds.coding_seq[hit_gi][:prot_id]}" inference = "similar to AA sequence:#{ref_cds.coding_seq[hit_gi][:db_source]}:#{ref_cds.coding_seq[hit_gi][:prot_id]}" if ref_cds.coding_seq[hit_gi][:org] != "" note += " from #{ref_cds.coding_seq[hit_gi][:org]}" end @contig_annotations_externaldb[contig_of_protein][v[:homology][:hits][0]] = { product: ref_cds.coding_seq[hit_gi][:product], feature: "cds", gene: nil, prot_id: ref_cds.coding_seq[hit_gi][:prot_id], locustag: nil, note: note, inference: inference } @annotation_stats[:flagged_cds].each do |flag| if flag.include? "#{k}" if v[:homology][:assert_cutoff].inject(:+) > 2 flag.replace("#{flag}\tAnnotated by externaldb (#{v[:homology][:hits][0]}|#{v[:homology][:pId]}|#{cov_query}|#{cov_subject}))") elsif v[:homology][:assert_cutoff] == [1,1,0] flag.replace("#{flag}\tPossible pseudogene (coverage subject = #{cov_subject} with #{v[:homology][:hits][0]}))") elsif v[:homology][:assert_cutoff] == [1,0,1] flag.replace("#{flag}\tPossible pseudogene (coverage query = #{cov_query} with #{v[:homology][:hits][0]}))") elsif v[:homology][:assert_cutoff] == [0,1,1] flag.replace("#{flag}\tLow similarity (percent identity = #{v[:homology][:pId]} with #{v[:homology][:hits][0]}))") end end end end end end |
#parse_genbank_files ⇒ Object
parse all genbank files
402 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.rb', line 402 def parse_genbank_files print "# Parsing annotation into genbank files.." start_time = Time.now @contig_annotations_cds.each do |contig, contig_prots| gbk_path = @query_fasta.annotation_files[:gbk_path] gbk_to_annotate = SequenceAnnotation.new(@root, "#{gbk_path}", "#{gbk_path}/#{contig}.gbk", "newGbk") if @with_external_db and @with_refence_genome gbk_to_annotate.add_annotation_ref_synteny_prot( (@prot_synteny_refgenome.query_sequences.merge(@externaldb_synteny.query_sequences)), @contig_annotations_externaldb[contig].merge(@ref_genome.coding_seq), (File.basename @options[:refgenome]).gsub(/.gb.*/,"") ) elsif @with_external_db gbk_to_annotate.add_annotation_ref_synteny_prot( @externaldb_synteny.query_sequences, @contig_annotations_externaldb[contig] ) elsif @with_db gbk_to_annotate.add_annotation_ref_synteny_prot( @prot_synteny_refgenome.query_sequences, @ref_genome.coding_seq ) else gbk_to_annotate.add_annotation_ref_synteny_prot( @prot_synteny_refgenome.query_sequences, @ref_genome.coding_seq, (File.basename @options[:refgenome]).gsub(/.gb.*/,"") ) end if @contig_annotations_rna and @contig_annotations_rna.has_key? contig # puts "RNA annotation" gbk_to_annotate.add_annotations @contig_annotations_rna[contig], "new" end gbk_to_annotate.save_genbank_to_file end end_time = Time.now c_time = Helper.sec2str(end_time-start_time) print "done (#{c_time})\n" end |
#prepare_files_for_annotation ⇒ Object
Prepare files for the annotation Will run prodigal on the query and prepare reference genome files
213 214 215 216 217 218 219 220 |
# File 'lib/bacterial-annotator.rb', line 213 def prepare_files_for_annotation print "# Running Prodigal on your genome.." start_time = Time.now @query_fasta.run_prodigal end_time = Time.now c_time = Helper.sec2str(end_time - start_time) print "done (#{c_time})\n" end |
#print_stats(file_dir) ⇒ Object
print statistics to file
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/bacterial-annotator.rb', line 511 def print_stats file_dir file = file_dir + "/Annotation-Stats.txt" total_nb_contigs = @annotation_stats[:foreign_contigs].length + @annotation_stats[:synteny_contigs].length + @annotation_stats[:short_contigs].length p_contigs_annotated = @annotation_stats[:synteny_contigs].length.to_f/total_nb_contigs.to_f p_cds_annotated = @annotation_stats[:annotated_cds].to_f/@annotation_stats[:total_cds].to_f File.open(file, "w") do |fopen| fopen.write("#Contigs annotation based on reference genomes\n") fopen.write("Short Contigs (< #{@minlength}) :\t\t" + @annotation_stats[:short_contigs].length.to_s + "\n") fopen.write("Foreign Contigs :\t\t" + @annotation_stats[:foreign_contigs].length.to_s + "\n") fopen.write("Annotated Contigs :\t\t" + @annotation_stats[:synteny_contigs].length.to_s + "\n") fopen.write("Total Contigs :\t\t\t" + total_nb_contigs.to_s + "\n") fopen.write("% Contigs annotated :\t\t" + (p_contigs_annotated*100).round(2).to_s + "\n") fopen.write("\n") fopen.write("#CDS annotations based on reference genomes\n") fopen.write("Annotated CDS :\t\t\t" + @annotation_stats[:annotated_cds].to_s + "\n") fopen.write("Flagged CDS :\t\t\t" + @annotation_stats[:flagged_cds].length.to_s + "\n") fopen.write("Total CDS :\t\t\t" + @annotation_stats[:total_cds].to_s + "\n") fopen.write("% CDS annotated :\t\t" + (p_cds_annotated*100).round(2).to_s + "\n") fopen.write("\n") end file_flagged_cds = file_dir + "/Prot-flagged.tsv" File.open(file_flagged_cds, "w") do |fopen| fopen.write("CDS locus\tAssertion-CutOff\tAA Identity\tCovQuery(%)\tCovSubject(%)\tNote\n") @annotation_stats[:flagged_cds].each do |fcds| fopen.write("#{fcds}\n") end end end |
#run_annotation ⇒ Object
run_alignment of reference genome proteins and the query
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 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 |
# File 'lib/bacterial-annotator.rb', line 96 def run_annotation prepare_files_for_annotation # process reference genome synteny if @with_refence_genome # Annotation with the Reference Genome @prot_synteny_refgenome = run_reference_synteny_prot # iterate over each contig # discard short contig # cumulate statistics of homolog CDS @query_fasta.annotation_files[:contigs].each_with_index do |contig, contig_index| # Skip short contigs if @query_fasta.annotation_files[:contigs_length][contig_index] < @minlength @annotation_stats[:short_contigs] << contig next end remaining_cds = cumulate_annotation_stats_reference contig if remaining_cds != [] @contig_foreign_cds[contig] = remaining_cds end end # dump foreign proteins to file foreign_cds_file = dump_cds # dump reference CDS synteny to file dump_ref_synteny_to_file # run RNA annotation @rna_synteny = SequenceSynteny.new(@root, @options[:outdir], @query_fasta.fasta_file, @ref_genome.rna_file, "RNA-Ref", @options[:pidentity], @options[:pcoverage], "dna") print "# Running alignment with Reference Genome RNA (blat).." start_time = Time.now @rna_synteny.run_blat end_time = Time.now c_time = Helper.sec2str(end_time-start_time) print "done (#{c_time})\n" # # takes too long # print "# Running alignment with Reference Genome RNA (fasta36).." # start_time = Time.now # @rna_synteny.run_fasta36 # end_time = Time.now # c_time = Helper.sec2str(end_time-start_time) # print "done (#{c_time})\n" @rna_synteny.extract_hits_dna :rna @contig_annotations_rna = {} @query_fasta.annotation_files[:contigs].each_with_index do |contig, contig_index| @contig_annotations_rna[contig] = @rna_synteny.get_annotation_for_contig contig end elsif @with_db @prot_synteny_refgenome = run_mergem_synteny_prot # iterate over each contig # discard short contig # cumulate statistics of homolog CDS @query_fasta.annotation_files[:contigs].each_with_index do |contig, contig_index| # Skip short contigs if @query_fasta.annotation_files[:contigs_length][contig_index] < @minlength @annotation_stats[:short_contigs] << contig next end remaining_cds = cumulate_annotation_stats_reference contig if remaining_cds != [] @contig_foreign_cds[contig] = remaining_cds end end # dump foreign proteins to file foreign_cds_file = dump_cds # dump reference CDS synteny to file dump_ref_synteny_to_file else # no reference genome # no reference genome .. will process all the CDS as foreign for the external db foreign_cds_file = @query_fasta.annotation_files[:proteins] end # Finishing annotation for foreign proteins finish_annotation foreign_cds_file # Parse annotations to genbank files parse_genbank_files print "# Printing Statistics.." print_stats "#{@options[:outdir]}" print "done\n" end |
#run_mergem_synteny_prot ⇒ Object
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 |
# File 'lib/bacterial-annotator.rb', line 223 def run_mergem_synteny_prot ref_synteny_prot = SequenceSynteny.new(@root, @options[:outdir], @query_fasta.annotation_files[:proteins], @ref_genome.cds_file, "Prot-Ref", @options[:pidentity], @options[:pcoverage], "prot") print "# Running alignment with Reference Genome CDS (diamond).." start_time = Time.now ref_synteny_prot.run_diamond end_time = Time.now c_time = Helper.sec2str(end_time - start_time) print "done (#{c_time})\n" ref_synteny_prot.extract_hits :refgenome ref_synteny_prot.query_sequences.each do |k,v| if v.has_key? :homology @contig_annotations_cds[v[:contig]] = [] if ! @contig_annotations_cds.has_key? v[:contig] @contig_annotations_cds[v[:contig]] << k end end ref_synteny_prot end |
#run_reference_synteny_prot ⇒ Object
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/bacterial-annotator.rb', line 258 def run_reference_synteny_prot ref_synteny_prot = SequenceSynteny.new(@root, @options[:outdir], @query_fasta.annotation_files[:proteins], @ref_genome.cds_file, "Prot-Ref", @options[:pidentity], @options[:pcoverage], "prot") print "# Running alignment with Reference Genome CDS (diamond).." start_time = Time.now ref_synteny_prot.run_diamond end_time = Time.now c_time = Helper.sec2str(end_time - start_time) print "done (#{c_time})\n" # print "# Running alignment with Reference Genome CDS (blat).." # start_time = Time.now # ref_synteny_prot.run_blat # end_time = Time.now # c_time = Helper.sec2str(end_time - start_time) # print "done (#{c_time})\n" # print "# Running alignment with Reference Genome CDS (fasta36).." # start_time = Time.now # ref_synteny_prot.run_fasta36 # end_time = Time.now # c_time = Helper.sec2str(end_time - start_time) # print "done (#{c_time})\n" ref_synteny_prot.extract_hits :refgenome ref_synteny_prot.query_sequences.each do |k,v| if v.has_key? :homology @contig_annotations_cds[v[:contig]] = [] if ! @contig_annotations_cds.has_key? v[:contig] @contig_annotations_cds[v[:contig]] << k end end ref_synteny_prot end |