| 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::Matrix; |
| 41 | use vars '@ISA'; |
| 42 | |
| 43 | use PDL; # this dependency has to be eliminated in the future versions |
| 44 | use TFBS::PatternI; |
| 45 | |
| 46 | use strict; |
| 47 | |
| 48 | @ISA = qw(TFBS::PatternI); |
| 49 | |
| 50 | sub new { |
| 51 | my $class = shift; |
| 52 | my %args = @_; |
| 53 | my $self = bless {}, 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 (defined $args{'-matrix'}) { |
| 60 | $self->set_matrix($args{'-matrix'}); |
| 61 | } |
| 62 | elsif (defined $args{'-matrixstring'}) { |
| 63 | $self->set_matrix($args{'-matrixstring'}); |
| 64 | } |
| 65 | elsif (defined $args{-matrixfile}) { |
| 66 | my $matrixstring; |
| 67 | open (FILE, $args{-matrixfile}) |
| 68 | or $self->throw("Could not open $args{-matrixfile}"); |
| 69 | { |
| 70 | local $/ = undef; |
| 71 | $matrixstring = <FILE>; |
| 72 | } |
| 73 | $self->set_matrix($matrixstring); |
| 74 | } |
| 75 | else { |
| 76 | $self->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} ? ((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 matrix { |
| 126 | my ($self, $matrixdata) = @_; |
| 127 | $self->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_matrix { |
| 143 | pdl $_[0]->{'matrix'}; |
| 144 | } |
| 145 | |
| 146 | sub set_matrix { |
| 147 | my ($self, $matrixdata) = @_; |
| 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 (ref($matrixdata) eq "ARRAY" |
| 157 | and ref($matrixdata->[0]) eq "ARRAY" |
| 158 | and scalar(@{$matrixdata}) == 4) |
| 159 | { |
| 160 | # it is a perl array |
| 161 | $self->{'matrix'} = $matrixdata; |
| 162 | } |
| 163 | elsif (ref($matrixdata) eq "PDL") |
| 164 | { |
| 165 | # it's a piddle |
| 166 | $self->{matrix} = _pdl_to_matrixref($matrixdata); |
| 167 | } |
| 168 | elsif (!ref($matrixdata)) |
| 169 | #and (scalar split "\n",$matrixdata) == 4) |
| 170 | { |
| 171 | # it's a string then |
| 172 | $self->{matrix} = $self->_matrix_from_string($matrixdata); |
| 173 | } |
| 174 | else { |
| 175 | $self->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_string { |
| 186 | my ($self, $matrixstring) = @_; |
| 187 | my @array = (); |
| 188 | foreach ((split "\n", $matrixstring)[0..3]) { |
| 189 | s/^\s+//; |
| 190 | s/\s+$//; |
| 191 | push @array, [split]; |
| 192 | } |
| 193 | return \@array; |
| 194 | } |
| 195 | |
| 196 | sub _set_min_max_score { |
| 197 | my ($self) = @_; |
| 198 | my $transpose = $self->pdl_matrix->xchg(0,1); |
| 199 | $self->{min_score} = sum(minimum $transpose); |
| 200 | $self->{max_score} = sum(maximum $transpose); |
| 201 | } |
| 202 | |
| 203 | sub _load { |
| 204 | my ($self, $field, $value) = @_; |
| 205 | if (substr(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_matrixlist($field, $value) |
| 211 | or do { |
| 212 | warn ("Matrix with $field=>$value not found."); |
| 213 | return undef; |
| 214 | }; |
| 215 | my $ID = $self->{ID}; |
| 216 | my $DIR = $self->{dbh}; |
| 217 | $self->set_matrix(scalar `cat $DIR/$ID.pwm`); # FIXME - temporary |
| 218 | |
| 219 | } |
| 220 | else { |
| 221 | $self->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 revcom { |
| 239 | my ($self) = @_; |
| 240 | my $revcom_matrix = |
| 241 | $self->new(-matrix => $self->pdl_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 rawprint { |
| 264 | my $self = shift; |
| 265 | my $pwmstring = sprintf ( $self->pdl_matrix ); |
| 266 | $pwmstring =~ s/\[|\]//g; # lose [] |
| 267 | $pwmstring =~ s/\n /\n/g; # lose leading spaces |
| 268 | my @pwmlines = split("\n", $pwmstring); # f |
| 269 | $pwmstring = 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 prettyprint { |
| 284 | my $self = shift; |
| 285 | my $pwmstring = sprintf ( $self->pdl_matrix ); |
| 286 | $pwmstring =~ s/\[|\]//g; # lose [] |
| 287 | $pwmstring =~ s/\n /\n/g; # lose leading spaces |
| 288 | my @pwmlines = split("\n", $pwmstring); # |
| 289 | @pwmlines = ("A [$pwmlines[2] ]", |
| 290 | "C [$pwmlines[3] ]", |
| 291 | "G [$pwmlines[4] ]", |
| 292 | "T [$pwmlines[5] ]"); |
| 293 | $pwmstring = 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 length { |
| 309 | my $self = shift; |
| 310 | return $self->pdl_matrix->getdim(0); |
| 311 | } |
| 312 | |
| 313 | sub _pdl_to_matrixref { |
| 314 | my ($matrixdata) = @_; |
| 315 | unless ($matrixdata->isa("PDL")) { |
| 316 | die "A non-PDL object passed to _pdl_to_matrixref"; |
| 317 | } |
| 318 | my @list = list $matrixdata; |
| 319 | my @array; |
| 320 | my $matrix_width = scalar(@list) / 4; |
| 321 | for (0..3) { |
| 322 | push @array, [splice(@list, 0, $matrix_width)]; |
| 323 | } |
| 324 | return \@array; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | sub DESTROY { |
| 329 | # nothing |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | 1; |
| 335 | |
| 336 | |
| 337 | |
| 338 |