~ubuntu-branches/ubuntu/raring/bioperl/raring

« back to all changes in this revision

Viewing changes to Bio/Seq/Meta.pm

  • Committer: Bazaar Package Importer
  • Author(s): Charles Plessy
  • Date: 2008-03-18 14:44:57 UTC
  • mfrom: (4 hardy)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20080318144457-1jjoztrvqwf0gruk
* debian/control:
  - Removed MIA Matt Hope (dopey) from the Uploaders field.
    Thank you for your work, Matt. I hope you are doing well.
  - Downgraded some recommended package to the 'Suggests' priority,
    according to the following discussion on Upstream's mail list.
    http://bioperl.org/pipermail/bioperl-l/2008-March/027379.html
    (Closes: #448890)
* debian/copyright converted to machine-readable format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: Meta.pm,v 1.4 2003/11/13 16:57:31 heikki Exp $
 
1
# $Id: Meta.pm,v 1.12.4.2 2006/11/08 19:00:33 cjfields Exp $
2
2
#
3
3
# BioPerl module for Bio::Seq::Meta
4
4
#
12
12
 
13
13
=head1 NAME
14
14
 
15
 
Bio::Seq::MetaI - Generic superclass for sequence objects with
 
15
Bio::Seq::Meta - Generic superclass for sequence objects with
16
16
residue-based meta information
17
17
 
18
18
=head1 SYNOPSIS
25
25
  my $seq = Bio::LocatableSeq->new(-id=>'test',
26
26
                                   -seq=>'ACTGCTAGCT',
27
27
                                   -start=>2434,
28
 
                                   -start=>2443,
 
28
                                   -end=>2443,
29
29
                                   -strand=>1,
30
30
                                   -verbose=>1, # to see warnings
31
31
                                  );
37
37
  $seq->isa("Bio::Seq::Meta")
38
38
      || $seq->throw("$seq is not a Bio::Seq::Meta");
39
39
 
 
40
 
40
41
  $seq->meta('1234567890');
 
42
  $seq = Bio::Seq::Meta->new(-id=>'test',
 
43
                             -seq=>'HACILMIFGT',
 
44
                             -start=>2434,
 
45
                             -end=>2443,
 
46
                             -strand=>1,
 
47
                             -meta=>'1234567890',
 
48
                             -verbose=>1, # to see warnings
 
49
                            );
41
50
 
42
51
  # accessors
43
 
 
44
52
  $string     = $seq->meta_text();
45
53
  $substring  = $seq->submeta_text(2,5);
46
54
  $unique_key = $seq->accession_number();
64
72
The meta information in this class is always one character per residue
65
73
long and blank values are space characters (ASCII 32).
66
74
 
67
 
The length of the meta data sequence is not dependent on the amount of
68
 
the meta information.  The meta information always covers all the
69
 
residues. If necessary, the implementation quietly truncates or extends
70
 
meta information with blank values.
 
75
After the latest rewrite, the meta information no longer covers all
 
76
the residues automatically. Methods to check the length of meta
 
77
information (L<meta_length>)and to see if the ends are flushed to the
 
78
sequence have been added (L<is_flush>). To force the old
 
79
functionality, set L<force_flush> to true.
71
80
 
72
81
It is assumed that meta data values do not depend on the nucleotide
73
82
sequence strand value.
75
84
Application specific implementations should inherit from this class to
76
85
override and add to these methods.
77
86
 
 
87
L<Bio::Seq::Meta::Array> allows for more complex meta values (scalars
 
88
or objects) to be used.
 
89
 
78
90
=head2 Method naming
79
91
 
80
92
Character based meta data is read and set by method meta() and its
99
111
Unlike subseq(), these methods are able to set values.  If the range
100
112
is not defined, it defaults to the complete sequence.
101
113
 
102
 
=item named_
 
114
=item named
103
115
 
104
116
Prefix B<named_> in method names allows the used to attach multiple
105
117
meta strings to one sequence by explicitly naming them. The name is
115
127
 
116
128
=head1 SEE ALSO
117
129
 
118
 
L<Bio::LocatableSeq>
 
130
L<Bio::LocatableSeq>, 
 
131
L<Bio::Seq::MetaI>, 
 
132
L<Bio::Seq::Meta::Array>
119
133
 
120
134
=head1 FEEDBACK
121
135
 
125
139
Bioperl modules. Send your comments and suggestions preferably to one
126
140
of the Bioperl mailing lists.  Your participation is much appreciated.
127
141
 
128
 
  bioperl-l@bioperl.org                      - General discussion
129
 
  http://bio.perl.org/MailList.html          - About the mailing lists
 
142
  bioperl-l@bioperl.org                  - General discussion
 
143
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
130
144
 
131
145
=head2 Reporting Bugs
132
146
 
133
147
Report bugs to the Bioperl bug tracking system to help us keep track
134
 
the bugs and their resolution.  Bug reports can be submitted via email
135
 
or the web:
 
148
the bugs and their resolution.  Bug reports can be submitted via the
 
149
web:
136
150
 
137
 
  bioperl-bugs@bio.perl.org
138
 
  http://bugzilla.bioperl.org/
 
151
  http://bugzilla.open-bio.org/
139
152
 
140
153
=head1 AUTHOR - Heikki Lehvaslaiho
141
154
 
142
 
Email heikki@ebi.ac.uk
 
155
Email heikki-at-bioperl-dot-org
143
156
 
144
157
=head1 CONTRIBUTORS
145
158
 
146
159
Chad Matsalla, bioinformatics@dieselwurks.com
 
160
 
147
161
Aaron Mackey, amackey@virginia.edu
148
162
 
149
163
=head1 APPENDIX
158
172
 
159
173
 
160
174
package Bio::Seq::Meta;
161
 
use vars qw(@ISA $DEFAULT_NAME $GAP $META_GAP);
 
175
use vars qw($DEFAULT_NAME $GAP $META_GAP);
162
176
use strict;
163
 
use Bio::LocatableSeq;
164
 
use Bio::Seq::MetaI;
165
177
 
166
178
#use overload '""' => \&to_string;
167
179
 
168
 
@ISA = qw( Bio::LocatableSeq Bio::Seq::MetaI );
 
180
use base qw(Bio::LocatableSeq Bio::Seq::MetaI);
169
181
 
170
182
 
171
183
BEGIN {
196
208
 
197
209
    my $self = $class->SUPER::new(@args);
198
210
 
199
 
    my($meta) =
 
211
    my($meta, $forceflush) =
200
212
        $self->_rearrange([qw(META
 
213
                              FORCE_FLUSH
201
214
                              )],
202
215
                          @args);
203
216
 
204
217
    #$self->{'_meta'} = {};
205
 
    $self->{'_meta'}->{$DEFAULT_NAME} = undef;
 
218
    $self->{'_meta'}->{$DEFAULT_NAME} = "";
206
219
 
207
220
    $meta && $self->meta($meta);
 
221
    $forceflush && $self->force_flush($forceflush);
208
222
 
209
223
    return $self;
210
224
}
221
235
           sequence, it needs to be manipulated after the sequence.
222
236
 
223
237
           The length of the returned value always matches the length
224
 
           of the sequence.
 
238
           of the sequence, if force_flush() is set. See L<force_flush>.
225
239
 
226
240
 Returns : meta data in a string
227
241
 Args    : new value, string, optional
278
292
 
279
293
       #$self->_test_gap_positions($name) if $self->verbose > 0;
280
294
   }
