Debian Codegraph @

blib/lib/TFBS/Matrix.pm

raw symbols SCIP
1 # TFBS module for TFBS::Matrix
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::Matrix - base class for matrix patterns, containing methods common
13 to all
14
15 =head1 DESCRIPTION
16
17 TFBS::Matrix is a base class consisting of universal constructor called by
18 its subclasses (TFBS::Matrix::*), and matrix manipulation methods that are
19 independent of the matrix type. It is not meant to be instantiated itself.
20
21 =head1 FEEDBACK
22
23 Please send bug reports and other comments to the author.
24
25 =head1 AUTHOR - Boris Lenhard
26
27 Boris Lenhard E<lt>[email protected]<gt>
28
29 =head1 APPENDIX
30
31 The rest of the documentation details each of the object
32 methods. Internal methods are preceded with an underscore.
33
34 =cut
35
36
37
38 # The code begins HERE:
39
40 package TFBS::Matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/;
41 use varsscip-perl cpan TFBS v0.5.0 vars/ '@ISA';
42
43 use PDLscip-perl cpan TFBS v0.5.0 PDL/; # this dependency has to be eliminated in the future versions
44 use TFBS::PatternIscip-perl cpan TFBS v0.5.0 TFBS/PatternI/;
45
46 use strictscip-perl cpan TFBS v0.5.0 strict/;
47
48 @ISAscip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@ISA`. = qw(TFBS::PatternI);
49
50 sub newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/new(). {
51 my $class = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().;
52 my %args = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
53 my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/Matrix/bless(). {}, refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($class) || $class;
54
55 # first figure out how it was called
56 # we need (-dbh and (-ID or -name) for fetching it from a database
57 # or -matrix for direct matrix input
58
59 if (definedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/defined(). $args{'-matrix'}) {
60 $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($args{'-matrix'});
61 }
62 elsif (definedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/defined(). $args{'-matrixstring'}) {
63 $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($args{'-matrixstring'});
64 }
65 elsif (definedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/defined(). $args{-matrixfile}) {
66 my $matrixstring;
67 openscip-perl cpan TFBS v0.5.0 TFBS/Matrix/open(). (FILE, $args{-matrixfile})
68 or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("Could not open $args{-matrixfile}");
69 {
70 local $/scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`$/`. = undef;
71 $matrixstring = <FILE>;
72 }
73 $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($matrixstring);
74 }
75 else {
76 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("No matrix or db object provided.");
77 }
78
79 # Set the object data.
80 # Parameters specified in constructor call override those
81 # fetched from the database.
82
83 $self->{'ID'} = ($args{-ID} or
84 $self->{ID} or
85 "Unknown");
86 $self->{'name'} = ($args{-name} or
87 $self->{name} or
88 "Unknown");
89 $self->{'class'} = ($args{-class} or
90 $self->{class} or
91 "Unknown");
92 $self->{'strand'} = ($args{-strand} or
93 $self->{strand} or
94 "+");
95 $self->{'bg_probabilities'} =
96 ($args{'-bg_probabilities'} || {A => 0.25,
97 C => 0.25,
98 G => 0.25,
99 T => 0.25});
100
101 $self->{'tags'} = $args{-tags} ? ((refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($args{-tags}) eq "HASH") ? $args{-tags} : {} ) :{};
102 return $self;
103 }
104
105
106
107 =head2 matrix
108
109 Title : matrix
110 Usage : my $matrix = $pwm->matrix();
111 $pwm->matrix( [ [12, 3, 0, 0, 4, 0],
112 [ 0, 0, 0,11, 7, 0],
113 [ 0, 9,12, 0, 0, 0],
114 [ 0, 0, 0, 1, 1,12]
115 ]);
116
117 Function: get/set for the matrix data
118 Returns : a reference to 2D array of integers(PFM) or floats (ICM, PWM)
119 Args : none for get;
120 a four line string, reference to 2D array, or a 2D piddle for set
121
122 =cut
123
124
125 sub matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/matrix(). {
126 my ($self, $matrixdata) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
127 $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($matrixdata) if $matrixdata;
128 return $self->{'matrix'};
129 }
130
131 =head2 pdl_matrix
132
133 Title : pdl_matrix
134 Usage : my $pdl = $pwm->pdl_matrix();
135 Function: access the PDL matrix used to store the actual
136 matrix data directly
137 Returns : a PDL object, aka a piddle
138 Args : none
139
140 =cut
141
142 sub pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix(). {
143 pdl $_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.[0]->{'matrix'};
144 }
145
146 sub set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix(). {
147 my ($self, $matrixdata) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
148
149 # The input matrix (specified as -array=> in the constructir call
150 # can either be
151 # * a 2D regular perl array with 4 rows,
152 # * a piddle (FIXME - check for 4 rows), or
153 # * a four-line string of numbers
154
155 # print STDERR "MATRIX>>>".$matrixdata;
156 if (refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata) eq "ARRAY"
157 and refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata->[0]) eq "ARRAY"
158 and scalarscip-perl cpan TFBS v0.5.0 TFBS/Matrix/scalar().(@{$matrixdata}) == 4)
159 {
160 # it is a perl array
161 $self->{'matrix'} = $matrixdata;
162 }
163 elsif (refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata) eq "PDL")
164 {
165 # it's a piddle
166 $self->{matrix} = _pdl_to_matrixrefscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_pdl_to_matrixref().($matrixdata);
167 }
168 elsif (!refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata))
169 #and (scalar split "\n",$matrixdata) == 4)
170 {
171 # it's a string then
172 $self->{matrix} = $self->_matrix_from_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_string().($matrixdata);
173 }
174 else {
175 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("Wrong data type/format for -matrix.\n".
176 "Acceptable formats are Array of Arrays (4 rows),\n".
177 "PDL Array, (4 rows),\n".
178 "or plain string (4 lines).");
179 }
180 # $self->_set_min_max_score();
181 return 1;
182
183 }
184
185 sub _matrix_from_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_string(). {
186 my ($self, $matrixstring) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
187 my @array = ();
188 foreach ((splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split(). "\n", $matrixstring)[0..3]) {
189 s/^\s+//;
190 s/\s+$//;
191 pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @array, [splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().];
192 }
193 return \@array;
194 }
195
196 sub _set_min_max_scorescip-perl cpan TFBS v0.5.0 TFBS/Matrix/_set_min_max_score(). {
197 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
198 my $transpose = $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix().->xchgscip-perl cpan TFBS v0.5.0 TFBS/Matrix/xchg().(0,1);
199 $self->{min_score} = sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sum().(minimumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/minimum(). $transpose);
200 $self->{max_score} = sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sum().(maximumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/maximum(). $transpose);
201 }
202
203 sub _loadscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_load(). {
204 my ($self, $field, $value) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
205 if (substrscip-perl cpan TFBS v0.5.0 TFBS/Matrix/substr().(refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($self->{db}),0,5) eq "DBI::") {
206 # database retrieval
207 }
208 elsif (-d $self->{dbh}) {
209 # retrieval from .pwm files in a directory
210 $self->_lookup_in_matrixlistscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_lookup_in_matrixlist().($field, $value)
211 or do {
212 warnscip-perl cpan TFBS v0.5.0 TFBS/Matrix/warn(). ("Matrix with $field=>$value not found.");
213 return undef;
214 };
215 my $ID = $self->{ID};
216 my $DIR = $self->{dbh};
217 $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().(scalarscip-perl cpan TFBS v0.5.0 TFBS/Matrix/scalar(). `cat $DIR/$ID.pwm`); # FIXME - temporary
218
219 }
220 else {
221 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("-dbh is not a valid database handle or a directory.");
222 }
223 }
224
225
226 =head2 revcom
227
228 Title : revcom
229 Usage : my $revcom_pfm = $pfm->revcom();
230 Function: create a matrix pattern object which is reverse complement
231 of the current one
232 Returns : a TFBS::Matrix::* object of the same type as the one
233 the method acted upon
234 Args : none
235
236 =cut
237
238 sub revcomscip-perl cpan TFBS v0.5.0 TFBS/Matrix/revcom(). {
239 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
240 my $revcom_matrix =
241 $self->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/new().(-matrix => $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix().->slicescip-perl cpan TFBS v0.5.0 TFBS/Matrix/slice().('-1:0,-1:0'),
242 # the above line rotates the original matrix 180 deg,
243 -ID => ($self->{ID} or ""),
244 -name => ($self->{name} or ""),
245 -class => ($self->{class} or ""),
246 -strand => ($self->{strand} and $self->{strand} eq "-") ? "+" : "-",
247 -tags => ($self->{tags} or {}) );
248 return $revcom_matrix;
249 }
250
251
252 =head2 rawprint
253
254 Title : rawprint
255 Usage : my $rawstring = $pfm->rawprint);
256 Function: convert matrix data to a simple tab-separated format
257 Returns : a four-line string of tab-separated integers or floats
258 Args : none
259
260 =cut
261
262
263 sub rawprintscip-perl cpan TFBS v0.5.0 TFBS/Matrix/rawprint(). {
264 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().;
265 my $pwmstring = sprintfscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sprintf(). ( $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix(). );
266 $pwmstring =~ s/\[|\]//g; # lose []
267 $pwmstring =~ s/\n /\n/g; # lose leading spaces
268 my @pwmlines = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().("\n", $pwmstring); # f
269 $pwmstring = joinscip-perl cpan TFBS v0.5.0 TFBS/Matrix/join(). ("\n", @pwmlines[2..5])."\n";
270 return $pwmstring;
271 }
272
273 =head2 prettyprint
274
275 Title : prettyprint
276 Usage : my $prettystring = $pfm->prettyprint();
277 Function: convert matrix data to a human-readable string format
278 Returns : a four-line string with nucleotides and aligned numbers
279 Args : none
280
281 =cut
282
283 sub prettyprintscip-perl cpan TFBS v0.5.0 TFBS/Matrix/prettyprint(). {
284 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().;
285 my $pwmstring = sprintfscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sprintf(). ( $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix(). );
286 $pwmstring =~ s/\[|\]//g; # lose []
287 $pwmstring =~ s/\n /\n/g; # lose leading spaces
288 my @pwmlines = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().("\n", $pwmstring); #
289 @pwmlines = ("A [$pwmlines[2] ]",
290 "C [$pwmlines[3] ]",
291 "G [$pwmlines[4] ]",
292 "T [$pwmlines[5] ]");
293 $pwmstring = joinscip-perl cpan TFBS v0.5.0 TFBS/Matrix/join(). ("\n", @pwmlines)."\n";
294 return $pwmstring;
295 }
296
297 =head2 length
298
299 Title : length
300 Usage : my $pattern_length = $pfm->length;
301 Function: gets the pattern length in nucleotides
302 (i.e. number of columns in the matrix)
303 Returns : an integer
304 Args : none
305
306 =cut
307
308 sub lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/length(). {
309 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().;
310 return $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix().->getdimscip-perl cpan TFBS v0.5.0 TFBS/Matrix/getdim().(0);
311 }
312
313 sub _pdl_to_matrixrefscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_pdl_to_matrixref(). {
314 my ($matrixdata) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.;
315 unless ($matrixdata->isascip-perl cpan TFBS v0.5.0 TFBS/Matrix/isa().("PDL")) {
316 diescip-perl cpan TFBS v0.5.0 TFBS/Matrix/die(). "A non-PDL object passed to _pdl_to_matrixref";
317 }
318 my @list = listscip-perl cpan TFBS v0.5.0 TFBS/Matrix/list(). $matrixdata;
319 my @array;
320 my $matrix_width = scalarscip-perl cpan TFBS v0.5.0 TFBS/Matrix/scalar().(@list) / 4;
321 for (0..3) {
322 pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @array, [splicescip-perl cpan TFBS v0.5.0 TFBS/Matrix/splice().(@list, 0, $matrix_width)];
323 }
324 return \@array;
325 }
326
327
328 sub DESTROYscip-perl cpan TFBS v0.5.0 TFBS/Matrix/DESTROY(). {
329 # nothing
330 }
331
332
333
334 1;
335
336
337
338