t/*: remove "use lib '.'" and t/lib/Error.pm
[bioperl-live.git] / t / SeqFeature / Location.t
blobfaf7e862cb4bda54d7648d9fb6791fbfeb444644
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use Bio::Root::Test;
8     
9     test_begin(-tests => 109);
10         
11     use_ok('Bio::Location::Simple');
12     use_ok('Bio::Location::Split');
13     use_ok('Bio::Location::Fuzzy');
14     use_ok('Bio::SeqFeature::Generic');
15     use_ok('Bio::SeqFeature::SimilarityPair');
16     use_ok('Bio::SeqFeature::FeaturePair');
17     use_ok('Bio::SeqFeature::Lite');
20 my $simple = Bio::Location::Simple->new('-start' => 10, '-end' => 20,
21                                        '-strand' => 1, -seq_id => 'my1');
22 isa_ok($simple, 'Bio::LocationI');
23 isa_ok($simple, 'Bio::RangeI');
25 is($simple->start, 10, 'Bio::Location::Simple tests');
26 is($simple->end, 20);
27 is($simple->seq_id, 'my1');
29 my ($loc) = $simple->each_Location();
30 ok($loc);
31 is("$loc", "$simple");
33 my $generic = Bio::SeqFeature::Generic->new('-start' => 5, '-end' => 30, 
34                                            '-strand' => 1);
36 isa_ok($generic,'Bio::SeqFeatureI', 'Bio::SeqFeature::Generic' );
37 isa_ok($generic,'Bio::RangeI');
38 is($generic->start, 5);
39 is($generic->end, 30);
41 my $lite_pos  = Bio::SeqFeature::Lite->new(-start => 1000, -stop => 2000, -strand => '+');
42 my $lite_neg  = Bio::SeqFeature::Lite->new(-start => 1000, -end  => 2000, -strand => '-');
43 my $lite_none = Bio::SeqFeature::Lite->new(-start => 1000, -stop => 2000, -strand => '.');
44 is($lite_pos->strand,  1);
45 is($lite_neg->strand, -1);
46 is($lite_neg->end,  2000);
47 is($lite_neg->stop, 2000);
48 is($lite_none->strand, 0);
50 my $similarity = Bio::SeqFeature::SimilarityPair->new();
52 my $feat1 = Bio::SeqFeature::Generic->new('-start' => 30, '-end' => 43, 
53                                          '-strand' => -1);
54 my $feat2 = Bio::SeqFeature::Generic->new('-start' => 80, '-end' => 90, 
55                                          '-strand' => -1);
57 my $featpair = Bio::SeqFeature::FeaturePair->new('-feature1' => $feat1,
58                                                 '-feature2' => $feat2 );
60 my $feat3 = Bio::SeqFeature::Generic->new('-start' => 35, '-end' => 50, 
61                                          '-strand' => -1);
63 is($featpair->start, 30,'Bio::SeqFeature::FeaturePair tests');
64 is($featpair->end,  43);
66 is($featpair->length, 14);
68 ok($featpair->overlaps($feat3));
69 ok($generic->overlaps($simple), 'Bio::SeqFeature::Generic tests');
70 ok($generic->contains($simple));
72 # fuzzy location tests
73 my $fuzzy = Bio::Location::Fuzzy->new('-start'  =>'<10', 
74                                      '-end'    => 20,
75                                      -strand   =>1, 
76                                      -seq_id   =>'my2');
78 is($fuzzy->strand, 1, 'Bio::Location::Fuzzy tests');
79 is($fuzzy->start, 10);
80 is($fuzzy->end,20);
81 ok(!defined $fuzzy->min_start);
82 is($fuzzy->max_start, 10);
83 is($fuzzy->min_end, 20);
84 is($fuzzy->max_end, 20);
85 is($fuzzy->location_type, 'EXACT');
86 is($fuzzy->start_pos_type, 'BEFORE');
87 is($fuzzy->end_pos_type, 'EXACT');
88 is($fuzzy->seq_id, 'my2');
89 is($fuzzy->seq_id('my3'), 'my3');
91 ($loc) = $fuzzy->each_Location();
92 ok($loc);
93 is("$loc", "$fuzzy");
95 # split location tests
96 my $splitlocation = Bio::Location::Split->new();
97 my $f = Bio::Location::Simple->new(-start  => 13,
98                                   -end    => 30,
99                                   -strand => 1);
100 $splitlocation->add_sub_Location($f);
101 is($f->start, 13, 'Bio::Location::Split tests');
102 is($f->min_start, 13);
103 is($f->max_start,13);
106 $f = Bio::Location::Simple->new(-start  =>30,
107                                -end    =>90,
108                                -strand =>1);
109 $splitlocation->add_sub_Location($f);
111 $f = Bio::Location::Simple->new(-start  =>11,
112                                -end    =>22,
113                                -strand =>1);
114 $splitlocation->add_sub_Location($f);
116 $f = Bio::Location::Simple->new(-start  =>19,
117                                -end    =>20,
118                                -strand =>1);
120 $splitlocation->add_sub_Location($f);
122 $f = Bio::Location::Fuzzy->new(-start  =>"<50",
123                               -end    =>61,
124                               -strand =>1);
125 is($f->start, 50);
126 ok(! defined $f->min_start);
127 is($f->max_start, 50);
129 is(scalar($splitlocation->each_Location()), 4);
131 $splitlocation->add_sub_Location($f);
133 # For unsorted split locations like this:
134 # ('join(13..30,30..90,11..22,19..20,<50..61)'),
135 # BioPerl will assume Start and End belongs to the
136 # first and last segments respectively, because sorting
137 # would break real cases like circular cut by origin features
138 is($splitlocation->end, 61);
139 is($splitlocation->start, 13);
140 is($splitlocation->sub_Location(),5);
141 # Minimum Start and Maximum End in unsorted sublocations can be
142 # achieved by asking explicitly sub_Location to sort the segments
143 my @increase_sort_sublocs = $splitlocation->sub_Location(1);  # Forward sort by Start
144 my @decrease_sort_sublocs = $splitlocation->sub_Location(-1); # Reverse sort by End
145 is($increase_sort_sublocs[0]->min_start, 11);
146 is($decrease_sort_sublocs[0]->max_end,   90);
148 is($fuzzy->to_FTstring(), '<10..20');
149 $fuzzy->strand(-1);
150 is($fuzzy->to_FTstring(), 'complement(<10..20)');
151 is($simple->to_FTstring(), '10..20');
152 $simple->strand(-1);
153 is($simple->to_FTstring(), 'complement(10..20)');
154 is( $splitlocation->to_FTstring(), 
155     'join(13..30,30..90,11..22,19..20,<50..61)');
157 # test for bug #1074
158 $f = Bio::Location::Simple->new(-start  => 5,
159                                -end    => 12,
160                                -strand => -1);
161 $splitlocation->add_sub_Location($f);
162 is( $splitlocation->to_FTstring(), 
163     'join(13..30,30..90,11..22,19..20,<50..61,complement(5..12))',
164         'Bugfix 1074');
165 $splitlocation->strand(-1);
166 is( $splitlocation->to_FTstring(), 
167     'complement(join(13..30,30..90,11..22,19..20,<50..61,5..12))');
169 $f = Bio::Location::Fuzzy->new(-start => '45.60',
170                               -end   => '75^80');
172 is($f->to_FTstring(), '(45.60)..(75^80)');
173 $f->start('20>');
174 is($f->to_FTstring(), '>20..(75^80)');
176 # test that even when end < start that length is always positive
178 $f = Bio::Location::Simple->new(-verbose => -1,
179                                -start   => 100, 
180                                -end     => 20, 
181                                -strand  => 1);
183 is($f->length, 81, 'Positive length');
184 is($f->strand,-1);
186 # test that can call seq_id() on a split location;
187 $splitlocation = Bio::Location::Split->new(-seq_id => 'mysplit1');
188 is($splitlocation->seq_id,'mysplit1', 'seq_id() on Bio::Location::Split');
189 is($splitlocation->seq_id('mysplit2'),'mysplit2');
192 # Test Bio::Location::Exact
194 ok(my $exact = Bio::Location::Simple->new(-start    => 10, 
195                                          -end      => 20,
196                                          -strand   => 1, 
197                                          -seq_id   => 'my1'));
198 isa_ok($exact, 'Bio::LocationI');
199 isa_ok($exact, 'Bio::RangeI');
201 is( $exact->start, 10, 'Bio::Location::Simple EXACT');
202 is( $exact->end, 20);
203 is( $exact->seq_id, 'my1');
204 is( $exact->length, 11);
205 is( $exact->location_type, 'EXACT');
207 ok ($exact = Bio::Location::Simple->new(-start         => 10, 
208                                       -end           => 11,
209                                       -location_type => 'IN-BETWEEN',
210                                       -strand        => 1, 
211                                       -seq_id        => 'my2'));
213 is($exact->start, 10, 'Bio::Location::Simple IN-BETWEEN');
214 is($exact->end, 11);
215 is($exact->seq_id, 'my2');
216 is($exact->length, 0);
217 is($exact->location_type, 'IN-BETWEEN');
219 eval {
220     $exact = Bio::Location::Simple->new(-start         => 10, 
221                                        -end           => 12,
222                                        -location_type => 'IN-BETWEEN');
224 ok( $@, 'Testing error handling' );
226 # testing error when assigning 10^11 simple location into fuzzy
227 eval {
228     ok $fuzzy = Bio::Location::Fuzzy->new(-start         => 10, 
229                                          -end           => 11,
230                                          -location_type => '^',
231                                          -strand        => 1, 
232                                          -seq_id        => 'my2');
234 ok( $@ );
236 $fuzzy = Bio::Location::Fuzzy->new(-location_type => '^',
237                                   -strand        => 1, 
238                                   -seq_id        => 'my2');
240 $fuzzy->start(10);
241 eval { $fuzzy->end(11) };
242 ok($@);
244 $fuzzy = Bio::Location::Fuzzy->new(-location_type => '^',
245                                   -strand        => 1, 
246                                   -seq_id        =>'my2');
248 $fuzzy->end(11);
249 eval {
250     $fuzzy->start(10);
252 ok($@);
254 # testing coodinate policy modules
256 use_ok('Bio::Location::WidestCoordPolicy');
257 use_ok('Bio::Location::NarrowestCoordPolicy');
258 use_ok('Bio::Location::AvWithinCoordPolicy');
260 $f = Bio::Location::Fuzzy->new(-start => '40.60',
261                               -end   => '80.100');
262 is $f->start, 40, 'Default coodinate policy';
263 is $f->end, 100;
264 is $f->length, 61;
265 is $f->to_FTstring, '(40.60)..(80.100)';
266 isa_ok($f->coordinate_policy, 'Bio::Location::WidestCoordPolicy');
268 # this gives an odd location string; is it legal?
269 $f->coordinate_policy(Bio::Location::NarrowestCoordPolicy->new());
270 is $f->start, 60, 'Narrowest coodinate policy';
271 is $f->end, 80;
272 is $f->length, 21;
273 is $f->to_FTstring, '(60.60)..(80.80)';
274 isa_ok($f->coordinate_policy, 'Bio::Location::NarrowestCoordPolicy');
276 # this gives an odd location string
277 $f->coordinate_policy(Bio::Location::AvWithinCoordPolicy->new());
278 is $f->start, 50, 'Average coodinate policy';
279 is $f->end, 90;
280 is $f->length, 41;
281 is $f->to_FTstring, '(50.60)..(80.90)';
282 isa_ok($f->coordinate_policy, 'Bio::Location::AvWithinCoordPolicy');
284 # to complete the circle
285 $f->coordinate_policy(Bio::Location::WidestCoordPolicy->new());
286 is $f->start, 40, 'Widest coodinate policy';
287 is $f->end, 100;
288 is $f->length, 61;
289 is $f->to_FTstring, '(40.60)..(80.100)';
290 isa_ok($f->coordinate_policy, 'Bio::Location::WidestCoordPolicy');