281
 
   return substr($self->{'_meta'}->{$name}, 0, $self->length)
282
 
       if defined $self->{'_meta'}->{$name} and
283
 
           CORE::length($self->{'_meta'}->{$name}) > $self->length;
284
 
   return $self->{'_meta'}->{$name} || (" " x $self->length);
 
295
 
 
296
   return " " x $self->length 
 
297
    if $self->force_flush && not defined $self->{'_meta'}->{$name};
 
298
 
 
299
 
 
300
   $self->_do_flush if $self->force_flush;
 
301
 
 
302
   return $self->{'_meta'}->{$name};
285
303
}
286
304
 
287
305
=head2 _test_gap_positions
314
332
        my $m = substr $self->{_meta}->{$name}, $i, 1;
315
333
        $self->warn("Gap mismatch [$m/$s] in column [". ($i+1). "] of [$name] meta data in seq [". $self->id. "]")
316
334
            and $success = 0
317
 
                #if ($s eq $GAP || $m eq $GAP) && $s ne $m;
318
335
                if ($s eq $META_GAP) && $s ne $m;
319
336
    }
320
337
    return $success;
405
422
 
406
423
    $name ||= $DEFAULT_NAME;
407
424
    $start ||=1;
 
425
 
 
426
 
408
427
    $start =~ /^[+]?\d+$/ and $start > 0 or
