| 1 | # TFBS module for TFBS::PatternGen |
| 2 | # |
| 3 | # Copyright Boris Lenhard |
| 4 | # |
| 5 | # You may distribute this module under the same terms as perl itself |
| 6 | # |
| 7 | |
| 8 | # POD |
| 9 | |
| 10 | =head1 NAME |
| 11 | |
| 12 | TFBS::PatternGen - a base class for pattern generators |
| 13 | |
| 14 | |
| 15 | =head1 DESCRIPTION |
| 16 | |
| 17 | TFBS::PatternGen is a base class providing methods common to all pattern generating |
| 18 | modules. It is meant to be inherited by a concrete pattern generator, which must have its own |
| 19 | constructor. |
| 20 | |
| 21 | =cut |
| 22 | |
| 23 | |
| 24 | package TFBS::PatternGenscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/; |
| 25 | |
| 26 | # Object preamble - inherits from TFBS::PatternGenI; |
| 27 | use varsscip-perl cpan TFBS v0.5.0 vars/ qw(@ISA); |
| 28 | use strictscip-perl cpan TFBS v0.5.0 strict/; |
| 29 | use TFBS::PatternGenIscip-perl cpan TFBS v0.5.0 TFBS/PatternGenI/; |
| 30 | # use TFBS::PatternGen::_Motif_; |
| 31 | use Bio::Seqscip-perl cpan TFBS v0.5.0 Bio/Seq/; |
| 32 | use Bio::SeqIOscip-perl cpan TFBS v0.5.0 Bio/SeqIO/; |
| 33 | use Carpscip-perl cpan TFBS v0.5.0 Carp/; |
| 34 | |
| 35 | @ISAscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@ISA`. = qw(TFBS::PatternGenI); |
| 36 | |
| 37 | sub newscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/new(). { |
| 38 | confessscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/confess().("TFBS::PatterGen is a base class for particular pattern generators". |
| 39 | "and cannot be instantiated itself."); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | =head2 pattern |
| 44 | |
| 45 | Title : pattern |
| 46 | Usage : my $pattern_obj = $patterngen->pattern() |
| 47 | Function: retrieves a pattern object produced by the pattern generator |
| 48 | Returns : a pattern object (currently available pattern generators |
| 49 | return a TFBS::Matrix::PFM object) |
| 50 | Args : none |
| 51 | Warning : If a pattern generator produces more than one pattern, |
| 52 | this method call returns only the first one and prints |
| 53 | a warning on STDERR, In those cases you should use |
| 54 | I<all_patterns> or I<patternSet> methods. |
| 55 | |
| 56 | |
| 57 | =cut |
| 58 | |
| 59 | sub patternscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/pattern(). { |
| 60 | my ($self, %args) =@_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.; |
| 61 | my @PFMs = $self->_motifs_to_patternsscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_motifs_to_patterns().(%args); |
| 62 | if (scalarscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/scalar().(@PFMs) > 1) { |
| 63 | $self->warnscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/warn().("The pattern generator produced multiple patterns. ". |
| 64 | "Please use patternSet method to retrieve a set object, ". |
| 65 | "or all_patterns method to retrieve an array of patterns"); |
| 66 | } |
| 67 | return $PFMs[0]; |
| 68 | } |
| 69 | |
| 70 | =head2 patternSet |
| 71 | |
| 72 | Title : patternSet |
| 73 | Usage : my $patternSet = $patterngen->patternSet() |
| 74 | Function: retrieves a pattern set object containing all the patterns |
| 75 | produced by the pattern generator |
| 76 | Returns : a pattern set object (currently available pattern generators |
| 77 | return a TFBS::MatrixSet object) |
| 78 | Args : none |
| 79 | |
| 80 | =cut |
| 81 | |
| 82 | |
| 83 | sub patternSetscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/patternSet(). { |
| 84 | my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.; |
| 85 | my @PFMs = $self->_motifs_to_patternsscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_motifs_to_patterns().(%args); |
| 86 | my $set = TFBS::MatrixSet->newscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/new().(); |
| 87 | $set->add_matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix().(@PFMs); |
| 88 | return $set; |
| 89 | } |
| 90 | |
| 91 | =head2 all_patterns |
| 92 | |
| 93 | Title : all_patterns |
| 94 | Usage : my @patterns = $patterngen->all_patterns() |
| 95 | Function: retrieves an array of pattern objects |
| 96 | produced by the pattern generator |
| 97 | Returns : an array of pattern set objects (currently available |
| 98 | pattern generators return an array of |
| 99 | TFBS::Matrix::PFM objects) |
| 100 | Args : none |
| 101 | |
| 102 | =cut |
| 103 | |
| 104 | |
| 105 | sub all_patternsscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/all_patterns(). { |
| 106 | my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.; |
| 107 | my @patterns = $self->_motifs_to_patternsscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_motifs_to_patterns().(%args); |
| 108 | return @patterns; |
| 109 | } |
| 110 | |
| 111 | sub _create_seq_setscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_create_seq_set(). { |
| 112 | my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.; |
| 113 | my (@raw_set, @final_set); |
| 114 | |
| 115 | if ($args{-seq_list}) { |
| 116 | @raw_set = @{$args{-seq_list}}; |
| 117 | } |
| 118 | elsif ($args{-seq_stream} ) { |
| 119 | while (my $seqobj = $args{-seq_stream}->next_seqscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/next_seq().()) { |
| 120 | pushscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/push(). @raw_set, $seqobj; |
| 121 | } |
| 122 | } |
| 123 | elsif ($args{-seq_file} ) { |
| 124 | my $seqstream = Bio::SeqIO->newscip-perl cpan TFBS v0.5.0 Bio/SeqIO/new().(-file=>$args{-seq_file}, |
| 125 | -format=>"fasta"); |
| 126 | while (my $seqobj = $seqstream->next_seqscip-perl cpan TFBS v0.5.0 Bio/SeqIO/next_seq().()) { |
| 127 | pushscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/push(). @raw_set, $seqobj; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | foreach my $seqobj (@raw_set) { |
| 132 | my $i = 1; #for unnamed sequences |
| 133 | if (refscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/ref().($seqobj)) { |
| 134 | my $seqstring; |
| 135 | eval { $seqstring = $seqscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/seq().obj->seq() }; |
| 136 | if ($@scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`$@`.) { |
| 137 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/throw().("Invalid sequence object passed in -seq_set."); |
| 138 | } |
| 139 | else { |
| 140 | _validate_seqscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_validate_seq().(ucscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/uc(). $seqstring) |
| 141 | or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/throw().("Illegal character(s) in sequence: $seqstring"); |
| 142 | } |
| 143 | pushscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/push(). @final_set, $seqobj; |
| 144 | } |
| 145 | else { |
| 146 | my $seqstring = $seqobj; |
| 147 | _validate_seqscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_validate_seq().(ucscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/uc(). $seqstring) |
| 148 | or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/throw().("Illegal character(s) in sequence: $seqstring"); |
| 149 | pushscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/push(). @final_set, Bio::Seq->newscip-perl cpan TFBS v0.5.0 Bio/Seq/new().(-seq=>$seqstring, |
| 150 | -ID=>"unnamed".$i++, |
| 151 | -type=>"dna"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | $self->{'seq_set'} = \@final_set; |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | sub _motifs_to_patternsscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_motifs_to_patterns(). { |
| 161 | my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.; |
| 162 | my $i = 1; |
| 163 | my @patterns; |
| 164 | my %params = ( -name => "motif", |
| 165 | -ID => "motif", |
| 166 | -class => "unknown", |
| 167 | %args); |
| 168 | foreach my $motif (@{ $self->{'motifs'} }) { |
| 169 | pushscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/push(). @patterns, $motif->patternscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/pattern().(-name => $params{-name}.$i, |
| 170 | -ID => $params{-ID}."#".$i, |
| 171 | -class => $params{-class}); |
| 172 | $i++; |
| 173 | } |
| 174 | return @patterns; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | sub _validate_seqscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_validate_seq(). { |
| 179 | # a utility function |
| 180 | my $sequence = ucscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/uc(). $_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.[0]; |
| 181 | $sequence=~ s/[ACGTN]//g; |
| 182 | return ($sequence eq "" ? 1 : 0); |
| 183 | } |
| 184 | |
| 185 | sub _check_seqs_for_uniform_lengthscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/_check_seqs_for_uniform_length(). { |
| 186 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/shift().; |
| 187 | my $reflength = $self->{'seq_set'}->[-1]->lengthscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/length().(); |
| 188 | foreach my $seqobj ( @{ $self->{'seq_set'} } ) { |
| 189 | if ($seqobj->lengthscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/length().() != $reflength) { |
| 190 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/throw().(refscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/ref().($self). "object has received sequences of unequal length"); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | sub all_motifsscip-perl cpan TFBS v0.5.0 TFBS/PatternGen/all_motifs(). { |
| 197 | return @{$_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.[0]->{'motifs'}} if $_scip-perl cpan TFBS v0.5.0 TFBS/PatternGen/`@_`.[0]->{'motifs'}; |
| 198 | } |