| 1 | package Bio::Graphics::Glyph::gene; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'Bio::Graphics::Glyph::processed_transcript'; | ||
| 5 | |||
| 6 | sub my_descripton { | ||
| 7 | return <<END; | ||
| 8 | This glyph is used for drawing genes that may have | ||
| 9 | alternatively-spliced transcripts. The various isoforms are stacked on | ||
| 10 | top of each other and given a single label and description that apply | ||
| 11 | to the entire stack. Each individual transcript\'s name is optionally | ||
| 12 | printed to the left of the transcript glyph. | ||
| 13 | |||
| 14 | Transcripts (splice isoforms) are drawn using the processed_transcript | ||
| 15 | glyph. CDS features are drawn in the background color, and the UTRs | ||
| 16 | are drawn in an alternate color selected by the utr_color option. In | ||
| 17 | addition, you can make the UTRs thinner than the CDS by setting the | ||
| 18 | "thin_utr" option. | ||
| 19 | |||
| 20 | This glyph is designed to work properly with GFF3-style three-tier | ||
| 21 | genes, in which the top level feature has the Sequence Ontology type | ||
| 22 | of "gene", the second level feature(s) have the SO type "mRNA", and | ||
| 23 | the third level feature(s) have the SO type "CDS", "five_prime_utr" | ||
| 24 | and "three_prime_utr." Subparts named "UTR" are also honored. The | ||
| 25 | feature can contain other subparts as well (e.g. exon, intron, | ||
| 26 | translation), but they are currently ignored unless the option | ||
| 27 | sub_part is supplied. If the sub_part option is used that feature | ||
| 28 | type will be used and CDS and UTR features will be excluded. | ||
| 29 | This could be used for specifying that exons be used instead, | ||
| 30 | for example. | ||
| 31 | |||
| 32 | This glyph is a subclass of processed_transcript, and recognizes the | ||
| 33 | same options. | ||
| 34 | END | ||
| 35 | } | ||
| 36 | |||
| 37 | sub my_options { | ||
| 38 | { | ||
| 39 | label_transcripts => [ | ||
| 40 | 'boolean', | ||
| 41 | undef, | ||
| 42 | 'If true, then the display_name of each transcript', | ||
| 43 | 'will be drawn to the left of the transcript glyph.'], | ||
| 44 | thin_utr => [ | ||
| 45 | 'boolean', | ||
| 46 | undef, | ||
| 47 | 'If true, UTRs will be drawn at 2/3 of the height of CDS segments.'], | ||
| 48 | utr_color => [ | ||
| 49 | 'color', | ||
| 50 | 'grey', | ||
| 51 | 'Color of UTR segments.'], | ||
| 52 | decorate_introns => [ | ||
| 53 | 'boolean', | ||
| 54 | undef, | ||
| 55 | 'Draw chevrons on the introns to indicate direction of transcription.' | ||
| 56 | ], | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub extra_arrow_length { | ||
| 61 | my $self = shift; | ||
| 62 | return 0 if $self->feature->primary_tag =~ /exon|utr/i; | ||
| 63 | return $self->SUPER::extra_arrow_length | ||
| 64 | unless $self->feature->primary_tag =~ /gene/; | ||
| 65 | return 0 unless $self->{level} == 1; | ||
| 66 | local $self->{level} = 0; # fake out superclass | ||
| 67 | return $self->SUPER::extra_arrow_length; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub pad_left { | ||
| 71 | my $self = shift; | ||
| 72 | my $type = $self->feature->primary_tag; | ||
| 73 | return 0 unless $type =~ /gene|rna|transcript/i; | ||
| 74 | $self->SUPER::pad_left; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub pad_right { | ||
| 78 | my $self = shift; | ||
| 79 | return 0 unless $self->{level} < 2; # don't invoke this expensive call on exons | ||
| 80 | return $self->SUPER::pad_right; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub pad_bottom { | ||
| 84 | my $self = shift; | ||
| 85 | return 0 unless $self->{level} < 2 || $self->is_utr; # don't invoke this expensive call on exons | ||
| 86 | return $self->SUPER::pad_bottom; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub pad_top { | ||
| 90 | my $self = shift; | ||
| 91 | return 0 unless $self->{level} < 2 || $self->is_utr; # don't invoke this expensive call on exons | ||
| 92 | return $self->SUPER::pad_top; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub bump { | ||
| 96 | my $self = shift; | ||
| 97 | my $bump; | ||
| 98 | if ($self->{level} == 0 | ||
| 99 | && lc $self->feature->primary_tag eq 'gene' | ||
| 100 | && eval {($self->subfeat($self->feature))[0]->type =~ /RNA|pseudogene/i}) { | ||
| 101 | $bump = $self->option('bump'); | ||
| 102 | } else { | ||
| 103 | $bump = $self->SUPER::bump; | ||
| 104 | } | ||
| 105 | return $bump; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub label { | ||
| 109 | my $self = shift; | ||
| 110 | return unless $self->{level} < 2; | ||
| 111 | if ($self->{feature}->primary_tag =~ /rna|transcript|pseudogene/i && $self->label_transcripts) { | ||
| 112 | return $self->_label; | ||
| 113 | } else { | ||
| 114 | return $self->SUPER::label; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub label_position { | ||
| 119 | my $self = shift; | ||
| 120 | return 'top' if $self->{level} == 0; | ||
| 121 | return 'left'; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub label_transcripts { | ||
| 125 | my $self = shift; | ||
| 126 | return $self->{label_transcripts} if exists $self->{label_transcripts}; | ||
| 127 | return $self->{label_transcripts} = $self->_label_transcripts; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _label_transcripts { | ||
| 131 | my $self = shift; | ||
| 132 | return $self->option('label_transcripts'); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub draw_connectors { | ||
| 136 | my $self = shift; | ||
| 137 | if ($self->feature->primary_tag eq 'gene') { | ||
| 138 | my @parts = $self->parts; | ||
| 139 | return if @parts && $parts[0]->feature->primary_tag =~ /rna|transcript|pseudogene/i; | ||
| 140 | } | ||
| 141 | $self->SUPER::draw_connectors(@_); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub maxdepth { | ||
| 145 | my $self = shift; | ||
| 146 | my $md = $self->Bio::Graphics::Glyph::maxdepth; | ||
| 147 | return $md if defined $md; | ||
| 148 | return 2; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub fixup_glyph { | ||
| 152 | my $self = shift; | ||
| 153 | return unless $self->level == 1; | ||
| 154 | $self->create_implied_utrs if $self->option('implied_utrs'); | ||
| 155 | $self->adjust_exons if $self->option('implied_utrs') || $self->option('adjust_exons'); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _subfeat { | ||
| 159 | my $class = shift; | ||
| 160 | my $feature = shift; | ||
| 161 | |||
| 162 | if ($feature->primary_tag =~ /^gene/i) { | ||
| 163 | my @transcripts; | ||
| 164 | for my $t (qw/mRNA tRNA snRNA snoRNA miRNA ncRNA pseudogene transcript/) { | ||
| 165 | push @transcripts, $feature->get_SeqFeatures($t); | ||
| 166 | } | ||
| 167 | return @transcripts if @transcripts; | ||
| 168 | my @features = $feature->get_SeqFeatures; # no transcripts?! return whatever's there | ||
| 169 | return @features if @features; | ||
| 170 | |||
| 171 | # fall back to drawing a solid box if no subparts and level 0 | ||
| 172 | return ($feature) if $class->{level} == 0; | ||
| 173 | } | ||
| 174 | elsif ($feature->primary_tag =~ /^CDS/i) { | ||
| 175 | my @parts = $feature->get_SeqFeatures(); | ||
| 176 | return ($feature) if $class->{level} == 0 and !@parts; | ||
| 177 | return @parts; | ||
| 178 | } | ||
| 179 | |||
| 180 | my @subparts; | ||
| 181 | if ($class->option('sub_part')) { | ||
| 182 | @subparts = $feature->get_SeqFeatures($class->option('sub_part')); | ||
| 183 | } | ||
| 184 | elsif ($feature->primary_tag =~ /^mRNA/i) { | ||
| 185 | if ($class->option('implied_utrs') || $class->option('adjust_exons')) { | ||
| 186 | @subparts = $feature->get_SeqFeatures(qw(CDS exon five_prime_UTR three_prime_UTR UTR)); | ||
| 187 | } else { | ||
| 188 | @subparts = $feature->get_SeqFeatures(qw(CDS five_prime_UTR three_prime_UTR UTR)); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | @subparts = $feature->get_SeqFeatures('exon'); | ||
| 193 | } | ||
| 194 | |||
| 195 | # The CDS and UTRs may be represented as a single feature with subparts or as several features | ||
| 196 | # that have different IDs. We handle both cases transparently. | ||
| 197 | my @result; | ||
| 198 | foreach (@subparts) { | ||
| 199 | if ($_->primary_tag =~ /CDS|UTR/i) { | ||
| 200 | my @cds_seg = $_->get_SeqFeatures; | ||
| 201 | if (@cds_seg > 0) { push @result,@cds_seg } else { push @result,$_ } | ||
| 202 | } else { | ||
| 203 | push @result,$_; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | # fall back to drawing a solid box if no subparts and level 0 | ||
| 207 | return ($feature) if $class->{level} == 0 && !@result; | ||
| 208 | |||
| 209 | return @result; | ||
| 210 | } | ||
| 211 | |||
| 212 | 1; | ||
| 213 | |||
| 214 | __END__ |