409
428
        $self->throw("Need at least a positive integer start value");
410
429
 
411
430
    if ($value) {
 
431
        $end ||= $start+length($value)-1;
412
432
        $self->warn("You are setting meta values beyond the length of the sequence\n".
413
433
                    "[$start > ". length($self->seq)."] in sequence ". $self->id)
414
434
            if $start > length $self->seq;
415
435
 
416
436
        # pad meta data if needed
 
437
        $self->{_meta}->{$name} = () unless defined $self->{_meta}->{$name};
417
438
        if (length($self->{_meta}->{$name}) < $start) {
418
 
            $self->{'_meta'}->{$name} .=  " " x ( $start - length($self->{'_meta'}->{$name}));
 
439
            $self->{'_meta'}->{$name} .=  " " x ( $start - length($self->{'_meta'}->{$name}) -1);
419
440
        }
420
441
 
421
 
        my $tail = substr ($self->{_meta}->{$name}, $start-1+length($value));
422
 
 
 
442
        my $tail = '';
 
443
        $tail = substr ($self->{_meta}->{$name}, $start-1+length($value))
 
444
            if length($self->{_meta}->{$name}) >= $start-1+length($value);
 
445
        
423
446
        substr ($self->{_meta}->{$name}, --$start) = $value;
424
447
        $self->{_meta}->{$name} .= $tail;
425
448
 
426
 
        return $value;
 
449
        return substr ($self->{_meta}->{$name}, $start, $end - $start + 1);
427
450
 
428
451
    } else {
429
452
 
430
453
        $end or $end = length $self->seq;
431
 
        $end = length $self->seq if $end > length $self->seq;
432
454
 
433
455
        # pad meta data if needed
434
456
        if (length($self->{_meta}->{$name}) < $end) {
463
485
 Title   : meta_names
464
486
 Usage   : @meta_names  = $obj->meta_names()
465
487
 Function: Retrieves an array of meta data set names. The default
466
 
           (unnamed) set name is guarantied to be the first name if it
467
 
           contains any data.
 
488
           (unnamed) set name is guarantied to be the first name.
468
489
 Returns : an array of names
469
490
 Args    : none
470
491
 
479
500
    }
480
501
    unshift @r, $DEFAULT_NAME if $self->{'_meta'}->{$DEFAULT_NAME};
481
502
    return @r;
482
 
 }
 
503
}
 
504
 
 
505
 
 
506
=head2 meta_length
 
507
 
 
508
 Title   : meta_length()
 
509
 Usage   : $meeta_len  = $obj->meta_length();
 
510
 Function: return the number of elements in the meta set
 
511
 Returns : integer
 
512
 Args    : -
 
513
 
 
514
=cut
 
515
 
 
516
sub meta_length {
 
517
   my ($self) = @_;
 
518
   return $self->named_meta_length($DEFAULT_NAME);
 
519
}
 
520
 
 
521
 
 
522
=head2 named_meta_length
 
523
 
 
524
 Title   : named_meta_length()
 
525
 Usage   : $meta_len  = $obj->named_meta_length($name);
 
526
 Function: return the number of elements in the named meta set
 
527
 Returns : integer
 
528
 Args    : -
 
529
 
 
530
=cut
 
531
 
 
532
sub named_meta_length {
 
533
   my ($self, $name) = @_;
 
534
   $name ||= $DEFAULT_NAME;
 
535
   return length ($self->{'_meta'}->{$name});
 
536
}
 
537
 
 
538
 
 
539
=head2 force_flush
 
540
 
 
541
 Title   : force_flush()
 
542
 Usage   : $force_flush = $obj->force_flush(1);
 
543
 Function: Automatically pad with empty values or truncate meta values
 
544
           to sequence length. Not done by default.
 
