| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use lib './blib/lib','../blib/lib'; | ||
| 5 | use Bio::Graphics::Panel; | ||
| 6 | use Bio::Graphics::Feature; | ||
| 7 | use Bio::Graphics::FeatureFile; | ||
| 8 | |||
| 9 | use Getopt::Long; | ||
| 10 | use List::Util qw/first/; | ||
| 11 | |||
| 12 | use constant WIDTH => 600; | ||
| 13 | my ($WIDTH,$RANGE,$BOXES); | ||
| 14 | |||
| 15 | GetOptions ('width:i' => \$WIDTH, | ||
| 16 | 'range:s' => \$RANGE, | ||
| 17 | 'boxes' => \$BOXES, | ||
| 18 | ) || die <<USAGE; | ||
| 19 | Usage: $0 [feature file 1] [feature file 2] ... | ||
| 20 | |||
| 21 | Options: | ||
| 22 | --width <pixels> Set width of image (${\WIDTH} pixels default) | ||
| 23 | --range <start-stop> Set range of region (base pairs, start-stop) | ||
| 24 | --boxes Draw grey boxes around the features (for debugging) | ||
| 25 | |||
| 26 | Render a Bio::Graphics feature file and produce a PNG image. | ||
| 27 | See the manual page for Bio::Graphics::FeatureFile for a | ||
| 28 | description of the file format. | ||
| 29 | USAGE | ||
| 30 | |||
| 31 | my @COLORS = qw(cyan blue red yellow green wheat turquoise orange); # default colors | ||
| 32 | my $color = 0; # position in color cycle | ||
| 33 | |||
| 34 | my $data = Bio::Graphics::FeatureFile->new(-file => '-'); | ||
| 35 | |||
| 36 | # general configuration of the image here | ||
| 37 | my $width = $WIDTH || $data->setting(general => 'pixels') | ||
| 38 | || $data->setting(general => 'width') | ||
| 39 | || WIDTH; | ||
| 40 | |||
| 41 | my ($start,$stop); | ||
| 42 | my $range_expr = '(-?\d+)(?:-|\.\.)(-?\d+)'; | ||
| 43 | |||
| 44 | if (defined $RANGE) { | ||
| 45 | ($start,$stop) = $RANGE =~ /$range_expr/o or die "$RANGE: invalid range specification"; | ||
| 46 | } elsif (my $bases = $data->setting(general => 'bases')) { | ||
| 47 | ($start,$stop) = $bases =~ /([\d-]+)(?:-|\.\.)([\d-]+)/; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (!defined $start || !defined $stop) { | ||
| 51 | $start = $data->min unless defined $start; | ||
| 52 | $stop = $data->max unless defined $stop; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Use the order of the stylesheet to determine features. Whatever is left | ||
| 56 | # over is presented in alphabetic order | ||
| 57 | my %types = map {$_=>1} $data->configured_types; | ||
| 58 | |||
| 59 | my @configured_types = grep {exists $data->features->{$_}} $data->configured_types; | ||
| 60 | my @unconfigured_types = sort grep {!exists $types{$_}} $data->types; | ||
| 61 | |||
| 62 | # create the segment,the panel and the arrow with tickmarks | ||
| 63 | my $segment = Bio::Graphics::Feature->new(-start=>$start,-stop=>$stop); | ||
| 64 | my $panel = Bio::Graphics::Panel->new(-segment => $segment, | ||
| 65 | -width => $width, | ||
| 66 | -key_style => 'between'); | ||
| 67 | $panel->add_track($segment,-glyph=>'arrow',-tick=>2); | ||
| 68 | |||
| 69 | my @base_config = $data->style('general'); | ||
| 70 | |||
| 71 | for my $type (@configured_types,@unconfigured_types) { | ||
| 72 | my @config = ( -glyph => 'segments', # really generic | ||
| 73 | -bgcolor => $COLORS[$color++ % @COLORS], | ||
| 74 | -label => 1, | ||
| 75 | -key => $type, | ||
| 76 | @base_config, # global | ||
| 77 | $data->style($type), # feature-specificp | ||
| 78 | ); | ||
| 79 | my $features = $data->features($type); | ||
| 80 | $panel->add_track($features,@config); | ||
| 81 | } | ||
| 82 | |||
| 83 | my $gd = $panel->gd; | ||
| 84 | |||
| 85 | if ($BOXES) { # debugging code | ||
| 86 | my $boxes = $panel->boxes; | ||
| 87 | debugging_rectangles($gd,$boxes); | ||
| 88 | } | ||
| 89 | |||
| 90 | if($gd->can('png')) { | ||
| 91 | print STDERR "Printing png\n"; | ||
| 92 | print $gd->png; | ||
| 93 | } elsif($gd->can('gif')) { | ||
| 94 | print STDERR "Printing gif\n"; | ||
| 95 | print $gd->gif; | ||
| 96 | }; | ||
| 97 | |||
| 98 | sub debugging_rectangles { | ||
| 99 | my ($image,$boxes) = @_; | ||
| 100 | my $grey = $image->colorClosest(100,100,100); | ||
| 101 | foreach (@$boxes) { | ||
| 102 | my @rect = @{$_}[1,2,3,4]; | ||
| 103 | $image->rectangle(@{$_}[1,2,3,4],$grey); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | =head1 NAME | ||
| 108 | |||
| 109 | feature_draw.pl -- Render a Bio::Graphics Feature File | ||
| 110 | |||
| 111 | =head1 SYNOPSIS | ||
| 112 | |||
| 113 | feature_draw.pl [options] file.txt [file2.txt...] > rendering.png | ||
| 114 | feature_draw.pl [options] file.txt [file2.txt...] | display - | ||
| 115 | |||
| 116 | =head1 DESCRIPTION | ||
| 117 | |||
| 118 | The feature_draw.pl script is a thin front end around the | ||
| 119 | Bio::Graphics module. It accepts a list of files containing sequence | ||
| 120 | (protein, nucleotide) feature coordinates from the file(s) listed on | ||
| 121 | the command line or on standard input, renders them, and produces a | ||
| 122 | PNG file on standard output. | ||
| 123 | |||
| 124 | =head2 Options | ||
| 125 | |||
| 126 | This script uses GNU-style long options. This allows you to specify | ||
| 127 | the image width option, for example, with any of the following alternative | ||
| 128 | forms: | ||
| 129 | |||
| 130 | --width=800 | ||
| 131 | --width 800 | ||
| 132 | -width 800 | ||
| 133 | -w 800 | ||
| 134 | |||
| 135 | =over 4 | ||
| 136 | |||
| 137 | =item --width | ||
| 138 | |||
| 139 | This sets the width of the image, in pixels. The default is 800 | ||
| 140 | pixels. | ||
| 141 | |||
| 142 | =item --range | ||
| 143 | |||
| 144 | This sets the range of the region displayed, in base pairs from start | ||
| 145 | to stop. Any of the following formats are accepted: | ||
| 146 | |||
| 147 | --range 1..1000 | ||
| 148 | --range 1,1000 | ||
| 149 | --range 1-1000 | ||
| 150 | |||
| 151 | Negative ranges are allowed. | ||
| 152 | |||
| 153 | =back | ||
| 154 | |||
| 155 | =head1 Feature Files Format | ||
| 156 | |||
| 157 | This script accepts and processes sequence annotations in a simple | ||
| 158 | tab-delimited format or in GFF format. | ||
| 159 | |||
| 160 | The feature file format has a configuration section and a data | ||
| 161 | section. The configuration section sets up the size and overall | ||
| 162 | properties of the image, and the data section gives the feature | ||
| 163 | data itself. | ||
| 164 | |||
| 165 | =head2 Configuration Section | ||
| 166 | |||
| 167 | If not provided, this scripts generates a reasonable default | ||
| 168 | configuration section for you, so you do not need to provide a | ||
| 169 | configuration section to get a reasonable image. However, to tune the | ||
| 170 | appearance of the image, you will probably want to tweak the | ||
| 171 | configuration. Here is an excerpt from the configuration section: | ||
| 172 | |||
| 173 | |||
| 174 | # example file | ||
| 175 | [general] | ||
| 176 | bases = -1000..21000 | ||
| 177 | height = 12 | ||
| 178 | |||
| 179 | [EST] | ||
| 180 | glyph = segments | ||
| 181 | bgcolor= yellow | ||
| 182 | connector = dashed | ||
| 183 | height = 5 | ||
| 184 | |||
| 185 | [FGENES] | ||
| 186 | glyph = transcript2 | ||
| 187 | bgcolor = green | ||
| 188 | description = 1 | ||
| 189 | |||
| 190 | |||
| 191 | The configuration section is divided into a set of sections, each one | ||
| 192 | labeled with a [section title]. The [general] section specifies global | ||
| 193 | options for the entire image. Other sections apply to particular | ||
| 194 | feature types. In the example above, the configuration in the [EST] | ||
| 195 | section applies to features labeled as ESTs, while the configuration | ||
| 196 | in the [FGENES] section applies to features labeled as predictions | ||
| 197 | from the FGENES gene prediction program. | ||
| 198 | |||
| 199 | Inside each section is a series of name=value pairs, where the name is | ||
| 200 | the name of an option to set. You can put whitespace around the = sign | ||
| 201 | to make it more readable, or even use a colon (:) if you prefer. The | ||
| 202 | following option names are recognized: | ||
| 203 | |||
| 204 | Option Value Example | ||
| 205 | ------ ----- ------- | ||
| 206 | |||
| 207 | bases Min & max of the sequence range (bp) 1200..60000 | ||
| 208 | width width of the image (pixels) 600 | ||
| 209 | height Height of each graphical element (pixels) 10 | ||
| 210 | glyph Style of each graphical element (see below) transcript | ||
| 211 | fgcolor Foreground color of each element yellow | ||
| 212 | bgcolor Background color of each element blue | ||
| 213 | linewidth Width of lines 3 | ||
| 214 | label Print the feature's name 1 | ||
| 215 | description Whether to print the feature's description 0 | ||
| 216 | bump Elements are not allowed to collide 1 | ||
| 217 | ticks Print tick marks on arrows 1 | ||
| 218 | connector Type of group connector (dashed, hat or solid) dashed | ||
| 219 | |||
| 220 | The "bases" and "width" options are only relevant in the [general] | ||
| 221 | section. They are overridden by the like-named command-line options. | ||
| 222 | |||
| 223 | The remainder of the options can be located in any section, but if | ||
| 224 | present in the [general] section will set defaults for the others. | ||
| 225 | |||
| 226 | Colors are English-language color names or Web-style #RRGGBB colors | ||
| 227 | (see a book on HTML for an explanation). True/false values are 1 for | ||
| 228 | true, and 0 for false. Numeric ranges can be expressed in start..end | ||
| 229 | fashion with two dots, or as start-end with a hyphen. | ||
| 230 | |||
| 231 | The "glyph" option controls how the features are rendered. The | ||
| 232 | following glyphs are implemented: | ||
| 233 | |||
| 234 | Name Description | ||
| 235 | ---- ----------- | ||
| 236 | |||
| 237 | box A filled rectangle, nondirectional. | ||
| 238 | ellipse An oval. | ||
| 239 | arrow An arrow; can be unidirectional or | ||
| 240 | bidirectional. It is also capable of displaying | ||
| 241 | a scale with major and minor tickmarks, and can | ||
| 242 | be oriented horizontally or vertically. | ||
| 243 | segments A set of filled rectangles connected by solid | ||
| 244 | lines. Used for interrupted features, such as | ||
| 245 | gapped alignments and exon groups. | ||
| 246 | transcript Similar to segments, but the connecting line is | ||
| 247 | a "hat" shape, and the direction of | ||
| 248 | transcription is indicated by a small arrow. | ||
| 249 | transcript2 Similar to transcript, but the direction of | ||
| 250 | transcription is indicated by a terminal segment | ||
| 251 | in the shape of an arrow. | ||
| 252 | primers Two inward pointing arrows connected by a line. Used for STSs. | ||
| 253 | |||
| 254 | The bump option is the most important option for controlling the look | ||
| 255 | of the image. If set to false (the number 0), then the features are | ||
| 256 | allowed to overlap. If set to true (the number 1), then the features | ||
| 257 | will move vertically to avoid colliding. If not specified, bump is | ||
| 258 | turned on if the number of any given type of sequence feature is | ||
| 259 | greater than 50. | ||
| 260 | |||
| 261 | =head2 Data Section | ||
| 262 | |||
| 263 | The data section can follow or proceed the configuration section. The | ||
| 264 | two sections can also be intermixed. The data section is a tab or | ||
| 265 | whitespace-delimited file which you can export from a spreadsheet | ||
| 266 | application or word processor file (be sure to save as text only!) | ||
| 267 | |||
| 268 | Here is an example data section: | ||
| 269 | |||
| 270 | |||
| 271 | Cosmid B0511 . 516-619 | ||
| 272 | Cosmid B0511 . 3185-3294 | ||
| 273 | Cosmid B0511 . 10946-11208 | ||
| 274 | Cosmid B0511 . 13126-13511 | ||
| 275 | Cosmid B0511 . 66-208 | ||
| 276 | Cosmid B0511 . 6354-6499 | ||
| 277 | Cosmid B0511 . 13955-14115 | ||
| 278 | EST yk595e6.5 + 3187-3294 | ||
| 279 | EST yk846e07.3 - 11015-11208 | ||
| 280 | EST yk53c10 | ||
| 281 | yk53c10.5 + 18892-19154 | ||
| 282 | yk53c10.3 - 15000-15500,15700-15800 | ||
| 283 | EST yk53c10.5 + 16032-16105 | ||
| 284 | SwissProt PECANEX + 13153-13656 Swedish fish | ||
| 285 | FGENESH "Gene 1" - 1-205,518-616,661-735,3187-3365,3436-3846 Transmembrane domain | ||
| 286 | FGENESH "Gene 2" - 16626-17396,17451-17597 Kinase and sushi domains | ||
| 287 | |||
| 288 | |||
| 289 | Each line of the file contains five columns. The columns are: | ||
| 290 | |||
| 291 | Column # Description | ||
| 292 | -------- ----------- | ||
| 293 | |||
| 294 | 1 feature type | ||
| 295 | 2 feature name | ||
| 296 | 3 strand | ||
| 297 | 4 coordinates | ||
| 298 | 5 description | ||
| 299 | |||
| 300 | =over 4 | ||
| 301 | |||
| 302 | =item Feature type | ||
| 303 | |||
| 304 | The feature type should correspond to one of the [feature type] | ||
| 305 | headings in the configuration section. If it doesn't, the [general] | ||
| 306 | options will be applied to the feature when rendering it. The feature | ||
| 307 | name is a name for the feature. Use a "." or "-" if this is not | ||
| 308 | relevant. If the name contains whitespace, put single or double quotes | ||
| 309 | ("") around the name. | ||
| 310 | |||
| 311 | =item Strand | ||
| 312 | |||
| 313 | The strand indicates which strand the feature is on. It is one of "+" | ||
| 314 | for the forward strand, "-" for the reverse strand, or "." for | ||
| 315 | features that are not stranded. | ||
| 316 | |||
| 317 | =item Coordinates | ||
| 318 | |||
| 319 | The coordinates column is a set of one or more ranges that the feature | ||
| 320 | occupies. Ranges are written using ".." as in start..stop, or with | ||
| 321 | hyphens, as in start-stop. For features that are composed of multiple | ||
| 322 | ranges &em; for example transcripts that have multiple exons &em; you | ||
| 323 | can either put the ranges on the same line separated by commas or | ||
| 324 | spaces, or put the ranges on individual lines and just use the same | ||
| 325 | feature name and type to group them. In the example above, the Cosmid | ||
| 326 | B0511 features use the individual line style, while the FGENESH | ||
| 327 | features use the all-ranges-on-one-line style. | ||
| 328 | |||
| 329 | =item Description | ||
| 330 | |||
| 331 | The last column contains some descriptive text. If the description | ||
| 332 | option is set to true, this text will be printed underneath the | ||
| 333 | feature in the rendering. | ||
| 334 | |||
| 335 | =back | ||
| 336 | |||
| 337 | Finally, it is possible to group related features together. An example | ||
| 338 | is the ESTs yk53c10.5 and yk53c10.3, which are related by being reads | ||
| 339 | from the two ends of the clone yk53c10. To indicate this relationship, | ||
| 340 | generate a section that looks like this: | ||
| 341 | |||
| 342 | EST yk53c10 | ||
| 343 | yk53c10.5 + 18892-19154 | ||
| 344 | yk53c10.3 - 15000-15500,15700-15800 | ||
| 345 | |||
| 346 | |||
| 347 | The group is indicated by a line that contains just two columns | ||
| 348 | containing the feature type and a unique name for the group. Follow | ||
| 349 | this line with all the features that form the group, but leave the | ||
| 350 | first column (the feature type) blank. The group will be rendered by | ||
| 351 | drawing a dashed line between all the members of the group. You can | ||
| 352 | change this by specifying a different connector option in the | ||
| 353 | configuration section for this feature type. | ||
| 354 | |||
| 355 | =head1 BUGS | ||
| 356 | |||
| 357 | Please report them to the author. | ||
| 358 | |||
| 359 | =head1 SEE ALSO | ||
| 360 | |||
| 361 | L<Bio::Graphics> | ||
| 362 | |||
| 363 | =head1 AUTHOR | ||
| 364 | |||
| 365 | Lincoln Stein, lstein@cshl.org | ||
| 366 | |||
| 367 | =cut | ||
| 368 | |||
| 369 |