EnsEMBL/Utils/Slice.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 79 CC 6 SCAR 30.9
CC
6
Cov
0%
split_Slices
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 22.0
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::Utils::Slice - Utility functions for slices
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::Utils::Slice qw(split_Slices);
38  
39 # ...
40  
41 # get all chromosomes in the database
42 my $slices = $slice_adaptor->fetch_all('chromosome');
43  
44 # split the chromosomes into equal chunks of size less than 1MB
45 # with an overlap of 1kb
46 $slices = split_Slices( $slices, 1e6, 1e3 );
47  
48 =head1 METHODS
49  
50 =cut
51  
52  
53 package Bio::EnsEMBL::Utils::Slice;
54 $Bio::EnsEMBL::Utils::Slice::VERSION = '114.0.0';
55 use strict;
56 use warnings;
57  
58 use Exporter;
59  
60 use vars qw(@ISA @EXPORT_OK);
61  
62 @ISA = qw(Exporter);
63  
64 @EXPORT_OK = qw(&split_Slices);
65  
66 use Bio::EnsEMBL::Utils::Exception qw(throw);
67 use POSIX;
68  
69 =head2 split_Slices
70  
71 Arg [1] : ref to list of slices
72 Arg [2] : int maxlength of sub slices
73 Arg [3] : int overlap length (optional)
74 Example : my $sub_slices = split_Slices($slices,$maxlen,$overlap)
75 Description: splits a slice into smaller slices
76 Returntype : ref to list of slices
77 Exceptions : maxlen <1 or overlap < 0
78  
79 =cut
80  
81 sub split_Slices{
82 my ($slice_big,$max_length,$overlap)=@_;
83  
84 if(!defined($max_length) or $max_length < 1){
85 throw("maxlength needs to be set and > 0");
86 }
87  
88 if(!defined($overlap)){
89 $overlap = 0;
90 }
91 elsif($overlap < 0){
92 throw("negative overlaps not allowed");
93 }
94  
95 my @out=();
96  
97 foreach my $slice (@$slice_big){
98  
99 my $start = $slice->start;
100 my $end;
101 my $multiple;
102 my $number;
103 my $length = $slice->length;
104  
105 if($max_length && ($length > $overlap)) {
106 #No seq region may be longer than max_length but we want to make
107 #them all similar size so that the last one isn't much shorter.
108 #Divide the seq_region into the largest equal pieces that are shorter
109 #than max_length
110  
111 #calculate number of slices to create
112 $number = ($length-$overlap) / ($max_length-$overlap);
113 $number = ceil($number); #round up to int
114  
115 #calculate length of created slices
116 $multiple = $length / $number;
117 $multiple = floor($multiple); #round down to int
118 } else {
119 #just one slice of the whole seq_region
120 $number = 1;
121 $multiple = $length;
122 }
123  
124 my $i;
125 for(my $i=0; $i < $number; $i++) {
126 $end = $start + $multiple + $overlap;
127  
128 #any remainder gets added to the last slice of the seq_region
129 $end = $slice->end if($i == $number-1);
130 push @out, Bio::EnsEMBL::Slice->new
131 (-START => $start,
132 -END => $end,
133 -STRAND => 1,
134 -SEQ_REGION_NAME => $slice->seq_region_name,
135 -SEQ_REGION_LENGTH => $slice->seq_region_length,
136 -COORD_SYSTEM => $slice->coord_system,
137 -ADAPTOR => $slice->adaptor);
138 $start += $multiple + 1;
139 }
140 }
141  
142 return \@out;
143 }
144  
145  
146  
147 1;