FTLocationFactory.pm: Fixed a long-standing issue at "from_string()"
[bioperl-live.git] / t / SeqFeature / LocationFactory.t
blob94c417ac5bf2cb45a4daef9e6b1cfd5a965d12b0
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib '.';
8     use Bio::Root::Test;
10     test_begin(-tests => 307);
12     use_ok('Bio::Factory::FTLocationFactory');
15 my $simple_impl = "Bio::Location::Simple";
16 my $fuzzy_impl  = "Bio::Location::Fuzzy";
17 my $split_impl  = "Bio::Location::Split";
19 # Holds strings and results. The latter is an array of expected class name,
20 # min/max start position and position type, min/max end position and position
21 # type, location type, the number of locations, and the strand.
23 my %testcases = (
24     # note: the following are directly taken from
25     # http://www.insdc.org/documents/feature_table.html#3.4.3
26     "467"
27         => [$simple_impl, 467, 467, "EXACT",
28                           467, 467, "EXACT",
29                           "EXACT", 1, 1],
30     "340..565"
31         => [$simple_impl, 340, 340, "EXACT",
32                           565, 565, "EXACT",
33                           "EXACT", 1, 1],
34     "<345..500"
35         => [$fuzzy_impl, undef, 345, "BEFORE",
36                          500,   500, "EXACT",
37                          "EXACT", 1, 1],
38     "<1..888"
39         => [$fuzzy_impl, undef, 1,   "BEFORE",
40                          888,   888, "EXACT",
41                          "EXACT", 1, 1],
42     "1..>888"
43         => [$fuzzy_impl, 1,   1,     "EXACT",
44                          888, undef, "AFTER",
45                          "EXACT", 1, 1],
46     "(102.110)"
47         => [$fuzzy_impl, 102, 102, "EXACT",
48                          110, 110, "EXACT",
49                          "WITHIN", 1, 1],
50     "(23.45)..600"
51         => [$fuzzy_impl, 23,  45,  "WITHIN",
52                          600, 600, "EXACT",
53                          "EXACT", 1, 1],
54     "(122.133)..(204.221)"
55         => [$fuzzy_impl, 122, 133, "WITHIN",
56                          204, 221, "WITHIN",
57                          "EXACT", 1, 1],
58     "123^124"
59         => [$simple_impl, 123, 123, "EXACT",
60                           124, 124, "EXACT",
61                           "IN-BETWEEN", 1, 1],
62     "145^177"
63         => [$fuzzy_impl, 145, 145, "EXACT",
64                          177, 177, "EXACT",
65                          "IN-BETWEEN", 1, 1],
66     "join(12..78,134..202)"
67         => [$split_impl, 12,  12,  "EXACT",
68                          202, 202, "EXACT",
69                          "EXACT", 2, 1],
70     "complement(join(2691..4571,4918..5163))"
71         => [$split_impl, 2691, 2691, "EXACT",
72                          5163, 5163, "EXACT",
73                          "EXACT", 2, -1],
74     "complement(34..(122.126))"
75         => [$fuzzy_impl, 34,  34,  "EXACT",
76                          122, 126, "WITHIN",
77                          "EXACT", 1, -1],
78     "J00194:100..202"
79         => [$simple_impl, 100, 100, "EXACT",
80                           202, 202, "EXACT",
81                           "EXACT", 1, 1],
82     "join(1..100,J00194.1:100..202)"
83         => [$split_impl, 1,   1,   "EXACT",
84                          100, 100, "EXACT",
85                          "EXACT", 2, undef],
87     # this variant is not really allowed by the FT definition
88     # document but we want to be able to cope with it
89     "J00194:(100..202)"
90         => [$simple_impl, 100, 100, "EXACT",
91                           202, 202, "EXACT",
92                           "EXACT", 1, 1],
93     "((122.133)..(204.221))"
94         => [$fuzzy_impl, 122, 133, "WITHIN",
95                          204, 221, "WITHIN",
96                          "EXACT", 1, 1],
97     "join(AY016290.1:108..185,AY016291.1:1546..1599)"
98         => [$split_impl, 108, 108, "EXACT",
99                          185, 185, "EXACT",
100                          "EXACT", 2, undef],
102     # UNCERTAIN locations and positions (Swissprot)
103     "?2465..2774"
104         => [$fuzzy_impl, 2465, 2465, "UNCERTAIN",
105                          2774, 2774, "EXACT",
106                          "EXACT", 1, 1],
107     "22..?64"
108         => [$fuzzy_impl, 22, 22, "EXACT",
109                          64, 64, "UNCERTAIN",
110                          "EXACT", 1, 1],
111     "?22..?64"
112         => [$fuzzy_impl, 22, 22, "UNCERTAIN",
113                          64, 64, "UNCERTAIN",
114                          "EXACT", 1, 1],
115     "?..>393"
116         => [$fuzzy_impl, undef, undef, "UNCERTAIN",
117                          393,   undef, "AFTER",
118                          "UNCERTAIN", 1, 1],
119     "<1..?"
120         => [$fuzzy_impl, undef, 1,     "BEFORE",
121                          undef, undef, "UNCERTAIN",
122                          "UNCERTAIN", 1, 1],
123     "?..536"
124         => [$fuzzy_impl, undef, undef, "UNCERTAIN",
125                          536,   536,   "EXACT",
126                          "UNCERTAIN", 1, 1],
127     "1..?"
128         => [$fuzzy_impl, 1,     1,     "EXACT",
129                          undef, undef, "UNCERTAIN",
130                          "UNCERTAIN", 1, 1],
131     "?..?"
132         => [$fuzzy_impl, undef, undef, "UNCERTAIN",
133                          undef, undef, "UNCERTAIN",
134                          "UNCERTAIN", 1, 1],
135     "?1..12"
136         => [$fuzzy_impl, 1,  1,  "UNCERTAIN",
137                          12, 12, "EXACT",
138                          "EXACT", 1, 1]
141 my $locfac = Bio::Factory::FTLocationFactory->new();
142 isa_ok($locfac,'Bio::Factory::LocationFactoryI');
144 # sorting is to keep the order constant from one run to the next
145 foreach my $locstr (keys %testcases) {
146     my $loc = $locfac->from_string($locstr);
147     if($locstr eq "join(AY016290.1:108..185,AY016291.1:1546..1599)") {
148         $loc->seq_id("AY016295.1");
149     }
150     if($locstr eq "join(1..100,J00194.1:100..202)") {
151         $loc->seq_id("unknown");
152     }
153     my @res = @{$testcases{$locstr}};
154     is(ref($loc), $res[0], $res[0]);
155     is($loc->min_start(), $res[1]);
156     is($loc->max_start(), $res[2]);
157     is($loc->start_pos_type(), $res[3]);
158     is($loc->min_end(), $res[4]);
159     is($loc->max_end(), $res[5]);
160     is($loc->end_pos_type(), $res[6]);
161     is($loc->location_type(), $res[7]);
162     my @locs = $loc->each_Location();
163     is(@locs, $res[8]);
164     my $ftstr = $loc->to_FTstring();
165     # this is a somewhat ugly hack, but we want clean output from to_FTstring()
166     # Umm, then these should really fail, correct?
167     # Should we be engineering workarounds for tests?
168     $locstr = "J00194:100..202"      if $locstr eq "J00194:(100..202)";
169     $locstr = "(122.133)..(204.221)" if $locstr eq "((122.133)..(204.221))";
170     # now test
171     is($ftstr, $locstr, "Location String: $locstr");
172     # test strand production
173     is($loc->strand(), $res[9]);
176 SKIP: {
177     skip('nested matches in regex only supported in v5.6.1 and higher', 8) unless $^V gt v5.6.0;
179     # Tests based on location definition (http://www.insdc.org/documents/feature_table.html#3.4)
180     my $string1  = 'complement(join(2691..4571,4918..5163))';
181     my $string2  = 'join(complement(4918..5163),complement(2691..4571))';
182     my $loc1     = $locfac->from_string($string1);
183     my $loc2     = $locfac->from_string($string2);
184     my $loc1_str = $loc1->to_FTstring;
185     my $loc2_str = $loc2->to_FTstring;
186     is($loc2_str, 'complement(join(2691..4571,4918..5163))', 'join(complement(4918..5163),complement(2691..4571))');
187     is($loc1_str, $loc2_str, 'equivalent location strings');
189     # bug #1674, #1765, 2101
190     # EMBL-like (BAC19856.3 protein)
191     # join(20464..20694,21548..22763,join(complement(314652..314672),complement(232596..232990),complement(231520..231669)))
192     # GenBank-like
193     # join(20464..20694,21548..22763,complement(join(231520..231669,232596..232990,314652..314672)))
194     # Note that
195     # join(1000..2000,join(3000..4000,join(5000..6000,7000..8000)),9000..10000)
196     # is the same as
197     # join(1000..2000,3000..4000,5000..6000,7000..8000,9000..10000)
199     my @expected = (# intentionally testing same expected string twice
200                     # as I am providing two different encodings
201                     # that should mean the same thing
202     'join(11025..11049,complement(join(315036..315294,251354..251412,241499..241580,239890..240081)))',
203     'join(11025..11049,complement(join(315036..315294,251354..251412,241499..241580,239890..240081)))',
204     # ditto
205     'join(20464..20694,21548..22763,complement(join(231520..231669,232596..232990,314652..314672)))',
206     'join(20464..20694,21548..22763,complement(join(231520..231669,232596..232990,314652..314672)))',
207     # this is just seen once
208     'join(1000..2000,3000..4000,5000..6000,7000..8000,9000..10000)',
209     'order(S67862.1:72..75,join(S67863.1:1..788,1..19))'
210    );
212     for my $locstr (
213         'join(11025..11049,join(complement(239890..240081),complement(241499..241580),complement(251354..251412),complement(315036..315294)))',
214         'join(11025..11049,complement(join(315036..315294,251354..251412,241499..241580,239890..240081)))',
215         'join(20464..20694,21548..22763,complement(join(231520..231669,232596..232990,314652..314672)))',
216         'join(20464..20694,21548..22763,join(complement(314652..314672),complement(232596..232990),complement(231520..231669)))',
217         'join(1000..2000,join(3000..4000,join(5000..6000,7000..8000)),9000..10000)',
218         'order(S67862.1:72..75,join(S67863.1:1..788,1..19))'
219        ) {
220         my $loc = $locfac->from_string($locstr);
221         my $ftstr = $loc->to_FTstring();
222         is($ftstr, shift @expected, $locstr);
223     }