| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SeqIO::FTHelper | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Ewan Birney <birney@ebi.ac.uk> | ||
| 7 | # | ||
| 8 | # Copyright Ewan Birney | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::SeqIO::FTHelper - Helper class for Embl/Genbank feature tables | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | Used by FAST::Bio::SeqIO::EMBL,FAST::Bio::SeqIO::genbank, and FAST::Bio::SeqIO::swiss to | ||
| 21 | help process the Feature Table | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Represents one particular Feature with the following fields | ||
| 26 | |||
| 27 | key - the key of the feature | ||
| 28 | loc - the location string of the feature | ||
| 29 | <other fields> - other fields | ||
| 30 | |||
| 31 | =head1 FEEDBACK | ||
| 32 | |||
| 33 | =head2 Mailing Lists | ||
| 34 | |||
| 35 | User feedback is an integral part of the evolution of this and other | ||
| 36 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 37 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 38 | |||
| 39 | bioperl-l@bioperl.org - General discussion | ||
| 40 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 41 | |||
| 42 | =head2 Support | ||
| 43 | |||
| 44 | Please direct usage questions or support issues to the mailing list: | ||
| 45 | |||
| 46 | I<bioperl-l@bioperl.org> | ||
| 47 | |||
| 48 | rather than to the module maintainer directly. Many experienced and | ||
| 49 | reponsive experts will be able look at the problem and quickly | ||
| 50 | address it. Please include a thorough description of the problem | ||
| 51 | with code and data examples if at all possible. | ||
| 52 | |||
| 53 | =head2 Reporting Bugs | ||
| 54 | |||
| 55 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 56 | the bugs and their resolution. Bug reports can be submitted via the web: | ||
| 57 | |||
| 58 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 59 | |||
| 60 | =head1 AUTHOR - Ewan Birney | ||
| 61 | |||
| 62 | Email birney@ebi.ac.uk | ||
| 63 | |||
| 64 | =head1 CONTRIBUTORS | ||
| 65 | |||
| 66 | Jason Stajich jason@bioperl.org | ||
| 67 | |||
| 68 | =head1 APPENDIX | ||
| 69 | |||
| 70 | The rest of the documentation details each of the object | ||
| 71 | methods. Internal methods are usually preceded with a _ | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | |||
| 76 | # Let the code begin... | ||
| 77 | |||
| 78 | |||
| 79 | package FAST::Bio::SeqIO::FTHelper; | ||
| 80 | use strict; | ||
| 81 | |||
| 82 | use FAST::Bio::SeqFeature::Generic; | ||
| 83 | use FAST::Bio::Location::Simple; | ||
| 84 | use FAST::Bio::Location::Fuzzy; | ||
| 85 | use FAST::Bio::Location::Split; | ||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | use base qw(FAST::Bio::Root::Root); | ||
| 90 | |||
| 91 | sub new { | ||
| 92 | my ($class, @args) = @_; | ||
| 93 | |||
| 94 | # no chained new because we make lots and lots of these. | ||
| 95 | my $self = {}; | ||
| 96 | bless $self,$class; | ||
| 97 | $self->{'_field'} = {}; | ||
| 98 | return $self; | ||
| 99 | } | ||
| 100 | |||
| 101 | =head2 _generic_seqfeature | ||
| 102 | |||
| 103 | Title : _generic_seqfeature | ||
| 104 | Usage : $fthelper->_generic_seqfeature($annseq, "GenBank") | ||
| 105 | Function: processes fthelper into a generic seqfeature | ||
| 106 | Returns : TRUE on success and otherwise FALSE | ||
| 107 | Args : The FAST::Bio::Factory::LocationFactoryI object to use for parsing | ||
| 108 | location strings. The ID (e.g., display_id) of the sequence on which | ||
| 109 | this feature is located, optionally a string indicating the source | ||
| 110 | (GenBank/EMBL/SwissProt) | ||
| 111 | |||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub _generic_seqfeature { | ||
| 116 | my ($fth, $locfac, $seqid, $source) = @_; | ||
| 117 | my ($sf); | ||
| 118 | |||
| 119 | # set a default if not specified | ||
| 120 | if(! defined($source)) { | ||
| 121 | $source = "EMBL/GenBank/SwissProt"; | ||
| 122 | } | ||
| 123 | |||
| 124 | # initialize feature object | ||
| 125 | $sf = FAST::Bio::SeqFeature::Generic->direct_new(); | ||
| 126 | |||
| 127 | # parse location; this may cause an exception, in which case we gently | ||
| 128 | # recover and ignore this feature | ||
| 129 | |||
| 130 | |||
| 131 | my $loc; | ||
| 132 | eval { | ||
| 133 | $loc = $locfac->from_string($fth->loc); | ||
| 134 | }; | ||
| 135 | |||
| 136 | if(! $loc) { | ||
| 137 | $fth->warn("exception while parsing location line [" . $fth->loc . | ||
| 138 | "] in reading $source, ignoring feature " . | ||
| 139 | $fth->key() . " (seqid=" . $seqid . "): " . $@); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | # set additional location attributes | ||
| 144 | if($seqid && (! $loc->is_remote())) { | ||
| 145 | $loc->seq_id($seqid); # propagates if it is a split location | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | # set attributes of feature | ||
| 150 | $sf->location($loc); | ||
| 151 | $sf->primary_tag($fth->key); | ||
| 152 | $sf->source_tag($source); | ||
| 153 | $sf->seq_id($seqid); | ||
| 154 | foreach my $key ( keys %{$fth->field} ){ | ||
| 155 | foreach my $value ( @{$fth->field->{$key}} ) { | ||
| 156 | $sf->add_tag_value($key,$value); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | return $sf; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | =head2 from_SeqFeature | ||
| 164 | |||
| 165 | Title : from_SeqFeature | ||
| 166 | Usage : @fthelperlist = FAST::Bio::SeqIO::FTHelper::from_SeqFeature($sf, | ||
| 167 | $context_annseq); | ||
| 168 | Function: constructor of fthelpers from SeqFeatures | ||
| 169 | : | ||
| 170 | : The additional annseq argument is to allow the building of FTHelper | ||
| 171 | : lines relevant to particular sequences (ie, when features are spread over | ||
| 172 | : enteries, knowing how to build this) | ||
| 173 | Returns : an array of FThelpers | ||
| 174 | Args : seq features | ||
| 175 | |||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub from_SeqFeature { | ||
| 180 | my ($sf, $context_annseq) = @_; | ||
| 181 | my @ret; | ||
| 182 | |||
| 183 | # | ||
| 184 | # If this object knows how to make FThelpers, then let it | ||
| 185 | # - this allows us to store *really* weird objects that can write | ||
| 186 | # themselves to the EMBL/GenBank... | ||
| 187 | # | ||
| 188 | |||
| 189 | if ( $sf->can("to_FTHelper") ) { | ||
| 190 | return $sf->to_FTHelper($context_annseq); | ||
| 191 | } | ||
| 192 | |||
| 193 | my $fth = FAST::Bio::SeqIO::FTHelper->new(); | ||
| 194 | my $key = $sf->primary_tag(); | ||
| 195 | my $locstr = $sf->location->to_FTstring; | ||
| 196 | |||
| 197 | # ES 25/06/01 Commented out this code, Jason to double check | ||
| 198 | #The location FT string for all simple subseqfeatures is already | ||
| 199 | #in the Split location FT string | ||
| 200 | |||
| 201 | # going into sub features | ||
| 202 | #foreach my $sub ( $sf->sub_SeqFeature() ) { | ||
| 203 | #my @subfth = &FAST::Bio::SeqIO::FTHelper::from_SeqFeature($sub); | ||
| 204 | #push(@ret, @subfth); | ||
| 205 | #} | ||
| 206 | |||
| 207 | $fth->loc($locstr); | ||
| 208 | $fth->key($key); | ||
| 209 | $fth->field->{'note'} = []; | ||
| 210 | |||
| 211 | # the lines below take specific tags (e.g. /score=23 ) and re-enter them as | ||
| 212 | # new tags like /note="score=25" - if the file is round-tripped this creates | ||
| 213 | # duplicate values | ||
| 214 | |||
| 215 | #$sf->source_tag && do { push(@{$fth->field->{'note'}},"source=" . $sf->source_tag ); }; | ||
| 216 | |||
| 217 | #($sf->can('score') && $sf->score) && do { push(@{$fth->field->{'note'}}, | ||
| 218 | # "score=" . $sf->score ); }; | ||
| 219 | |||
| 220 | #($sf->can('frame') && $sf->frame) && do { push(@{$fth->field->{'note'}}, | ||
| 221 | # "frame=" . $sf->frame ); }; | ||
| 222 | |||
| 223 | #$sf->strand && do { push(@{$fth->field->{'note'}},"strand=" . $sf->strand ); }; | ||
| 224 | |||
| 225 | foreach my $tag ( $sf->get_all_tags ) { | ||
| 226 | # Tags which begin with underscores are considered | ||
| 227 | # private, and are therefore not printed | ||
| 228 | next if $tag =~ /^_/; | ||
| 229 | if ( !defined $fth->field->{$tag} ) { | ||
| 230 | $fth->field->{$tag} = []; | ||
| 231 | } | ||
| 232 | foreach my $val ( $sf->get_tag_values($tag) ) { | ||
| 233 | push(@{$fth->field->{$tag}},$val); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | push(@ret, $fth); | ||
| 237 | |||
| 238 | unless (@ret) { | ||
| 239 | $context_annseq->throw("Problem in processing seqfeature $sf - no fthelpers. Error!"); | ||
| 240 | } | ||
| 241 | foreach my $ft (@ret) { | ||
| 242 | if ( !$ft->isa('FAST::Bio::SeqIO::FTHelper') ) { | ||
| 243 | $sf->throw("Problem in processing seqfeature $sf - made a $fth!"); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | return @ret; | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | =head2 key | ||
| 252 | |||
| 253 | Title : key | ||
| 254 | Usage : $obj->key($newval) | ||
| 255 | Function: | ||
| 256 | Example : | ||
| 257 | Returns : value of key | ||
| 258 | Args : newvalue (optional) | ||
| 259 | |||
| 260 | |||
| 261 | =cut | ||
| 262 | |||
| 263 | sub key { | ||
| 264 | my ($obj, $value) = @_; | ||
| 265 | if ( defined $value ) { | ||
| 266 | $obj->{'key'} = $value; | ||
| 267 | } | ||
| 268 | return $obj->{'key'}; | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | =head2 loc | ||
| 273 | |||
| 274 | Title : loc | ||
| 275 | Usage : $obj->loc($newval) | ||
| 276 | Function: | ||
| 277 | Example : | ||
| 278 | Returns : value of loc | ||
| 279 | Args : newvalue (optional) | ||
| 280 | |||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub loc { | ||
| 285 | my ($obj, $value) = @_; | ||
| 286 | if ( defined $value ) { | ||
| 287 | $obj->{'loc'} = $value; | ||
| 288 | } | ||
| 289 | return $obj->{'loc'}; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | =head2 field | ||
| 294 | |||
| 295 | Title : field | ||
| 296 | Usage : | ||
| 297 | Function: | ||
| 298 | Example : | ||
| 299 | Returns : | ||
| 300 | Args : | ||
| 301 | |||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | sub field { | ||
| 306 | my ($self) = @_; | ||
| 307 | |||
| 308 | return $self->{'_field'}; | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 add_field | ||
| 312 | |||
| 313 | Title : add_field | ||
| 314 | Usage : | ||
| 315 | Function: | ||
| 316 | Example : | ||
| 317 | Returns : | ||
| 318 | Args : | ||
| 319 | |||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | sub add_field { | ||
| 324 | my ($self, $key, $val) = @_; | ||
| 325 | |||
| 326 | if ( !exists $self->field->{$key} ) { | ||
| 327 | $self->field->{$key} = []; | ||
| 328 | } | ||
| 329 | push( @{$self->field->{$key}} , $val); | ||
| 330 | |||
| 331 | } | ||
| 332 | |||
| 333 | 1; |