Debian Codegraph @

TFBS/Run/ConservationProfileGenerator.pm

raw symbols SCIP
1 package TFBS::Run::ConservationProfileGenerator;
2
3 use strict;
4
5 use Bio::Root::Root;
6 use TFBS::ConservationProfile;
7 use Bio::AlignIO;
8
9 use constant DEFAULT_WINDOW => 50;
10 use constant DEFAULT_CUTOFF => 0.7;
11
12 use vars qw'@ISA';
13 @ISA = qw'Bio::Root::Root';
14
15 sub new {
16 my ( $caller, %args ) = @_;
17 my $self = bless {
18 alignment => undef,
19 ref_sequence => undef,
20 method => undef,
21 window => DEFAULT_WINDOW,
22 cutoff => DEFAULT_CUTOFF,
23 %args
24 },
25 ref $caller || $caller;
26
27 if ( !defined( $self->alignment )
28 or !$self->alignment->isa("Bio::SimpleAlign") )
29 {
30 $self->throw( "alignment: argument missing or wrong object type: "
31 . ref( $self->alignment ) );
32 }
33
34 return $self;
35 }
36
37 sub run {
38 my ( $self, %args ) = @_;
39 my $method = ( $args{method} or $self->method or "simple" );
40
41 my %method_subref = (
42 simple => \&_run_simple,
43 malin => \&_run_Malins,
44 align_cons => \&_run_align_cons
45 );
46
47 if ( !defined( $method_subref{$method} ) ) {
48 $self->throw("method $method not supported");
49 }
50 $method_subref{$method}->( $self, %args );
51
52 }
53
54 sub alignment {
55 $_[0]->{'alignment'};
56 }
57
58 sub ref_sequence {
59 $_[0]->{'ref_sequence'};
60 }
61
62 sub method {
63 $_[0]->{'method'};
64 }
65
66 sub window {
67 $_[0]->{'window'};
68 }
69
70 sub cutoff {
71 $_[0]->{'cutoff'};
72 }
73
74 sub _run_simple {
75 my ( $self, %args ) = @_;
76 my ( $window_size, $cutoff, $ref_seq_nr, $other_seq_nr ) =
77 $self->_rearrange( [qw(WINDOW CUTOFF REF_SEQ_NR OTHER_SEQ_NR)], %args );
78 $window_size = $self->window unless $window_size;
79 $cutoff = $self->cutoff unless $cutoff;
80 $ref_seq_nr = 1 if !$ref_seq_nr;
81 $other_seq_nr = ( $other_seq_nr or 3 - $ref_seq_nr );
82
83 my @seq1 = split "", $self->alignment->get_seq_by_pos($ref_seq_nr)->seq;
84 my @seq2 = split "", $self->alignment->get_seq_by_pos($other_seq_nr)->seq;
85
86 my @CONSERVATION;
87 my @match;
88
89 while ( $seq1[0] eq "-" or $seq1[0] eq "." ) {
90 shift @seq1;
91 shift @seq2;
92 }
93
94 for my $i ( 0 .. $#seq1 ) {
95 push( @match, ( uc( $seq1[$i] ) eq uc( $seq2[$i] ) ? 1 : 0 ) )
96 unless ( $seq1[$i] eq "-" or $seq1[$i] eq "." );
97 }
98 my @graph = ( $match[0] );
99 for my $i ( 1 .. ( $#match + $window_size / 2 ) ) {
100 $graph[$i] = $graph[ $i - 1 ] + ( $i > $#match ? 0 : $match[$i] ) -
101 ( $i < $window_size ? 0 : $match[ $i - $window_size ] );
102 }
103
104 # at this point, the graph values are shifted $window_size/2 to the right
105 # i.e. the score at a certain position is the score of the window
106 # UPSTREAM of it: To fix it, we should discard the first $window_size/2 scores:
107 #$self->conservation1 ([]);
108 foreach my $match_point ( @graph[ int( $window_size / 2 ) .. $#graph ] ) {
109 push @CONSERVATION, $match_point / $window_size;
110 }
111
112 return TFBS::ConservationProfile->new(
113 conservation => \@CONSERVATION,
114 parameters => {
115 window => $window_size,
116 cutoff => $cutoff,
117 ref_seq_nr => $ref_seq_nr,
118 other_seq_nr => $other_seq_nr,
119 method => "simple"
120 },
121 ref_sequence => $self->ref_sequence,
122 alignment => $self->alignment
123 );
124
125 }
126
127 sub _run_Malins {
128 shift->throw(
129 "Not implemented, sorry. Pick another method for the time being");
130 }
131
132 sub _run_align_cons {
133 my ( $self, %args ) = @_;
134 my ( $window_size, $increment, $cutoff, $stringency, $format, $prog ) =
135 $self->_rearrange(
136 [qw(WINDOW INCREMENT CUTOFF STRINGENCY FORMAT PROGRAM)], %args );
137
138 my %params = (
139 -w => $window_size,
140 -n => $increment,
141 -t => $cutoff,
142 -s => $stringency,
143 -r => "p",
144 -f => ( $format or "c" ) # center by default
145 );
146 $prog = "align_cons" unless defined $prog;
147
148 my @cl_args;
149 while ( my ( $param, $value ) = each %params ) {
150 if ( defined $value ) {
151 push @cl_args, $param, $value;
152 }
153 }
154 my $alnstring = $self->_alignment_to_string("fasta");
155 $alnstring =~ s/[\"\$]/\\$1/gs; # escape things that might confuse echo
156 my $command = join " ", $prog, @cl_args;
157 my @output_lines = `echo "$alnstring" | $command`;
158
159 # add error checking here!!!
160
161 my @CONSERVATION;
162 foreach my $line (@output_lines) {
163 chomp $line;
164 $line =~ s/^\D+//;
165 my ( $pos, $value ) = split /\s+/, $line;
166 push @CONSERVATION, $value;
167 }
168
169 return TFBS::ConservationProfile->new(
170 conservation => \@CONSERVATION,
171 parameters => {
172 window => $window_size,
173 cutoff => $cutoff,
174 increment => $increment,
175 stringency => $stringency,
176 method => "align_cons"
177 },
178 alignment => $self->alignment,
179 ref_sequence => $self->ref_sequence
180 );
181
182 }
183
184 sub _alignment_to_string {
185 my ( $self, $format ) = ( @_, "fasta" );
186 my $alnstring;
187 my $fh = IO::String->new($alnstring);
188 my $outstream = Bio::AlignIO->new( -fh => $fh, -format => $format );
189 $outstream->write_aln( $self->alignment );
190 $outstream->close;
191 return $alnstring;
192 }
193
194
195
196
197 #sub _UNIT_TESTS {
198 # require Test;
199 # require CONSNP::Test::TestObjects;
200 # my $to = CONSNP::Test::TestObjects->new;
201 #
202 # plan(tests => 5);
203 #
204 # exit(0);
205 #
206 #
207 #}
208
209
210 1;