~ubuntu-branches/ubuntu/oneiric/bioperl/oneiric

« back to all changes in this revision

Viewing changes to Bio/PrimarySeq.pm

  • Committer: Bazaar Package Importer
  • Author(s): Charles Plessy
  • Date: 2007-09-21 22:52:22 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070921225222-tt20m2yy6ycuy2d8
Tags: 1.5.2.102-1
* Developer release.
* Upgraded source package to debhelper 5 and standards-version 3.7.2.
* Added libmodule-build-perl and libtest-harness-perl to
  build-depends-indep.
* Disabled automatic CRAN download.
* Using quilt instead of .diff.gz to manage modifications.
* Updated Recommends list for the binary package.
* Moved the "production-quality" scripts to /usr/bin/.
* New maintainer: Debian-Med packaging team mailing list.
* New uploaders: Charles Plessy and Steffen Moeller.
* Updated Depends, Recommends and Suggests.
* Imported in Debian-Med's SVN repository on Alioth.
* Executing the regression tests during package building.
* Moved the Homepage: field out from the package's description.
* Updated watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: PrimarySeq.pm,v 1.79 2003/07/19 22:20:30 jason Exp $
 
1
# $Id: PrimarySeq.pm,v 1.95.4.4 2006/10/02 23:10:12 sendu Exp $
2
2
#
3
3
# bioperl module for Bio::PrimarySeq
4
4
#
5
 
# Cared for by Ewan Birney <birney@sanger.ac.uk>
 
5
# Cared for by Ewan Birney <birney@ebi.ac.uk>
6
6
#
7
7
# Copyright Ewan Birney
8
8
#
16
16
 
17
17
=head1 SYNOPSIS
18
18
 
19
 
  # The Bio::SeqIO for file reading, Bio::DB::GenBank for
 
19
  # Bio::SeqIO for file reading, Bio::DB::GenBank for
20
20
  # database reading
21
21
 
22
22
  use Bio::Seq;
24
24
  use Bio::DB::GenBank;
25
25
 
26
26
  # make from memory
 
27
 
27
28
  $seqobj = Bio::PrimarySeq->new ( -seq => 'ATGGGGTGGGCGGTGGGTGGTTTG',
28
 
                                   -id  => 'GeneFragment-12',
29
 
                                   -accession_number => 'X78121',
30
 
                                   -alphabet => 'dna',
31
 
                                   -is_circular => 1
32
 
                                   );
33
 
  print "Sequence ", $seqobj->id(), " with accession ", 
 
29
                                   -id  => 'GeneFragment-12',
 
30
                                   -accession_number => 'X78121',
 
31
                                   -alphabet => 'dna',
 
32
                                   -is_circular => 1 );
 
33
  print "Sequence ", $seqobj->id(), " with accession ",
34
34
    $seqobj->accession_number, "\n";
35
35
 
36
36
  # read from file
37
 
  $inputstream = Bio::SeqIO->new(-file => "myseq.fa",-format => 'Fasta');
 
37
 
 
38
  $inputstream = Bio::SeqIO->new(-file => "myseq.fa",
 
39
                                 -format => 'Fasta');
38
40
  $seqobj = $inputstream->next_seq();
39
41
  print "Sequence ", $seqobj->id(), " and desc ", $seqobj->desc, "\n";
40
42
 
41
 
 
42
43
  # to get out parts of the sequence.
43
44
 
44
 
  print "Sequence ", $seqobj->id(), " with accession ", 
 
45
  print "Sequence ", $seqobj->id(), " with accession ",
45
46
    $seqobj->accession_number, " and desc ", $seqobj->desc, "\n";
46
47
 
47
48
  $string  = $seqobj->seq();
48
49
  $string2 = $seqobj->subseq(1,40);
49
50
 
50
 
 
51
51
=head1 DESCRIPTION
52
52
 
53
53
PrimarySeq is a lightweight Sequence object, storing the sequence, its
83
83
Bioperl modules. Send your comments and suggestions preferably to one
84
84
of the Bioperl mailing lists.  Your participation is much appreciated.
85
85
 
86
 
  bioperl-l@bioperl.org             - General discussion
87
 
  http://bio.perl.org/MailList.html - About the mailing lists
 
86
  bioperl-l@bioperl.org                  - General discussion
 
87
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
88
88
 
89
89
=head2 Reporting Bugs
90
90
 
91
91
Report bugs to the Bioperl bug tracking system to help us keep track
92
 
the bugs and their resolution.  Bug reports can be submitted via email
93
 
or the web:
 
92
the bugs and their resolution.  Bug reports can be submitted via the
 
