| 1 | package Bio::Graphics::Browser2::PadAlignment; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Bio::Graphics::Browser2::Markup; | ||
| 5 | use constant DEBUG=>0; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Bio::Graphics::Browser2::PadAlignment - Insert pads into a multiple alignment | ||
| 11 | |||
| 12 | =head1 VERSION (CVS-info) | ||
| 13 | |||
| 14 | $RCSfile: PadAlignment.pm,v $ | ||
| 15 | $Revision$ | ||
| 16 | $Author: lstein $ | ||
| 17 | $Date: 2008-09-09 20:53:36 $ | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Bio::Graphics::Browser2::PadAlignment; | ||
| 22 | |||
| 23 | my @dnas = ( | ||
| 24 | dna1 =>'FFFFgatcGATCgatcGATCgatcGATCgatcGBATCgatcGATCatcGATCgatcGATCgatcGATCgatcGATgatcGATCgatcNNNNGATC', | ||
| 25 | dna2 =>'FFgatcGATCGATCgatcNNGATCgatcGATCgatcGATCgatcGATCgatcGATCtcGATBCgatcGATCatcGATCgatcNNNNGATCFFFF', | ||
| 26 | dna3 =>'FFFFgatcGATCgatcGATCgatcGATCgatcBBBGATCgatcGATCatcGATCBgatcGATCgatcGATCgatcGATgatcGATCgatcNNNNGATCFF', | ||
| 27 | dna4 =>'ZZFFFFgatcGATCgatcGATCgatcGATCgatc', | ||
| 28 | dna5 =>'ATBGGATtcttttttt', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # target st en tst ten | ||
| 32 | my @alignments = ([ 'dna2', 4, 11, 2, 9 ], | ||
| 33 | [ 'dna2', 16, 23, 10, 17 ], | ||
| 34 | [ 'dna2', 24, 32, 20, 28 ], | ||
| 35 | [ 'dna2', 34, 44, 29, 39 ], | ||
| 36 | [ 'dna2', 45, 59, 41, 55 ], | ||
| 37 | [ 'dna2', 62, 66, 56, 60 ], | ||
| 38 | [ 'dna2', 67, 74, 62, 69 ], | ||
| 39 | [ 'dna2', 76, 86, 71, 81 ], | ||
| 40 | [ 'dna2', 91, 94, 86, 89 ], | ||
| 41 | |||
| 42 | [ 'dna3', 4, 31, 4, 31 ], | ||
| 43 | [ 'dna3', 33, 33, 34, 34 ], | ||
| 44 | [ 'dna3', 34, 51, 36, 53 ], | ||
| 45 | [ 'dna3', 52, 86, 55, 89 ], | ||
| 46 | [ 'dna3', 91, 94, 94, 97 ], | ||
| 47 | |||
| 48 | [ 'dna4', 0, 31, 2, 33 ], | ||
| 49 | |||
| 50 | [ 'dna5', 17, 18, 0, 1 ], | ||
| 51 | [ 'dna5', 41, 43, 4, 6 ], | ||
| 52 | [ 'dna5', 85, 86, 7, 8 ], | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | my $align = Bio::Graphics::Browser2::PadAlignment->new(\@dnas,\@alignments); | ||
| 57 | |||
| 58 | my @padded = $align->padded_sequences; | ||
| 59 | print join "\n",@padded,"\n"; | ||
| 60 | # ..FFFFgatcGATCgatcGATCgatc--GATCgatcG-B-ATCgatcGATC-atcGATC-gatcGATCgatcGAT-CgatcGATgatcGATCgatcNNNNGATC.... | ||
| 61 | # ....FFgatcGATC----GATCgatcNNGATCgatcG---ATCgatcGATCgatcGATC-gatcGATC--tcGATBCgatcGATCatcGATCgatcNNNNGATCFFFF | ||
| 62 | # ..FFFFgatcGATCgatcGATCgatc--GATCgatcBBBGATCgatcGATC-atcGATCBgatcGATCgatcGAT-CgatcGATgatcGATCgatcNNNNGATCFF.. | ||
| 63 | # ZZFFFFgatcGATCgatcGATCgatc--GATCgatc........................................................................ | ||
| 64 | # ...................AT-----------------BG-------GAT--------------------------------------------tcttttttt..... | ||
| 65 | |||
| 66 | my $pretty = $align->alignment; | ||
| 67 | print $pretty,"\n"; | ||
| 68 | # dna1 1 ..FFFFgatc GATCgatcGA TCgatc--GA TCgatcG-B- ATCgatcGAT C-atcGATC- gatcGATCga tcGAT-Cgat | ||
| 69 | # dna2 1 ....FFgatc GATC----GA TCgatcNNGA TCgatcG--- ATCgatcGAT CgatcGATC- gatcGATC-- tcGATBCgat | ||
| 70 | # dna3 1 ..FFFFgatc GATCgatcGA TCgatc--GA TCgatcBBBG ATCgatcGAT C-atcGATCB gatcGATCga tcGAT-Cgat | ||
| 71 | # dna4 1 ZZFFFFgatc GATCgatcGA TCgatc--GA TCgatc.... .......... .......... .......... .......... | ||
| 72 | # dna5 1 .......... .........A T--------- ---------- -----BGGAT ---------- ---------- ---------- | ||
| 73 | |||
| 74 | # dna1 72 cGATgatcGA TCgatcNNNN GATC.... | ||
| 75 | # dna2 67 cGATCatcGA TCgatcNNNN GATCFFFF | ||
| 76 | # dna3 75 cGATgatcGA TCgatcNNNN GATCFF.. | ||
| 77 | # dna4 35 .......... .......... ........ | ||
| 78 | # dna5 8 ---------- ----tctttt ttt..... | ||
| 79 | |||
| 80 | =head1 DESCRIPTION | ||
| 81 | |||
| 82 | This is a utility module for pretty-printing the type of alignment | ||
| 83 | that comes out of gbrowse, namely a multiple alignment in which each | ||
| 84 | target is aligned to a reference genome without explicit pads or | ||
| 85 | other spaces. | ||
| 86 | |||
| 87 | For speed and ease of use, the module does not use form Bio::SeqI | ||
| 88 | objects, but raw strings and alignment data structures. This may | ||
| 89 | change. | ||
| 90 | |||
| 91 | This module does B<not> perform multiple alignments! It merely | ||
| 92 | pretty-prints them! | ||
| 93 | |||
| 94 | =head2 METHODS | ||
| 95 | |||
| 96 | This section describes the methods used by this class. | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item $aligner = Bio::Graphics::Browser2::PadAlignment->new(\@sequences,\@alignments) | ||
| 101 | |||
| 102 | Create a new aligner. The two arguments are \@sequences, an array ref | ||
| 103 | to the list of sequences to be aligned, and \@alignments, an array ref | ||
| 104 | describing how the sequences are to be aligned. | ||
| 105 | |||
| 106 | \@sequences should have the following structure: | ||
| 107 | |||
| 108 | [ name1 => $sequence1, | ||
| 109 | name2 => $sequence2, | ||
| 110 | name3 => $sequence3 ] | ||
| 111 | |||
| 112 | The sequences will be displayed in top to bottom order in the order | ||
| 113 | provided. The first sequence in the list is special because it is the | ||
| 114 | reference sequence. All alignments are relative to it. | ||
| 115 | |||
| 116 | \@alignments should have the following structure: | ||
| 117 | |||
| 118 | [ [ target1, $start1, $end1, $tstart1, $tend1 ], | ||
| 119 | [ target1, $start2, $end2, $tstart2, $tend2 ], | ||
| 120 | ... | ||
| 121 | ] | ||
| 122 | |||
| 123 | Each element of @alignments is an arrayref with five elements. The | ||
| 124 | first element is the name of the target sequence, which must be one of | ||
| 125 | the named sequences given in @sequences. The second and third | ||
| 126 | elements are the start and stop position of the aligned target segment | ||
| 127 | relative to the reference sequence, "name1" in the example given | ||
| 128 | above. The fourth and fifth elements are the start and stop position | ||
| 129 | of the aligned target segment in the coordinate space of the target. | ||
| 130 | |||
| 131 | Example: | ||
| 132 | |||
| 133 | @dnas = ('dna1' => 'ccccccaaaaaatttt', | ||
| 134 | 'dna2' => 'aaaaaa'); | ||
| 135 | @alignment = ( ['dna2', 6, 11, 0, 5 ]); | ||
| 136 | |||
| 137 | Positions 0 to 5 of "dna2" align to positions 6-11 of "dna1". | ||
| 138 | |||
| 139 | Note that sequence positions are zero based. This may change. | ||
| 140 | |||
| 141 | =item @lines = $aligner->padded_sequences | ||
| 142 | |||
| 143 | This inserts pads into the sequences and returns them as a list of | ||
| 144 | strings in the order specified in new(). In a scalar context, this | ||
| 145 | method will return a hashref in which the keys are the sequence names | ||
| 146 | and the values are their padded strings. | ||
| 147 | |||
| 148 | =item $map = $aligner->gap_map | ||
| 149 | |||
| 150 | This returns an arrayref indicating the position of each base in the | ||
| 151 | gapped reference sequence. The indexes are base positions, and the | ||
| 152 | element values are their positions in the reference sequence as | ||
| 153 | returned by padded_sequences(). | ||
| 154 | |||
| 155 | Note that the gap map only provides coordinate mapping for the | ||
| 156 | reference sequence. For an alternative implementation that provides | ||
| 157 | gap maps for each of the targets (at the cost of speed and memory | ||
| 158 | efficiency) see the section after __END__ in the source file for this | ||
| 159 | module. | ||
| 160 | |||
| 161 | =item $align_string = $aligner->alignment(\%origins [,\%options]) | ||
| 162 | |||
| 163 | This method returns a pretty-printed string of the aligned sequences. | ||
| 164 | You may provide a hashref of sequence origins in order to control the | ||
| 165 | numbers printed next to each line of the alignment. The keys of the | ||
| 166 | %origins hashref are the names of the sequences, and the values are | ||
| 167 | the coordinate to be assigned to the first base of the sequence. Use | ||
| 168 | a negative number if you wish to indicate that the sequence has been | ||
| 169 | reverse complemented (the negative number should indicate the | ||
| 170 | coordinate of the first base in the provided sequence). | ||
| 171 | |||
| 172 | An optional second argument, if present, contains a hash reference to | ||
| 173 | a set of option=>value pairs. Three options are recognized: | ||
| 174 | |||
| 175 | show_mismatches 0|1 if true, highlight mismatches in pink | ||
| 176 | show_matches 0|1 if true, hightligt matches in yellow | ||
| 177 | color_code_proteins 0|1 if true, highlight amino acids thus: | ||
| 178 | Acidic amino acids in red | ||
| 179 | Basic amino acids in blue | ||
| 180 | Hydrophobic amino acids in grey | ||
| 181 | Polar amino acids in yellow | ||
| 182 | flip 0|1 if true, reverse complement the whole alignment | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | |||
| 187 | =head1 SEE ALSO | ||
| 188 | |||
| 189 | L<Bio::Graphics::Panel>, | ||
| 190 | L<Bio::Graphics::Glyph>, | ||
| 191 | L<Bio::Graphics::Feature>, | ||
| 192 | L<Bio::Graphics::FeatureFile>, | ||
| 193 | L<Bio::Graphics::Browser>, | ||
| 194 | L<Bio::Graphics::Browser2::Plugin> | ||
| 195 | |||
| 196 | =head1 AUTHOR | ||
| 197 | |||
| 198 | Lincoln Stein E<lt>lstein@cshl.orgE<gt>. | ||
| 199 | |||
| 200 | Copyright (c) 2002 Cold Spring Harbor Laboratory | ||
| 201 | |||
| 202 | This library is free software; you can redistribute it and/or modify | ||
| 203 | it under the same terms as Perl itself. See DISCLAIMER.txt for | ||
| 204 | disclaimers of warranty. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | # A specific package for padding multiple alignments into an ASCII text string. | ||
| 209 | # It is designed for cases in which all alignments are against | ||
| 210 | # a single reference sequence, such as ESTs against a genome. The reference | ||
| 211 | # sequence must be the first one provided. | ||
| 212 | |||
| 213 | # for efficiency, we use zero-based coordinates throughout. | ||
| 214 | |||
| 215 | # IMPORTANT NOTE: see the section after __END__ for a slightly | ||
| 216 | # different implementation which keeps a separate gap map for each | ||
| 217 | # sequence in the alignment | ||
| 218 | |||
| 219 | # define the types of amino acids -- this was done by an undergrad and is subject to change | ||
| 220 | # modified according to: http://www.ann.com.au/MedSci/amino.htm, method 1 | ||
| 221 | |||
| 222 | my %aa_type = ( | ||
| 223 | K=> "basic_aa", | ||
| 224 | R=> "basic_aa", | ||
| 225 | H=> "basic_aa", | ||
| 226 | S=> "polar_aa", | ||
| 227 | T=> "polar_aa", | ||
| 228 | N=> "polar_aa", | ||
| 229 | Q=> "polar_aa", | ||
| 230 | C=> "polar_aa", | ||
| 231 | Y=> "polar_aa", | ||
| 232 | D=> "acidic_aa", | ||
| 233 | E=> "acidic_aa", | ||
| 234 | G=> "npolar_aa", | ||
| 235 | A=> "npolar_aa", | ||
| 236 | V=> "npolar_aa", | ||
| 237 | L=> "npolar_aa", | ||
| 238 | I=> "npolar_aa", | ||
| 239 | P=> "npolar_aa", | ||
| 240 | M=> "npolar_aa", | ||
| 241 | F=> "npolar_aa", | ||
| 242 | C=> "npolar_aa", | ||
| 243 | W=> "npolar_aa", | ||
| 244 | X=> "special_aa", | ||
| 245 | "*" => "special_aa" | ||
| 246 | ); | ||
| 247 | |||
| 248 | sub new { | ||
| 249 | my $class = shift; | ||
| 250 | my $dnas = shift; # array ref of DNAs in the order in which they will be printed | ||
| 251 | # in format [ [name1=>dna1],[name2=>dna2]...] | ||
| 252 | my $aligns = shift; # array ref of alignments in format [ [targetname,srcstart,srcend,targetstart,targetend] ] | ||
| 253 | |||
| 254 | # remap data structures | ||
| 255 | my $count = 0; | ||
| 256 | my (@dnas,%dnas); | ||
| 257 | while (my($name,$dna) = splice(@$dnas,0,2)) { | ||
| 258 | $dnas{$name} = $count++; | ||
| 259 | push @dnas,$dna; | ||
| 260 | } | ||
| 261 | return bless { | ||
| 262 | names => \%dnas, | ||
| 263 | dnas => \@dnas, | ||
| 264 | aligns => $aligns | ||
| 265 | }; | ||
| 266 | } | ||
| 267 | |||
| 268 | # return a hashref in which the keys are the | ||
| 269 | # dna names and the values are the padded strings | ||
| 270 | sub padded_sequences { | ||
| 271 | my $self = shift; | ||
| 272 | my @lines; | ||
| 273 | |||
| 274 | my @dnas = @{$self->{dnas}}; | ||
| 275 | |||
| 276 | # initialize top line with src sequences | ||
| 277 | $lines[0] = $dnas[0]; | ||
| 278 | my $len = length($lines[0]); | ||
| 279 | for (my $i = 1; $i < @dnas; $i++) { | ||
| 280 | $lines[$i] = '-' x $len; | ||
| 281 | } | ||
| 282 | |||
| 283 | # running total of number of gaps, indexed by position on source | ||
| 284 | my @gap_map = 0..length($dnas[0])-1; | ||
| 285 | |||
| 286 | # place where DNA[$i] left off | ||
| 287 | my @last_end; | ||
| 288 | my @added = (length($dnas[0])-1); | ||
| 289 | |||
| 290 | # leader sequence to add back after gapping | ||
| 291 | my @leader = (0); | ||
| 292 | |||
| 293 | # alignments must be sorted according to target | ||
| 294 | foreach (sort {$a->[0] cmp $b->[0] | ||
| 295 | || $a->[1] <=> $b->[1] | ||
| 296 | } @{$self->{aligns}}) { | ||
| 297 | my ($targ, $start, $end, | ||
| 298 | $tstart,$tend) = @{$_}; | ||
| 299 | |||
| 300 | defined ($targ = $self->{names}{$targ}) or next; | ||
| 301 | |||
| 302 | warn "$start $end $tstart $tend\n" if DEBUG; | ||
| 303 | |||
| 304 | my $src = 0; # first DNA is the reference | ||
| 305 | |||
| 306 | # position in src coordinates where we last stopped | ||
| 307 | my $last_src = $last_end[$targ][$src] ||= -1; | ||
| 308 | |||
| 309 | # position in target coordinates where we last stopped | ||
| 310 | my $last_target = $last_end[$targ][$targ] ||= -1; | ||
| 311 | |||
| 312 | if ($last_src >= 0 && $last_target >= 0) { | ||
| 313 | # This section adds the unaligned region between the last place that | ||
| 314 | # we stopped and the current alignment | ||
| 315 | my $gap = 0; | ||
| 316 | my $tpos = $last_target+ 1; | ||
| 317 | my $spos = $gap_map[$last_src]+1; | ||
| 318 | warn "last_src=$last_src, spos=$spos" if DEBUG; | ||
| 319 | my $deficit = ($tstart-$last_target) - ($gap_map[$start] - $gap_map[$last_src]); | ||
| 320 | warn "add $deficit gaps" if DEBUG; | ||
| 321 | if ($deficit > 0) { | ||
| 322 | for (my $i=0; $i<@lines; $i++) { | ||
| 323 | eval {substr($lines[$i],$spos,0) = '-'x$deficit}; | ||
| 324 | } | ||
| 325 | @gap_map[$start..$#gap_map] = map {$_+$deficit} @gap_map[$start..$#gap_map]; | ||
| 326 | } | ||
| 327 | while ($tpos < $tstart) { | ||
| 328 | eval { substr($lines[$targ],$spos++,1) = substr($dnas[$targ],$tpos++,1) }; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | else { # remember to add the extra stuff at beginning | ||
| 333 | $leader[$src] = $start if !defined $leader[$src] || $start < $leader[$src]; | ||
| 334 | $leader[$targ] = $tstart if !defined $leader[$targ] || $tstart< $leader[$targ]; | ||
| 335 | } | ||
| 336 | |||
| 337 | # insert the aligned bit now | ||
| 338 | for (my $pos = $start; $pos <= $end; $pos++) { | ||
| 339 | my $gap_pos = $gap_map[$pos]; | ||
| 340 | defined $gap_pos or next; | ||
| 341 | warn "inserting $gap_pos with ",substr($dnas[$targ],$tstart,1),"\n" if DEBUG; | ||
| 342 | eval {substr($lines[$targ],$gap_pos,1) = substr($dnas[$targ],$tstart++,1) }; | ||
| 343 | } | ||
| 344 | |||
| 345 | $last_end[$targ][$src] = $end; | ||
| 346 | $last_end[$targ][$targ] = $tend; | ||
| 347 | |||
| 348 | $added[$targ] = $tend; | ||
| 349 | |||
| 350 | warn join("\n",@lines),"\n\n" if DEBUG; | ||
| 351 | } | ||
| 352 | |||
| 353 | # take care of the extra stuff at the end | ||
| 354 | for (my $i=1; $i < @dnas; $i++) { | ||
| 355 | my $last_bit = length($dnas[$i]) - $added[$i]; | ||
| 356 | next unless defined $gap_map[$last_end[$i][0]]; | ||
| 357 | local $^W = 0; # prevent uninit variable warnings | ||
| 358 | eval {substr($lines[$i],$gap_map[$last_end[$i][0]]+1,$last_bit) | ||
| 359 | = substr($dnas[$i],$added[$i]+1,$last_bit) }; | ||
| 360 | } | ||
| 361 | |||
| 362 | # take care of the extra unaligned stuff at the beginning | ||
| 363 | my $max = 0; | ||
| 364 | for (my $i=0; $i < @dnas; $i++) { | ||
| 365 | $leader[$i] ||= 0; # to prevent uninit variable warnings | ||
| 366 | next unless $leader[$i]; | ||
| 367 | my ($leading_gaps) = $lines[$i] =~ /^(-+)/; | ||
| 368 | my $leading_pads = length($leading_gaps||''); | ||
| 369 | |||
| 370 | warn "\$leader[$i] = $leader[$i], \$leading_pads = $leading_pads\n" if DEBUG; | ||
| 371 | |||
| 372 | my $insert_length = $leading_pads >= $leader[$i] ? $leader[$i] : $leading_pads; | ||
| 373 | my $append_length = $leading_pads >= $leader[$i] ? 0 : $leader[$i]-$leading_pads; | ||
| 374 | warn "insert length = $insert_length, append_length=$append_length\n" if DEBUG; | ||
| 375 | |||
| 376 | if ($insert_length > 0) { | ||
| 377 | substr($lines[$i],$leading_pads-$insert_length,$insert_length) = | ||
| 378 | substr($dnas[$i],$leader[$i]-$insert_length,$insert_length); | ||
| 379 | $leader[$i] -= $insert_length; | ||
| 380 | } | ||
| 381 | if ($append_length > 0) { | ||
| 382 | substr($lines[$i],0,0) = $i>0 ? substr($dnas[$i],0,$append_length) : '-'x$append_length; | ||
| 383 | } | ||
| 384 | |||
| 385 | $max = $append_length if $append_length > $max; | ||
| 386 | } | ||
| 387 | warn "\n" if DEBUG; | ||
| 388 | warn join("\n",@lines),"\n\n" if DEBUG; | ||
| 389 | |||
| 390 | warn "finished adding stuff for everything but reference sequence\n" if DEBUG; | ||
| 391 | |||
| 392 | for (my $i=0; $i<@dnas; $i++) { | ||
| 393 | warn "i = $i, max = $max, leader = $leader[$i]\n" if DEBUG; | ||
| 394 | my $delta = $max - $leader[$i]; | ||
| 395 | next unless $delta > 0; | ||
| 396 | substr($lines[$i],0,0) = '-'x$delta; | ||
| 397 | } | ||
| 398 | |||
| 399 | warn join("\n",@lines),"\n\n" if DEBUG; | ||
| 400 | |||
| 401 | # change starts and ends to . characters | ||
| 402 | $max = 0; | ||
| 403 | foreach (@lines) { | ||
| 404 | $max = length if $max < length; | ||
| 405 | } | ||
| 406 | foreach (@lines) { | ||
| 407 | my $deficit = $max - length; | ||
| 408 | s/^(-+)/'.'x length $1/e; | ||
| 409 | s/(-+)$/'.'x length $1/e; | ||
| 410 | $_ .= '.' x $deficit if $deficit > 0; | ||
| 411 | } | ||
| 412 | |||
| 413 | $self->{gaps} = \@gap_map; | ||
| 414 | |||
| 415 | return @lines if wantarray; | ||
| 416 | |||
| 417 | my %names = reverse %{$self->{names}}; | ||
| 418 | my %result = map {$names{$_} => $lines[$_]} 0..$#lines; | ||
| 419 | return \%result; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub gap_map { | ||
| 423 | my $self = shift; | ||
| 424 | $self->padded_sequences unless $self->{gaps}; | ||
| 425 | my $map = $self->{gaps}; | ||
| 426 | return $map; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub alignment { | ||
| 430 | my $self = shift; | ||
| 431 | my $origins = shift; | ||
| 432 | my $options = shift || {}; | ||
| 433 | |||
| 434 | my $show_mismatches = $options->{show_mismatches}; | ||
| 435 | my $show_matches = $options->{show_matches}; | ||
| 436 | my $show_similarities = $options->{show_similarities}; | ||
| 437 | my $color_code_proteins = $options->{color_code_proteins}; | ||
| 438 | warn "color code = $color_code_proteins" if DEBUG; | ||
| 439 | my $flip = $options->{flip}; | ||
| 440 | |||
| 441 | my @lines = $self->padded_sequences; | ||
| 442 | my %names = reverse %{$self->{names}}; # index to name | ||
| 443 | |||
| 444 | $origins ||= {}; | ||
| 445 | |||
| 446 | foreach (values %names) { | ||
| 447 | $origins->{$_} = 1 unless defined $origins->{$_}; | ||
| 448 | } | ||
| 449 | |||
| 450 | my $longest_name = 0; | ||
| 451 | foreach (values %names) { | ||
| 452 | my $offset = $origins->{$_}; | ||
| 453 | my $length = length($_); | ||
| 454 | $length += 2 if $offset < 0; | ||
| 455 | $longest_name = $length if $length > $longest_name; | ||
| 456 | } | ||
| 457 | my $longest_line = 0; | ||
| 458 | for (my $i=0; $i<@lines; $i++) { | ||
| 459 | my $offset = abs($origins->{$names{$i}}); | ||
| 460 | $longest_line = length($self->{dnas}[$i])+$offset | ||
| 461 | if (length($self->{dnas}[$i])+$offset > $longest_line); | ||
| 462 | } | ||
| 463 | |||
| 464 | $longest_line = length $longest_line; # looks like an error but isn't | ||
| 465 | |||
| 466 | # if flip is set, then we do amazing things to reorganize the display! | ||
| 467 | if ($flip) { | ||
| 468 | for (my $i = 0; $i < @lines; $i++) { | ||
| 469 | $lines[$i] = reverse $lines[$i]; | ||
| 470 | $lines[$i] =~ tr/gatcGATC/ctagCTAG/; | ||
| 471 | my $name = $names{$i}; | ||
| 472 | $origins->{$name} *= -1; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | # use markup to insert word and line breaks | ||
| 477 | my $markup = Bio::Graphics::Browser2::Markup->new; | ||
| 478 | $markup->add_style(space => ' '); | ||
| 479 | $markup->add_style(newline => "\n"); | ||
| 480 | $markup->add_style(mismatch => "BGCOLOR pink"); | ||
| 481 | $markup->add_style(match => "BGCOLOR darkorange"); | ||
| 482 | $markup->add_style(conserved => "BGCOLOR tan"); | ||
| 483 | |||
| 484 | # Styles for printing protein alignments | ||
| 485 | $markup->add_style(acidic_aa => "BGCOLOR lightgreen"); | ||
| 486 | $markup->add_style(basic_aa => "BGCOLOR lightskyblue"); | ||
| 487 | $markup->add_style(npolar_aa => "BGCOLOR lightgrey"); | ||
| 488 | $markup->add_style(polar_aa => "BGCOLOR burlywood"); | ||
| 489 | $markup->add_style(special_aa => "BGCOLOR red"); | ||
| 490 | |||
| 491 | # add word and line breaks | ||
| 492 | for (my $i=0; $i < @lines; $i++) { | ||
| 493 | my $pad = \$lines[$i]; | ||
| 494 | my @markup; | ||
| 495 | for (my $j=0; $j < length $$pad; $j += 10) { | ||
| 496 | push (@markup,[$j % 80 ? 'space':'newline', | ||
| 497 | $j => $j]); | ||
| 498 | } | ||
| 499 | $markup->markup($pad,\@markup); | ||
| 500 | } | ||
| 501 | |||
| 502 | my (@padded, @labels, @fixed); | ||
| 503 | |||
| 504 | for (my $i = 0; $i < @lines; $i++) { | ||
| 505 | my @segments = split "\n",$lines[$i]; | ||
| 506 | for (my $j = 0; $j < @segments; $j++) { | ||
| 507 | $padded[$j][$i] = $segments[$j]; | ||
| 508 | $fixed[$j][$i] = $segments[$j]; | ||
| 509 | } | ||
| 510 | my $origin = $origins->{$names{$i}}; | ||
| 511 | $labels[$i] = $origin if $origin >= 0; | ||
| 512 | $labels[$i] = length($self->{dnas}[$i]) + abs($origin) - 1 if $origin < 0; | ||
| 513 | } | ||
| 514 | |||
| 515 | my $result; | ||
| 516 | my @length; | ||
| 517 | |||
| 518 | #$i: number of blocks; $j: number of sequences | ||
| 519 | for (my $i = 0; $i < @padded; $i++) { | ||
| 520 | for (my $j = 0; $j < @{$padded[$i]}; $j++) { | ||
| 521 | |||
| 522 | next unless $padded[$i][$j]; | ||
| 523 | my $origin = $origins->{$names{$j}}; | ||
| 524 | my $offset = $padded[$i][$j] =~ tr/. -/. -/; | ||
| 525 | my $skipit = $offset == length($padded[$i][$j]); | ||
| 526 | my @markup; | ||
| 527 | |||
| 528 | #warn "Block ", $i, "\tsequence ", $j, "\t", $origin, "\t", $offset, "\t", $skipit, "\n"; | ||
| 529 | |||
| 530 | if ($color_code_proteins){ | ||
| 531 | if ($j==0) { # colouring reference seq | ||
| 532 | for(my $q=0; $q<length $padded[$i][$j]; $q++) { | ||
| 533 | my $refPos = substr($padded[$i][$j],$q,1); | ||
| 534 | next if $refPos =~ /^[.\s-]$/; # move on if not amino acid | ||
| 535 | push(@markup,[$aa_type{$refPos},$q=>$q+1]); | ||
| 536 | } # end for | ||
| 537 | } | ||
| 538 | |||
| 539 | else { | ||
| 540 | my @markup; | ||
| 541 | for (my $r=0; $r<length $padded[$i][$j]; $r++) { | ||
| 542 | my $targ = substr($padded[$i][$j],$r,1); | ||
| 543 | next if $targ =~ /^[.\s-]$/; | ||
| 544 | push(@markup,[$aa_type{$targ}, $r => $r+1]); | ||
| 545 | } | ||
| 546 | } | ||
| 547 | } | ||
| 548 | |||
| 549 | elsif ($show_mismatches) { | ||
| 550 | if ($j>0) { | ||
| 551 | for (my $r=0; $r<length $padded[$i][$j]; $r++) { | ||
| 552 | my $targ = substr($padded[$i][$j],$r,1); | ||
| 553 | next if $targ =~ /^[.\s-]$/; | ||
| 554 | my $source = substr($padded[$i][0],$r,1); | ||
| 555 | next if $source=~ /^[.\s-]$/; | ||
| 556 | |||
| 557 | push(@markup,['mismatch',$r => $r+1]) | ||
| 558 | if (lc($source) ne lc($targ)); | ||
| 559 | |||
| 560 | } | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | elsif ($show_matches) { | ||
| 565 | for (my $r=0; $r<length $padded[$i][$j]; $r++) { | ||
| 566 | |||
| 567 | my $targ = substr($padded[$i][$j],$r,1); | ||
| 568 | my $identical = 1; | ||
| 569 | my $conserved = 1; | ||
| 570 | |||
| 571 | for (my $m=0; $m<@{$fixed[$i]}; $m++){ | ||
| 572 | my $source = substr($fixed[$i][$m],$r,1); | ||
| 573 | |||
| 574 | if(($source =~ /[.\s-]/)||($targ =~ /[.\s-]/)){ | ||
| 575 | $identical = undef; | ||
| 576 | } elsif (lc($source) ne lc($targ)){ | ||
| 577 | $identical = undef; | ||
| 578 | } | ||
| 579 | |||
| 580 | if(($source =~ /[.\s-]/)||($targ =~ /[.\s-]/)){ | ||
| 581 | $conserved = undef; | ||
| 582 | }elsif (($aa_type{$source}) ne ($aa_type{$targ})){ | ||
| 583 | $conserved = undef; | ||
| 584 | } | ||
| 585 | } | ||
| 586 | push(@markup,['match',$r => $r+1]) if ($identical); | ||
| 587 | push(@markup,['conserved',$r => $r+1]) if ($conserved); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | elsif ($show_similarities) { #highligt resides same to the reference protein | ||
| 592 | if ($j == 0){ | ||
| 593 | for (my $r=0; $r<length $padded[$i][$j]; $r++) { | ||
| 594 | my $identical = undef; | ||
| 595 | my $conserved = undef; | ||
| 596 | |||
| 597 | my $targ = substr($padded[$i][$j],$r,1); | ||
| 598 | next if ($targ =~ /^[.\-]$/); | ||
| 599 | |||
| 600 | for (my $m=1; $m<@{$fixed[$i]}; $m++){ | ||
| 601 | my $source = substr($fixed[$i][$m],$r,1); | ||
| 602 | next if ($source =~ /^[.\-]$/); | ||
| 603 | |||
| 604 | if (($source !~ /^[.\s-]$/) && ($targ !~ /^[.\s-]$/) && (lc($source) eq lc($targ))){ | ||
| 605 | $identical = 1; | ||
| 606 | } | ||
| 607 | |||
| 608 | if (($source !~ /^[.\s-]$/) && ($targ !~ /^[.\s-]$/) && | ||
| 609 | (lc($aa_type{$source}) eq lc($aa_type{$targ})) && | ||
| 610 | (lc($source) ne lc($targ)) | ||
| 611 | ){ | ||
| 612 | $conserved = 1; | ||
| 613 | } | ||
| 614 | } | ||
| 615 | push(@markup,['conserved',$r => $r+1]) if ($conserved); | ||
| 616 | push(@markup,['match',$r => $r+1]) if ($identical); | ||
| 617 | } | ||
| 618 | } | ||
| 619 | |||
| 620 | else { | ||
| 621 | for (my $r=0; $r<length $padded[$i][$j]; $r++) { | ||
| 622 | |||
| 623 | my $targ = substr($padded[$i][$j],$r,1); | ||
| 624 | my $identical = undef; | ||
| 625 | my $conserved = undef; | ||
| 626 | |||
| 627 | my $source = substr($fixed[$i][0],$r,1); | ||
| 628 | next if ($source =~ /^[.\-]$/); | ||
| 629 | |||
| 630 | if (($source !~ /[.\s-]/) && (lc($source) eq lc($targ))){ | ||
| 631 | $identical = 1; | ||
| 632 | } | ||
| 633 | |||
| 634 | if (($source !~ /^[.\s-]$/) && ($targ !~ /^[.\s-]$/) && | ||
| 635 | (lc($aa_type{$source}) eq lc($aa_type{$targ})) && | ||
| 636 | (lc($source) ne lc($targ)) | ||
| 637 | ){ | ||
| 638 | $conserved = 1; | ||
| 639 | } | ||
| 640 | |||
| 641 | push(@markup,['conserved',$r => $r+1]) if ($conserved); | ||
| 642 | push(@markup,['match',$r => $r+1]) if ($identical); | ||
| 643 | } | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | $length[$i][$j] = length $padded[$i][$j]; | ||
| 648 | $markup->markup(\$padded[$i][$j],\@markup) if @markup; | ||
| 649 | |||
| 650 | my $l = $longest_name; | ||
| 651 | $result .= $skipit ? "" | ||
| 652 | : sprintf ("\%${l}s \%${longest_line}d %s\n", | ||
| 653 | $origin < 0 ? "($names{$j})" | ||
| 654 | : $names{$j}, | ||
| 655 | $labels[$j],$padded[$i][$j]); | ||
| 656 | |||
| 657 | $labels[$j] += $length[$i][$j] - $offset if $origin >= 0; | ||
| 658 | $labels[$j] -= $length[$i][$j] - $offset if $origin < 0; | ||
| 659 | } | ||
| 660 | $result .= "\n"; # unless $result && $result =~ /^[.\s]+$/; # skip completely empty lines | ||
| 661 | } | ||
| 662 | return $result; | ||
| 663 | } | ||
| 664 | |||
| 665 | 1; | ||
| 666 | |||
| 667 | __END__ |