| 1 | package TFBS::Run::ConservationProfileGeneratorscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/; |
| 2 | |
| 3 | use strictscip-perl cpan TFBS v0.5.0 strict/; |
| 4 | |
| 5 | use Bio::Root::Rootscip-perl cpan TFBS v0.5.0 Bio/Root/Root/; |
| 6 | use TFBS::ConservationProfilescip-perl cpan TFBS v0.5.0 TFBS/ConservationProfile/; |
| 7 | use Bio::AlignIOscip-perl cpan TFBS v0.5.0 Bio/AlignIO/; |
| 8 | |
| 9 | use constantscip-perl cpan TFBS v0.5.0 constant/ DEFAULT_WINDOWscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/DEFAULT_WINDOW(). => 50; |
| 10 | use constantscip-perl cpan TFBS v0.5.0 constant/ DEFAULT_CUTOFFscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/DEFAULT_CUTOFF(). => 0.7; |
| 11 | |
| 12 | use varsscip-perl cpan TFBS v0.5.0 vars/ qw'@ISA'; |
| 13 | @ISAscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@ISA`. = qw'Bio::Root::Root'; |
| 14 | |
| 15 | sub newscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/new(). { |
| 16 | my ( $caller, %args ) = @_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.; |
| 17 | my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/bless(). { |
| 18 | alignment => undef, |
| 19 | ref_sequence => undef, |
| 20 | method => undef, |
| 21 | window => DEFAULT_WINDOW, |
| 22 | cutoff => DEFAULT_CUTOFF, |
| 23 | %args |
| 24 | }, |
| 25 | refscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/ref(). $caller || $caller; |
| 26 | |
| 27 | if ( !definedscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/defined().( $self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment(). ) |
| 28 | or !$self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment().->isascip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/isa().("Bio::SimpleAlign") ) |
| 29 | { |
| 30 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/throw().( "alignment: argument missing or wrong object type: " |
| 31 | . refscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/ref().( $self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment(). ) ); |
| 32 | } |
| 33 | |
| 34 | return $self; |
| 35 | } |
| 36 | |
| 37 | sub runscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/run(). { |
| 38 | my ( $self, %args ) = @_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.; |
| 39 | my $method = ( $args{method} or $self->methodscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/method(). or "simple" ); |
| 40 | |
| 41 | my %method_subref = ( |
| 42 | simple => \&_run_simplescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_run_simple()., |
| 43 | malin => \&_run_Malinsscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_run_Malins()., |
| 44 | align_cons => \&_run_align_consscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_run_align_cons(). |
| 45 | ); |
| 46 | |
| 47 | if ( !definedscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/defined().( $method_subref{$method} ) ) { |
| 48 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/throw().("method $method not supported"); |
| 49 | } |
| 50 | $method_subref{scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`->()`().$method}->( scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`->()`().$self, scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`->()`().%args )scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`->()`().; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | sub alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment(). { |
| 55 | $_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.[0]->{'alignment'}; |
| 56 | } |
| 57 | |
| 58 | sub ref_sequencescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/ref_sequence(). { |
| 59 | $_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.[0]->{'ref_sequence'}; |
| 60 | } |
| 61 | |
| 62 | sub methodscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/method(). { |
| 63 | $_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.[0]->{'method'}; |
| 64 | } |
| 65 | |
| 66 | sub windowscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/window(). { |
| 67 | $_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.[0]->{'window'}; |
| 68 | } |
| 69 | |
| 70 | sub cutoffscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/cutoff(). { |
| 71 | $_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.[0]->{'cutoff'}; |
| 72 | } |
| 73 | |
| 74 | sub _run_simplescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_run_simple(). { |
| 75 | my ( $self, %args ) = @_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.; |
| 76 | my ( $window_size, $cutoff, $ref_seq_nr, $other_seq_nr ) = |
| 77 | $self->_rearrangescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_rearrange().( [qw(WINDOW CUTOFF REF_SEQ_NR OTHER_SEQ_NR)], %args ); |
| 78 | $window_size = $self->windowscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/window(). unless $window_size; |
| 79 | $cutoff = $self->cutoffscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/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 = splitscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/split(). "", $self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment().->get_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/get_seq_by_pos().seqscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/seq()._by_posscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/get_seq_by_pos().($ref_seq_nr)->seq; |
| 84 | my @seq2 = splitscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/split(). "", $self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment().->get_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/get_seq_by_pos().seqscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/seq()._by_posscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/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 | shiftscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/shift(). @seq1; |
| 91 | shiftscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/shift(). @seq2; |
| 92 | } |
| 93 | |
| 94 | for my $i ( 0 .. $#seq1scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`$#seq1`. ) { |
| 95 | pushscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/push().( @match, ( ucscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/uc().( $seq1[$i] ) eq ucscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/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 .. ( $#matchscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`$#match`. + $window_size / 2 ) ) { |
| 100 | $graph[$i] = $graph[ $i - 1 ] + ( $i > $#matchscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`$#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[ intscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/int().( $window_size / 2 ) .. $#graphscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`$#graph`. ] ) { |
| 109 | pushscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/push(). @CONSERVATION, $match_point / $window_size; |
| 110 | } |
| 111 | |
| 112 | return TFBS::ConservationProfile->newscip-perl cpan TFBS v0.5.0 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_sequencescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/ref_sequence()., |
| 122 | alignment => $self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment(). |
| 123 | ); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | sub _run_Malinsscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_run_Malins(). { |
| 128 | shift->throwscip-perl cpan TFBS v0.5.0 shift/throw().( |
| 129 | "Not implemented, sorry. Pick another method for the time being"); |
| 130 | } |
| 131 | |
| 132 | sub _run_align_consscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_run_align_cons(). { |
| 133 | my ( $self, %args ) = @_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`.; |
| 134 | my ( $window_size, $increment, $cutoff, $stringency, $format, $prog ) = |
| 135 | $self->_rearrangescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_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 definedscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/defined(). $prog; |
| 147 | |
| 148 | my @cl_args; |
| 149 | while ( my ( $param, $value ) = eachscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/each(). %params ) { |
| 150 | if ( definedscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/defined(). $value ) { |
| 151 | pushscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/push(). @cl_args, $param, $value; |
| 152 | } |
| 153 | } |
| 154 | my $alnstring = $self->_alignment_to_stringscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_alignment_to_string().("fasta"); |
| 155 | $alnstring =~ s/[\"\$]/\\$1/gs; # escape things that might confuse echo |
| 156 | my $command = joinscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/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 | chompscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/chomp(). $line; |
| 164 | $line =~ s/^\D+//; |
| 165 | my ( $pos, $value ) = splitscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/split(). /\s+/, $line; |
| 166 | pushscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/push(). @CONSERVATION, $value; |
| 167 | } |
| 168 | |
| 169 | return TFBS::ConservationProfile->newscip-perl cpan TFBS v0.5.0 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->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment()., |
| 179 | ref_sequence => $self->ref_sequencescip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/ref_sequence(). |
| 180 | ); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | sub _alignment_to_stringscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/_alignment_to_string(). { |
| 185 | my ( $self, $format ) = ( @_scip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/`@_`., "fasta" ); |
| 186 | my $alnstring; |
| 187 | my $fh = IO::String->newscip-perl cpan TFBS v0.5.0 IO/String/new().($alnstring); |
| 188 | my $outstream = Bio::AlignIO->newscip-perl cpan TFBS v0.5.0 Bio/AlignIO/new().( -fh => $fh, -format => $format ); |
| 189 | $outstream->write_alnscip-perl cpan TFBS v0.5.0 Bio/AlignIO/write_aln().( $self->alignmentscip-perl cpan TFBS v0.5.0 TFBS/Run/ConservationProfileGenerator/alignment(). ); |
| 190 | $outstream->closescip-perl cpan TFBS v0.5.0 Bio/AlignIO/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; |