93
web:
94
94
 
95
 
  bioperl-bugs@bio.perl.org
96
 
  http://bugzilla.bioperl.org/
 
95
  http://bugzilla.open-bio.org/
97
96
 
98
97
=head1 AUTHOR - Ewan Birney
99
98
 
100
 
Email birney@sanger.ac.uk
101
 
 
102
 
Describe contact details here
 
99
Email birney@ebi.ac.uk
103
100
 
104
101
=head1 APPENDIX
105
102
 
113
110
 
114
111
 
115
112
package Bio::PrimarySeq;
116
 
use vars qw(@ISA);
 
113
use vars qw($MATCHPATTERN);
117
114
use strict;
118
115
 
119
 
use Bio::Root::Root;
120
 
use Bio::PrimarySeqI;
121
 
use Bio::IdentifiableI;
122
 
use Bio::DescribableI;
 
116
$MATCHPATTERN = 'A-Za-z\-\.\*\?=~';
123
117
 
124
 
@ISA = qw(Bio::Root::Root Bio::PrimarySeqI
125
 
          Bio::IdentifiableI Bio::DescribableI);
 
118
use base qw(Bio::Root::Root Bio::PrimarySeqI Bio::IdentifiableI Bio::DescribableI);
126
119
 
127
120
#
128
121
# setup the allowed values for alphabet()
148
141
           values.
149
142
 Returns : a new Bio::PrimarySeq object
150
143
 Args    : -seq         => sequence string
151
 
           -display_id  => display id of the sequence (locus name) 
 
144
           -display_id  => display id of the sequence (locus name)
152
145
           -accession_number => accession number
153
146
           -primary_id  => primary id (Genbank id)
154
147
           -namespace   => the namespace for the accession
188
181
                              )],
189
182
                          @args);
190
183
    if( defined $id && defined $given_id ) {
191
 
        if( $id ne $given_id ) {
192
 
            $self->throw("Provided both id and display_id constructor ".
193
 
                         "functions. [$id] [$given_id]");       
194
 
        }
 
184
                 if( $id ne $given_id ) {
 
185
                         $self->throw("Provided both id and display_id constructor ".
 
186
                                                          "functions. [$id] [$given_id]");
 
187
                 }
195
188
    }
196
189
    if( defined $given_id ) { $id = $given_id; }
197
190
 
202
195
    # if alphabet is provided we set it first, so that it won't be guessed
203
196
    # when the sequence is set
204
197
    $alphabet && $self->alphabet($alphabet);
205
 
    
 
198
 
206
199
    # if there is an alphabet, and direct is passed in, assumme the alphabet
207
 
    # and sequence is ok 
 
200
    # and sequence is ok
208
201
 
209
202
    if( $direct && $ref_to_seq) {
210
 
        $self->{'seq'} = $$ref_to_seq;
211
 
        if( ! $alphabet ) {
212
 
            $self->_guess_alphabet();
213
 
        } # else it has been set already above
 
203
                 $self->{'seq'} = $$ref_to_seq;
 
204
                 if( ! $alphabet ) {
 
205
                     $self->_guess_alphabet();
 
206
                 } # else it has been set already above
214
207
    } else {
215
 
#       print STDERR "DEBUG: setting sequence to [$seq]\n";
216
 
        # note: the sequence string may be empty
217
 
        $self->seq($seq) if defined($seq);
218
 
    }
 
208
                 #      print STDERR "DEBUG: setting sequence to [$seq]\n";
 
209
                 # note: the sequence string may be empty
 
210
                 $self->seq($seq) if defined($seq);
 
211
         }
219
212
 
220
213
    $id          && $self->display_id($id);
221
214
    $acc         && $self->accession_number($acc);
227
220
    $auth        && $self->authority($auth);
228
221
    defined($v)  && $self->version($v);
229
222
    defined($oid) && $self->object_id($oid);
230
 
    
 
223
 
231
224
    return $self;
232
225
}
233
226
 
234
227
sub direct_seq_set {
235
228
    my $obj = shift;
236
229
    return $obj->{'seq'} = shift if @_;
237
 
    return undef;
 
230
    return;
238
231
}
239
232
 
240
233
 
249
242
 Returns : A scalar
250
243
 Args    : Optionally on set the new value (a string). An optional second
251
244
           argument presets the alphabet (otherwise it will be guessed).
252
 
           Both parameters may also be given in named paramater style
253
 
           with -seq and -alphabet being the names.
254
245
 
255
246
=cut
256
247
 
263
254
 
264
255
   my ($value,$alphabet) = @args;
