/
Analysis.pm
1324 lines (1020 loc) · 40.2 KB
/
Analysis.pm
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# BioPerl module Bio::Restriction::Analysis
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Rob Edwards <redwards@utmem.edu>
#
# You may distribute this module under the same terms as perl itself
## POD Documentation:
=head1 NAME
Bio::Restriction::Analysis - cutting sequences with restriction
enzymes
=head1 SYNOPSIS
# analyze a DNA sequence for restriction enzymes
use Bio::Restriction::Analysis;
use Bio::PrimarySeq;
use Data::Dumper;
# get a DNA sequence from somewhere
my $seq = Bio::PrimarySeq->new
(-seq =>'AGCTTAATTCATTAGCTCTGACTGCAACGGGCAATATGTCTC',
-primary_id => 'synopsis',
-molecule => 'dna');
# now start an analysis.
# this is using the default set of enzymes
my $ra = Bio::Restriction::Analysis->new(-seq=>$seq);
# find unique cutters. This returns a
# Bio::Restriction::EnzymeCollection object
my $enzymes = $ra->unique_cutters;
print "Unique cutters: ", join (', ',
map {$_->name} $enzymes->unique_cutters), "\n";
# AluI is one them. Where does it cut?
# This is will return an array of the sequence strings
my $enz = 'AluI';
my @frags = $ra->fragments($enz);
# how big are the fragments?
print "AluI fragment lengths: ", join(' & ', map {length $_} @frags), "\n";
# You can also bypass fragments and call sizes directly:
# to see all the fragment sizes
print "All sizes: ", join " ", $ra->sizes($enz), "\n";
# to see all the fragment sizes sorted by size like on a gel
print "All sizes, sorted ", join (" ", $ra->sizes($enz, 0, 1)), "\n";
# how many times does each enzyme cut
my $cuts = $ra->cuts_by_enzyme('BamHI');
print "BamHI cuts $cuts times\n";
# How many enzymes do not cut at all?
print "There are ", scalar $ra->zero_cutters->each_enzyme,
" enzymes that do not cut\n";
# what about enzymes that cut twice?
my $two_cutters = $ra->cutters(2);
print join (" ", map {$_->name} $two_cutters->each_enzyme),
" cut the sequence twice\n";
# what are all the enzymes that cut, and how often do they cut
printf "\n%-10s%s\n", 'Enzyme', 'Number of Cuts';
my $all_cutters = $ra->cutters;
map {
printf "%-10s%s\n", $_->name, $ra->cuts_by_enzyme($_->name)
} $all_cutters->each_enzyme;
# Finally, we can interact the restriction enzyme object by
# retrieving it from the collection object see the docs for
# Bio::Restriction::Enzyme.pm
my $enzobj = $enzymes->get_enzyme($enz);
=head1 DESCRIPTION
Bio::Restriction::Analysis describes the results of cutting a DNA
sequence with restriction enzymes.
To use this module you can pass a sequence object and optionally a
Bio::Restriction::EnzymeCollection that contains the enzyme(s) to cut the
sequences with. There is a default set of enzymes that will be loaded
if you do not pass in a Bio::Restriction::EnzymeCollection.
To cut a sequence, set up a Restriction::Analysis object with a sequence
like this:
use Bio::Restriction::Analysis;
my $ra = Bio::Restriction::Analysis->new(-seq=>$seqobj);
or
my $ra = Bio::Restriction::Analysis->new
(-seq=>$seqobj, -enzymes=>$enzs);
Then, to get the fragments for a particular enzyme use this:
@fragments = $ra->fragments('EcoRI');
Note that the naming of restriction enzymes is that the last numbers
are usually Roman numbers (I, II, III, etc). You may want to use
something like this:
# get a reference to an array of unique (single) cutters
$singles = $re->unique_cutters;
foreach my $enz ($singles->each_enzyme) {
@fragments = $re->fragments($enz);
... do something here ...
}
Note that if your sequence is circular, the first and last fragment
will be joined so that they are the appropriate length and sequence
for further analysis. This fragment will also be checked for cuts
by the enzyme(s). However, this will change the start of the
sequence!
There are two separate algorithms used depending on whether your
enzyme has ambiguity. The non-ambiguous algorithm is a lot faster,
and if you are using very large sequences you should try and use
this algorithm. If you have a large sequence (e.g. genome) and
want to use ambgiuous enzymes you may want to make separate
Bio::Restriction::Enzyme objects for each of the possible
alternatives and make sure that you do not set is_ambiguous!
This version should correctly deal with overlapping cut sites
in both ambiguous and non-ambiguous enzymes.
I have tried to write this module with speed and memory in mind
so that it can be effectively used for large (e.g. genome sized)
sequence. This module only stores the cut positions internally,
and calculates everything else on an as-needed basis. Therefore
when you call fragment_maps (for example), there may be another
delay while these are generated.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
https://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR
Rob Edwards, redwards@utmem.edu,
Steve Chervitz, sac@bioperl.org
=head1 CONTRIBUTORS
Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
Mark A. Jensen, maj-at-fortinbras-dot-us
=head1 COPYRIGHT
Copyright (c) 2003 Rob Edwards. Some of this work is Copyright (c)
1997-2002 Steve A. Chervitz. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 SEE ALSO
L<Bio::Restriction::Enzyme>,
L<Bio::Restriction::EnzymeCollection>
=head1 APPENDIX
Methods beginning with a leading underscore are considered private and
are intended for internal use by this module. They are not considered
part of the public interface and are described here for documentation
purposes only.
=cut
package Bio::Restriction::Analysis;
use Bio::Restriction::EnzymeCollection;
use strict;
use Data::Dumper;
use base qw(Bio::Root::Root);
use Scalar::Util qw(blessed);
=head1 new
Title : new
Function : Initializes the restriction enzyme object
Returns : The Restriction::Analysis object
Arguments :
$re_anal->new(-seq=$seqobj,
-enzymes=>Restriction::EnzymeCollection object)
-seq requires a Bio::PrimarySeq object
-enzymes is optional.
If omitted it will use the default set of enzymes
This is the place to start. Pass in a sequence, and you will be able
to get the fragments back out. Several other things are available
like the number of zero cutters or single cutters.
=cut
sub new {
my($class, @args) = @_;
my $self = $class->SUPER::new(@args);
my ($seq,$enzymes) =
$self->_rearrange([qw(
SEQ
ENZYMES
)], @args);
$seq && $self->seq($seq);
$enzymes ? $self->enzymes($enzymes)
: ($self->{'_enzymes'} = Bio::Restriction::EnzymeCollection->new );
# keep track of status
$self->{'_cut'} = 0;
# left these here because we want to reforce a _cut if someone
# just calls new
$self->{maximum_cuts} = 0;
$self->{'_number_of_cuts_by_enzyme'} = {};
$self->{'_number_of_cuts_by_cuts'} = {};
$self->{'_fragments'} = {};
$self->{'_cut_positions'} = {}; # cut position is the real position
$self->{'_frag_map_list'} = {};
return $self;
}
=head1 Methods to set parameters
=cut
=head2 seq
Title : seq
Usage : $ranalysis->seq($newval);
Function : get/set method for the sequence to be cut
Example : $re->seq($seq);
Returns : value of seq
Args : A Bio::PrimarySeqI dna object (optional)
=cut
sub seq {
my $self = shift;
if (@_) {
my $seq = shift;
$self->throw('Need a sequence object ['. ref $seq. ']')
unless $seq->isa('Bio::PrimarySeqI');
$self->throw('Need a DNA sequence object ['. $seq->alphabet. ']')
unless $seq->alphabet eq 'dna';
$self->{'_seq'} = $seq;
$self->{'_cut'} = 0;
}
return $self->{'_seq'};
}
=head2 enzymes
Title : enzymes
Usage : $re->enzymes($newval)
Function : gets/Set the restriction enzyme enzymes
Example : $re->enzymes('EcoRI')
Returns : reference to the collection
Args : an array of Bio::Restriction::EnzymeCollection and/or
Bio::Restriction::Enzyme objects
The default object for this method is
Bio::Restriction::EnzymeCollection. However, you can also pass it a
list of Bio::Restriction::Enzyme objects - even mixed with Collection
objects. They will all be stored into one collection.
=cut
sub enzymes {
my $self = shift;
if (@_) {
$self->{'_enzymes'} = Bio::Restriction::EnzymeCollection->new (-empty => 1)
unless $self->{'_enzymes'};
$self->{'_enzymes'}->enzymes(@_);
$self->{'_cut'} = 0;
}
return $self->{'_enzymes'};
}
=head1 Perform the analysis
=cut
=head2 cut
Title : cut
Usage : $re->cut()
Function : Cut the sequence with the enzymes
Example : $re->cut(); $re->cut('single'); or $re->cut('multiple', $enzymecollection);
Returns : $self
Args : 'single' (optional), 'multiple' with enzyme collection.
An explicit cut method is needed to pass arguments to it.
There are two varieties of cut. Single is the default, and need
not be explicitly called. This cuts the sequence with each
enzyme separately.
Multiple cuts a sequence with more than one enzyme. You must pass
it a Bio::Restriction::EnzymeCollection object of the set
of enzymes that you want to use in the double digest. The results
will be stored as an enzyme named "multiple_digest", so you can
use all the retrieval methods to get the data.
If you want to use the default setting there is no need to call cut
directly. Every method in the class that needs output checks the
object's internal status and recalculates the cuts if needed.
Note: cut doesn't now re-initialize everything before figuring
out cuts. This is so that you can do multiple digests, or add more
data or whatever. You'll have to use new to reset everything.
See also the comments in above about ambiguous and non-ambiguous
sequences.
=cut
sub cut {
my ($self, $opt, $ec) = @_;
# for the moment I have left this as a separate routine so
# the user calls cut rather than _cuts. This also initializes
# some stuff we need to use.
$self->throw("A sequence must be supplied")
unless $self->seq;
if ($opt && uc($opt) eq "MULTIPLE") {
$self->throw("You must supply a separate enzyme collection for multiple digests") unless $ec;
$self->_multiple_cuts($ec); # multiple digests
} else {
# reset some of the things that we save
$self->{maximum_cuts} = 0;
$self->{'_number_of_cuts_by_enzyme'} = {};
$self->{'_number_of_cuts_by_cuts'} = {};
$self->{'_fragments'} = {};
$self->{'_cut_positions'} = {}; # cut position is the real position
$self->{'_frag_map_list'} = {};
$self->_cuts;
}
$self->{'_cut'} = 1;
return $self;
}
=head2 mulitple_digest
Title : multiple_digest
Function : perform a multiple digest on a sequence
Returns : $self so you can go and get any of the other methods
Arguments : An enzyme collection
Multiple digests can use 1 or more enzymes, and the data is stored
in as if it were an enzyme called multiple_digest. You can then
retrieve information about multiple digests from any of the other
methods.
You can use this method in place of $re->cut('multiple', $enz_coll);
=cut
sub multiple_digest {
my ($self, $ec)=@_;
return $self->cut('multiple', $ec);
}
=head1 Query the results of the analysis
=cut
=head2 positions
Title : positions
Function : Retrieve the positions that an enzyme cuts at
Returns : An array of the positions that an enzyme cuts at
: or an empty array if the enzyme doesn't cut
Arguments: An enzyme name to retrieve the positions for
Comments : The cut occurs after the base specified.
=cut
sub positions {
my ($self, $enz) = @_;
$self->cut unless $self->{'_cut'};
$self->throw('no enzyme selected to get positions for')
unless $enz;
return defined $self->{'_cut_positions'}->{$enz} ?
@{$self->{'_cut_positions'}->{$enz}} :
();
}
=head2 fragments
Title : fragments
Function : Retrieve the fragments that we cut
Returns : An array of the fragments retrieved.
Arguments: An enzyme name to retrieve the fragments for
For example this code will retrieve the fragments for all enzymes that
cut your sequence
my $all_cutters = $analysis->cutters;
foreach my $enz ($$all_cutters->each_enzyme}) {
@fragments=$analysis->fragments($enz);
}
=cut
sub fragments {
my ($self, $enz) = @_;
$self->cut unless $self->{'_cut'};
$self->throw('no enzyme selected to get fragments for')
unless $enz;
my @fragments;
for ($self->fragment_maps($enz)) {push @fragments, $_->{seq}}
return @fragments;
}
=head2 fragment_maps
Title : fragment_maps
Function : Retrieves fragment sequences with start and end
points. Useful for feature construction.
Returns : An array containing a hash reference for each fragment,
containing the start point, end point and DNA
sequence. The hash keys are 'start', 'end' and
'seq'. Returns an empty array if not defined.
Arguments : An enzyme name, enzyme object,
or enzyme collection to retrieve the fragments for.
If passes an enzyme collection it will return the result of a multiple
digest. This : will also cause the special enzyme 'multiple_digest' to
be created so you can get : other information about this multiple
digest. (TMTOWTDI).
There is a minor problem with this and $self-E<gt>fragments that I
haven't got a good answer for (at the moment). If the sequence is not
cut, do we return undef, or the whole sequence?
For linear fragments it would be good to return the whole
sequence. For circular fragments I am not sure.
At the moment it returns the whole sequence with start of 1 and end of
length of the sequence. For example:
use Bio::Restriction::Analysis;
use Bio::Restriction::EnzymeCollection;
use Bio::PrimarySeq;
my $seq = Bio::PrimarySeq->new
(-seq =>'AGCTTAATTCATTAGCTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATCCAAAAAAGAGTGAGCTTCTGAT',
-primary_id => 'synopsis',
-molecule => 'dna');
my $ra = Bio::Restriction::Analysis->new(-seq=>$seq);
my @gel;
my @bam_maps = $ra->fragment_maps('BamHI');
foreach my $i (@bam_maps) {
my $start = $i->{start};
my $end = $i->{end};
my $sequence = $i->{seq};
push @gel, "$start--$sequence--$end";
@gel = sort {length $b <=> length $a} @gel;
}
print join("\n", @gel) . "\n";
=cut
sub fragment_maps {
my ($self, $enz) = @_;
$self->cut unless $self->{'_cut'};
$self->throw('no enzyme selected to get fragment maps for')
unless $enz;
# we are going to generate this on an as-needed basis rather than
# for every enzyme this should cut down on the amount of
# duplicated data we are trying to save in memory and make this
# faster and easier for large sequences, e.g. genome analysis
my @cut_positions;
if (ref $enz eq '' && exists $self->{'_cut_positions'}->{$enz}) {
@cut_positions=@{$self->{'_cut_positions'}->{$enz}};
} elsif ($enz->isa("Bio::Restriction::EnzymeI")) {
@cut_positions=@{$self->{'_cut_positions'}->{$enz->name}};
} elsif ($enz->isa("Bio::Restriction::EnzymeCollection")) {
$self->cut('multiple', $enz);
@cut_positions=@{$self->{'_cut_positions'}->{'multiple_digest'}};
}
unless (defined($cut_positions[0])) {
# it doesn't cut
# return the whole sequence
# this should probably have the is_circular command
my %map=(
'start' => 1,
'end' => $self->{'_seq'}->length,
'seq' => $self->{'_seq'}->seq
);
push (@{$self->{'_frag_map_list'}->{$enz}}, \%map);
return defined $self->{'_frag_map_list'}->{$enz} ?
@{$self->{'_frag_map_list'}->{$enz}} : ();
}
@cut_positions=sort {$a <=> $b} @cut_positions;
push my @cuts, $cut_positions[0];
foreach my $i (@cut_positions) {
push @cuts, $i if $i != $cuts[$#cuts];
}
my $start=1; my $stop; my %seq; my %stop;
foreach $stop (@cuts) {
next if !$stop; # cuts at beginning of sequence
$seq{$start}=$self->{'_seq'}->subseq($start, $stop);
$stop{$start}=$stop;
$start=$stop+1;
}
$stop=$self->{'_seq'}->length;
if ($start > $stop) {
# borderline case. The enzyme cleaved at the end of the sequence
# what do I do now?
}
else {
$seq{$start}=$self->{'_seq'}->subseq($start, $stop);
$stop{$start}=$stop;
}
if ($self->{'_seq'}->is_circular) {
# join the first and last fragments
$seq{$start}.=$seq{'1'};
delete $seq{'1'};
$stop{$start}=$stop{'1'};
delete $stop{'1'};
}
foreach my $start (sort {$a <=> $b} keys %seq) {
my %map=(
'start' => $start,
'end' => $stop{$start},
'seq' => $seq{$start}
);
push (@{$self->{'_frag_map_list'}->{$enz}}, \%map);
}
return defined $self->{'_frag_map_list'}->{$enz} ?
@{$self->{'_frag_map_list'}->{$enz}} : ();
}
=head2 sizes
Title : sizes
Function : Retrieves an array with the sizes of the fragments
Returns : Array that has the sizes of the fragments ordered from
largest to smallest like they would appear in a gel.
Arguments: An enzyme name to retrieve the sizes for is required and
kilobases to the nearest 0.1 kb, else it will be in
bp. If the optional third entry is set the results will
be sorted.
This is designed to make it easy to see what fragments you should get
on a gel!
You should be able to do these:
# to see all the fragment sizes,
print join "\n", $re->sizes($enz), "\n";
# to see all the fragment sizes sorted
print join "\n", $re->sizes($enz, 0, 1), "\n";
# to see all the fragment sizes in kb sorted
print join "\n", $re->sizes($enz, 1, 1), "\n";
=cut
sub sizes {
my ($self, $enz, $kb, $sort) = @_;
$self->throw('no enzyme selected to get fragments for')
unless $enz;
if (blessed($enz)) {
$self->throw("Enzyme must be enzyme name or a Bio::Restriction::EnzymeI, not ".ref($enz))
if !$enz->isa('Bio::Restriction::EnzymeI');
$enz = $enz->name;
}
$self->cut unless $self->{'_cut'};
my @frag; my $lastsite=0;
foreach my $site (@{$self->{'_cut_positions'}->{$enz}}) {
$kb ? push (@frag, (int($site-($lastsite))/100)/10)
: push (@frag, $site-($lastsite));
$lastsite=$site;
}
$kb ? push (@frag, (int($self->{'_seq'}->length-($lastsite))/100)/10)
: push (@frag, $self->{'_seq'}->length-($lastsite));
if ($self->{'_seq'}->is_circular) {
my $first=shift @frag;
my $last=pop @frag;
push @frag, ($first+$last);
}
$sort ? @frag = sort {$b <=> $a} @frag : 1;
return @frag;
}
=head1 How many times does enzymes X cut?
=cut
=head2 cuts_by_enzyme
Title : cuts_by_enzyme
Function : Return the number of cuts for an enzyme
Returns : An integer with the number of times each enzyme cuts.
Returns 0 if doesn't cut or undef if not defined
Arguments : An enzyme name string
=cut
sub cuts_by_enzyme {
my ($self, $enz)=@_;
$self->throw("Need an enzyme name")
unless defined $enz;
$self->cut unless $self->{'_cut'};
return $self->{'_number_of_cuts_by_enzyme'}->{$enz};
}
=head1 Which enzymes cut the sequence N times?
=cut
=head2 cutters
Title : cutters
Function : Find enzymes that cut a given number of times
Returns : a Bio::Restriction::EnzymeCollection
Arguments : 1. exact time or lower limit,
non-negative integer, optional
2. upper limit, non-negative integer,
larger or equalthan first, optional
If no arguments are given, the method returns all enzymes that do cut
the sequence. The argument zero, '0', is same as method
zero_cutters(). The argument one, '1', corresponds to unique_cutters.
If either of the limits is larger than number of cuts any enzyme cuts the
sequence, the that limit is automagically lowered. The method max_cuts()
gives the largest number of cuts.
See Also : L<unique_cutters|unique_cutters>,
L<zero_cutters|zero_cutters>, L<max_cuts|max_cuts>
=cut
sub cutters {
my ($self, $a, $z) = @_;
$self->cut unless $self->{'_cut'};
my ($start, $end);
if (defined $a) {
$self->throw("Need a non-zero integer [$a]")
unless $a =~ /^[+]?\d+$/;
$start = $a;
} else {
$start = 1;
}
$start = $self->{'maximum_cuts'} if $start > $self->{'maximum_cuts'};
if (defined $z) {
$self->throw("Need a non-zero integer no smaller than start [0]")
unless $z =~ /^[+]?\d+$/ and $z >= $a;
$end = $z;
}
elsif (defined $a) {
$end = $start;
} else {
$end = $self->{'maximum_cuts'};
}
$end = $self->{'maximum_cuts'} if $end > $self->{'maximum_cuts'};
my $set = Bio::Restriction::EnzymeCollection->new(-empty => 1);
#return an empty set if nothing cuts
return $set unless $self->{'maximum_cuts'};
for (my $i=$start; $i<=$end; $i++) {
$set->enzymes( @{$self->{_number_of_cuts_by_cuts}->{$i}} )
if defined $self->{_number_of_cuts_by_cuts}->{$i};
}
return $set;
}
=head2 unique_cutters
Title : unique_cutters
Function : A special case if cutters() where enzymes only cut once
Returns : a Bio::Restriction::EnzymeCollection
Arguments : -
See also: L<cutters>, L<zero_cutters>
=cut
sub unique_cutters {
shift->cutters(1);
}
=head2 zero_cutters
Title : zero_cutters
Function : A special case if cutters() where enzymes don't cut the sequence
Returns : a Bio::Restriction::EnzymeCollection
Arguments : -
See also: L<cutters>, L<unique_cutters>
=cut
sub zero_cutters {
shift->cutters(0);
}
=head2 max_cuts
Title : max_cuts
Function : Find the most number of cuts
Returns : The number of times the enzyme that cuts most cuts.
Arguments : None
This is not a very practical method, but if you are curious...
=cut
sub max_cuts { return shift->{maximum_cuts} }
=head1 Internal methods
=cut
=head2 _cuts
Title : _cuts
Function : Figures out which enzymes we know about and cuts the sequence.
Returns : Nothing.
Arguments : None.
Comments : An internal method. This will figure out where the sequence
should be cut, and provide the appropriate results.
=cut
sub _cuts {
my $self = shift;
my $target_seq=uc $self->{'_seq'}->seq; # I have been burned on this before :)
# first, find out all the enzymes that we have
foreach my $enz ($self->{'_enzymes'}->each_enzyme) {
my @all_cuts;
my @others = $enz->others if $enz->can("others");
foreach my $enzyme ($enz, @others) {
# cut the sequence
# _make_cuts handles all cases (amibiguous, non-ambiguous) X
# (palindromic X non-palindromic)
#
my $cut_positions = $self->_make_cuts($target_seq, $enzyme);
push @all_cuts, @$cut_positions;
#### need to refactor circular handling....
####
# deal with is_circular sequences
if ($self->{'_seq'}->is_circular) {
$cut_positions=$self->_circular($target_seq, $enzyme);
push @all_cuts, @$cut_positions;
}
# non-symmetric cutters (most external cutters, e.g.) need
# special handling
unless ($enzyme->is_symmetric) {
# do all of above with explicit use of the
# enzyme's 'complementary_cut'...
$cut_positions = $self->_make_cuts($target_seq, $enzyme, 'COMP');
push @all_cuts, @$cut_positions;
# deal with is_circular sequences
if ($self->{'_seq'}->is_circular) {
$cut_positions=$self->_circular($target_seq, $enzyme, 'COMP');
push @all_cuts, @$cut_positions;
}
}
}
if (defined $all_cuts[0]) {
# now just remove any duplicate cut sites
@all_cuts = sort {$a <=> $b} @all_cuts;
push @{$self->{'_cut_positions'}->{$enz->name}}, $all_cuts[0];
foreach my $i (@all_cuts) {
push @{$self->{'_cut_positions'}->{$enz->name}}, $i
if $i != ${$self->{'_cut_positions'}->{$enz->name}}[$#{$self->{'_cut_positions'}->{$enz->name}}];
}
} else {
# this just fixes an eror when @all_cuts is not defined!
@{$self->{'_cut_positions'}->{$enz->name}}=();
}
# note I have removed saving any other information except the
# cut_positions this should significantly decrease the amount
# of memory that is required for large sequences. It should
# also speed things up dramatically, because fragments and
# fragment maps are only calculated for those enzymes they are
# needed for.
# finally, save minimal information about each enzyme
my $number_of_cuts=scalar @{$self->{'_cut_positions'}->{$enz->name}};
# now just store the number of cuts
$self->{_number_of_cuts_by_enzyme}->{$enz->name}=$number_of_cuts;
push (@{$self->{_number_of_cuts_by_cuts}->{$number_of_cuts}}, $enz);
if ($number_of_cuts > $self->{maximum_cuts}) {
$self->{maximum_cuts}=$number_of_cuts;
}
}
}
=head2 _enzyme_sites
Title : _enzyme_sites
Function : An internal method to figure out the two sides of an enzyme
Returns : The sequence before the cut and the sequence after the cut
Arguments : A Bio::Restriction::Enzyme object,
$comp : boolean, calculate based on $enz->complementary_cut()
if true, $enz->cut() if false
Status : NOW DEPRECATED - maj
=cut
sub _enzyme_sites {
my ($self, $enz, $comp )=@_;
# get the cut site
# I have reworked this so that it uses $enz->cut to get the site
my $site= ( $comp ? $enz->complementary_cut : $enz->cut );
# split it into the two fragments for the sequence before and after.
$site=0 unless defined $site;
# the default values just stop an error from an undefined
# string. But they don't affect the split.
my ($beforeseq, $afterseq)= ('.', '.');
# extra-site cutting
# the before seq is going to be the entire site
# the after seq is empty
# BUT, need to communicate how to cut within the sample sequence
# relative to the end of the site (do through $enz->cut), and
# ALSO, need to check length of sample seq so that if cut falls
# outside the input sequence, we have a warning/throw. /maj
# pre-site cutting
# need to handle negative site numbers
if ($site <= 0) { # <= to handle pre-site cutting
$afterseq=$enz->string;
}
elsif ($site >= $enz->seq->length) { # >= to handle extrasite cutters/maj
$beforeseq=$enz->string;
}
else { # $site < $enz->seq->length
$beforeseq=$enz->seq->subseq(1, $site);
$afterseq=$enz->seq->subseq($site+1, $enz->seq->length);
}
# if the enzyme is ambiguous we need to convert this into a perl string
if ($enz->is_ambiguous) {
$beforeseq=$self->_expanded_string($beforeseq);
$afterseq =$self->_expanded_string($afterseq);
}
return ($beforeseq, $afterseq);
}
=head2 _non_pal_enz
Title : _non_pal_enz
Function : Analyses non_palindromic enzymes for cuts in both ways
(in fact, delivers only minus strand cut positions in the
plus strand coordinates/maj)
Returns : A reference to an array of cut positions
Arguments: The sequence to check and the enzyme object
NOW DEPRECATED/maj
=cut
sub _non_pal_enz {
my ($self, $target_seq, $enz) =@_;
# add support for non-palindromic sequences
# the enzyme is not the same forwards and backwards
my $site=$enz->complementary_cut;
# complementary_cut is in plus strand coordinates
# we are going to rc the sequence, so complementary_cut becomes length-complementary_cut
# I think this is wrong; cut sites are a matter of position with respect
# to the plus strand: the recognition site is double stranded and
# directly identifiable on the plus strand sequence. /maj
# what really needs doing is to keep track of plus strand and minus strand
# nicks separately./maj
my ($beforeseq, $afterseq)=('.', '.');
# now, for extra-site cuts, $site > length...so...?/maj
my $new_left_cut=$enz->seq->length-$site;
# there is a problem when this is actually zero
if ($new_left_cut == 0) {$afterseq=$enz->seq->revcom->seq}
elsif ($new_left_cut == $enz->seq->length) {$beforeseq=$enz->seq->revcom->seq}
else {
# this can't be right./maj
$beforeseq=$enz->seq->revcom->subseq(1, ($enz->seq->length-$site));
$afterseq=$enz->seq->revcom->subseq(($enz->seq->length-$site), $enz->seq->length);
}
# do this correctly, in the context of the current code design,
# by providing a "complement" argument to _ambig_cuts and _nonambig_cuts,
# use these explicitly rather than this wrapper./maj
my $results=[];
if ($enz->is_ambiguous) {
$results= $self->_ambig_cuts($beforeseq, $afterseq, $target_seq, $enz);
} else {
$results= $self->_nonambig_cuts($beforeseq, $afterseq, $target_seq, $enz);
}
# deal with is_circular
my $more_results=[];
$more_results=$self->_circular($beforeseq, $afterseq, $enz)
if ($self->{'_seq'}->is_circular);
return [@$more_results, @$results];
}
=head2 _ambig_cuts
Title : _ambig_cuts
Function : An internal method to localize the cuts in the sequence
Returns : A reference to an array of cut positions
Arguments : The separated enzyme site, the target sequence, and the enzyme object
Comments : This is a slow implementation but works for ambiguous sequences.
Whenever possible, _nonambig_cuts should be used as it is a lot faster.
=cut