Debian Codegraph @

TFBS/MatrixSet.pm

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # TFBS module for TFBS::MatrixSet
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::Set - an agregate class representing a set of matrix patterns, containing methods for manipulating the set as a whole
13
14 =head1 SYNOPSIS
15
16 # creation of a TFBS::MatrixSet object
17 # let @list_of_matrix_objects be a list of TFBS::Matrix::* objects
18
19 ###################################
20 # Create a TFBS::MatrixSet object:
21
22 my $matrixset = TFBS::MatrixSet->new(); # creates an empty set
23 $matrixset->add_Matrix(@list_of_matrix_objects); #add matrix objects to set
24 $matrixset->add_Matrix($matrixobj); # adds a single matrix object to set
25
26 # or, same as above:
27
28 my $matrixset = TFBS::MatrixSet->new(@list_of_matrix_objects, $matrixobj);
29
30 ###################################
31 #
32
33
34 =head1 DESCRIPTION
35
36 TFBS::MatrixSet is an aggregate class storing a set of TFBS::Matrix::* subclass objects, and providing methods form manipulating those sets as a whole. TFBS::MatrixSet objects are created <I>de novo<I> or returned by some database (TFBS::DB::*) retrieval methods.
37
38 =head1 FEEDBACK
39
40 Please send bug reports and other comments to the author.
41
42 =head1 AUTHOR - Boris Lenhard
43
44 Boris Lenhard E<lt>[email protected]<gt>
45 Modified by Eivind Valen [email protected]
46
47 =head1 APPENDIX
48
49 The rest of the documentation details each of the object
50 methods. Internal methods are preceded with an underscore.
51
52 =cut
53
54
55
56 # The code begins HERE:
57
58 package TFBS::MatrixSetscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/;
59 use varsscip-perl cpan TFBS v0.5.0 vars/ '@ISA';
60
61 use PDLscip-perl cpan TFBS v0.5.0 PDL/;
62 use Bio::Seqscip-perl cpan TFBS v0.5.0 Bio/Seq/;
63 use Bio::SeqIOscip-perl cpan TFBS v0.5.0 Bio/SeqIO/;
64 use Bio::Root::Rootscip-perl cpan TFBS v0.5.0 Bio/Root/Root/;
65 use Bio::TreeIOscip-perl cpan TFBS v0.5.0 Bio/TreeIO/;
66 use File::Tempscip-perl cpan TFBS v0.5.0 File/Temp/ qw/:POSIX/;
67
68 use TFBS::Matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/;
69 use TFBS::_Iterator::_MatrixSetIteratorscip-perl cpan TFBS v0.5.0 TFBS/_Iterator/_MatrixSetIterator/;
70 use TFBS::SiteSetscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/;
71
72 use strictscip-perl cpan TFBS v0.5.0 strict/;
73
74 use constantscip-perl cpan TFBS v0.5.0 constant/ TRUEscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/TRUE(). => 1;
75 use constantscip-perl cpan TFBS v0.5.0 constant/ FALSEscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/FALSE(). => 0;
76
77 @ISAscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@ISA`. = qw(Bio::Root::Root);
78
79
80 # Hash of accepted options and their arguments for the program
81 # STAMP. Reference to empty list means the option take no arguments
82 # This test for legal arguments is maybe superfluous and can
83 # potentially be removed.
84 my %stamp_opt = (
85 -tf => [],
86 -sd => [],
87 -cc => [ "PCC", "ALLR", "ALLR_LL", "CS", "KL", "SSD" ],
88 -align => [ "NW", "SW", "SWA", "SWU" ],
89 -go => [],
90 -ge => [],
91 -out => [],
92 -overlapalign => [],
93 -nooverlapalign => [],
94 -extendedoverlap => [],
95 -printpairwise => [],
96 -tree => [ "UPGMA", "SOTA" ],
97 -ch => [],
98 -ma => [ "PPA", "IR" ],
99 -match => [],
100 -matchtop => [],
101 -prot => [],
102 -genrand => [],
103 -genscores => [],
104 -stampdir => [],
105 -tempdir => [],
106 -noclean => []
107 );
108
109
110
111
112 =head2 new
113
114 =cut
115
116
117 sub newscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/new(). {
118 my ($caller, @matrices) = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift().;
119 my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/bless(). {matrix_list =>[]}, refscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/ref().($caller) || $caller;
120 $self->add_matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix().(@matrices) if @matrices;
121 return $self;
122 }
123
124
125 =head2 new2
126
127 =cut
128
129
130 sub new2scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/new2(). {
131 my $class = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift().;
132 my %args = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
133 my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/bless(). {}, refscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/ref().($class) || $class;
134
135 if (definedscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/defined(). $args{'-matrices'}) {
136 $self->add_matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix().( @{$args{'-matrices'}} ) if @{$args{'-matrices'}};
137 }
138
139 if (definedscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/defined(). $args{'-matrixfile'}) {
140 my @matrices;
141
142 openscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/open(). (FILE, $args{-matrixfile})
143 or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/throw().("Could not open $args{-matrixfile}");
144
145 while (<FILE>) {
146 /^\s*$/ && next;
147 if (/^>/) {
148
149 }
150 }
151 closescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/close().(FILE);
152
153
154 }
155
156 return $self;
157 }
158
159
160 =head2 add_matrix
161
162 Title : add_matrix
163 Usage : $matrixset->add_matrix(@list_of_matrix_objects);
164 Function: Adds matrix objects to matrixset
165 Returns : object reference (usually ignored)
166 Args : one or more TFBS::Matrix::* objects
167
168 =cut
169
170 sub add_matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix(). {
171 my ($self, @matrices) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
172 foreach my $matrix (@matrices) {
173 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/throw().("Argument to add_matrix_set not a TFBS::Matrix object")
174 unless $matrix->isascip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/isa().("TFBS::Matrix");
175 }
176 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @{$self->{matrix_list}}, @matrices;
177 return $self;
178 }
179
180
181 sub add_Matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_Matrix(). {
182 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift().;
183 return $self->add_matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix().(@_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.);
184 }
185
186
187 =head2 add_matrix_set
188
189 Title : add_matrix
190 Usage : $matrixset->add_matrix(@list_of_matrixset_objects);
191 Function: Adds to the matrixset matrix objects contained in one or
192 more other matrixsets
193 Returns : object reference (usually ignored)
194 Args : one or more TFBS::MatrixSet objects
195
196 =cut
197
198
199 sub add_matrix_setscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix_set(). {
200 my ($self, @sets) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
201 foreach my $matrixset (@sets) {
202 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/throw().("Argument to add_matrix_set not a TFBS::Matrixset object")
203 unless $matrixset->isascip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/isa().("TFBS::MatrixSet");
204 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @{$self->{matrix_list}}, @{$matrixset->{matrix_list}};
205 }
206 }
207
208 sub resetscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/reset(). {
209 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
210 $self->warnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/warn().("reset: Deprecated method use Iterator instead.");
211 @{$self->{_iterator_list}} = @{$self->{matrix_list}};
212 }
213
214 sub sort_by_namescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/sort_by_name(). {
215 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
216 $self->warnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/warn().("sort_by_name: Deprecated method use Iterator instead.");
217 @{$self->{matrix_list}} = sortscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/sort(). { ucscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/uc().($ascip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$a.->{name}) cmp ucscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/uc(). ($bscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$b.->{name}) }
218 @{$self->{matrix_list}};
219 $self->resetscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/reset().();
220 }
221
222 sub nextscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next(). {
223 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
224 $self->warnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/warn().("next: Deprecated method use Iterator instead.");
225 if (my $next_matrix = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift(). (@{$self->{_iterator_list}})) {
226 return $next_matrix;
227 }
228 else {
229 $self->resetscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/reset().;
230 return undef;
231 }
232 }
233
234
235 =head2 search_seq
236
237 Title : search_seq
238 Usage : my $siteset = $matrixset->search_seq(%args)
239 Function: scans a nucleotide sequence with all patterns represented
240 stored in $matrixset;
241
242 It works only if all matrix objects in $matrixset understand
243 search_seq method (currently only TFBS::Matrix::PWM objects do)
244 Returns : a TFBS::SiteSet object
245 Args : # you must specify either one of the following three:
246
247 -file, # the name od a fasta file (single sequence)
248 #or
249 -seqobj # a Bio::Seq object
250 # (more accurately, a Bio::PrimarySeqobject or a
251 # subclass thereof)
252 #or
253 -seqstring # a string containing the sequence
254
255 -threshold, # minimum score for the hit, either absolute
256 # (e.g. 11.2) or relative (e.g. "75%")
257 # OPTIONAL: default "80%"
258
259 =cut
260
261
262 sub search_seqscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/search_seq(). {
263 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
264 $self->_searchscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_search().(%args);
265 }
266
267
268 =head2 search_aln
269
270 Title : search_aln
271 Usage : my $site_pair_set = $matrixset->search_aln(%args)
272 Function: Scans a pairwise alignment of nucleotide sequences
273 with the pattern represented by the PWM: it reports only
274 those hits that are present in equivalent positions of both
275 sequences and exceed a specified threshold score in both, AND
276 are found in regions of the alignment above the specified
277 conservation cutoff value.
278 It works only if all matrix object in $matrixset understand
279 search_aln method (currently only TFBS::Matrix::PWM objects do)
280
281 Returns : a TFBS::SitePairSet object
282 Args : # you must specify either one of the following three:
283
284 -file, # the name of the alignment file in Clustal
285 format
286 #or
287 -alignobj # a Bio::SimpleAlign object
288 # (more accurately, a Bio::PrimarySeqobject or a
289 # subclass thereof)
290 #or
291 -alignstring # a multi-line string containing the alignment
292 # in clustal format
293 #############
294
295 -threshold, # minimum score for the hit, either absolute
296 # (e.g. 11.2) or relative (e.g. "75%")
297 # OPTIONAL: default "80%"
298
299 -window, # size of the sliding window (inn nucleotides)
300 # for calculating local conservation in the
301 # alignment
302 # OPTIONAL: default 50
303
304 -cutoff # conservation cutoff (%) for including the
305 # region in the results of the pattern search
306 # OPTIONAL: default "70%"
307
308 -subpart # subpart of the alignment to search, given as e.g.
309 # -subpart => { relative_to => 1,
310 # start => 140,
311 # end => 180 }
312 # where start and end are coordinates in the
313 # sequence indicated by relative_to (1 for the
314 # 1st sequence in the alignment, 2 for the 2nd)
315 # OPTIONAL: by default searches entire alignment
316
317 -conservation
318 # conservation profile, a TFBS::ConservationProfile
319 # OPTIONAL: by default the conservation profile is
320 # computed internally on the fly (less efficient)
321
322 =cut
323
324 sub search_alnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/search_aln(). {
325 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
326 my $mxit = $self->Iteratorscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/Iterator().();
327 my $sitepairset = TFBS::SitePairSet->newscip-perl cpan TFBS v0.5.0 TFBS/SitePairSet/new().;
328 my $aln = TFBS::Matrix::_Alignment->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/_Alignment/new().(%args);
329 while (my $mx = $mxit->nextscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next().) {
330 my $singleset = $mx->search_alnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/search_aln().(%args,
331 -alignment_setup => $aln);
332 $sitepairset->add_site_pair_setscip-perl cpan TFBS v0.5.0 TFBS/SitePairSet/add_site_pair_set().($singleset);
333 }
334 return $sitepairset;
335 }
336
337
338
339 =head2 size
340
341 Title : size
342 Usage : my $number_of_matrices = $matrixset->size;
343 Function: gets the number of matrix objects in the $matrixset
344 (i.e. the size of the set)
345 Returns : a number
346 Args : none
347
348 =cut
349
350
351 sub sizescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/size(). {
352 scalarscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/scalar(). @{ $_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.[0]->{matrix_list} };
353 }
354
355
356 =head2 Iterator
357
358 Title : Iterator
359 Usage : my $matrixset_iterator =
360 $matrixset->Iterator(-sort_by =>'total_ic');
361 while (my $matrix_object = $matrix_iterator->next) {
362 # do whatever you want with individual matrix objects
363 }
364 Function: Returns an iterator object that can be used to go through
365 all members of the set
366 Returns : an iterator object (currently undocumentened in TFBS -
367 but understands the 'next' method)
368 Args : -sort_by # optional - currently it accepts
369 # 'ID' (alphabetically)
370 # 'name' (alphabetically)
371 # 'class' (alphabetically)
372 # 'total_ic' (numerically, decreasing order)
373
374 -reverse # optional - reverses the default sorting order if true
375
376 =cut
377
378 sub Iteratorscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/Iterator(). {
379
380 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
381 return TFBS::_Iterator::_MatrixSetIterator->newscip-perl cpan TFBS v0.5.0 TFBS/_Iterator/_MatrixSetIterator/new().($self->{matrix_list},
382 $args{'-sort_by'},
383 $args{'-reverse'}
384 );
385 }
386
387
388
389 =head2 randomize_columns
390
391 Title : randomize_columns
392 Usage : $matrixset->randomize_columns();
393 Function: Randomizes the columns between all the matrices in the set (in place).
394 Returns : nothing
395 Args : none
396
397 =cut
398
399
400 sub randomize_columnsscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/randomize_columns(). {
401 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift().;
402 my (@lengths, @concat);
403 my ($length, $i) = (-1, 0);
404
405 # Concatenate to one big matrix
406 for my $matrix (@{$self->{matrix_list}}) {
407 $length += $matrix->lengthscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/length().();
408 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @lengths, $matrix->lengthscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/length().();
409 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @{$concat[$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.]}, @{${$matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/matrix().->matrix()}[$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.]} for (0..3);
410 }
411
412 # Schwartzian transform to get random permutation
413 mapscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/map(). { ( undef, $concat[0][$i], $concat[1][$i], $concat[2][$i], $concat[3][$i] ) = @$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.; $i++; }
414 sortscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/sort(). { $ascip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$a.->[0] <=> $bscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$b.->[0] }
415 mapscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/map(). { [ randscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/rand().(), $concat[0][$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.], $concat[1][$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.], $concat[2][$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.], $concat[3][$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.] ] } ( 0 .. $length );
416
417 # Split it up again
418 my $start = 0;
419 for my $matrix (@{$self->{matrix_list}}) {
420 my $length = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift().(@lengths);
421 my $end = $start + $length - 1;
422
423 $matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/matrix().->matrix( [
424 [ @{$concat[0]}[$start..$end] ],
425 [ @{$concat[1]}[$start..$end] ],
426 [ @{$concat[2]}[$start..$end] ],
427 [ @{$concat[3]}[$start..$end] ]
428 ]
429 );
430 $start += $length;
431 }
432
433 }
434
435
436 sub _searchscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_search(). {
437
438 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
439
440 # DIRTY - stick tmp file name to seq object
441
442 my $seqobj = $self->_to_seqobjscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_to_seqobj().(%args);
443 ($seqobj->{_fastaFH}, $seqobj->{_fastafile}) = tmpnamscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/tmpnam().();
444 # we need $fastafile below
445
446 my $outstream = Bio::SeqIO->newscip-perl cpan TFBS v0.5.0 Bio/SeqIO/new().(-file=>">".$seqobj->{_fastafile}, -format=>"Fasta");
447 my $subseqobj;
448 if(my $subpart = $args{-subpart}) {
449 my $subseq_start = $subpart->{-start};
450 my $subseq_end = $subpart->{-end};
451 unless($subseq_start and $subseq_end) {
452 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/throw().("Option -subpart missing suboption -relative_to, -start or -end");
453 }
454 $subseqobj = Bio::Seq->newscip-perl cpan TFBS v0.5.0 Bio/Seq/new().(-seq => $seqobj->subseqscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/subseq().($subseq_start, $subseq_end),
455 -id => $seqobj->idscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/id().);
456 }
457 $outstream->write_seq($subseqobj or $seqobj);
458 $outstream->closescip-perl cpan TFBS v0.5.0 Bio/SeqIO/close().;
459
460 # iterate through pwms
461 my @PWMs;
462 my $mxit = $self->Iteratorscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/Iterator().();
463
464 while (my $pwm = $mxit->nextscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next().() ) {
465 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @PWMs,$pwm;
466 }
467
468 # do the analysis
469
470 my $hitlist = TFBS::SiteSet->newscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/new().();
471
472 foreach my $pwm (@PWMs) {
473 my $threshold = ($args{-threshold} or $pwm->{minscore});
474 $hitlist->add_sitesetscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/add_siteset().($pwm->search_seqscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/search_seq().(-seqobj=>$seqobj,
475 -threshold =>$threshold,
476 -subpart=>$args{-subpart}));
477 }
478 deletescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/delete(). $seqobj->{_fastaFH};
479 unlinkscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/unlink(). $seqobj->{_fastafile};
480 deletescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/delete(). $seqobj->{_fastafile};
481 return $hitlist;
482 }
483
484
485
486 sub _csearchscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_csearch(). {
487
488 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
489 my $PWM_SEARCH = '/home/httpd/cgi-bin/CONSITE/bin/pwm_searchPFF';
490
491 # DIRTY - stick tmp file name to seq object
492
493 my $seqobj = $self->_to_seqobjscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_to_seqobj().(%args);
494 ($seqobj->{_fastaFH}, $seqobj->{_fastafile}) = tmpnamscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/tmpnam().();
495 # we need $fastafile below
496
497 my $seqFH = Bio::SeqIO->newFhscip-perl cpan TFBS v0.5.0 Bio/SeqIO/newFh().(-fh=>$seqobj->{_fastaFH}, -format=>"Fasta");
498 print $seqFH $seqobj;
499
500
501 # iterate through pwms
502 my @PWMs;
503 $self->resetscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/reset().();
504
505 while (my $pwm = $self->nextscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next().() ) {
506 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @PWMs,$pwm;
507 }
508
509 # do the analysis
510
511 my $hitlist = TFBS::SiteSet->newscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/new().();
512
513 foreach my $pwm (@PWMs) {
514 my $threshold = ($args{-threshold} or $pwm->{minscore});
515 $hitlist->add_sitesetscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/add_siteset().($pwm->search_seqscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/search_seq().(-seqobj=>$seqobj,
516 -threshold =>$threshold ));
517 }
518 deletescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/delete(). $seqobj->{_fastaFH};
519 deletescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/delete(). $seqobj->{_fastafile};
520 return $hitlist;
521
522 }
523
524
525
526 sub _bsearchscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_bsearch(). {
527 my ($self,%args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.; #the rest of @_ goes to _to_seqob;
528 my @PWMs;
529
530 # prepare the sequence
531
532 my $seqobj = $self->_to_seqobjscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_to_seqobj().(%args);
533 $seqobj->{_pdl_matrix} = _seq_to_pdlmatrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_seq_to_pdlmatrix().($seqobj);
534
535 # prepare the PWMs
536
537 $self->resetscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/reset().();
538
539 while (my $pwm = $self->nextscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next().() ) {
540 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @PWMs,$pwm;
541 }
542
543 # do the analysis
544
545 my $hitlist = TFBS::SiteSet->newscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/new().();
546
547 foreach my $pwm (@PWMs) {
548 my $threshold = ($args{-threshold} or $pwm->{minscore});
549 $hitlist->add_sitesetscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/add_siteset().($pwm->bsearchscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/bsearch().(-seqobj=>$seqobj,
550 -threshold =>$threshold ));
551 }
552 deletescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/delete(). $seqobj->{_pdl_matrix};
553 return $hitlist;
554 }
555
556 sub _seq_to_pdlmatrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_seq_to_pdlmatrix(). {
557
558 # not OO - help function for search
559
560 my $seqobj = shiftscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/shift().;
561 my $seqstring = ucscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/uc().($seqscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/seq().obj->seq());
562
563 my @perlarray;
564 foreach (qw(A C G T)) {
565 my $seqtobits = $seqstring;
566 eval "\$seqtobits =~ tr/$_/1/"; # curr. letter $_ to 1
567 eval "\$seqtobits =~ tr/1/0/c"; # non-1s to 0
568 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @perlarray, [splitscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/split().("", $seqtobits)];
569 }
570 return bytescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/byte(). (\@perlarray);
571 }
572
573
574
575 sub _to_seqobjscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_to_seqobj(). {
576 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
577
578 my $seq;
579 if ($args{-file}) { # not a Bio::Seq
580 return Bio::SeqIO->newscip-perl cpan TFBS v0.5.0 Bio/SeqIO/new().(-file => $args{-file},
581 -format => 'fasta',
582 -moltype => 'dna')->next_seqscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next_seq().();
583 }
584 elsif ($args{-seqstring}
585 or $args{-seq})
586 { # I guess it's a string then
587 return Bio::Seq->newscip-perl cpan TFBS v0.5.0 Bio/Seq/new().(-seq => ($args{-seqstring} or $args{-seq}),
588 -id => ($args{-seq_id} or "undefined"),
589 -moltype => 'dna');
590 }
591 elsif ($args{'-seqobj'} and refscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/ref().($args{'-seqobj'}) =~ /Bio\:\:Seq/) {
592 # do nothing (maybe check later)
593 return $args{'-seqobj'};
594 }
595 #elsif (ref($format) =~ /Bio\:\:Seq/ and !defined $seq) {
596 # if only one parameter passed and it's a Bio::Seq
597 #return $format;
598 #}
599 else {
600 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/throw(). ("Wrong parameters passed to search method: ".%args);
601 }
602
603
604 # CONTINUE HERE TOMORROW
605
606 }
607
608
609
610
611 =head2 remove_Matrix_by_ID
612
613 Title : remove_Matrix_by_ID
614 Usage : $matrixset->remove_Matrix_by_ID($id);
615 Function: Removes a matrix from the set
616 Returns : Nothing
617 Args : None
618
619 =cut
620
621 sub remove_Matrix_by_IDscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/remove_Matrix_by_ID(). {
622 my ($self, $id) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
623
624 my @list = grepscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/grep(). { $_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.->IDscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/ID().() ne $id } @{$self->{matrix_list}};
625 $self->{matrix_list} = \@list;
626 }
627
628 my $error;
629
630
631 sub _check_optscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_check_opt(). {
632 my ($self, $opt, $arg, $list) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
633
634 # Invalid argument
635 if (not definedscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/defined().($list)) {
636 $error = "Invalid argument: $opt\n";
637 return FALSE;
638 }
639
640 # Valid flag or switch.
641 return TRUE if (not scalarscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/scalar().(@$list));
642
643
644 # Valid switch, check the argument
645 for (@$list) {
646 return TRUE if ($arg eq $_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.) ;
647 }
648
649 # Valid switch, invalid argument
650 $error = "$arg is invalid argument to $opt";
651 return FALSE;
652 }
653
654
655 sub _find_optimalscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_find_optimal(). {
656 my ($self, $output) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
657 my ($optimal, $score_best, $in) = (undef, undef, 0);
658
659 for (@$output) {
660 if (/NumClust/) {
661 $in = 1;
662 next;
663 }
664
665 last if (/Tree Built/);
666
667 if ($in) {
668 my (undef, $clusters, $score) = splitscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/split().(/\t/);
669
670 if ((not definedscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/defined().($score_best)) || $score < $score_best) {
671 $score_best = $score;
672 $optimal = $clusters;
673 }
674 }
675 }
676
677 return $optimal;
678 }
679
680
681 sub _run_STAMPscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_run_STAMP(). {
682 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
683 my $fh;
684
685 for (keysscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/keys().(%args)) {
686 diescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/die(). $error unless ($self->_check_optscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_check_opt().($_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_., $args{$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.}, $stamp_opt{$_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.}));
687 }
688
689 # Write matrices to temporary file
690 if (not existsscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/exists().($args{-tf})) {
691 $fh = new File::Temp( TEMPLATE => 'STAMP-XXXXX',
692 DIR => $args{-tempdir} || '/tmp',
693 SUFFIX => '.set');
694
695 print $fh $_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.->STAMPprintscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/STAMPprint().() for (@{$self->{matrix_list}});
696 $args{-tf} = $fh->filenamescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/filename().();
697 }
698 # Set some default options
699 $args{-tree} ||= "UPGMA";
700 $args{-ma} ||= "IR";
701 $args{-cc} ||= "PCC";
702 $args{-align} ||= "SWU";
703
704
705 # Make sure we find all files
706 my $path;
707 if ($args{-stampdir}) {
708 $path = $args{-stampdir};
709 diescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/die(). "Could not find STAMP at $path\n" if (not -e "$path/STAMP");
710 } else {
711 $path = (grepscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/grep(). {-e "$_/STAMP"} splitscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/split().(/:+/, $ENVscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`%ENV`.{PATH}))[0];
712 $path || diescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/die(). "Could not find STAMP in path\n";
713 }
714
715 $args{-sd} ||= $path."/ScoreDists/JaspRand_".$args{-cc}."_".$args{-align}.".scores";
716 diescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/die(). "No score distribution file found or not readable at '$args{-sd}'.\n Use -sd.\n" unless (-r $args{-sd});
717
718 # Execute STAMP
719 my $args = "";
720 $args .= "$_ $args{$_} " for (keysscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/keys().(%args));
721 my @output = `$path/STAMP -ch $args -out $fh`;
722
723 # Get tree
724 my $treeio = new Bio::TreeIO(-format => 'newick', -file => $fh->filenamescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/filename().().".tree");
725 my $tree = $treeio->next_treescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/next_tree().;
726
727 # Get FBP
728 my $fbp = TFBS::Matrix::PFM->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/new().(-matrixfile => $fh->filenamescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/filename().()."FBP.txt");
729 $fbp->{'filename'} = $fh->filenamescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/filename().()."FBP.txt";
730
731 print STDERR "::: $fbp->{'filename'} \n";
732
733 if (not $args{-noclean}) {
734 my $deleted = unlinkscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/unlink().($fh->filenamescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/filename().()."FBP.txt", $fh->filenamescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/filename().().".tree");
735 warnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/warn().("Couldn't remove temporary files") if ($deleted != 2);
736 }
737
738 return ($fh, \@output, $tree, $fbp);
739 }
740
741
742
743 sub _build_clusterscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_build_cluster(). {
744 my ($self, $cluster, $node) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
745
746 if ($node->is_Leafscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/is_Leaf().()) {
747 for (@{$self->{matrix_list}}) {
748 if ($_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.->IDscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/ID().() eq $node->idscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/id().()) {
749 $cluster->add_matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_matrix().($_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.);
750 return;
751 }
752 }
753 } else {
754 $self->_build_clusterscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_build_cluster().($cluster, $_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.) for ($node->each_Descendentscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/each_Descendent().());
755 }
756 }
757
758
759
760
761 =head2 cluster
762
763 Title : cluster
764 Usage : $matrixset->cluster(%args)
765 Function: Clusters the matrices in the set
•766 Returns : The root node of the hierarchical clustering tree.
767 An integer specifying the optimal number of clusters.
768 An array of TFBS::MatrixSets, one for each cluster.
769 Args : Many:
770 -stampdir Directory where stamp is located. Not necessary if it is in the PATH.
771 -tempdir Directory to put temporary files. Defaults to "/tmp"
772 -noclean 0 to clean up temporary files, 1 otherwise
773 -tree Method for constructing tree (UPGMA/SOTA). Def:UPGMA
774
775 =cut
776
777 sub clusterscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/cluster(). {
778 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
779
780 if ($self->sizescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/size().() <= 1) {
781 warnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/warn().("Can't cluster MatrixSet of size less than 2");
782 return;
783 }
784
785 my ($fh, $output, $tree, $fbp) = $self->_run_STAMPscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_run_STAMP().(%args);
786
787 # Find optimal cluster number
788 my $optimal = $args{-optimal} || $self->_find_optimalscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_find_optimal().($output);
789 my $root = $tree->get_root_nodescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/get_root_node().();
790
791 my @nodes = ($root);
792 my @leaves;
793
794 # Descend the tree until the optimal cluster number is reached
795 while (scalarscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/scalar().(@nodes) && (scalarscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/scalar().(@nodes) + scalarscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/scalar().(@leaves)) < $optimal) {
796 my $node = popscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/pop(). @nodes;
797
798 if ($node->is_Leafscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/is_Leaf().()) {
799 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @leaves, $node;
800 } else {
801 @nodes = sortscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/sort(). {$ascip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$a.->heightscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/height().() <=> $bscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$b.->heightscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/height().()} (@nodes, $node->each_Descendentscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/each_Descendent().());
802 }
803 }
804
805 # Build the clusters
806 my @clusters;
807 for (@leaves, @nodes) {
808 my $cluster = $self->newscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/new().();
809 $self->_build_clusterscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_build_cluster().($cluster, $_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/$_.);
810 pushscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/push(). @clusters, $cluster;
811 }
812
813
814 return ($tree, $optimal, \@clusters);
815 }
816
817
818
819 =head2 fbp
820
821 Title : fbp
822 Usage : $matrixset->fbp(%args);
823 Function: Creates a familial binding profile (FBP) for the set
824 Returns : A familial binding profile represented as a TFBS::Matrix::PFM
825 Args : Many
826 -stampdir Directory where stamp is located. Not necessary if it is in the PATH.
827 -tempdir Directory to put temporary files. Defaults to "/tmp"
828 -noclean 0 to clean up temporary files, 1 otherwise
829 -align Alignment method
830 =cut
831
832 sub fbpscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/fbp(). {
833 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/`@_`.;
834 if ($self->sizescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/size().() == 0) {
835 warnscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/warn().("Can't create FBP for MatrixSet of size 0");
836 return;
837 } elsif ($self->sizescip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/size().() == 1) {
838 return @{$self->{'matrix_list'}}[0];
839 }
840
841 my ($fh, $output, $tree, $fbp) = $self->_run_STAMPscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/_run_STAMP().(%args);
842
843 return $fbp;
844 }
845
846
847
848
849 1;
850
851
852