| 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 | Modified by Eivind Valen [email protected] |
| 29 | |
| 30 | =head1 APPENDIX |
| 31 | |
| 32 | The rest of the documentation details each of the object |
| 33 | methods. Internal methods are preceded with an underscore. |
| 34 | |
| 35 | =cut |
| 36 | |
| 37 | |
| 38 | |
| 39 | # The code begins HERE: |
| 40 | |
| 41 | package TFBS::Matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/; |
| 42 | use varsscip-perl cpan TFBS v0.5.0 vars/ '@ISA'; |
| 43 | |
| 44 | use PDLscip-perl cpan TFBS v0.5.0 PDL/; # this dependency has to be eliminated in the future versions |
| 45 | use TFBS::PatternIscip-perl cpan TFBS v0.5.0 TFBS/PatternI/; |
| 46 | |
| 47 | use strictscip-perl cpan TFBS v0.5.0 strict/; |
| 48 | |
| 49 | @ISAscip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@ISA`. = qw(TFBS::PatternI); |
| 50 | |
| 51 | # IUPAC codes |
| 52 | my %dna = ("A" => [12,0,0,0], |
| 53 | "C" => [0,12,0,0], |
| 54 | "G" => [0,0,12,0], |
| 55 | "T" => [0,0,0,12], |
| 56 | "U" => [0,0,0,12], |
| 57 | "R" => [6,0,6,0], |
| 58 | "Y" => [0,6,0,6], |
| 59 | "M" => [6,6,0,0], |
| 60 | "K" => [0,0,6,6], |
| 61 | "W" => [6,0,0,6], |
| 62 | "S" => [0,6,6,0], |
| 63 | "B" => [0,4,4,4], |
| 64 | "D" => [4,0,4,4], |
| 65 | "H" => [4,4,0,4], |
| 66 | "V" => [4,4,4,0], |
| 67 | "N" => [3,3,3,3]); |
| 68 | |
| 69 | sub newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/new(). { |
| 70 | my $class = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().; |
| 71 | my %args = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 72 | my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/Matrix/bless(). {}, refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($class) || $class; |
| 73 | |
| 74 | # first figure out how it was called |
| 75 | # we need (-dbh and (-ID or -name) for fetching it from a database |
| 76 | # or -matrix for direct matrix input |
| 77 | |
| 78 | if (definedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/defined(). $args{'-matrix'}) { |
| 79 | $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($args{'-matrix'}); |
| 80 | } |
| 81 | elsif (definedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/defined(). $args{'-matrixstring'}) { |
| 82 | $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($args{'-matrixstring'}); |
| 83 | } |
| 84 | elsif (definedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/defined(). $args{-matrixfile}) { |
| 85 | my $matrixstring; |
| 86 | openscip-perl cpan TFBS v0.5.0 TFBS/Matrix/open(). (FILE, $args{-matrixfile}) |
| 87 | or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("Could not open $args{-matrixfile}"); |
| 88 | { |
| 89 | local $/scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`$/`. = undef; |
| 90 | $matrixstring = <FILE>; |
| 91 | } |
| 92 | $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($matrixstring); |
| 93 | } |
| 94 | else { |
| 95 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("No matrix or db object provided."); |
| 96 | } |
| 97 | |
| 98 | # Set the object data. |
| 99 | # Parameters specified in constructor call override those |
| 100 | # fetched from the database. |
| 101 | |
| 102 | $self->{'ID'} = ($args{-ID} or |
| 103 | $self->{ID} or |
| 104 | "Unknown"); |
| 105 | $self->{'name'} = ($args{-name} or |
| 106 | $self->{name} or |
| 107 | "Unknown"); |
| 108 | $self->{'class'} = ($args{-class} or |
| 109 | $self->{class} or |
| 110 | "Unknown"); |
| 111 | $self->{'strand'} = ($args{-strand} or |
| 112 | $self->{strand} or |
| 113 | "+"); |
| 114 | $self->{'bg_probabilities'} = |
| 115 | ($args{'-bg_probabilities'} || {A => 0.25, |
| 116 | C => 0.25, |
| 117 | G => 0.25, |
| 118 | T => 0.25}); |
| 119 | |
| 120 | $self->{'tags'} = $args{-tags} ? ((refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($args{-tags}) eq "HASH") ? $args{-tags} : {} ) :{}; |
| 121 | |
| 122 | return $self; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | =head2 matrix |
| 128 | |
| 129 | Title : matrix |
| 130 | Usage : my $matrix = $pwm->matrix(); |
| 131 | $pwm->matrix( [ [12, 3, 0, 0, 4, 0], |
| 132 | [ 0, 0, 0,11, 7, 0], |
| 133 | [ 0, 9,12, 0, 0, 0], |
| 134 | [ 0, 0, 0, 1, 1,12] |
| 135 | ]); |
| 136 | |
| 137 | Function: get/set for the matrix data |
| 138 | Returns : a reference to 2D array of integers(PFM) or floats (ICM, PWM) |
| 139 | Args : none for get; |
| 140 | a four line string, reference to 2D array, or a 2D piddle for set |
| 141 | |
| 142 | =cut |
| 143 | |
| 144 | |
| 145 | sub matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/matrix(). { |
| 146 | my ($self, $matrixdata) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 147 | $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix().($matrixdata) if $matrixdata; |
| 148 | return $self->{'matrix'}; |
| 149 | } |
| 150 | |
| 151 | =head2 pdl_matrix |
| 152 | |
| 153 | Title : pdl_matrix |
| 154 | Usage : my $pdl = $pwm->pdl_matrix(); |
| 155 | Function: access the PDL matrix used to store the actual |
| 156 | matrix data directly |
| 157 | Returns : a PDL object, aka a piddle |
| 158 | Args : none |
| 159 | |
| 160 | =cut |
| 161 | |
| 162 | sub pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pdl_matrix(). { |
| 163 | pdl $_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.[0]->{'matrix'}; |
| 164 | } |
| 165 | |
| 166 | sub set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/set_matrix(). { |
| 167 | my ($self, $matrixdata) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 168 | |
| 169 | # The input matrix (specified as -array=> in the constructir call |
| 170 | # can either be |
| 171 | # * a 2D regular perl array with 4 rows, |
| 172 | # * a piddle (FIXME - check for 4 rows), or |
| 173 | # * a four-line string of numbers |
| 174 | |
| 175 | # print STDERR "MATRIX>>>".$matrixdata; |
| 176 | if (refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata) eq "ARRAY" |
| 177 | and refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata->[0]) eq "ARRAY" |
| 178 | and scalarscip-perl cpan TFBS v0.5.0 TFBS/Matrix/scalar().(@{$matrixdata}) == 4) |
| 179 | { |
| 180 | # it is a perl array |
| 181 | $self->{'matrix'} = $matrixdata; |
| 182 | } |
| 183 | elsif (refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata) eq "PDL") |
| 184 | { |
| 185 | # it's a piddle |
| 186 | $self->{matrix} = _pdl_to_matrixrefscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_pdl_to_matrixref().($matrixdata); |
| 187 | } |
| 188 | elsif (!refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ref().($matrixdata)) |
| 189 | #and (scalar split "\n",$matrixdata) == 4) |
| 190 | { |
| 191 | # it's a string then, but what string? |
| 192 | if ($matrixdata =~ /^DE/) { |
| 193 | # STAMP string |
| 194 | $self->{matrix} = $self->_matrix_from_STAMP_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_STAMP_string().($matrixdata); |
| 195 | } elsif ($matrixdata =~ /[ACGTURYMKWSBDHVNacgturymkwsbdhvn]{3,}/) { |
| 196 | $self->{matrix} = $self->_matrix_from_IUPAC_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_IUPAC_string().($matrixdata); |
| 197 | } else { |
| 198 | # Regular string |
| 199 | $self->{matrix} = $self->_matrix_from_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_string().($matrixdata); |
| 200 | } |
| 201 | } |
| 202 | else { |
| 203 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("Wrong data type/format for -matrix.\n". |
| 204 | "Acceptable formats are Array of Arrays (4 rows),\n". |
| 205 | "PDL Array, (4 rows),\n". |
| 206 | "or plain string (4 lines)."); |
| 207 | } |
| 208 | # $self->_set_min_max_score(); |
| 209 | |
| 210 | # print STDERR $self->prettyprint(); |
| 211 | |
| 212 | return 1; |
| 213 | |
| 214 | } |
| 215 | |
| 216 | sub _matrix_from_IUPAC_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_IUPAC_string(). { |
| 217 | my ($self, $matrixstring) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 218 | $matrixstring =~ s/^\s+//; |
| 219 | $matrixstring =~ s/\s+$//; |
| 220 | my @str = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().(//, $matrixstring); |
| 221 | my @matrix; |
| 222 | |
| 223 | for my $ltr (0..3) { |
| 224 | my @l = mapscip-perl cpan TFBS v0.5.0 TFBS/Matrix/map(). {$dna{ucscip-perl cpan TFBS v0.5.0 TFBS/Matrix/uc().($_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.)}->[$ltr]} @str; |
| 225 | pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @matrix, \@l; |
| 226 | } |
| 227 | |
| 228 | return \@matrix; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | sub _matrix_from_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_string(). { |
| 234 | my ($self, $matrixstring) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 235 | my @array = (); |
| 236 | foreach ((splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split(). "\n", $matrixstring)[0..3]) { |
| 237 | s/^\s+//; |
| 238 | s/\s+$//; |
| 239 | pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @array, [splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().]; |
| 240 | } |
| 241 | return \@array; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | sub _matrix_from_STAMP_stringscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_matrix_from_STAMP_string(). { |
| 246 | my ($self, $matrixstring) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 247 | my @lines = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().("\n", $matrixstring); |
| 248 | my (@a, @c, @g, @t); |
| 249 | |
| 250 | # Remove garbage |
| 251 | shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().(@lines); |
| 252 | popscip-perl cpan TFBS v0.5.0 TFBS/Matrix/pop().(@lines); |
| 253 | |
| 254 | for (@lines) { |
| 255 | my @l = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().; |
| 256 | pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @a, $l[1]; |
| 257 | pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @c, $l[2]; |
| 258 | pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @g, $l[3]; |
| 259 | pushscip-perl cpan TFBS v0.5.0 TFBS/Matrix/push(). @t, $l[4]; |
| 260 | } |
| 261 | |
| 262 | return [\@a, \@c, \@g, \@t]; |
| 263 | } |
| 264 | |
| 265 | sub _set_min_max_scorescip-perl cpan TFBS v0.5.0 TFBS/Matrix/_set_min_max_score(). { |
| 266 | my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 267 | 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); |
| 268 | $self->{min_score} = sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sum().(minimumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/minimum(). $transpose); |
| 269 | $self->{max_score} = sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sum().(maximumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/maximum(). $transpose); |
| 270 | } |
| 271 | |
| 272 | sub _loadscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_load(). { |
| 273 | my ($self, $field, $value) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 274 | 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::") { |
| 275 | # database retrieval |
| 276 | } |
| 277 | elsif (-d $self->{dbh}) { |
| 278 | # retrieval from .pwm files in a directory |
| 279 | $self->_lookup_in_matrixlistscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_lookup_in_matrixlist().($field, $value) |
| 280 | or do { |
| 281 | warnscip-perl cpan TFBS v0.5.0 TFBS/Matrix/warn(). ("Matrix with $field=>$value not found."); |
| 282 | return undef; |
| 283 | }; |
| 284 | my $ID = $self->{ID}; |
| 285 | my $DIR = $self->{dbh}; |
| 286 | $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 |
| 287 | |
| 288 | } |
| 289 | else { |
| 290 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/throw().("-dbh is not a valid database handle or a directory."); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | =head2 revcom |
| 296 | |
| 297 | Title : revcom |
| 298 | Usage : my $revcom_pfm = $pfm->revcom(); |
| 299 | Function: create a matrix pattern object which is reverse complement |
| 300 | of the current one |
| 301 | Returns : a TFBS::Matrix::* object of the same type as the one |
| 302 | the method acted upon |
| 303 | Args : none |
| 304 | |
| 305 | =cut |
| 306 | |
| 307 | sub revcomscip-perl cpan TFBS v0.5.0 TFBS/Matrix/revcom(). { |
| 308 | my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 309 | my $revcom_matrix = |
| 310 | $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'), |
| 311 | # the above line rotates the original matrix 180 deg, |
| 312 | -ID => ($self->{ID} or ""), |
| 313 | -name => ($self->{name} or ""), |
| 314 | -class => ($self->{class} or ""), |
| 315 | -strand => ($self->{strand} and $self->{strand} eq "-") ? "+" : "-", |
| 316 | -tags => ($self->{tags} or {}) ); |
| 317 | return $revcom_matrix; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | =head2 rawprint |
| 322 | |
| 323 | Title : rawprint |
| 324 | Usage : my $rawstring = $pfm->rawprint); |
| 325 | Function: convert matrix data to a simple tab-separated format |
| 326 | Returns : a four-line string of tab-separated integers or floats |
| 327 | Args : none |
| 328 | |
| 329 | =cut |
| 330 | |
| 331 | sub rawprintscip-perl cpan TFBS v0.5.0 TFBS/Matrix/rawprint(). { |
| 332 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().; |
| 333 | 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(). ); |
| 334 | $pwmstring =~ s/\[|\]//g; # lose [] |
| 335 | $pwmstring =~ s/\n /\n/g; # lose leading spaces |
| 336 | my @pwmlines = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().("\n", $pwmstring); # f |
| 337 | $pwmstring = joinscip-perl cpan TFBS v0.5.0 TFBS/Matrix/join(). ("\n", @pwmlines[2..5])."\n"; |
| 338 | return $pwmstring; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | =head2 prettyprint |
| 343 | |
| 344 | Title : prettyprint |
| 345 | Usage : my $prettystring = $pfm->prettyprint(); |
| 346 | Function: convert matrix data to a human-readable string format |
| 347 | Returns : a four-line string with nucleotides and aligned numbers |
| 348 | Args : none |
| 349 | |
| 350 | =cut |
| 351 | |
| 352 | sub prettyprintscip-perl cpan TFBS v0.5.0 TFBS/Matrix/prettyprint(). { |
| 353 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().; |
| 354 | 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(). ); |
| 355 | $pwmstring =~ s/\[|\]//g; # lose [] |
| 356 | $pwmstring =~ s/\n /\n/g; # lose leading spaces |
| 357 | my @pwmlines = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().("\n", $pwmstring); # |
| 358 | @pwmlines = ("A [$pwmlines[2] ]", |
| 359 | "C [$pwmlines[3] ]", |
| 360 | "G [$pwmlines[4] ]", |
| 361 | "T [$pwmlines[5] ]"); |
| 362 | $pwmstring = joinscip-perl cpan TFBS v0.5.0 TFBS/Matrix/join(). ("\n", @pwmlines)."\n"; |
| 363 | return $pwmstring; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | =head2 STAMPprint |
| 368 | |
| 369 | Title : STAMPprint |
| 370 | Usage : my $STAMPstring = $pfm->STAMPprint(); |
| 371 | Function: Convert the matrix to STAMP readable format |
| 372 | Returns : A string of the matrix in a TRANSFAC like format for STAMP |
| 373 | Args : none |
| 374 | |
| 375 | =cut |
| 376 | |
| 377 | sub STAMPprintscip-perl cpan TFBS v0.5.0 TFBS/Matrix/STAMPprint(). { |
| 378 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().; |
| 379 | 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(). ); |
| 380 | $pwmstring =~ s/\[|\]//g; # lose [] |
| 381 | $pwmstring =~ s/\n\s+/\n/g; # lose leading spaces |
| 382 | my @pwmlines = splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().(/\n+/, $pwmstring); |
| 383 | @pwmlines = mapscip-perl cpan TFBS v0.5.0 TFBS/Matrix/map(). { [ splitscip-perl cpan TFBS v0.5.0 TFBS/Matrix/split().(/\s+/, $_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.) ] } @pwmlines; |
| 384 | |
| 385 | $pwmstring = "DE ".$self->{'ID'}."\t".$self->{'name'}."\t".$self->{'class'}."\n"; |
| 386 | |
| 387 | for my $row (0..$#{ $pwmlines[1] }) { |
| 388 | $pwmstring .= sprintfscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sprintf(). "%02d\t", $row; |
| 389 | $pwmstring .= $pwmlines[$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.]->[$row]."\t" for (1..$#pwmlinesscip-perl cpan TFBS v0.5.0 TFBS/Matrix/`$#pwmlines`.); |
| 390 | $pwmstring .= "\n"; |
| 391 | } |
| 392 | $pwmstring .= "XX\n"; |
| 393 | |
| 394 | return $pwmstring; |
| 395 | } |
| 396 | |
| 397 | =head2 length |
| 398 | |
| 399 | Title : length |
| 400 | Usage : my $pattern_length = $pfm->length; |
| 401 | Function: gets the pattern length in nucleotides |
| 402 | (i.e. number of columns in the matrix) |
| 403 | Returns : an integer |
| 404 | Args : none |
| 405 | |
| 406 | =cut |
| 407 | |
| 408 | sub lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/length(). { |
| 409 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().; |
| 410 | 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); |
| 411 | } |
| 412 | |
| 413 | sub _pdl_to_matrixrefscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_pdl_to_matrixref(). { |
| 414 | my ($matrixdata) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/`@_`.; |
| 415 | unless ($matrixdata->isascip-perl cpan TFBS v0.5.0 TFBS/Matrix/isa().("PDL")) { |
| 416 | diescip-perl cpan TFBS v0.5.0 TFBS/Matrix/die(). "A non-PDL object passed to _pdl_to_matrixref"; |
| 417 | } |
| 418 | my @list = listscip-perl cpan TFBS v0.5.0 TFBS/Matrix/list(). $matrixdata; |
| 419 | my @array; |
| 420 | my $matrix_width = scalarscip-perl cpan TFBS v0.5.0 TFBS/Matrix/scalar().(@list) / 4; |
| 421 | for (0..3) { |
| 422 | 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)]; |
| 423 | } |
| 424 | return \@array; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | =head2 randomize_columns |
| 429 | |
| 430 | Title : randomize_columns |
| 431 | Usage : $pfm->randomize_columns(); |
| 432 | Function: Randomizes the columns of a matrix (in place). |
| 433 | Returns : Nothing |
| 434 | Args : none |
| 435 | |
| 436 | =cut |
| 437 | |
| 438 | sub randomize_columnsscip-perl cpan TFBS v0.5.0 TFBS/Matrix/randomize_columns(). { |
| 439 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/shift().; |
| 440 | my $matrix = $self->{'matrix'}; |
| 441 | my $i = 0; |
| 442 | |
| 443 | # Schwartzian Transform to get random permutation |
| 444 | mapscip-perl cpan TFBS v0.5.0 TFBS/Matrix/map(). { ( undef, $$matrix[0][$i], $$matrix[1][$i], $$matrix[2][$i], $$matrix[3][$i] ) = @$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.; $i++; } |
| 445 | sortscip-perl cpan TFBS v0.5.0 TFBS/Matrix/sort(). { $ascip-perl cpan TFBS v0.5.0 TFBS/Matrix/$a.->[0] <=> $bscip-perl cpan TFBS v0.5.0 TFBS/Matrix/$b.->[0] } |
| 446 | mapscip-perl cpan TFBS v0.5.0 TFBS/Matrix/map(). { [ randscip-perl cpan TFBS v0.5.0 TFBS/Matrix/rand().(), $$matrix[0][$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.], $$matrix[1][$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.], $$matrix[2][$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.], $$matrix[3][$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/$_.] ] } ( 0 .. ($self->lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/length().()-1) ); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | sub DESTROYscip-perl cpan TFBS v0.5.0 TFBS/Matrix/DESTROY(). { |
| 451 | # nothing |
| 452 | } |
| 453 | |
| 454 | |
| 455 | |
| 456 | 1; |
| 457 | |
| 458 | |
| 459 | |
| 460 |