545
 Returns : boolean 1 or 0
 
546
 Args    : optional boolean value
 
547
 
 
548
Note that if you turn this forced padding off, the previously padded
 
549
values are not changed.
 
550
 
 
551
=cut
 
552
 
 
553
sub force_flush {
 
554
    my ($self, $value) = @_;
 
555
 
 
556
    if (defined $value) {
 
557
        if ($value) {
 
558
            $self->{force_flush} = 1;
 
559
            $self->_do_flush;
 
560
        } else {
 
561
            $self->{force_flush} = 0;
 
562
        }
 
563
    }
 
564
    return $self->{force_flush};
 
565
}
 
566
 
 
567
 
 
568
=head2 _do_flush
 
569
 
 
570
 Title   : _do_flush
 
571
 Usage   : 
 
572
 Function: internal method to do the force that meta values are same 
 
573
           length as the sequence . Called from L<force_flush>
 
574
 Returns : 
 
575
 Args    : 
 
576
 
 
577
=cut
 
578
 
 
579
 
 
580
sub _do_flush {
 
581
    my ($self) = @_;
 
582
 
 
583
    foreach my $name ( ('DEFAULT', $self->meta_names) ) {
 
584
 
 
585
        # elongnation
 
586
        if ($self->length > $self->named_meta_length($name)) {
 
587
            $self->{'_meta'}->{$name} .= $META_GAP x ($self->length - $self->named_meta_length($name)) ;
 
588
        }
 
589
        # truncation
 
590
        elsif ( $self->length < $self->named_meta_length($name) ) {
 
591
            $self->{_meta}->{$name} = substr($self->{_meta}->{$name}, 0, $self->length-1);
 
592
        }
 
593
    }
 
594
 
 
595
}
 
596
 
 
597
 
 
598
=head2 is_flush
 
599
 
 
600
 Title   : is_flush
 
601
 Usage   : $is_flush  = $obj->is_flush()
 
602
           or  $is_flush = $obj->is_flush($my_meta_name)
 
603
 Function: Boolean to tell if all meta values are in
 
604
           flush with the sequence length.
 
605
           Returns true if force_flush() is set
 
606
           Set verbosity to a positive value to see failed meta sets
 
607
 Returns : boolean 1 or 0
 
608
 Args    : optional name of the meta set
 
609
 
 
610
=cut
 
611
 
 
612
 
 
613
sub is_flush {
 
614
 
 
615
    my ($self, $name) = shift;
 
616
 
 
617
    return 1 if $self->force_flush;
 
618
 
 
619
    my $sticky = '';
 
620
 
 
621
 
 
622
    if ($name) {
 
623
        $sticky .= "$name " if $self->length != $self->named_meta_length($name);
 
624
    } else {
 
625
        foreach my $m ($self->meta_names) {
 
626
            $sticky .= "$m " if $self->length != $self->named_meta_length($m);
 
627
        }
 
628
    }
 
629
 
 
630
    if ($sticky) {
 
631
        print "These meta set are not flush: $sticky\n" if $self->verbose; 
 
632
        return 0;
 
633
    }
 
634
 
 
635
    return 1;
 
636
}
483
637
 
484
638
 
485
639
=head1 Bio::PrimarySeqI methods
492
646
           the order of residues and their meta information is reversed.
493
647
 Returns : A new (fresh) Bio::Seq::Meta object
494
648
 Args    : none
 
649
 Throws  : if the object returns false on is_flush()
 
650
 
 
651
Note: The method does nothing to meta values, it reorders them, only.
495
652
 
496
653
=cut
497
654
 
498
655
sub revcom {
499
656
    my $self = shift;
500
657
 
 
658
    $self->throw("Can not get a reverse complement. The object is not flush.")
 
659
        unless $self->is_flush;
 
660
 
501
661
    my $new = $self->SUPER::revcom;
502
662
    foreach (keys %{$self->{_meta}}) {
503
663
        $new->named_meta($_, scalar reverse $self->{_meta}->{$_} );
544
704
    my $out = Bio::SeqIO->new(-format=>'metafasta');
545
705
    $out->write_seq($self);
546
706
    return 1;
547
 
#    undef;
548
707
}
549
708
 
550
709
1;