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