265
256
 
266
 
 
267
257
   if(@args) {
268
258
       if(defined($value) && (! $obj->validate_seq($value))) {
269
259
           $obj->throw("Attempting to set the sequence to [$value] ".
270
 
                       "which does not look healthy");
271
 
       }
 
260
                                                        "which does not look healthy");
 
261
                }
272
262
       # if a sequence was already set we make sure that we re-adjust the
273
263
       # alphabet, otherwise we skip guessing if alphabet is already set
274
264
       # note: if the new seq is empty or undef, we don't consider that a
275
265
       # change (we wouldn't have anything to guess on anyway)
276
 
       my $is_changed_seq =
277
 
           exists($obj->{'seq'}) && (CORE::length($value || '') > 0);
278
 
       $obj->{'seq'} = $value;
 
266
                my $is_changed_seq =
 
267
                  exists($obj->{'seq'}) && (CORE::length($value || '') > 0);
 
268
                $obj->{'seq'} = $value;
279
269
       # new alphabet overridden by arguments?
280
 
       if($alphabet) {
 
270
                if($alphabet) {
281
271
           # yes, set it no matter what
282
 
           $obj->alphabet($alphabet);
283
 
       } elsif( # if we changed a previous sequence to a new one
284
 
                $is_changed_seq ||
285
 
                # or if there is no alphabet yet at all
286
 
                (! defined($obj->alphabet()))) {
287
 
           # we need to guess the (possibly new) alphabet
288
 
           $obj->_guess_alphabet();
289
 
       } # else (seq not changed and alphabet was defined) do nothing
290
 
       # if the seq is changed, make sure we unset a possibly set length
291
 
       $obj->length(undef) if $is_changed_seq || $obj->{'seq'};
 
272
                        $obj->alphabet($alphabet);
 
273
                } elsif( # if we changed a previous sequence to a new one
 
274
                                  $is_changed_seq ||
 
275
                                  # or if there is no alphabet yet at all
 
276
                                  (! defined($obj->alphabet()))) {
 
277
                        # we need to guess the (possibly new) alphabet
 
278
                        $obj->_guess_alphabet();
 
279
                } # else (seq not changed and alphabet was defined) do nothing
 
280
                # if the seq is changed, make sure we unset a possibly set length
 
281
                $obj->length(undef) if $is_changed_seq || $obj->{'seq'};
292
282
   }
293
283
   return $obj->{'seq'};
294
284
}
297
287
 
298
288
 Title   : validate_seq
299
289
 Usage   : if(! $seq->validate_seq($seq_str) ) {
300
 
                print "sequence $seq_str is not valid for an object of 
 
290
                print "sequence $seq_str is not valid for an object of
301
291
                alphabet ",$seq->alphabet, "\n";
302
292
           }
303
293
 Function: Validates a given sequence string. A validating sequence string
305
295
           lead to an exception if passed to seq().
306
296
 
307
297
           The implementation provided here does not take alphabet() into
308
 
           account. Allowed are all letters (A-Z) and '-','.', '*' and '?'.
 
298
           account. Allowed are all letters (A-Z) and '-','.','*','?','=',
 
299
           and '~'.
309
300
 
310
301
 Example :
311
302
 Returns : 1 if the supplied sequence string is valid for the object, and
316
307
=cut
317
308
 
318
309
sub validate_seq {
319
 
    my ($self,$seqstr) = @_;
320
 
    if( ! defined $seqstr ){ $seqstr = $self->seq(); }
321
 
    return 0 unless( defined $seqstr); 
322
 
    if((CORE::length($seqstr) > 0) && ($seqstr !~ /^([A-Za-z\-\.\*\?]+)$/)) {
323
 
        $self->warn("seq doesn't validate, mismatch is " .
324
 
                   ($seqstr =~ /([^A-Za-z\-\.\*\?]+)/g));
325
 
        return 0;
326
 
    }
327
 
    return 1;
 
310
        my ($self,$seqstr) = @_;
 
311
        if( ! defined $seqstr ){ $seqstr = $self->seq(); }
 
312
        return 0 unless( defined $seqstr);
 
313
        if((CORE::length($seqstr) > 0) &&
 
314
           ($seqstr !~ /^([$MATCHPATTERN]+)$/)) {
 
315
            $self->warn("seq doesn't validate, mismatch is " .
 
316
                        join(",",($seqstr =~ /([^$MATCHPATTERN]+)/g)));
 
317
                return 0;
 
318
        }
 
319
        return 1;
328
320
}
329
321
 
330
322
=head2 subseq
378
370
           return substr( $self->seq(), $start, ($end-$start));
