| 1 | package TFBS::ConservationProfile; |
| 2 | |
| 3 | use strict; |
| 4 | |
| 5 | use Bio::Root::Root; |
| 6 | |
| 7 | use vars qw'@ISA'; |
| 8 | @ISA = qw'Bio::Root::Root'; |
| 9 | |
| 10 | sub new { |
| 11 | my ($caller, %args) = @_; |
| 12 | my $self = bless {parameters=>{}, |
| 13 | alignment => undef, |
| 14 | ref_sequence => undef, |
| 15 | %args |
| 16 | } ,ref $caller || $caller; |
| 17 | |
| 18 | if (!defined($self->{conservation}) or ref($self->{conservation}) ne "ARRAY") { |
| 19 | $self->throw("conservation: argument missing or wrong object type"); |
| 20 | } |
| 21 | |
| 22 | return $self; |
| 23 | |
| 24 | } |
| 25 | |
| 26 | sub param { |
| 27 | my ($self, $param, $value) = @_; |
| 28 | if (!defined $param) { |
| 29 | return keys %{$self->{parameters}}; |
| 30 | } |
| 31 | elsif (defined $value) { |
| 32 | $self->{parameters}->{$param} = $value; |
| 33 | } |
| 34 | return $self->{parameters}->{$param}; |
| 35 | |
| 36 | } |
| 37 | |
| 38 | sub alignment { |
| 39 | $_[0]->{alignment} |
| 40 | } |
| 41 | |
| 42 | sub ref_sequence { |
| 43 | $_[0]->{ref_sequence} |
| 44 | } |
| 45 | |
| 46 | sub conserved_regions_as_gff { |
| 47 | my ($self, %args) = @_; |
| 48 | my @features = $self->conserved_regions_as_features; |
| 49 | my $output = ""; |
| 50 | foreach my $f (@features) { |
| 51 | $output .= $f->gff_string."\n"; |
| 52 | } |
| 53 | return $output; |
| 54 | } |
| 55 | |
| 56 | sub conserved_regions_as_features { |
| 57 | my ($self, %args) = @_; |
| 58 | my ($excl_feature_ref, $cutoff) = |
| 59 | $self->_rearrange([qw(exclude_features cutoff)], %args); |
| 60 | $cutoff = ($cutoff or $self->param("cutoff") or 0.7); |
| 61 | |
| 62 | print STDERR "CURRENT CUTOFF $cutoff\n"; |
| 63 | |
| 64 | my @conservation = $self->conservation; |
| 65 | my ($START, $END); |
| 66 | if ($self->ref_sequence and $self->ref_sequence->isa("Bio::LocatableSeq")) { |
| 67 | $START = $self->ref_sequence->start; |
| 68 | $END = $self->ref_sequence->end; |
| 69 | } |
| 70 | else { |
| 71 | $START = $self->alignment->get_seq_by_pos($self->param("ref_seq_nr" or 1))->start; |
| 72 | $END = $self->alignment->get_seq_by_pos($self->param("ref_seq_nr" or 1))->end; |
| 73 | } |
| 74 | if ($excl_feature_ref and ref($excl_feature_ref) eq "ARRAY") { |
| 75 | foreach my $f (@$excl_feature_ref) { |
| 76 | my ($s, $e) = ($f->start, $f->end); |
| 77 | next if ($e<$START or $s>$END); |
| 78 | $s = $START if $s<$START; |
| 79 | $e = $END if $e>$END; |
| 80 | map { $conservation[$_] = 0 } ($s-$START..$e-$START); |
| 81 | } |
| 82 | } |
| 83 | # obtain a list of positions with threshold |
| 84 | my @cons_positions = ( (grep {$conservation[$_-$START]>=$cutoff} ($START..$#conservation+$START)), $END+1000); |
| 85 | # $END+1000 is a dummy position that simplifies the procedure |
| 86 | |
| 87 | my @features; |
| 88 | my $region_start = $cons_positions[0]; |
| 89 | my $counter = 0; |
| 90 | foreach my $i (0..$#cons_positions ) { |
| 91 | if ($cons_positions[$i+1]-$cons_positions[$i]>10) { # 10 can be changed to max allowed gap in a conserved region |
| 92 | $counter++; |
| 93 | my $f = Bio::SeqFeature::Generic->new(-start=>$region_start, |
| 94 | -end =>$cons_positions[$i], |
| 95 | -primary => "CR$counter", |
| 96 | -score => $cons_positions[$i]-$region_start+1 |
| 97 | ); |
| 98 | |
| 99 | push @features, $f; |
| 100 | print STDERR $features[-1]->gff_string."\n"; |
| 101 | $region_start = $cons_positions[$i+1]; |
| 102 | } |
| 103 | } |
| 104 | return @features; |
| 105 | } |
| 106 | |
| 107 | sub conserved_regions_as_feature_pairs { |
| 108 | |
| 109 | |
| 110 | } |
| 111 | |
| 112 | sub conservation { |
| 113 | my ($self, $start, $end) = @_; |
| 114 | |
| 115 | if (!defined $start) { |
| 116 | return @{$self->{conservation}} |
| 117 | } |
| 118 | else { |
| 119 | my ($START, $END); |
| 120 | if ($self->alignment) { |
| 121 | my $ref_seq_nr = ($self->param("ref_seq_nr") or 1); |
| 122 | $START = $self->alignment->get_seq_by_pos($ref_seq_nr)->start; |
| 123 | $END = $self->alignment->get_seq_by_pos($ref_seq_nr)->end; |
| 124 | } |
| 125 | else { |
| 126 | $START = 1; |
| 127 | } |
| 128 | if (defined $end) { |
| 129 | my ($pad_start, $pad_end) = (0, 0); |
| 130 | if ($start < $START) { $pad_start = $START-$start; $start = $START; } |
| 131 | if ($end > $END) { $pad_end = $end-$END; $end = $END } |
| 132 | return (_blank_array($pad_start), |
| 133 | @{$self->{conservation}}[$start-$START, $end-$START], |
| 134 | _blank_array($pad_end) |
| 135 | ); |
| 136 | } |
| 137 | elsif ($start< $START) { |
| 138 | return undef; |
| 139 | } |
| 140 | else { |
| 141 | return $self->{conservation}->[$start-$START]; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | sub _blank_array { |
| 148 | my ($length) = @_; |
| 149 | my @arr = map {undef} (0..$length); |
| 150 | pop @arr; |
| 151 | return @arr |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | sub conserved_sequences { |
| 158 | |
| 159 | } |
| 160 | |
| 161 | sub conserved_subalignments { |
| 162 | |
| 163 | } |
| 164 | |
| 165 | 1; |