| 1 | package TFBS::Matrix::_Alignment; |
| 2 | |
| 3 | use vars qw(@ISA $AUTOLOAD); |
| 4 | |
| 5 | use TFBS::SitePair; |
| 6 | use TFBS::SitePairSet; |
| 7 | use Bio::Root::Root; |
| 8 | use Bio::Seq; |
| 9 | use Bio::SimpleAlign; |
| 10 | use Bio::AlignIO; |
| 11 | use IO::String; |
| 12 | use PDL; |
| 13 | |
| 14 | use strict; |
| 15 | |
| 16 | @ISA =('Bio::Root::Root'); |
| 17 | |
| 18 | |
| 19 | # CONSTANTS |
| 20 | |
| 21 | use constant DEFAULT_WINDOW => 50; |
| 22 | use constant DEFAULT_CUTOFF => 70; |
| 23 | use constant DEFAULT_THRESHOLD => "80%"; |
| 24 | |
| 25 | |
| 26 | sub new { |
| 27 | |
| 28 | # this is ugly; OK, OK, I'll rewrite it as soon as I can |
| 29 | my ($caller, %args) = @_; |
| 30 | my $self = bless {}, ref $caller || $caller; |
| 31 | $self->window($args{-window} or DEFAULT_WINDOW); |
| 32 | $self->_parse_alignment(%args); |
| 33 | $self->seq1length(length(_strip_gaps($self->alignseq1()))); |
| 34 | $self->seq2length(length(_strip_gaps($self->alignseq2()))); |
| 35 | $self->_set_subpart_bounds($args{-subpart}); |
| 36 | # |
| 37 | # If a conservation profile is provided, no need to compute it again. |
| 38 | # NOTE: conservation2 never seems to be used anywhere else so don't worry |
| 39 | # about the fact we are ignoring it if conservation is passed in :) |
| 40 | # |
| 41 | my $cp = $args{-conservation}; |
| 42 | if ($cp) { |
| 43 | $self->conservation1([$cp->conservation()]); |
| 44 | } else { |
| 45 | $self->conservation1($self->_calculate_conservation($self->window(),1)); |
| 46 | $self->conservation2($self->_calculate_conservation($self->window(),2)); |
| 47 | } |
| 48 | |
| 49 | $self->cutoff($args{-cutoff} or DEFAULT_CUTOFF); |
| 50 | #$self->threshold($args{-threshold} or DEFAULT_THRESHOLD); |
| 51 | #$self->_do_sitesearch |
| 52 | #(($args{-pattern_set} or $self->throw("No -matrixset parameter")), |
| 53 | # ($args{-threshold} or DEFAULT_THRESHOLD), |
| 54 | # ()); |
| 55 | |
| 56 | # $self->_set_start_end(%args); # Maybe later... |
| 57 | |
| 58 | return $self; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | sub DESTROY { |
| 65 | # empty |
| 66 | } |
| 67 | |
| 68 | sub _parse_alignment { |
| 69 | my ($self, %args) = @_; |
| 70 | my ($seq1, $seq2, $start); |
| 71 | my $alignobj; |
| 72 | |
| 73 | if (defined $args{'-alignstring'}) { |
| 74 | $alignobj = _alignstring_to_alignobj($args{'-alignstring'}); |
| 75 | } |
| 76 | elsif (defined $args{'-file'}) { |
| 77 | $alignobj = _alignfile_to_alignobj($args{'-file'}); |
| 78 | } |
| 79 | elsif (defined $args{-alignobj}) { |
| 80 | $alignobj = $args{'-alignobj'}; |
| 81 | } |
| 82 | else { |
| 83 | $self->throw("No -alignstring, -file or -alignobj passed."); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | my @match; |
| 88 | my ($seqobj1, $seqobj2) = $alignobj->each_seq; |
| 89 | ($seq1, $seq2) = ($seqobj1->seq, $seqobj2->seq); |
| 90 | $start = 1; |
| 91 | $self->seq1name($seqobj1->display_id); |
| 92 | $self->seq2name($seqobj2->display_id); |
| 93 | |
| 94 | |
| 95 | $self->alignseq1($seq1); |
| 96 | $self->alignseq2($seq2); |
| 97 | my @seq1 = ("-", split('', $seq1) ); |
| 98 | my @seq2 = ("-", split('', $seq2) ); |
| 99 | $self->{alignseq1array} = [@seq1]; |
| 100 | $self->{alignseq2array} = [@seq2]; |
| 101 | |
| 102 | my (@seq1index, @seq2index); |
| 103 | my ($i1, $i2) = (0, 0); |
| 104 | for my $pos (0..$#seq1) { |
| 105 | my ($s1, $s2) = (0, 0); |
| 106 | $seq1[$pos] ne "-" and $s1 = ++$i1; |
| 107 | $seq2[$pos] ne "-" and $s2 = ++$i2; |
| 108 | push @seq1index, $s1; |
| 109 | push @seq2index, $s2; |
| 110 | } |
| 111 | |
| 112 | $self->pdlindex( pdl [ [list sequence($#seq1+1)], |
| 113 | [@seq1index], |
| 114 | [@seq2index], |
| 115 | [list zeroes ($#seq1+1)] ]) ; |
| 116 | |
| 117 | return 1; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | sub pdlindex { |
| 122 | my ($self, $input, $p1, $p2) = @_ ; |
| 123 | # print ("PARAMS ", join(":", @_), "\n"); |
| 124 | if (ref($input) eq "PDL") { |
| 125 | $self->{pdlindex} = $input; |
| 126 | } |
| 127 | |
| 128 | unless (defined $p2) { |
| 129 | return $self->{pdlindex}; |
| 130 | } |
| 131 | else { |
| 132 | my @results = list |
| 133 | $self->{pdlindex}->xchg(0,1)->slice($p2)->where |
| 134 | ($self->{pdlindex}->xchg(0,1)->slice($p1)==$input); |
| 135 | wantarray ? return @results : return $results[0]; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | sub lower_pdlindex { |
| 140 | my ($self, $input, $p1, $p2) = @_; |
| 141 | unless (defined $p2) { |
| 142 | $self->throw("Wrong number of parameters passed to lower_pdlindex"); |
| 143 | } |
| 144 | my $result; |
| 145 | my $i = $input; |
| 146 | |
| 147 | until ($result = $self->pdlindex($i, $p1 => $p2)) { |
| 148 | $i--; |
| 149 | |
| 150 | last if $i==0; |
| 151 | } |
| 152 | return $result or 1; |
| 153 | } |
| 154 | |
| 155 | sub higher_pdlindex { |
| 156 | my ($self, $input, $p1, $p2) = @_; |
| 157 | unless (defined $p2) { |
| 158 | $self->throw("Wrong number of parameters passed to lower_pdlindex"); |
| 159 | } |
| 160 | my $result; |
| 161 | my $i = $input; |
| 162 | until ($result = $self->pdlindex($i, $p1 => $p2)) { |
| 163 | $i++; |
| 164 | last unless ($self->pdlindex($i, $p1=>0) > 0); |
| 165 | } |
| 166 | return $result; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | sub _calculate_conservation { |
| 171 | my ($self, $WINDOW, $which) = @_; |
| 172 | my (@seq1, @seq2); |
| 173 | if ($which==2) { |
| 174 | @seq1 = @{$self->{alignseq2array}}; |
| 175 | @seq2 = @{$self->{alignseq1array}}; |
| 176 | } |
| 177 | else { |
| 178 | @seq1 = @{$self->{alignseq1array}}; |
| 179 | @seq2 = @{$self->{alignseq2array}}; |
| 180 | $which=1; |
| 181 | } |
| 182 | |
| 183 | my @CONSERVATION; |
| 184 | my @match; |
| 185 | |
| 186 | while ($seq1[0] eq "-") { |
| 187 | shift @seq1; |
| 188 | shift @seq2; |
| 189 | } |
| 190 | |
| 191 | for my $i (0..$#seq1) { |
| 192 | push (@match,( uc($seq1[$i]) eq uc($seq2[$i]) ? 1:0)) |
| 193 | unless ($seq1[$i] eq "-" or $seq1[$i] eq "."); |
| 194 | } |
| 195 | my @graph=($match[0]); |
| 196 | for my $i (1..($#match+$WINDOW/2)) { |
| 197 | $graph[$i] = ($graph[$i-1] or 0) |
| 198 | + ($i>$#match ? 0: $match[$i]) |
| 199 | - ($i<$WINDOW ? 0: $match[$i-$WINDOW]); |
| 200 | } |
| 201 | |
| 202 | # at this point, the graph values are shifted $WINDOW/2 to the right |
| 203 | # i.e. the score at a certain position is the score of the window |
| 204 | # UPSTREAM of it: To fix it, we should discard the first $WINDOW/2 scores: |
| 205 | #$self->conservation1 ([]); |
| 206 | foreach my $pos (@graph[int($WINDOW/2)..$#graph]) { |
| 207 | push @CONSERVATION, 100*$pos/$WINDOW; |
| 208 | } |
| 209 | |
| 210 | # correction |
| 211 | foreach my $pos (0..int($WINDOW/2)) { |
| 212 | $CONSERVATION[$pos] = |
| 213 | $CONSERVATION[$pos]*$WINDOW/(int($WINDOW/2)+$pos); |
| 214 | $CONSERVATION[$#CONSERVATION - $pos] = |
| 215 | $CONSERVATION[$#CONSERVATION - $pos]*$WINDOW/(int($WINDOW/2)+$pos); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | return [@CONSERVATION]; |
| 220 | |
| 221 | } |
| 222 | |
| 223 | |
| 224 | sub _strip_gaps { |
| 225 | # a utility function |
| 226 | my $seq = shift; |
| 227 | $seq =~ s/\-|\.//g; |
| 228 | return $seq; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | sub do_sitesearch { |
| 233 | my ($self, @args ) = @_; |
| 234 | |
| 235 | my ($MATRIXSET, $THRESHOLD, $CUTOFF) = |
| 236 | $self->_rearrange([qw(PATTERN_SET THRESHOLD CUTOFF)], @args); |
| 237 | if (!$MATRIXSET) { |
| 238 | $self->throw("No -pattern_set passed to do_sitesearch"); |
| 239 | } |
| 240 | $CUTOFF = ($CUTOFF or DEFAULT_CUTOFF); |
| 241 | $THRESHOLD = ($THRESHOLD or DEFAULT_THRESHOLD); |
| 242 | $self->site_pair_set(TFBS::SitePairSet->new()); |
| 243 | |
| 244 | return if(($self->subpart1 and $self->subpart1->{-start} == 0) or |
| 245 | ($self->subpart2 and $self->subpart2->{-start} == 0)); |
| 246 | # ^^^ If one of the subparts is a gap, there's no point in searching |
| 247 | |
| 248 | my $seqobj1 = Bio::Seq->new(-seq=>_strip_gaps($self->alignseq1()), |
| 249 | -id => "Seq1"); |
| 250 | my $siteset1 = |
| 251 | $MATRIXSET->search_seq(-seqobj => $seqobj1, |
| 252 | -threshold => $THRESHOLD, |
| 253 | -subpart => $self->subpart1); |
| 254 | my $siteset1_itr = $siteset1->Iterator(-sort_by => "start"); |
| 255 | |
| 256 | my $seqobj2 = Bio::Seq->new(-seq=>_strip_gaps($self->alignseq2()), |
| 257 | -id => "Seq2"); |
| 258 | my $siteset2 = |
| 259 | $MATRIXSET->search_seq(-seqobj => $seqobj2, |
| 260 | -threshold => $THRESHOLD, |
| 261 | -subpart => $self->subpart2); |
| 262 | my $siteset2_itr = $siteset2->Iterator(-sort_by => "start"); |
| 263 | |
| 264 | my $site1 = $siteset1_itr->next(); |
| 265 | my $site2 = $siteset2_itr->next(); |
| 266 | |
| 267 | while (defined $site1 and defined $site2) { |
| 268 | my $pos1_in_aln = $self->pdlindex($site1->start(), 1=>0); |
| 269 | my $pos2_in_aln = $self->pdlindex($site2->start(), 2=>0); |
| 270 | my $cmp = (($pos1_in_aln <=> $pos2_in_aln) |
| 271 | or ($site1->pattern->name() cmp $site2->pattern->name()) |
| 272 | or ($site1->strand() cmp $site2->strand())); |
| 273 | |
| 274 | if ($cmp==0) { ### match |
| 275 | if (# threshold test: |
| 276 | $self->conservation1->[$site1->start()] |
| 277 | >= |
| 278 | $self->cutoff() |
| 279 | ) |
| 280 | { |
| 281 | my $site_pair = TFBS::SitePair->new($site1, $site2); |
| 282 | $self->site_pair_set->add_site_pair($site_pair); |
| 283 | } |
| 284 | $site1 = $siteset1_itr->next(); |
| 285 | $site2 = $siteset2_itr->next(); |
| 286 | } |
| 287 | elsif ($cmp<0) { ### $siteset1 is behind |
| 288 | $site1 = $siteset1_itr->next(); |
| 289 | } |
| 290 | elsif ($cmp>0) { ### $siteset2 is behind |
| 291 | $site2 = $siteset2_itr->next(); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | sub _set_subpart_bounds { |
| 298 | my ($self, $subpart) = @_; |
| 299 | if(defined $subpart) { |
| 300 | my ($relative_to, $start, $end) = ($subpart->{-relative_to}, |
| 301 | $subpart->{-start}, |
| 302 | $subpart->{-end}); |
| 303 | unless(defined($relative_to) and defined($start) and defined($end) ) { |
| 304 | $self->throw("Option -subpart missing suboption -relative_to, -start or -end"); |
| 305 | } |
| 306 | if($relative_to == 1) { |
| 307 | my $other_start = $self->higher_pdlindex($start, 1 => 2); |
| 308 | my $other_end = $self->lower_pdlindex($end, 1 => 2); |
| 309 | ($other_start, $other_end) = (0,0) if($other_start > $other_end); |
| 310 | $self->subpart1({ -start => $start, -end => $end }); |
| 311 | $self->subpart2({ -start => $other_start, -end => $other_end }); |
| 312 | } |
| 313 | elsif($relative_to == 2) { |
| 314 | my $other_start = $self->higher_pdlindex($start, 2 => 1); |
| 315 | my $other_end = $self->lower_pdlindex($end, 2 => 1); |
| 316 | ($other_start, $other_end) = (0,0) if($other_start > $other_end); |
| 317 | $self->subpart1({ -start => $other_start, -end => $other_end }); |
| 318 | $self->subpart2({ -start => $start, -end => $end }); |
| 319 | } |
| 320 | else { |
| 321 | $self->throw("Suboption -relative_to should be 1 or 2"); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | |
| 327 | sub _calculate_cutoff { |
| 328 | my ($self) = @_; |
| 329 | my $ile = 0.9; |
| 330 | my @conservation_array = sort {$a <=> $b} @{$self->conservation1()}; |
| 331 | |
| 332 | my $perc_90 = $conservation_array[int($ile*scalar(@conservation_array))]; |
| 333 | return $perc_90; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | sub _alignfile_to_string { |
| 338 | # a utility function |
| 339 | # DEPRECATED !!! |
| 340 | my $alignfile = shift; |
| 341 | if ($alignfile =~ /\.msf$/i) { |
| 342 | my $alignobj = Bio::SimpleAlign->new(); |
| 343 | $alignobj->read_MSF($alignfile); |
| 344 | return _alignobj_to_string($alignobj); |
| 345 | } |
| 346 | else { #assumed clustalw - no AlignIO import yet |
| 347 | local $/ = undef; |
| 348 | open FILE, $alignfile |
| 349 | or die("Could not read alignfile $alignfile, stopped"); |
| 350 | my $alignstring = <FILE>; |
| 351 | return $alignstring; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | sub _alignfile_to_alignobj { |
| 356 | # a utility function |
| 357 | my ($alignfile, $format) = (@_,'clustalw'); |
| 358 | if (!$format and $alignfile =~ /\.msf$/i) { $format = 'msf' ;} |
| 359 | my $alnio = Bio::AlignIO->new(-file=>$alignfile, -format=>$format); |
| 360 | return $alnio->next_aln; |
| 361 | } |
| 362 | |
| 363 | sub _alignobj_to_string { |
| 364 | # a utility function |
| 365 | # DEPRECATED |
| 366 | my $alignobj = shift; |
| 367 | my $alignstring; |
| 368 | my $io = IO::String->new($alignstring); |
| 369 | my $alnio = Bio::AlignIO->new(-fh=>$io, -format=>"clustalw"); |
| 370 | $alnio->write_aln($alignobj); |
| 371 | $alnio->close(); |
| 372 | # $io->close; |
| 373 | return $alignstring; |
| 374 | } |
| 375 | |
| 376 | sub _alignstring_to_alignobj { |
| 377 | # a utility function |
| 378 | my ($alignstring, $format) = (@_, 'clustalw'); |
| 379 | my $io = IO::String->new($alignstring); |
| 380 | my $alnio = Bio::AlignIO->new(-fh=>$io, -format=>$format); |
| 381 | my $alignobj = $alnio->next_aln(); |
| 382 | $alnio->close(); |
| 383 | # $io->close; |
| 384 | return $alignstring; |
| 385 | } |
| 386 | |
| 387 | # uglier than AUTOLOAD, but faster - a quick fix to get rid of Class::MethodMaker |
| 388 | |
| 389 | |
| 390 | sub cutoff |
| 391 | { $_[0]->{'cutoff'} = $_[1] if exists $_[1]; $_[0]->{'cutoff'}; } |
| 392 | |
| 393 | sub window |
| 394 | { $_[0]->{'window '} = $_[1] if exists $_[1]; $_[0]->{'window '}; } |
| 395 | |
| 396 | sub alignseq1 |
| 397 | { $_[0]->{'alignseq1'} = $_[1] if exists $_[1]; $_[0]->{'alignseq1'}; } |
| 398 | |
| 399 | sub alignseq2 |
| 400 | { $_[0]->{'alignseq2'} = $_[1] if exists $_[1]; $_[0]->{'alignseq2'}; } |
| 401 | |
| 402 | sub site_pair_set |
| 403 | { $_[0]->{'site_pair_set'} = $_[1] if exists $_[1]; $_[0]->{'site_pair_set'};} |
| 404 | |
| 405 | sub seq1name |
| 406 | { $_[0]->{'seq1name'} = $_[1] if exists $_[1]; $_[0]->{'seq1name'}; } |
| 407 | |
| 408 | sub seq2name |
| 409 | { $_[0]->{'seq2name'} = $_[1] if exists $_[1]; $_[0]->{'seq2name'}; } |
| 410 | |
| 411 | sub seq1length |
| 412 | { $_[0]->{'seq1length'} = $_[1] if exists $_[1]; $_[0]->{'seq1length'}; } |
| 413 | |
| 414 | sub seq2length |
| 415 | { $_[0]->{'seq2length'} = $_[1] if exists $_[1]; $_[0]->{'seq2length'}; } |
| 416 | |
| 417 | sub subpart1 |
| 418 | { $_[0]->{'subpart1'} = $_[1] if exists $_[1]; $_[0]->{'subpart1'}; } |
| 419 | |
| 420 | sub subpart2 |
| 421 | { $_[0]->{'subpart2'} = $_[1] if exists $_[1]; $_[0]->{'subpart2'}; } |
| 422 | |
| 423 | sub conservation1 |
| 424 | { $_[0]->{'conservation1'} = $_[1] if exists $_[1]; $_[0]->{'conservation1'};} |
| 425 | |
| 426 | sub conservation2 |
| 427 | { $_[0]->{'conservation2'} = $_[1] if exists $_[1]; $_[0]->{'conservation2'};} |
| 428 | |
| 429 | sub exclude_orf |
| 430 | { $_[0]->{'exclude_orf'} = $_[1] if exists $_[1]; $_[0]->{'exclude_orf'}; } |
| 431 | |
| 432 | sub start_at |
| 433 | { $_[0]->{'start_at'} = $_[1] if exists $_[1]; $_[0]->{'start_at'}; } |
| 434 | |
| 435 | sub end_at |
| 436 | { $_[0]->{'end_at'} = $_[1] if exists $_[1]; $_[0]->{'end_at'}; } |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | 1; |
| 442 | |
| 443 |