379
371
       }
380
372
   } else {
381
 
       $self->warn("Incorrect parameters to subseq - must be two integers ".
382
 
                   "or a Bio::LocationI object not ($start,$end)");
 
373
       $self->warn("Incorrect parameters to subseq - must be two integers or a Bio::LocationI object");
 
374
       return;
383
375
   }
384
376
}
385
377
 
413
405
sub length {
414
406
    my $self = shift;
415
407
    my $len = CORE::length($self->seq() || '');
416
 
    
 
408
 
417
409
    if(@_) {
418
 
        my $val = shift;
419
 
        if(defined($val) && $len && ($len != $val)) {
420
 
            $self->throw("You're trying to lie about the length: ".
421
 
                         "is $len but you say ".$val);
422
 
        }
423
 
        $self->{'_seq_length'} = $val;
 
410
                 my $val = shift;
 
411
                 if(defined($val) && $len && ($len != $val)) {
 
412
                         $self->throw("You're trying to lie about the length: ".
 
413
                                                          "is $len but you say ".$val);
 
414
                 }
 
415
                 $self->{'_seq_length'} = $val;
424
416
    } elsif(defined($self->{'_seq_length'})) {
425
 
        return $self->{'_seq_length'};
 
417
                 return $self->{'_seq_length'};
426
418
    }
427
419
    return $len;
428
420
}
457
449
   my ($obj,$value) = @_;
458
450
   if( defined $value) {
459
451
      $obj->{'display_id'} = $value;
460
 
    }
461
 
    return $obj->{'display_id'};
462
 
 
 
452
        }
 
453
        return $obj->{'display_id'};
463
454
}
464
455
 
465
456
=head2 accession_number
478
469
 
479
470
           [Note this method name is likely to change in 1.3]
480
471
 
481
 
           With the new Bio::IdentifiableI interface, this is aliased 
 
472
           With the new Bio::IdentifiableI interface, this is aliased
482
473
           to object_id
483
474
 
484
475
 Returns : A string
490
481
    my( $obj, $acc ) = @_;
491
482
 
492
483
    if (defined $acc) {
493
 
        $obj->{'accession_number'} = $acc;
 
484
                 $obj->{'accession_number'} = $acc;
494
485
    } else {
495
 
        $acc = $obj->{'accession_number'};
496
 
        $acc = 'unknown' unless defined $acc;
 
486
                 $acc = $obj->{'accession_number'};
 
487
                 $acc = 'unknown' unless defined $acc;
497
488
    }
498
489
    return $acc;
499
490
}
516
507
=cut
517
508
 
518
509
sub primary_id {
519
 
   my ($obj,$value) = @_;
520
 
   if( defined $value) {
521
 
      $obj->{'primary_id'} = $value;
522
 
    }
523
 
   if( ! exists $obj->{'primary_id'} ) {
524
 
       return "$obj";
525
 
   }
526
 
   return $obj->{'primary_id'};
 
510
    my $obj = shift;
527
511
 
 
512
    if(@_) {
 
513
                 $obj->{'primary_id'} = shift;
 
514
    }
 
515
    if( ! defined($obj->{'primary_id'}) ) {
 
516
                 return "$obj";
 
517
    }
 
518
    return $obj->{'primary_id'};
528
519
}
529
520
 
530
521
 
532
523
 
533
524
 Title   : alphabet
534
525
 Usage   : if( $obj->alphabet eq 'dna' ) { /Do Something/ }
535
 
 Function: Returns the alphabet of sequence, one of
 
526
 Function: Get/Set the alphabet of sequence, one of
536
527
           'dna', 'rna' or 'protein'. This is case sensitive.
537
528
 
538
529
           This is not called <type> because this would cause
541
532
 Returns : a string either 'dna','rna','protein'. NB - the object must
542
533
           make a call of the type - if there is no alphabet specified it
543
534
           has to guess.
544
 
 Args    : none
 
535
 Args    : optional string to set : 'dna' | 'rna' | 'protein'
545
536
 
546
537
 
547
538
=cut
549
540
sub alphabet {
550
541
    my ($obj,$value) = @_;
551
542
    if (defined $value) {
552
 
        $value = lc $value;
553
 
        unless ( $valid_type{$value} ) {
554
 
            $obj->throw("Alphabet '$value' is not a valid alphabet (".
555
 
                        join(',', map "'$_'", sort keys %valid_type) .
556
 
                        ") lowercase");
557
 
        }
558
 
        $obj->{'alphabet'} = $value;
 
543
                 $value = lc $value;
 
544
                 unless ( $valid_type{$value} ) {
 
545
                         $obj->throw("Alphabet '$value' is not a valid alphabet (".
 
546
                                                         join(',', map "'$_'", sort keys %valid_type) .
 
547
                                                         ") lowercase");
 
548
                 }
 
549
                 $obj->{'alphabet'} = $value;
559
550
    }
560
551
    return $obj->{'alphabet'};
561
552
}
599
590
   my ($self) = @_;
