Archive Ensembl HomeArchive Ensembl Home
Greedy.pm
Go to the documentation of this file.
00001 package Bio::EnsEMBL::Compara::Filter::Greedy;
00002 
00003 use vars qw(@ISA);
00004 use strict;
00005 
00006 #use Bio::EnsEMBL::DnaDnaAlignFeature;
00007 
00008 =head2 filter
00009 
00010     Title   :   filter
00011     Usage   :   filter(\@)
00012     Function:   Clean the Array of Bio::EnsEMBL::FeaturePairs in three steps, 
00013                 First, determines the highest scored hit, and fix the expected strand hit
00014                 Second, hits on expected strand are kept if they do not overlap, 
00015                 either on query or subject sequence, previous strored, higher scored hits.
00016                 If hit goes trough the second step, the third test makes sure that the hit
00017                 is coherent position according to previous ones. 
00018     Returns :   Array reference of Bio::EnsEMBL::FeaturePairs
00019     Args    :   Array reference of Bio::EnsEMBL::FeaturePairs
00020 
00021 =cut
00022 
00023 sub filter {
00024   my ($self,$DnaDnaAlignFeatures) = @_;
00025 
00026   my @{$DnaDnaAlignFeatures} = sort {$b->score <=> $a->score} @{$DnaDnaAlignFeatures};
00027   
00028   my @DnaDnaAlignFeatures_filtered;
00029   my $ref_strand;
00030 
00031   foreach my $fp (@{$DnaDnaAlignFeatures}) {
00032 
00033     if ($fp->strand < 0) {
00034       $fp->reverse_complement;
00035     }
00036 
00037     if (! scalar @DnaDnaAlignFeatures_filtered) {
00038         push @DnaDnaAlignFeatures_filtered, $fp;
00039     $ref_strand = $fp->hstrand;
00040         next;
00041     }
00042 
00043     next if ($fp->hstrand != $ref_strand);
00044 
00045     my $add_fp = 1;
00046 
00047     foreach my $feature_filtered (@DnaDnaAlignFeatures_filtered) {
00048 
00049       my ($start,$end,$hstart,$hend) = ($feature_filtered->start,$feature_filtered->end,$feature_filtered->hstart,$feature_filtered->hend);
00050 
00051       if (($fp->start >= $start && $fp->start <= $end) ||
00052       ($fp->end >= $start && $fp->end <= $end) ||
00053       ($fp->hstart >= $hstart && $fp->hstart <= $hend) ||
00054       ($fp->hend >= $hstart && $fp->hend <= $hend)) {
00055     $add_fp = 0;
00056     last;
00057       }
00058 
00059       if ($ref_strand == 1) {
00060     unless (($fp->start > $end && $fp->hstart > $hend) ||
00061         ($fp->end < $start && $fp->hend < $hend)) {
00062       $add_fp = 0;
00063       last;
00064     }
00065       } elsif ($ref_strand == -1) {
00066     unless (($fp->start > $end && $fp->hstart < $hend) ||
00067         ($fp->end < $start && $fp->hend > $hend)) {
00068       $add_fp = 0;
00069       last;
00070     }
00071       }
00072     }
00073     push @DnaDnaAlignFeatures_filtered, $fp if ($add_fp);
00074   }
00075   return \@DnaDnaAlignFeatures_filtered;
00076 }
00077 
00078 1;