1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 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 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 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 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 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 399 400 401 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 450 451 452 453 454 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 508 509 510 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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
|
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Getopt::Long qw(:config no_ignore_case bundling);
use lib ("/usr/lib/trinityrnaseq/PerlLib");
use Fasta_reader;
use Gene_obj;
use GFF3_utils;
use Nuc_translator;
use Data::Dumper;
#DB::disable_profile(); # Devel::NYTProf
my $usage = <<__EOUSAGE__;
##############################################################################
#
# --gff3 gene annotations in gff3 format
# --genome genome sequence in fasta format.
# --vcf SNP data in vcf format
#
# -X write the .coding_mutations_described.txt file
#
##############################################################################
__EOUSAGE__
;
my ($gff3_file, $genome_file, $vcf_file);
my $WRITE_CODING_MUTANT_DESCRIPTIONS = 0;
&GetOptions ( 'gff3=s' => \$gff3_file,
'genome=s' => \$genome_file,
'vcf=s' => \$vcf_file,
'X' => \$WRITE_CODING_MUTANT_DESCRIPTIONS,
);
unless ($gff3_file && $genome_file && $vcf_file) {
die $usage;
}
foreach my $file ($gff3_file, $genome_file, $vcf_file) {
unless (-s $file) {
die "Error, cannot find $file";
}
}
my %CDS_SEQUENCES; # storing all CDS sequences here... very hacky, not proud.
my $SNP_LINE_COUNTER = 0;
main: {
# get gene annotations.
my $gene_obj_indexer = {};
my $contig_to_gene_list_href = &GFF3_utils::index_GFF3_gene_objs($gff3_file, $gene_obj_indexer);
my $fasta_reader = new Fasta_reader($genome_file);
my %genome = $fasta_reader->retrieve_all_seqs_hash();
my $curr_contig = "";
my $snp_text = "";
my $coding_mutations_explained_file = "$vcf_file.coding_mutations_described.txt";
my $cod_mut_exp_fh;
if ($WRITE_CODING_MUTANT_DESCRIPTIONS) {
open (my $cod_mut_exp_fh, ">$coding_mutations_explained_file") or die "Error, cannot write to $coding_mutations_explained_file";
}
#DB::enable_profile();
my $counter = 0;
print STDERR "-reading vcf file: $vcf_file\n";
open (my $fh, $vcf_file) or die "Error, cannot open file $vcf_file";
while (<$fh>) {
#print STDERR $_;
my $line = $_;
if (/^\#/) {
## retain header for Gustavo's importer.
print;
next;
}
unless (/\w/) { next; }
chomp;
my @x = split(/\t/);
my $contig = $x[0];
my $snp_call = $x[4];
my $snp_quality_tag = $x[6];
#if ($snp_call eq ".") { next; }
#if ($snp_quality_tag =~ /GATKStandard|LowQual/) { next; } # low quality, ignore.
if ($curr_contig ne $contig) {
print STDERR "\n-analyzing snps on contig: $curr_contig\n" if $curr_contig;
&analyze_snps($curr_contig, $snp_text, $gene_obj_indexer, $contig_to_gene_list_href, \%genome, $cod_mut_exp_fh) if $curr_contig;
$curr_contig = $contig;
$snp_text = "";
$counter = 0;
print STDERR "\n----\nReading VCF for contig: $contig\n";
}
$snp_text .= $line;
$counter++;
print STDERR "\r[$counter vcf lines read for $contig] " if ($counter % 1000 == 0);
}
close $fh;
&analyze_snps($curr_contig, $snp_text, $gene_obj_indexer, $contig_to_gene_list_href, \%genome, $cod_mut_exp_fh) if $curr_contig;
print STDERR "\n\nDone.\n\n";
exit(0);
}
####
sub analyze_snps {
my ($curr_contig, $snp_text, $gene_obj_indexer, $contig_to_gene_list_href, $genome_href, $cod_mut_exp_fh) = @_;
my $contig_seq = $genome_href->{$curr_contig} or die "Error, no sequence for contig: $curr_contig";
my @contig_chars = split(//, $contig_seq);
## annotate every base.
my @base_annotations; # populated as genes are encountered below. Maintained as a FIFO
## decorate with annotations.
my @gene_structs;
if (exists $contig_to_gene_list_href->{$curr_contig}) {
my @gene_ids = @{$contig_to_gene_list_href->{$curr_contig}};
foreach my $gene_id (@gene_ids) {
my $gene_obj = $gene_obj_indexer->{$gene_id};
my ($lend, $rend) = sort {$a<=>$b} $gene_obj->get_gene_span();
push (@gene_structs, { lend => $lend,
rend => $rend,
gene => $gene_obj,
} );
}
@gene_structs = sort {$a->{lend}<=>$b->{lend}} @gene_structs;
}
my $prev_lend_gene_struct; # track for intergenics
####
# annotate the vcf file
my @snp_lines = split(/\n/, $snp_text);
my $counter = 0;
print STDERR "-annotating SNPs on contig: $curr_contig\n";
foreach my $snp_line (@snp_lines) {
# print STDERR "SNPLINE: $snp_line\n";
$counter++;
$SNP_LINE_COUNTER++;
if ($counter % 1000 == 0) {
print STDERR "\r[contig_snp_count: $counter, total_snp_count: $SNP_LINE_COUNTER] ";
}
my @x = split(/\t/, $snp_line);
my ($chrom, $pos, $id, $ref, $alt, $qual, $filter, $snp_info, @rest) = @x;
if (length($ref) == 1 && $contig_chars[$pos-1] ne $ref) {
print STDERR "\n!! ERROR !! $snp_line\nError, $chrom $pos $ref != actual base: " . $contig_chars[$pos-1] . "\n";
die;
}
## encode base annotations according to gene structure
while (@gene_structs) {
if ($gene_structs[0]->{rend} < $pos) {
# we're past the point of this feature in the contig.
$prev_lend_gene_struct = shift @gene_structs;
}
elsif ($gene_structs[0]->{lend} <= $pos && $gene_structs[0]->{rend} >= $pos) {
# gene overlaps SNP
$prev_lend_gene_struct = shift @gene_structs;
&decorate_with_gene_annotations($prev_lend_gene_struct->{gene}, \$contig_seq, \@base_annotations);
}
elsif ($gene_structs[0]->{lend} > $pos) {
# not there yet
last;
}
#print STDERR "Got gene: " . Dumper($gene_structs[0]) . " and position: $pos\n";
}
if (@base_annotations) {
## adjust cursor into base annotations
my $left_index = $base_annotations[0]->{pos};
unless (defined $left_index) {
die "Error, no index defined: " . Dumper(\@base_annotations);
}
while (@base_annotations && ($base_annotations[0]->{pos} < $pos)) {
my $base_annot = shift @base_annotations;
unless (defined $base_annot->{pos}) {
die "Error, no index defined: " . Dumper(\@base_annotations);
}
}
if (@base_annotations && ($base_annotations[0]->{pos} < $pos) ) {
die "Wassup!";
}
}
## define annotations
my $annotation = "";
if (@base_annotations) {
my $left_index = $base_annotations[0]->{pos};
#print Dumper($base_annotations[0]);
if ($left_index == $pos) {
my $annot_string = $base_annotations[0]->{annot};
my @annotations = split(/\t/, $annot_string);
foreach my $indiv_annot (@annotations) {
## if a coding mutation, examine the impact on the codon
if ($indiv_annot =~ /^CDS/) {
## see if indel
if ($snp_info && (length($alt) != length($ref) || $snp_info =~ /INDEL/)) {
my $indel_len = length($alt) - length($ref);
my $indel_type = ($indel_len > 0) ? "INSERTION" : "DELETION";
$indiv_annot .= ",$indel_type\[$indel_len]";
}
elsif ($alt ne '.') {
$indiv_annot = &examine_coding_mutation($indiv_annot, $alt, $cod_mut_exp_fh);
}
}
}
$annotation = join("\t", @annotations); ## build back into the single string.
}
else {
die "Error, should have a base annotation";
}
}
unless ($annotation) {
# must be intergenic
$annotation = "intergenic";
if ($prev_lend_gene_struct) {
my $prev_gene_obj = $prev_lend_gene_struct->{gene};
my $prev_gene_id = $prev_gene_obj->{TU_feat_name};
my $prev_gene_lend = $prev_lend_gene_struct->{lend};
my $prev_gene_rend = $prev_lend_gene_struct->{rend};
my $prev_gene_orient = $prev_gene_obj->get_orientation();
my $delta_pos = $pos - $prev_gene_rend;
my $prev_gene_name = $prev_gene_obj->{com_name};
$annotation .= " -- prev_gene($prev_gene_lend-$prev_gene_rend)\[<-($delta_pos)\]: $prev_gene_id $prev_gene_name ($prev_gene_orient) ";
}
if (my $next_gene_struct = $gene_structs[0]) {
my $next_gene_obj = $next_gene_struct->{gene};
my $next_gene_id = $next_gene_obj->{TU_feat_name};
my $next_gene_lend = $next_gene_struct->{lend};
my $next_gene_rend = $next_gene_struct->{rend};
my $next_gene_orient = $next_gene_obj->get_orientation();
my $delta_pos = $next_gene_lend - $pos;
my $next_gene_name = $next_gene_obj->{com_name};
$annotation .= " -- next_gene($next_gene_lend-$next_gene_rend)\[($delta_pos)-->]: $next_gene_id $next_gene_name ($next_gene_orient) ";
}
}
print join("\t", @x, $annotation) . "\n";
}
print STDERR "\n";
}
####
sub decorate_with_gene_annotations {
my ($gene_obj, $contig_seq_sref, $base_annotations_aref) = @_;
my ($lend, $rend);
{
my @coords;
foreach my $isoform ($gene_obj, $gene_obj->get_additional_isoforms()) {
my ($iso_lend, $iso_rend) = $isoform->get_transcript_span();
push (@coords, $iso_lend, $iso_rend);
}
@coords = sort {$a<=>$b} @coords;
$lend = shift @coords;
$rend = pop @coords;
}
#@$base_annotations_aref = (); # clear it out ## NO!! bad idea... retain it.
my $base_annots_start_add_pos = $lend;
if (@$base_annotations_aref) {
$base_annots_start_add_pos = $base_annotations_aref->[$#$base_annotations_aref]->{pos} + 1;
}
for (my $i = $base_annots_start_add_pos; $i <= $rend; $i++) {
push (@$base_annotations_aref, { pos => $i,
annot => "",
} );
}
my $left_index = $base_annotations_aref->[0]->{pos};
$gene_obj->create_all_sequence_types($contig_seq_sref);
my $gene_id = $gene_obj->{TU_feat_name};
my $orient = $gene_obj->get_orientation();
foreach my $isoform ($gene_obj, $gene_obj->get_additional_isoforms()) {
my $model_id = $isoform->{Model_feat_name};
my $com_name = $isoform->{com_name};
my $protein = $isoform->get_protein_sequence();
my $cds_seq = $isoform->get_CDS_sequence();
$CDS_SEQUENCES{$model_id} = $cds_seq; # for studying coding mutations later, if they exist
my $complete_protein = ($protein && $protein =~ /^M/ && $protein =~ /\*$/) ? 1 : 0;
## process UTR information.
if ($isoform->has_UTRs()) {
my @coordsets5p = $isoform->get_5prime_UTR_coords();
my @coordsets3p = $isoform->get_3prime_UTR_coords();
#print "Got UTRs:" . Dumper(\@coordsets5p) . Dumper(\@coordsets3p) . $isoform->toString();
foreach my $coordset ($isoform->get_5prime_UTR_coords()) {
my ($lend, $rend) = sort {$a<=>$b} @$coordset;
#print STDERR "\tprocessing 5pUTR $lend-$rend.\n";
for (my $i = $lend; $i <= $rend; $i++) {
my $annot_text = join(",", "p5UTR", $gene_id, $model_id, $com_name, $orient);
&add_annotation($base_annotations_aref, $i, $annot_text);
}
}
foreach my $coordset ($isoform->get_3prime_UTR_coords()) {
my ($lend, $rend) = sort {$a<=>$b} @$coordset;
#print STDERR "\tprocessing 3pUTR $lend-$rend.\n";
for (my $i = $lend; $i <= $rend; $i++) {
my $annot_text = join(",", "p3UTR", $gene_id, $model_id, $com_name, $orient);
&add_annotation($base_annotations_aref, $i, $annot_text);
}
}
}
## annotate introns:
if (my @introns = $isoform->get_intron_coordinates()) {
foreach my $intron (@introns) {
my ($lend, $rend) = sort {$a<=>$b} @$intron;
# print STDERR "\tprocessing intron $lend-$rend\n";
for (my $i = $lend; $i <= $rend; $i++) {
my $annot_text = join(",", "intron", $gene_id, $model_id, $com_name, $orient);
&add_annotation($base_annotations_aref, $i, $annot_text);
}
}
}
## Process coding regions of exons.
my @exons = sort {$a->{end5}<=>$b->{end5}} $isoform->get_exons();
if ($orient eq '-') {
@exons = reverse @exons;
}
my $sum_cds_length = 0;
foreach my $exon (@exons) {
if (my $cds_obj = $exon->get_CDS_obj()) {
my $end5 = $cds_obj->{end5};
my $end3 = $cds_obj->{end3};
# print STDERR "\tprocessing exon $end5-$end3\n";
if ($orient eq '+') {
for (my $i = $end5; $i <= $end3; $i++) {
$sum_cds_length++;
my $frame = (($sum_cds_length -1) % 3 ) + 1;
my $codon = &get_codon(\$cds_seq, $sum_cds_length);
my $annot = join(",", "CDS", $gene_id, $model_id, $com_name, "trans_orient:$orient", "loc_in_cds:$sum_cds_length", "codon_pos:$frame", "codon:$codon");
# print "$annot\n";
&add_annotation($base_annotations_aref, $i, $annot);
}
}
else {
# minus orientation
for (my $i = $end5; $i >= $end3; $i--) {
$sum_cds_length++;
my $frame = (($sum_cds_length -1) % 3 ) + 1;
my $codon = &get_codon(\$cds_seq, $sum_cds_length);
my $annot = join(",", "CDS", $gene_id, $model_id, $com_name, "trans_orient:$orient", "loc_in_cds:$sum_cds_length", "codon_pos:$frame", "codon:$codon");
&add_annotation($base_annotations_aref, $i, $annot);
}
}
}
}
}
return;
}
####
sub add_annotation {
my ($base_annotations_aref, $position, $annotation) = @_;
my $left_pos = $base_annotations_aref->[0]->{pos};
my $right_pos = $base_annotations_aref->[$#$base_annotations_aref]->{pos};
if (! defined ($left_pos) || ! defined ($right_pos)) {
croak "Error, existing base_annotations_aref lacks boundary positions defined: " . Dumper(\$base_annotations_aref);
}
if ($position > $right_pos) {
croak "Error, position: $position is out of range: $left_pos-$right_pos" . Dumper($base_annotations_aref);
}
if ($position < $left_pos) {
croak "Error, position: $position is out of range: $left_pos-$right_pos" . Dumper($base_annotations_aref);
}
my $delta = $position - $left_pos;
if ($base_annotations_aref->[$delta]->{pos} != $position) {
croak "Error, position: $position is not at $delta in the existing list: " . Dumper($base_annotations_aref->[$delta]);
}
## if got this far, all should be OK AFAICT
if ($base_annotations_aref->[$delta]->{annot}) {
$base_annotations_aref->[$delta]->{annot} .= "\t";
}
$base_annotations_aref->[$delta]->{annot} .= $annotation;
return;
}
####
sub get_codon {
my ($cds_seq_sref, $pos) = @_;
my $last_pos = length($$cds_seq_sref) - 1;
my $codon_pos = int(($pos-1)/3) * 3;
my $codon_string = "";
for (my $i = $codon_pos; $i <= $codon_pos + 2; $i++) {
if ($i > $last_pos) {
last;
}
$codon_string .= substr($$cds_seq_sref, $i, 1);
}
return($codon_string);
}
####
sub examine_coding_mutation {
my ($annotation, $alt_base, $cod_mut_exp_fh) = @_;
my @x = split(/,/, $annotation);
my $model_id = $x[2];
my $cds_seq = $CDS_SEQUENCES{$model_id} or die "Error, no CDS sequence for $model_id, [annot: $annotation]";
my $codon = pop @x;
$codon =~ s/codon://;
if (length($codon) != 3) {
return($annotation); # unchanged
}
$annotation = join(",", @x); #stripped of codon. We'll add it back shortly, and decorate it.
my $codon_pos = pop @x;
$codon_pos =~ s/^\S+://;
my $cds_base_pos = pop @x;
$cds_base_pos =~ s/^\S+://;
my $aa_pos = int(($cds_base_pos-1)/3)+1;
my $transcript_orientation = pop @x;
$transcript_orientation =~ s/^\S+://;
if ($transcript_orientation eq '-') {
$alt_base = &reverse_complement($alt_base);
}
my $original_aa = &translate_sequence($codon, 1);
## mutate the codon
my @codon_chars = split(//, $codon);
$codon_chars[$codon_pos-1] = $alt_base;
my $mutated_codon = join("", @codon_chars);
my $mutated_aa = &translate_sequence($mutated_codon, 1);
{
## make codons pretty and case-informative
my @codon_chars = split(//, lc $codon);
$codon_chars[$codon_pos-1] = uc $codon_chars[$codon_pos-1];
$codon = join("", @codon_chars);
my @mutated_codon_chars = split(//, lc $mutated_codon);
$mutated_codon_chars[$codon_pos-1] = uc $mutated_codon_chars[$codon_pos-1];
$mutated_codon = join("", @mutated_codon_chars);
}
my $mutation_type = "";
if ($mutated_aa eq $original_aa) {
$mutation_type = "SYN";
}
elsif ($mutated_aa eq '*' && $original_aa ne '*') {
$mutation_type = "NON";
}
elsif ($original_aa eq '*' && $mutated_aa ne '*') {
$mutation_type = "RTH";
}
else {
$mutation_type = "NSY";
}
$annotation .= ",codon:$codon-$mutated_codon,pep:$original_aa\->$mutated_aa,"
. &get_amino_acid_triplet_label($original_aa) . "-" . $aa_pos . "-"
. &get_amino_acid_triplet_label($mutated_aa) . ",($mutation_type)";
if ($cod_mut_exp_fh) {
my $coding_mutation_explained_text = &explain_coding_mutation_pedantically($cds_seq, $cds_base_pos, $codon, $mutated_codon, $annotation);
print $cod_mut_exp_fh ">$model_id $annotation\n$coding_mutation_explained_text\n";
}
return($annotation);
}
####
sub explain_coding_mutation_pedantically {
my ($cds_seq, $cds_base_pos, $codon, $mutated_codon, $annotation) = @_;
my $ret_text = "";
my @codons;
while ($cds_seq =~ /(\S{3})/g) {
push (@codons, $1);
}
my $mutated_codon_index = int(($cds_base_pos-1)/3);
my $codon_counter = 0;
my $number_line = "";
my $codon_line = "";
my $translation_line = "";
my $mutation_codon_line = "";
my $mutation_translation_line = "";
my $codons_per_line = 15;
for (my $i = 0; $i < $#codons; $i++) {
if ($i > 0 && $i % $codons_per_line == 0) {
foreach my $line ($number_line, $codon_line, $translation_line, $mutation_codon_line, $mutation_translation_line) {
$ret_text .= "$line\n" if ($line =~ /\w/);
}
$ret_text .= "\n";
## reinit
$number_line = "";
$codon_line = "";
$translation_line = "";
$mutation_codon_line = "";
$mutation_translation_line = "";
}
$number_line .= sprintf("%5d", $i+1); # use codon numbering starting from 1 rather than zero
$codon_line .= sprintf("%5s", $codons[$i]);
$translation_line .= sprintf("%4s ", &translate_sequence($codons[$i], 1));
if ($i == $mutated_codon_index) {
$mutation_codon_line .= sprintf("%5s", $mutated_codon);
$mutation_translation_line .= sprintf("%4s ", &translate_sequence($mutated_codon, 1)) . "\t$annotation";
}
else {
$mutation_codon_line .= " " x 5;
$mutation_translation_line .= " " x 5;
}
}
if ($number_line) {
foreach my $line ($number_line, $codon_line, $translation_line, $mutation_codon_line, $mutation_translation_line) {
$ret_text .= "$line\n" if ($line =~ /\w/);
}
$ret_text .= "\n";
}
return($ret_text);
}
####
sub get_amino_acid_triplet_label {
my ($aa_letter) = @_;
my %triplets = ( 'A' => "Ala",
'C' => "Cys",
'D' => 'Asp',
'E' => 'Glu',
'F' => 'Phe',
'G' => 'Gly',
'H' => 'His',
'I' => 'Ile',
'K' => 'Lys',
'L' => 'Leu',
'M' => 'Met',
'N' => 'Asn',
'P' => 'Pro',
'Q' => 'Gln',
'R' => 'Arg',
'S' => 'Ser',
'T' => 'Thr',
'V' => 'Val',
'W' => 'Trp',
'Y' => 'Tyr',
'X' => 'XXX',
'*' => 'STP',
);
my $triplet = $triplets{ uc $aa_letter };
unless ($triplet) {
die "Error, residue($aa_letter) is unknown";
}
return($triplet);
}
|