600
591
 
601
592
   return 1;
602
 
 
603
593
}
604
594
 
605
595
=head2 id
673
663
sub version{
674
664
    my ($self,$value) = @_;
675
665
    if( defined $value) {
676
 
        $self->{'_version'} = $value;
 
666
                 $self->{'_version'} = $value;
677
667
    }
678
668
    return $self->{'_version'};
679
669
}
684
674
 Title   : authority
685
675
 Usage   : $authority    = $obj->authority()
686
676
 Function: A string which represents the organisation which
687
 
           granted the namespace, written as the DNS name for  
 
677
           granted the namespace, written as the DNS name for
688
678
           organisation (eg, wormbase.org).
689
679
 
690
680
 Returns : A scalar
694
684
sub authority {
695
685
    my ($obj,$value) = @_;
696
686
    if( defined $value) {
697
 
        $obj->{'authority'} = $value;
 
687
                 $obj->{'authority'} = $value;
698
688
    }
699
689
    return $obj->{'authority'};
700
690
}
715
705
sub namespace{
716
706
    my ($self,$value) = @_;
717
707
    if( defined $value) {
718
 
        $self->{'namespace'} = $value;
 
708
                 $self->{'namespace'} = $value;
719
709
    }
720
710
    return $self->{'namespace'} || "";
721
711
}
733
723
 Function: A string which is what should be displayed to the user.
734
724
           The string should have no spaces (ideally, though a cautious
735
725
           user of this interface would not assumme this) and should be
736
 
           less than thirty characters (though again, double checking 
 
726
           less than thirty characters (though again, double checking
737
727
           this is a good idea).
738
728
 
739
729
           This is aliased to display_id().
749
739
 
750
740
 Title   : description
751
741
 Usage   : $string    = $obj->description()
752
 
 Function: A text string suitable for displaying to the user a 
 
742
 Function: A text string suitable for displaying to the user a
753
743
           description. This string is likely to have spaces, but
754
744
           should not have any newlines or formatting - just plain
755
745
           text. The string should not be greater than 255 characters
820
810
 
821
811
 Title   : _guess_alphabet
822
812
 Usage   :
823
 
 Function:
 
813
 Function: Determines (and sets) the type of sequence: dna, rna, protein
824
814
 Example :
825
 
 Returns :
826
 
 Args    :
 
815
 Returns : one of strings 'dna', 'rna' or 'protein'.
 
816
 Args    : none
827
817
 
828
818
 
829
819
=cut
830
820
 
831
821
sub _guess_alphabet {
832
822
   my ($self) = @_;
833
 
   my ($str,$str2,$total,$atgc,$u,$type);
834
 
 
835
 
   $str = $self->seq();
836
 
   $str =~ s/\-\.\?//g;
837
 
 
838
 
   $total = CORE::length($str);
 
823
   my $type;
 
824
 
 
825
        #return if $self->alphabet;
 
826
 
 
827
   my $str = $self->seq();
 
828
        # Remove char's that clearly denote ambiguity
 
829
   $str =~ s/[-.?x]//gi;
 
830
 
 
831
   my $total = CORE::length($str);
839
832
   if( $total == 0 ) {
840
 
       $self->throw("Got a sequence with no letters in - ".
841
 
                    "cannot guess alphabet [$str]");
 
833
       $self->warn("Got a sequence with no letters in it ".
 
834
                   "cannot guess alphabet [$str]");
 
835
       return '';
842
836
   }
843
 
   
844
 
   $u = ($str =~ tr/Uu//);
845
 
   $atgc = ($str =~ tr/ATGCNatgcn//);
846
 
   
 
837
 
 
838
   my $u = ($str =~ tr/Uu//);
 
839
        # The assumption here is that most of sequences comprised of mainly
 
840
   # ATGC, with some N, will be 'dna' despite the fact that N could
 
841
        # also be Asparagine
 
842
   my $atgc = ($str =~ tr/ATGCNatgcn//);
 
843
 
847
844
   if( ($atgc / $total) > 0.85 ) {
848
845
       $type = 'dna';
849
846
   } elsif( (($atgc + $u) / $total) > 0.85 ) {