Debian Codegraph @

TFBS/PatternGen.pm

raw symbols SCIP
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::PatternGen;
25
26 # Object preamble - inherits from TFBS::PatternGenI;
27 use vars qw(@ISA);
28 use strict;
29 use TFBS::PatternGenI;
30 # use TFBS::PatternGen::_Motif_;
31 use Bio::Seq;
32 use Bio::SeqIO;
33 use Carp;
34
35 @ISA = qw(TFBS::PatternGenI);
36
37 sub new {
38 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 pattern {
60 my ($self, %args) =@_;
61 my @PFMs = $self->_motifs_to_patterns(%args);
62 if (scalar(@PFMs) > 1) {
63 $self->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 patternSet {
84 my ($self, %args) = @_;
85 my @PFMs = $self->_motifs_to_patterns(%args);
86 my $set = TFBS::MatrixSet->new();
87 $set->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_patterns {
106 my ($self, %args) = @_;
107 my @patterns = $self->_motifs_to_patterns(%args);
108 return @patterns;
109 }
110
111 sub _create_seq_set {
112 my ($self, %args) = @_;
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_seq()) {
120 push @raw_set, $seqobj;
121 }
122 }
123 elsif ($args{-seq_file} ) {
124 my $seqstream = Bio::SeqIO->new(-file=>$args{-seq_file},
125 -format=>"fasta");
126 while (my $seqobj = $seqstream->next_seq()) {
127 push @raw_set, $seqobj;
128 }
129 }
130
131 foreach my $seqobj (@raw_set) {
132 my $i = 1; #for unnamed sequences
133 if (ref($seqobj)) {
134 my $seqstring;
135 eval { $seqstring = $seqobj->seq() };
136 if ($@) {
137 $self->throw("Invalid sequence object passed in -seq_set.");
138 }
139 else {
140 _validate_seq(uc $seqstring)
141 or $self->throw("Illegal character(s) in sequence: $seqstring");
142 }
143 push @final_set, $seqobj;
144 }
145 else {
146 my $seqstring = $seqobj;
147 _validate_seq(uc $seqstring)
148 or $self->throw("Illegal character(s) in sequence: $seqstring");
149 push @final_set, 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_patterns {
161 my ($self, %args) = @_;
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 push @patterns, $motif->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_seq {
179 # a utility function
180 my $sequence = uc $_[0];
181 $sequence=~ s/[ACGTN]//g;
182 return ($sequence eq "" ? 1 : 0);
183 }
184
185 sub _check_seqs_for_uniform_length {
186 my $self = shift;
187 my $reflength = $self->{'seq_set'}->[-1]->length();
188 foreach my $seqobj ( @{ $self->{'seq_set'} } ) {
189 if ($seqobj->length() != $reflength) {
190 $self->throw(ref($self). "object has received sequences of unequal length");
191 }
192 }
193 }
194
195
196 sub all_motifs {
197 return @{$_[0]->{'motifs'}} if $_[0]->{'motifs'};
198 }