2 # BioPerl module for Bio::Factory::FTLocationFactory
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Hilmar Lapp <hlapp at gmx.net>
8 # Copyright Hilmar Lapp
10 # You may distribute this module under the same terms as perl itself
12 # (c) Hilmar Lapp, hlapp at gnf.org, 2002.
13 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
15 # You may distribute this module under the same terms as perl itself.
16 # Refer to the Perl Artistic License (see the license accompanying this
17 # software package, or see http://www.perl.com/language/misc/Artistic.html)
18 # for the terms under which you may use, modify, and redistribute this module.
20 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
21 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 # POD documentation - main docs before the code
29 Bio::Factory::FTLocationFactory - A FeatureTable Location Parser
33 # parse a string into a location object
34 $loc = Bio::Factory::FTLocationFactory->from_string("join(100..200,
39 Implementation of string-encoded location parsing for the Genbank feature
40 table encoding of locations.
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
55 Please direct usage questions or support issues to the mailing list:
57 I<bioperl-l@bioperl.org>
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via the
70 https://github.com/bioperl/bioperl-live/issues
72 =head1 AUTHOR - Hilmar Lapp
74 Email hlapp at gmx.net
78 Jason Stajich, jason-at-bioperl-dot-org
79 Chris Fields, cjfields-at-uiuc-dot-edu
83 The rest of the documentation details each of the object methods.
84 Internal methods are usually preceded with a _
89 # Let the code begin...
91 package Bio
::Factory
::FTLocationFactory
;
96 # Object preamble - inherits from Bio::Root::Root
98 use Bio::Location::Simple;
99 use Bio::Location::Split;
100 use Bio::Location::Fuzzy;
103 use base qw(Bio::Root::Root Bio::Factory::LocationFactoryI);
106 # the below is an optimized regex obj. from J. Freidl's Mastering Reg Exp.
121 Usage : my $obj = Bio::Factory::FTLocationFactory->new();
122 Function: Builds a new Bio::Factory::FTLocationFactory object
123 Returns : an instance of Bio::Factory::FTLocationFactory
131 Usage : $loc = $locfactory->from_string("100..200");
132 Function: Parses the given string and returns a Bio::LocationI implementing
133 object representing the location encoded by the string.
135 This implementation parses the Genbank feature table
136 encoding of locations.
138 Returns : A Bio::LocationI implementing object.
144 my ($self,$locstr,$op) = @_;
147 #$self->debug("$locstr\n");
149 # $op for operator (error handling)
151 # run on first pass only
152 # Note : These location types are now deprecated in GenBank (Oct. 2006)
154 # convert all (X.Y) to [X.Y]
155 $locstr =~ s{\((\d+\.\d+)\)}{\[$1\]}g;
156 # convert ABC123:(X..Y) to ABC123:[X..Y]
157 # we should never see the above
158 $locstr =~ s{:\((\d+\.{2}\d+)\)}{:\[$1\]}g;
161 if ($locstr =~ m{(.*?)\(($LOCREG)\)(.*)}o) { # any matching parentheses?
162 my ($beg, $mid, $end) = ($1, $2, $3);
163 my (@sublocs) = (split(q
(,),$beg), $mid, split(q
(,),$end));
170 my $subloc = shift @sublocs;
172 my $oparg = ($subloc eq 'join' || $subloc eq 'bond' ||
173 $subloc eq 'order' || $subloc eq 'complement') ?
$subloc : undef;
174 # has operator, requires further work (recurse)
176 my $sub = shift @sublocs;
177 # simple split operators (no recursive calls needed)
178 if (($oparg eq 'join' || $oparg eq 'order' || $oparg eq 'bond' )
179 && $sub !~ m{(?:join|order|bond)}) {
180 my @splitlocs = split(q
(,), $sub);
181 $loc_obj = Bio
::Location
::Split
->new(-verbose
=> 1,
182 -splittype
=> $oparg);
183 # Store strand values for later consistency check
186 foreach my $splitloc (@splitlocs) {
187 next unless $splitloc;
189 if ($splitloc =~ m{\(($LOCREG)\)}) {
191 $sobj = $self->_parse_location($comploc);
193 push @subloc_strands, -1;
195 $sobj = $self->_parse_location($splitloc);
196 push @subloc_strands, 1;
201 # Sublocations strand values consistency check to set
202 # Guide Strand and sublocations adding order
203 if (scalar @s_objs > 0) {
206 my $first_value = $subloc_strands[0];
207 foreach my $strand (@subloc_strands) {
208 $identical++ if ($strand == $first_value);
211 if ($identical == scalar @subloc_strands) {
212 # Set guide_strand if all sublocations have the same strand
213 $loc_obj->guide_strand($first_value);
215 # Reverse sublocation order for negative strand locations, e.g.:
216 # Common (CAA24672.1):
217 # join(complement(4918..5163),complement(2691..4571))
218 # Trans-splicing (NP_958375.1):
219 # join(32737..32825,complement(174205..174384),complement(69520..71506))
220 if ($first_value == -1) {
221 @s_objs = reverse @s_objs;
225 # Mixed strand values
226 $loc_obj->guide_strand(undef);
230 foreach my $s_obj (@s_objs) {
231 $loc_obj->add_sub_Location($s_obj);
235 $loc_obj = $self->from_string($sub, $oparg);
236 # reinsure the operator is set correctly for this level
237 # unless it is complement
238 $loc_obj->splittype($oparg) unless $oparg eq 'complement';
241 # no operator, simple or fuzzy
243 $loc_obj = $self->from_string($subloc,1);
245 if ($op && $op eq 'complement') {
246 $loc_obj->strand(-1);
249 # For Split-type $loc_obj, if guide strand is set (meaning consistent strand for
250 # all sublocs) and guide strand is the same than the last location from @loc_objs,
251 # then recover the sublocations and add them to @loc_objs. This way,
252 # "join(10..20,join(30..40,50..60))" becomes "join(10..20,30..40,50..60)"
253 my $guide_strand = ($loc_obj->isa('Bio::Location::SplitLocationI')) ?
($loc_obj->guide_strand || 0) : 0;
254 my $last_strand = (scalar @loc_objs > 0) ?
$loc_objs[-1]->strand : 0;
255 if ( $guide_strand != 0
256 and $guide_strand == $last_strand
257 and $oparg eq $op # join(,join()) OK, order(join()) NOT OK
259 my @subloc_objs = $loc_obj->sub_Location(0);
260 foreach my $subloc_obj (@subloc_objs) {
261 push @loc_objs, $subloc_obj;
265 push @loc_objs, $loc_obj;
269 if ($op && !($op eq 'join' || $op eq 'order' || $op eq 'bond')
271 $self->throw("Bad operator $op: had multiple locations ".
272 scalar(@loc_objs).", should be SplitLocationI");
275 $loc = Bio
::Location
::Split
->new();
276 $loc->add_sub_Location(shift @loc_objs) while (@loc_objs);
279 $loc = shift @loc_objs;
282 } else { # simple location(s)
283 $loc = $self->_parse_location($locstr);
284 $loc->strand(-1) if ($op && $op eq 'complement');
289 =head2 _parse_location
291 Title : _parse_location
292 Usage : $loc = $locfactory->_parse_location( $loc_string)
294 Function: Parses the given location string and returns a location object
295 with start() and end() and strand() set appropriately.
296 Note that this method is private.
297 Returns : A Bio::LocationI implementing object or undef on failure
298 Args : location string
302 sub _parse_location
{
303 my ($self, $locstr) = @_;
305 #$self->debug( "Location parse, processing $locstr\n");
307 if($locstr =~ m{^(\S+):(.*)$}o) {
308 # yes; memorize remote ID and strip from location string
313 # split into start and end
314 my ($start, $end) = split(/\.\./, $locstr);
315 # remove enclosing parentheses if any; note that because of parentheses
316 # possibly surrounding the entire location the parentheses around start
317 # and/or may be asymmetrical
318 # Note: these are from X.Y fuzzy locations, which are deprecated!
319 $start =~ s/(?:^\[+|\]+$)//g if $start;
320 $end =~ s/(?:^\[+|\]+$)//g if $end;
322 # Is this a simple (exact) or a fuzzy location? Simples have exact start
323 # and end, or is between two adjacent bases. Everything else is fuzzy.
324 my $loctype = ".."; # exact with start and end as default
326 $loctype = '?' if ( ($locstr =~ /\?/) && ($locstr !~ /\?\d+/) );
328 my $locclass = "Bio::Location::Simple";
329 if(! defined($end)) {
330 if($locstr =~ /(\d+)([\.\^])(\d+)/) {
334 $locclass = "Bio::Location::Fuzzy"
335 unless (abs($end-$start) <= 1) && ($loctype eq "^");
340 # start_num and end_num are for the numeric only versions of
341 # start and end so they can be compared
343 my ($start_num, $end_num) = ($start,$end);
344 if ( ($start =~ /[\>\<\?\.\^]/) || ($end =~ /[\>\<\?\.\^]/) ) {
345 $locclass = 'Bio::Location::Fuzzy';
346 if($start =~ /(\d+)/) {
351 if ($end =~ /(\d+)/) {
353 } else { $end_num = 0 }
357 if( $start_num > $end_num && $loctype ne '?') {
358 ($start,$end,$strand) = ($end,$start,-1);
360 # instantiate location and initialize
361 $loc = $locclass->new(-verbose
=> $self->verbose,
365 -location_type
=> $loctype);
366 # set remote ID if remote location
369 $loc->seq_id($seqid);