sync with trunk (to r15946)
[bioperl-live.git] / Bio / SeqIO / scf.pm
blobcbd09451ba3e4feac76b869fccd0e8b6b92cd2d9
1 # $Id$
3 # Copyright (c) 1997-2001 bioperl, Chad Matsalla. All Rights Reserved.
4 # This module is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
7 # Copyright Chad Matsalla
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
13 =head1 NAME
15 Bio::SeqIO::scf - .scf file input/output stream
17 =head1 SYNOPSIS
19 Do not use this module directly. Use it via the Bio::SeqIO class, see
20 L<Bio::SeqIO> for more information.
22 =head1 DESCRIPTION
24 This object can transform .scf files to and from Bio::Seq::SequenceTrace
25 objects. Mechanisms are present to retrieve trace data from scf
26 files.
28 =head1 FEEDBACK
30 =head2 Mailing Lists
32 User feedback is an integral part of the evolution of this and other
33 Bioperl modules. Send your comments and suggestions preferably to one
34 of the Bioperl mailing lists. Your participation is much appreciated.
36 bioperl-l@bioperl.org - General discussion
37 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
39 =head2 Support
41 Please direct usage questions or support issues to the mailing list:
43 L<bioperl-l@bioperl.org>
45 rather than to the module maintainer directly. Many experienced and
46 reponsive experts will be able look at the problem and quickly
47 address it. Please include a thorough description of the problem
48 with code and data examples if at all possible.
50 =head2 Reporting Bugs
52 Report bugs to the Bioperl bug tracking system to help us keep track
53 the bugs and their resolution. Bug reports can be submitted via
54 the web:
56 http://bugzilla.open-bio.org/
58 =head1 AUTHOR Chad Matsalla
60 Chad Matsalla
61 bioinformatics@dieselwurks.com
63 =head1 CONTRIBUTORS
65 Jason Stajich, jason@bioperl.org
66 Tony Cox, avc@sanger.ac.uk
67 Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
68 Nancy Hansen, nhansen at mail.nih.gov
70 =head1 APPENDIX
72 The rest of the documentation details each of the object
73 methods. Internal methods are usually preceded with a _
75 =cut
77 # Let the code begin...
79 package Bio::SeqIO::scf;
80 use vars qw($DEFAULT_QUALITY);
81 use strict;
82 use Bio::Seq::SeqFactory;
83 use Bio::Seq::SequenceTrace;
84 use Bio::Annotation::Comment;
85 use Dumpvalue;
87 my $dumper = new Dumpvalue();
88 $dumper->veryCompact(1);
90 BEGIN {
91 $DEFAULT_QUALITY= 10;
94 use base qw(Bio::SeqIO);
96 sub _initialize {
97 my($self,@args) = @_;
98 $self->SUPER::_initialize(@args);
99 if( ! defined $self->sequence_factory ) {
100 $self->sequence_factory(Bio::Seq::SeqFactory->new
101 (-verbose => $self->verbose(),
102 -type => 'Bio::Seq::Quality'));
106 =head2 next_seq()
108 Title : next_seq()
109 Usage : $scf = $stream->next_seq()
110 Function: returns the next scf sequence in the stream
111 Returns : a Bio::Seq::SequenceTrace object
112 Args : NONE
113 Notes : Fills the interface specification for SeqIO.
114 The SCF specification does not provide for having more then
115 one sequence in a given scf. So once the filehandle has been open
116 and passed to SeqIO do not expect to run this function more then
117 once on a given scf unless you embraced and extended the SCF
118 standard. SCF comments are accessible through the Bio::SeqI
119 interface method annotation().
121 =cut
124 sub next_seq {
125 my ($self) = @_;
126 my ($seq, $seqc, $fh, $buffer, $offset, $length, $read_bytes, @read,
127 %names);
128 # set up a filehandle to read in the scf
129 return if $self->{_readfile};
130 $fh = $self->_fh();
131 unless ($fh) { # simulate the <> function
132 if ( !fileno(ARGV) or eof(ARGV) ) {
133 return unless my $ARGV = shift;
134 open(ARGV,$ARGV) or
135 $self->throw("Could not open $ARGV for SCF stream reading $!");
137 $fh = \*ARGV;
139 binmode $fh; # for the Win32/Mac crowds
140 return unless read $fh, $buffer, 128; # no exception; probably end of file
141 # now, the master data structure will be the creator
142 my $creator;
143 # he first thing to do is parse the header. This is common
144 # among all versions of scf.
145 # the rest of the the information is different between the
146 # the different versions of scf.
148 $creator->{header} = $self->_get_header($buffer);
149 if ($creator->{header}->{'version'} lt "3.00") {
150 $self->debug("scf.pm is working with a version 2 scf.\n");
151 # first gather the trace information
152 $length = $creator->{header}->{'samples'} *
153 $creator->{header}->{sample_size}*4;
154 $buffer = $self->read_from_buffer($fh, $buffer, $length,
155 $creator->{header}->{samples_offset});
156 # @read = unpack "n$length",$buffer;
157 # these traces need to be split
158 # returns a reference to a hash
159 $creator->{traces} = $self->_parse_v2_traces(
160 $buffer,$creator->{header}->{sample_size});
161 # now go and get the base information
162 $offset = $creator->{header}->{bases_offset};
163 $length = ($creator->{header}->{bases} * 12);
164 seek $fh,$offset,0;
165 $buffer = $self->read_from_buffer($fh,$buffer,$length,$creator->{header}->{bases_offset});
166 # now distill the information into its fractions.
167 # the old way : $self->_set_v2_bases($buffer);
168 # ref to an array, ref to a hash, string
169 ($creator->{peak_indices},
170 $creator->{qualities},
171 $creator->{sequence},
172 $creator->{accuracies}) = $self->_parse_v2_bases($buffer);
174 } else {
175 $self->debug("scf.pm is working with a version 3+ scf.\n");
176 my $transformed_read;
177 my $current_read_position = $creator->{header}->{sample_offset};
178 $length = $creator->{header}->{'samples'}*
179 $creator->{header}->{sample_size};
180 # $dumper->dumpValue($creator->{header});
181 foreach (qw(a c g t)) {
182 $buffer = $self->read_from_buffer($fh,$buffer,$length,$current_read_position);
183 my $byte = "n";
184 if ($creator->{header}->{sample_size} == 1) {
185 $byte = "c";
187 @read = unpack "${byte}${length}",$buffer;
188 # this little spurt of nonsense is because
189 # the trace values are given in the binary
190 # file as unsigned shorts but they really
191 # are signed deltas. 30000 is an arbitrary number
192 # (will there be any traces with a given
193 # point greater then 30000? I hope not.
194 # once the read is read, it must be changed
195 # from relative
196 foreach (@read) {
197 if ($_ > 30000) {
198 $_ -= 65536;
201 $transformed_read = $self->_delta(\@read,"backward");
202 # For 8-bit data we need to emulate a signed/unsigned
203 # cast that is implicit in the C implementations.....
204 if ($creator->{header}->{sample_size} == 1) {
205 foreach (@{$transformed_read}) {
206 $_ += 256 if ($_ < 0);
209 $current_read_position += $length;
210 $creator->{'traces'}->{$_} = join(' ',@{$transformed_read});
213 # now go and get the peak index information
214 $offset = $creator->{header}->{bases_offset};
215 $length = ($creator->{header}->{bases} * 4);
216 $buffer = $self->read_from_buffer($fh,$buffer,$length,$offset);
217 $creator->{peak_indices} = $self->_get_v3_peak_indices($buffer);
218 $offset += $length;
219 # now go and get the accuracy information
220 $buffer = $self->read_from_buffer($fh,$buffer,$length,$offset);
221 $creator->{accuracies} = $self->_get_v3_base_accuracies($buffer);
222 # OK, now go and get the base information.
223 $offset += $length;
224 $length = $creator->{header}->{bases};
225 $buffer = $self->read_from_buffer($fh,$buffer,$length,$offset);
226 $creator->{'sequence'} = unpack("a$length",$buffer);
227 # now, finally, extract the calls from the accuracy information.
228 $creator->{qualities} = $self->_get_v3_quality(
229 $creator->{'sequence'},$creator->{accuracies});
231 # now go and get the comment information
232 $offset = $creator->{header}->{comments_offset};
233 seek $fh,$offset,0;
234 $length = $creator->{header}->{comment_size};
235 $buffer = $self->read_from_buffer($fh,$buffer,$length);
236 $creator->{comments} = $self->_get_comments($buffer);
237 my @name_comments = grep {$_->tagname() eq 'NAME'}
238 $creator->{comments}->get_Annotations('comment');
239 my $name_comment;
240 if (@name_comments){
241 $name_comment = $name_comments[0]->as_text();
242 $name_comment =~ s/^Comment:\s+//;
245 my $swq = Bio::Seq::Quality->new(
246 -seq => $creator->{'sequence'},
247 -qual => $creator->{'qualities'},
248 -id => $name_comment
250 my $returner = Bio::Seq::SequenceTrace->new(
251 -swq => $swq,
252 -trace_a => $creator->{'traces'}->{'a'},
253 -trace_t => $creator->{'traces'}->{'t'},
254 -trace_g => $creator->{'traces'}->{'g'},
255 -trace_c => $creator->{'traces'}->{'c'},
256 -accuracy_a => $creator->{'accuracies'}->{'a'},
257 -accuracy_t => $creator->{'accuracies'}->{'t'},
258 -accuracy_g => $creator->{'accuracies'}->{'g'},
259 -accuracy_c => $creator->{'accuracies'}->{'c'},
260 -peak_indices => $creator->{'peak_indices'}
263 $returner->annotation($creator->{'comments'}); # add SCF comments
264 $self->{'_readfile'} = 1;
265 return $returner;
269 =head2 _get_v3_quality()
271 Title : _get_v3_quality()
272 Usage : $self->_get_v3_quality()
273 Function: Set the base qualities from version3 scf
274 Returns : Nothing. Alters $self.
275 Args : None.
276 Notes :
278 =cut
281 sub _get_v3_quality {
282 my ($self,$sequence,$accuracies) = @_;
283 my @bases = split//,$sequence;
284 my (@qualities,$currbase,$currqual,$counter);
285 for ($counter=0; $counter <= $#bases ; $counter++) {
286 $currbase = lc($bases[$counter]);
287 if ($currbase eq "a") { $currqual = $accuracies->{'a'}->[$counter]; }
288 elsif ($currbase eq "c") { $currqual = $accuracies->{'c'}->[$counter]; }
289 elsif ($currbase eq "g") { $currqual = $accuracies->{'g'}->[$counter]; }
290 elsif ($currbase eq "t") { $currqual = $accuracies->{'t'}->[$counter]; }
291 else { $currqual = "unknown"; }
292 push @qualities,$currqual;
294 return \@qualities;
297 =head2 _get_v3_peak_indices($buffer)
299 Title : _get_v3_peak_indices($buffer)
300 Usage : $self->_get_v3_peak_indices($buffer);
301 Function: Unpacks the base accuracies for version3 scf
302 Returns : Nothing. Alters $self
303 Args : A scalar containing binary data.
304 Notes :
306 =cut
308 sub _get_v3_peak_indices {
309 my ($self,$buffer) = @_;
310 my $length = length($buffer);
311 my @read = unpack "N$length",$buffer;
312 return join(' ',@read);
315 =head2 _get_v3_base_accuracies($buffer)
317 Title : _get_v3_base_accuracies($buffer)
318 Usage : $self->_get_v3_base_accuracies($buffer)
319 Function: Set the base accuracies for version 3 scf's
320 Returns : Nothing. Alters $self.
321 Args : A scalar containing binary data.
322 Notes :
324 =cut
327 sub _get_v3_base_accuracies {
328 my ($self,$buffer) = @_;
329 my $length = length($buffer);
330 my $qlength = $length/4;
331 my $offset = 0;
332 my (@qualities,@sorter,$counter,$round,$last_base,$accuracies,$currbase);
333 foreach $currbase (qw(a c g t)) {
334 my @read;
335 $last_base = $offset + $qlength;
336 for (;$offset < $last_base; $offset += $qlength) {
337 # a bioperler (perhaps me?) changed the unpack string to include 'n' rather than 'C'
338 # on 040322 I think that 'C' is correct. please email chad if you would like to accuse me of being incorrect
339 @read = unpack "C$qlength", substr($buffer,$offset,$qlength);
340 $accuracies->{$currbase} = \@read;
343 return $accuracies;
347 =head2 _get_comments($buffer)
349 Title : _get_comments($buffer)
350 Usage : $self->_get_comments($buffer);
351 Function: Gather the comments section from the scf and parse it into its
352 components.
353 Returns : a Bio::Annotation::Collection object
354 Args : The buffer. It is expected that the buffer contains a binary
355 string for the comments section of an scf file according to
356 the scf file specifications.
357 Notes :
359 =cut
361 sub _get_comments {
362 my ($self,$buffer) = @_;
363 my $comments = Bio::Annotation::Collection->new();
364 my $size = length($buffer);
365 my $comments_retrieved = unpack "a$size",$buffer;
366 $comments_retrieved =~ s/\0//;
367 my @comments_split = split/\n/,$comments_retrieved;
368 if (@comments_split) {
369 foreach (@comments_split) {
370 /(\w+)=(.*)/;
371 if ($1 && $2) {
372 my ($tagname, $text) = ($1, $2);
373 my $comment_obj = Bio::Annotation::Comment->new(
374 -text => $text,
375 -tagname => $tagname);
377 $comments->add_Annotation('comment', $comment_obj);
381 $self->{'comments'} = $comments;
382 return $comments;
385 =head2 _get_header()
387 Title : _get_header($buffer)
388 Usage : $self->_get_header($buffer);
389 Function: Gather the header section from the scf and parse it into its
390 components.
391 Returns : Reference to a hash containing the header components.
392 Args : The buffer. It is expected that the buffer contains a binary
393 string for the header section of an scf file according to the
394 scf file specifications.
395 Notes : None.
397 =cut
399 sub _get_header {
400 my ($self,$buffer) = @_;
401 my $header;
402 ($header->{'scf'},
403 $header->{'samples'},
404 $header->{'sample_offset'},
405 $header->{'bases'},
406 $header->{'bases_left_clip'},
407 $header->{'bases_right_clip'},
408 $header->{'bases_offset'},
409 $header->{'comment_size'},
410 $header->{'comments_offset'},
411 $header->{'version'},
412 $header->{'sample_size'},
413 $header->{'code_set'},
414 @{$header->{'header_spare'}} ) = unpack "a4 NNNNNNNN a4 NN N20", $buffer;
416 $self->{'header'} = $header;
417 return $header;
420 =head2 _parse_v2_bases($buffer)
422 Title : _parse_v2_bases($buffer)
423 Usage : $self->_parse_v2_bases($buffer);
424 Function: Gather the bases section from the scf and parse it into its
425 components.
426 Returns :
427 Args : The buffer. It is expected that the buffer contains a binary
428 string for the bases section of an scf file according to the
429 scf file specifications.
430 Notes : None.
432 =cut
434 sub _parse_v2_bases {
435 my ($self,$buffer) = @_;
436 my $length = length($buffer);
437 my ($offset2,$currbuff,$currbase,$currqual,$sequence,@qualities,@indices);
438 my (@read,$harvester,$accuracies);
439 for ($offset2=0;$offset2<$length;$offset2+=12) {
440 @read = unpack "N C C C C a C3", substr($buffer,$offset2,$length);
441 push @indices,$read[0];
442 $currbase = lc($read[5]);
443 if ($currbase eq "a") { $currqual = $read[1]; }
444 elsif ($currbase eq "c") { $currqual = $read[2]; }
445 elsif ($currbase eq "g") { $currqual = $read[3]; }
446 elsif ($currbase eq "t") { $currqual = $read[4]; }
447 else { $currqual = "UNKNOWN"; }
448 push @{$accuracies->{"a"}},$read[1];
449 push @{$accuracies->{"c"}},$read[2];
450 push @{$accuracies->{"g"}},$read[3];
451 push @{$accuracies->{"t"}},$read[4];
453 $sequence .= $currbase;
454 push @qualities,$currqual;
456 return (\@indices,\@qualities,$sequence,$accuracies)
459 =head2 _parse_v2_traces(\@traces_array)
461 Title : _pares_v2_traces(\@traces_array)
462 Usage : $self->_parse_v2_traces(\@traces_array);
463 Function: Parses an scf Version2 trace array into its base components.
464 Returns : Nothing. Modifies $self.
465 Args : A reference to an array of the unpacked traces section of an
466 scf version2 file.
468 =cut
470 sub _parse_v2_traces {
471 my ($self,$buffer,$sample_size) = @_;
472 my $byte;
473 if ($sample_size == 1) { $byte = "c"; }
474 else { $byte = "n"; }
475 my $length = CORE::length($buffer);
476 my @read = unpack "${byte}${length}",$buffer;
477 # this will be an array to the reference holding the array
478 my $traces;
479 my $array = 0;
480 for (my $offset2 = 0; $offset2< scalar(@read); $offset2+=4) {
481 push @{$traces->{'a'}},$read[$offset2];
482 push @{$traces->{'t'}},$read[$offset2+1];
483 push @{$traces->{'g'}},$read[$offset2+3];
484 push @{$traces->{'c'}},$read[$offset2+2];
486 return $traces;
490 sub get_trace_deprecated_use_the_sequencetrace_object_instead {
491 # my ($self,$base_channel,$traces) = @_;
492 # $base_channel =~ tr/a-z/A-Z/;
493 # if ($base_channel !~ /A|T|G|C/) {
494 # $self->throw("You tried to ask for a base channel that wasn't A,T,G, or C. Ask for one of those next time.");
495 ##} elsif ($base_channel) {
496 # my @temp = split(' ',$self->{'traces'}->{$base_channel});
497 #return \@temp;
501 sub _deprecated_get_peak_indices_deprecated_use_the_sequencetrace_object_instead {
502 my ($self) = shift;
503 my @temp = split(' ',$self->{'parsed'}->{'peak_indices'});
504 return \@temp;
508 =head2 get_header()
510 Title : get_header()
511 Usage : %header = %{$obj->get_header()};
512 Function: Return the header for this scf.
513 Returns : A reference to a hash containing the header for this scf.
514 Args : None.
515 Notes :
517 =cut
519 sub get_header {
520 my ($self) = shift;
521 return $self->{'header'};
524 =head2 get_comments()
526 Title : get_comments()
527 Usage : %comments = %{$obj->get_comments()};
528 Function: Return the comments for this scf.
529 Returns : A Bio::Annotation::Collection object
530 Args : None.
531 Notes :
533 =cut
535 sub get_comments {
536 my ($self) = shift;
537 return $self->{'comments'};
540 sub _dump_traces_outgoing_deprecated_use_the_sequencetrace_object {
541 my ($self,$transformed) = @_;
542 my (@sA,@sT,@sG,@sC);
543 if ($transformed) {
544 @sA = @{$self->{'text'}->{'t_samples_a'}};
545 @sC = @{$self->{'text'}->{'t_samples_c'}};
546 @sG = @{$self->{'text'}->{'t_samples_g'}};
547 @sT = @{$self->{'text'}->{'t_samples_t'}};
549 else {
550 @sA = @{$self->{'text'}->{'samples_a'}};
551 @sC = @{$self->{'text'}->{'samples_c'}};
552 @sG = @{$self->{'text'}->{'samples_g'}};
553 @sT = @{$self->{'text'}->{'samples_t'}};
555 print ("Count\ta\tc\tg\tt\n");
556 for (my $curr=0; $curr < scalar(@sG); $curr++) {
557 print("$curr\t$sA[$curr]\t$sC[$curr]\t$sG[$curr]\t$sT[$curr]\n");
559 return;
562 sub _dump_traces_incoming_deprecated_use_the_sequencetrace_object {
563 # my ($self) = @_;
564 # my (@sA,@sT,@sG,@sC);
565 # @sA = @{$self->{'traces'}->{'A'}};
566 # @sC = @{$self->{'traces'}->{'C'}};
567 # @sG = @{$self->{'traces'}->{'G'}};
568 # @sT = @{$self->{'traces'}->{'T'}};
569 # @sA = @{$self->get_trace('A')};
570 # @sC = @{$self->get_trace('C')};
571 # @sG = @{$self->get_trace('G')};
572 # @sT = @{$self->get_trace('t')};
573 # print ("Count\ta\tc\tg\tt\n");
574 # for (my $curr=0; $curr < scalar(@sG); $curr++) {
575 # print("$curr\t$sA[$curr]\t$sC[$curr]\t$sG[$curr]\t$sT[$curr]\n");
577 #return;
580 =head2 write_seq
582 Title : write_seq(-target => $swq, <comments>)
583 Usage : $obj->write_seq(
584 -target => $swq,
585 -version => 2,
586 -CONV => "Bioperl-Chads Mighty SCF writer.");
587 Function: Write out an scf.
588 Returns : Nothing.
589 Args : Requires: a reference to a Bio::Seq::Quality object to form the
590 basis for the scf.
591 if -version is provided, it should be "2" or "3". A SCF of that
592 version will be written.
593 Any other arguments are assumed to be comments and are put into
594 the comments section of the scf. Read the specifications for scf
595 to decide what might be good to put in here.
597 Notes :
598 For best results, use a SequenceTrace object.
599 The things that you need to write an scf:
600 a) sequence
601 b) quality
602 c) peak indices
603 d) traces
604 - You _can_ write an scf with just a and b by passing in a
605 Bio::Seq::Quality object- false traces will be synthesized
606 for you.
608 =cut
610 sub write_seq {
611 my ($self,%args) = @_;
612 my %comments;
613 my ($label,$arg);
614 my ($swq) = $self->_rearrange([qw(TARGET)], %args);
615 my $writer_fodder;
616 if (ref($swq) =~ /Bio::Seq::SequenceTrace|Bio::Seq::Quality/) {
617 if (ref($swq) eq "Bio::Seq::Quality") {
618 # this means that the object *has no trace data*
619 # we might as well synthesize some now, ok?
620 $swq = Bio::Seq::SequenceTrace->new(
621 -swq => $swq
625 else {
626 $self->throw("You must pass a Bio::Seq::Quality or a Bio::Seq::SequenceTrace object to write_seq as a parameter named \"target\"");
628 # all of the rest of the arguments are comments for the scf
629 foreach $arg (sort keys %args) {
630 next if ($arg =~ /target/i);
631 ($label = $arg) =~ s/^\-//;
632 $writer_fodder->{comments}->{$label} = $args{$arg};
634 if (!$comments{'NAME'}) { $comments{'NAME'} = $swq->id(); }
635 # HA! Bwahahahaha.
636 $writer_fodder->{comments}->{'CONV'} = "Bioperl-Chads Mighty SCF writer." unless defined $comments{'CONV'};
637 # now deal with the version of scf they want to write
638 if ($writer_fodder->{comments}->{version}) {
639 if ($writer_fodder->{comments}->{version} != 2 && $writer_fodder->{comments}->{version} != 3) {
640 $self->warn("This module can only write version 2.0 or 3.0 scf's. Writing a version 2.0 scf by default.");
641 $writer_fodder->{header}->{version} = "2.00";
643 elsif ($writer_fodder->{comments}->{'version'} > 2) {
644 $writer_fodder->{header}->{'version'} = "3.00";
646 else {
647 $writer_fodder->{header}->{version} = "2";
650 else {
651 $writer_fodder->{header}->{'version'} = "3.00";
653 # set a few things in the header
654 $writer_fodder->{'header'}->{'magic'} = ".scf";
655 $writer_fodder->{'header'}->{'sample_size'} = "2";
656 $writer_fodder->{'header'}->{'bases'} = length($swq->seq());
657 $writer_fodder->{'header'}->{'bases_left_clip'} = "0";
658 $writer_fodder->{'header'}->{'bases_right_clip'} = "0";
659 $writer_fodder->{'header'}->{'sample_size'} = "2";
660 $writer_fodder->{'header'}->{'code_set'} = "9";
661 @{$writer_fodder->{'header'}->{'spare'}} = qw(0 0 0 0 0 0 0 0 0 0
662 0 0 0 0 0 0 0 0 0 0);
663 $writer_fodder->{'header'}->{'samples_offset'} = "128";
664 $writer_fodder->{'header'}->{'samples'} = $swq->trace_length();
665 # create the binary for the comments and file it in writer_fodder
666 $writer_fodder->{comments} = $self->_get_binary_comments(
667 $writer_fodder->{comments});
668 # create the binary and the strings for the traces, bases,
669 # offsets (if necessary), and accuracies (if necessary)
670 $writer_fodder->{traces} = $self->_get_binary_traces(
671 $writer_fodder->{'header'}->{'version'},
672 $swq,$writer_fodder->{'header'}->{'sample_size'});
673 my ($b_base_offsets,$b_base_accuracies,$samples_size,$bases_size);
675 # version 2
677 if ($writer_fodder->{'header'}->{'version'} == 2) {
678 $writer_fodder->{bases} = $self->_get_binary_bases(
680 $swq,
681 $writer_fodder->{'header'}->{'sample_size'});
682 $samples_size = CORE::length($writer_fodder->{traces}->{'binary'});
683 $bases_size = CORE::length($writer_fodder->{bases}->{binary});
684 $writer_fodder->{'header'}->{'bases_offset'} = 128 + $samples_size;
685 $writer_fodder->{'header'}->{'comments_offset'} = 128 +
686 $samples_size + $bases_size;
687 $writer_fodder->{'header'}->{'comments_size'} =
688 length($writer_fodder->{'comments'}->{binary});
689 $writer_fodder->{'header'}->{'private_size'} = "0";
690 $writer_fodder->{'header'}->{'private_offset'} = 128 +
691 $samples_size + $bases_size +
692 $writer_fodder->{'header'}->{'comments_size'};
693 $writer_fodder->{'header'}->{'binary'} =
694 $self->_get_binary_header($writer_fodder->{header});
695 $dumper->dumpValue($writer_fodder) if $self->verbose > 0;
696 $self->_print ($writer_fodder->{'header'}->{'binary'})
697 or print("Could not write binary header...\n");
698 $self->_print ($writer_fodder->{'traces'}->{'binary'})
699 or print("Could not write binary traces...\n");
700 $self->_print ($writer_fodder->{'bases'}->{'binary'})
701 or print("Could not write binary base structures...\n");
702 $self->_print ($writer_fodder->{'comments'}->{'binary'})
703 or print("Could not write binary comments...\n");
705 else {
706 ($writer_fodder->{peak_indices},
707 $writer_fodder->{accuracies},
708 $writer_fodder->{bases},
709 $writer_fodder->{reserved} ) =
710 $self->_get_binary_bases(
712 $swq,
713 $writer_fodder->{'header'}->{'sample_size'}
715 $writer_fodder->{'header'}->{'bases_offset'} = 128 +
716 length($writer_fodder->{'traces'}->{'binary'});
717 $writer_fodder->{'header'}->{'comments_size'} =
718 length($writer_fodder->{'comments'}->{'binary'});
719 # this is:
720 # bases_offset + base_offsets + accuracies + called_bases +
721 # reserved
722 $writer_fodder->{'header'}->{'private_size'} = "0";
724 $writer_fodder->{'header'}->{'comments_offset'} =
725 128+length($writer_fodder->{'traces'}->{'binary'})+
726 length($writer_fodder->{'peak_indices'}->{'binary'})+
727 length($writer_fodder->{'accuracies'}->{'binary'})+
728 length($writer_fodder->{'bases'}->{'binary'})+
729 length($writer_fodder->{'reserved'}->{'binary'});
730 $writer_fodder->{'header'}->{'private_offset'} =
731 $writer_fodder->{'header'}->{'comments_offset'} +
732 $writer_fodder->{'header'}->{'comments_size'};
733 $writer_fodder->{'header'}->{'spare'}->[1] =
734 $writer_fodder->{'header'}->{'comments_offset'} +
735 length($writer_fodder->{'comments'}->{'binary'});
736 $writer_fodder->{header}->{binary} =
737 $self->_get_binary_header($writer_fodder->{header});
738 $self->_print ($writer_fodder->{'header'}->{'binary'})
739 or print("Couldn't write header\n");
740 $self->_print ($writer_fodder->{'traces'}->{'binary'})
741 or print("Couldn't write samples\n");
742 $self->_print ($writer_fodder->{'peak_indices'}->{'binary'})
743 or print("Couldn't write peak offsets\n");
744 $self->_print ($writer_fodder->{'accuracies'}->{'binary'})
745 or print("Couldn't write accuracies\n");
746 $self->_print ($writer_fodder->{'bases'}->{'binary'})
747 or print("Couldn't write called_bases\n");
748 $self->_print ($writer_fodder->{'reserved'}->{'binary'})
749 or print("Couldn't write reserved\n");
750 $self->_print ($writer_fodder->{'comments'}->{'binary'})
751 or print ("Couldn't write comments\n");
754 # kinda unnecessary, given the close() below, but maybe that'll go
755 # away someday.
756 $self->flush if $self->_flush_on_write && defined $self->_fh;
758 $self->close();
759 return 1;
766 =head2 _get_binary_header()
768 Title : _get_binary_header();
769 Usage : $self->_get_binary_header();
770 Function: Provide the binary string that will be used as the header for
771 a scfv2 document.
772 Returns : A binary string.
773 Args : None. Uses the entries in the $self->{'header'} hash. These
774 are set on construction of the object (hopefully correctly!).
775 Notes :
777 =cut
779 sub _get_binary_header {
780 my ($self,$header) = @_;
781 my $binary = pack "a4 NNNNNNNN a4 NN N20",
783 $header->{'magic'},
784 $header->{'samples'},
785 $header->{'samples_offset'},
786 $header->{'bases'},
787 $header->{'bases_left_clip'},
788 $header->{'bases_right_clip'},
789 $header->{'bases_offset'},
790 $header->{'comments_size'},
791 $header->{'comments_offset'},
792 $header->{'version'},
793 $header->{'sample_size'},
794 $header->{'code_set'},
795 @{$header->{'spare'}}
797 return $binary;
800 =head2 _get_binary_traces($version,$ref)
802 Title : _set_binary_tracesbases($version,$ref)
803 Usage : $self->_set_binary_tracesbases($version,$ref);
804 Function: Constructs the trace and base strings for all scfs
805 Returns : Nothing. Alters self.
806 Args : $version - "2" or "3"
807 $sequence - a scalar containing arbitrary sequence data
808 $ref - a reference to either a SequenceTraces or a
809 SequenceWithQuality object.
810 Notes : This is a really complicated thing.
812 =cut
814 sub _get_binary_traces {
815 my ($self,$version,$ref,$sample_size) = @_;
816 # ref _should_ be a Bio::Seq::SequenceTrace, but might be a
817 # Bio::Seq::Quality
818 my $returner;
819 my $sequence = $ref->seq();
820 my $sequence_length = length($sequence);
821 # first of all, do we need to synthesize the trace?
822 # if so, call synthesize_base
823 my ($traceobj,@traces,$current);
824 if ( ref($ref) eq "Bio::Seq::Quality" ) {
825 $traceobj = Bio::Seq::Quality->new(
826 -target => $ref
828 $traceobj->_synthesize_traces();
830 else {
831 $traceobj = $ref;
832 if ($version eq "2") {
833 my $trace_length = $traceobj->trace_length();
834 for ($current = 1; $current <= $trace_length; $current++) {
835 foreach (qw(a c g t)) {
836 push @traces,$traceobj->trace_value_at($_,$current);
840 elsif ($version == 3) {
841 foreach my $current_trace (qw(a c g t)) {
842 my @trace = @{$traceobj->trace($current_trace)};
843 foreach (@trace) {
844 if ($_ > 30000) {
845 $_ -= 65536;
848 my $transformed = $self->_delta(\@trace,"forward");
849 if($sample_size == 1){
850 foreach (@{$transformed}) {
851 $_ += 256 if ($_ < 0);
854 push @traces,@{$transformed};
858 $returner->{version} = $version;
859 $returner->{string} = \@traces;
860 my $length_of_traces = scalar(@traces);
861 my $byte;
862 if ($sample_size == 1) { $byte = "c"; } else { $byte = "n"; }
863 # an unsigned integer should be I, but this is too long
865 $returner->{binary} = pack "n${length_of_traces}",@traces;
866 $returner->{length} = CORE::length($returner->{binary});
867 return $returner;
871 sub _get_binary_bases {
872 my ($self,$version,$trace,$sample_size) = @_;
873 my $byte;
874 if ($sample_size == 1) { $byte = "c"; } else { $byte = "n"; }
875 my ($returner,@current_row,$current_base,$string,$binary);
876 my $length = $trace->length();
877 if ($version == 2) {
878 $returner->{'version'} = "2";
879 for (my $current_base =1; $current_base <= $length; $current_base++) {
880 my @current_row;
881 push @current_row,$trace->peak_index_at($current_base);
882 push @current_row,$trace->accuracy_at("a",$current_base);
883 push @current_row,$trace->accuracy_at("c",$current_base);
884 push @current_row,$trace->accuracy_at("g",$current_base);
885 push @current_row,$trace->accuracy_at("t",$current_base);
886 push @current_row,$trace->baseat($current_base);
887 push @current_row,0,0,0;
888 push @{$returner->{string}},@current_row;
889 $returner->{binary} .= pack "N C C C C a C3",@current_row;
891 return $returner;
893 else {
894 $returner->{'version'} = "3.00";
895 $returner->{peak_indices}->{string} = $trace->peak_indices();
896 my $length = scalar(@{$returner->{peak_indices}->{string}});
897 $returner->{peak_indices}->{binary} =
898 pack "N$length",@{$returner->{peak_indices}->{string}};
899 $returner->{peak_indices}->{length} =
900 CORE::length($returner->{peak_indices}->{binary});
901 my @accuracies;
902 foreach my $base (qw(a c g t)) {
903 $returner->{accuracies}->{$base} = $trace->accuracies($base);
904 push @accuracies,@{$trace->accuracies($base)};
906 $returner->{sequence} = $trace->seq();
907 $length = scalar(@accuracies);
908 # this really is "c" for samplesize == 2
909 $returner->{accuracies}->{binary} = pack "C${length}",@accuracies;
910 $returner->{accuracies}->{length} =
911 CORE::length($returner->{accuracies}->{binary});
912 $length = $trace->seq_obj()->length();
913 for (my $count=0; $count< $length; $count++) {
914 push @{$returner->{reserved}->{string}},0,0,0;
917 $length = scalar(@{$returner->{reserved}->{string}});
918 # this _must_ be "c"
919 $returner->{'reserved'}->{'binary'} =
920 pack "c$length",@{$returner->{reserved}->{string}};
921 $returner->{'reserved'}->{'length'} =
922 CORE::length($returner->{'reserved'}->{'binary'});
923 # $returner->{'bases'}->{'string'} = $trace->seq();
924 my @bases = split('',$trace->seq());
925 $length = $trace->length();
926 $returner->{'bases'}->{'binary'} = $trace->seq();
927 # print("Returning this:\n");
928 # $dumper->dumpValue($returner);
929 return ($returner->{peak_indices},
930 $returner->{accuracies},
931 $returner->{bases},
932 $returner->{reserved});
937 =head2 _make_trace_string($version)
939 Title : _make_trace_string($version)
940 Usage : $self->_make_trace_string($version)
941 Function: Merges trace data for the four bases to produce an scf
942 trace string. _requires_ $version
943 Returns : Nothing. Alters $self.
944 Args : $version - a version number. "2" or "3"
945 Notes :
947 =cut
949 sub _make_trace_string {
950 my ($self,$version) = @_;
951 my @traces;
952 my @traces_view;
953 my @as = @{$self->{'text'}->{'samples_a'}};
954 my @cs = @{$self->{'text'}->{'samples_c'}};
955 my @gs = @{$self->{'text'}->{'samples_g'}};
956 my @ts = @{$self->{'text'}->{'samples_t'}};
957 if ($version == 2) {
958 for (my $curr=0; $curr < scalar(@as); $curr++) {
959 $as[$curr] = $DEFAULT_QUALITY unless defined $as[$curr];
960 $cs[$curr] = $DEFAULT_QUALITY unless defined $cs[$curr];
961 $gs[$curr] = $DEFAULT_QUALITY unless defined $gs[$curr];
962 $ts[$curr] = $DEFAULT_QUALITY unless defined $ts[$curr];
963 push @traces,($as[$curr],$cs[$curr],$gs[$curr],$ts[$curr]);
966 elsif ($version == 3) {
967 @traces = (@as,@cs,@gs,@ts);
969 else {
970 $self->throw("No idea what version required to make traces here. You gave #$version# Bailing.");
972 my $length = scalar(@traces);
973 $self->{'text'}->{'samples_all'} = \@traces;
977 =head2 _get_binary_comments(\@comments)
979 Title : _get_binary_comments(\@comments)
980 Usage : $self->_get_binary_comments(\@comments);
981 Function: Provide a binary string that will be the comments section of
982 the scf file. See the scf specifications for detailed
983 specifications for the comments section of an scf file. Hint:
984 CODE=something\nBODE=something\n\0
985 Returns :
986 Args : A reference to an array containing comments.
987 Notes : None.
989 =cut
991 sub _get_binary_comments {
992 my ($self,$rcomments) = @_;
993 my $returner;
994 my $comments_string = '';
995 my %comments = %$rcomments;
996 foreach my $key (sort keys %comments) {
997 $comments{$key} ||= '';
998 $comments_string .= "$key=$comments{$key}\n";
1000 $comments_string .= "\n\0";
1001 my $length = CORE::length($comments_string);
1002 $returner->{length} = $length;
1003 $returner->{string} = $comments_string;
1004 $returner->{binary} = pack "A$length",$comments_string;
1005 return $returner;
1008 #=head2 _fill_missing_data($swq)
1010 # Title : _fill_missing_data($swq)
1011 # Usage : $self->_fill_missing_data($swq);
1012 # Function: If the $swq with quality has no qualities, set all qualities
1013 # to 0.
1014 # If the $swq has no sequence, set the sequence to N's.
1015 # Returns : Nothing. Modifies the Bio::Seq::Quality that was passed as an
1016 # argument.
1017 # Args : A reference to a Bio::Seq::Quality
1018 # Notes : None.
1020 #=cut
1023 #sub _fill_missing_data {
1024 # my ($self,$swq) = @_;
1025 # my $qual_obj = $swq->qual_obj();
1026 # my $seq_obj = $swq->seq_obj();
1027 # if ($qual_obj->length() == 0 && $seq_obj->length() != 0) {
1028 # my $fake_qualities = ("$DEFAULT_QUALITY ")x$seq_obj->length();
1029 # $swq->qual($fake_qualities);
1031 # if ($seq_obj->length() == 0 && $qual_obj->length != 0) {
1032 # my $sequence = ("N")x$qual_obj->length();
1033 # $swq->seq($sequence);
1037 =head2 _delta(\@trace_data,$direction)
1039 Title : _delta(\@trace_data,$direction)
1040 Usage : $self->_delta(\@trace_data,$direction);
1041 Function:
1042 Returns : A reference to an array containing modified trace values.
1043 Args : A reference to an array containing trace data and a string
1044 indicating the direction of conversion. ("forward" or
1045 "backward").
1046 Notes : This code is taken from the specification for SCF3.2.
1047 http://www.mrc-lmb.cam.ac.uk/pubseq/manual/formats_unix_4.html
1049 =cut
1052 sub _delta {
1053 my ($self,$rsamples,$direction) = @_;
1054 my @samples = @$rsamples;
1055 # /* If job == DELTA_IT:
1056 # * change a series of sample points to a series of delta delta values:
1057 # * ie change them in two steps:
1058 # * first: delta = current_value - previous_value
1059 # * then: delta_delta = delta - previous_delta
1060 # * else
1061 # * do the reverse
1062 # */
1063 # int i;
1064 # uint_2 p_delta, p_sample;
1066 my ($i,$num_samples,$p_delta,$p_sample,@samples_converted,$p_sample1,$p_sample2);
1067 my $SLOW_BUT_CLEAR = 0;
1068 $num_samples = scalar(@samples);
1069 # c-programmers are funny people with their single-letter variables
1071 if ( $direction eq "forward" ) {
1072 if($SLOW_BUT_CLEAR){
1073 $p_delta = 0;
1074 for ($i=0; $i < $num_samples; $i++) {
1075 $p_sample = $samples[$i];
1076 $samples[$i] = $samples[$i] - $p_delta;
1077 $p_delta = $p_sample;
1079 $p_delta = 0;
1080 for ($i=0; $i < $num_samples; $i++) {
1081 $p_sample = $samples[$i];
1082 $samples[$i] = $samples[$i] - $p_delta;
1083 $p_delta = $p_sample;
1085 } else {
1086 for ($i = $num_samples-1; $i > 1; $i--){
1087 $samples[$i] = $samples[$i] - 2*$samples[$i-1] + $samples[$i-2];
1089 $samples[1] = $samples[1] - 2*$samples[0];
1092 elsif ($direction eq "backward") {
1093 if($SLOW_BUT_CLEAR){
1094 $p_sample = 0;
1095 for ($i=0; $i < $num_samples; $i++) {
1096 $samples[$i] = $samples[$i] + $p_sample;
1097 $p_sample = $samples[$i];
1099 $p_sample = 0;
1100 for ($i=0; $i < $num_samples; $i++) {
1101 $samples[$i] = $samples[$i] + $p_sample;
1102 $p_sample = $samples[$i];
1104 } else {
1105 $p_sample1 = $p_sample2 = 0;
1106 for ($i = 0; $i < $num_samples; $i++){
1107 $p_sample1 = $p_sample1 + $samples[$i];
1108 $samples[$i] = $p_sample1 + $p_sample2;
1109 $p_sample2 = $samples[$i];
1114 else {
1115 $self->warn("Bad direction. Use \"forward\" or \"backward\".");
1117 return \@samples;
1120 =head2 _unpack_magik($buffer)
1122 Title : _unpack_magik($buffer)
1123 Usage : $self->_unpack_magik($buffer)
1124 Function: What unpack specification should be used? Try them all.
1125 Returns : Nothing.
1126 Args : A buffer containing arbitrary binary data.
1127 Notes : Eliminate the ambiguity and the guesswork. Used in the
1128 adaptation of _delta(), mostly.
1130 =cut
1132 sub _unpack_magik {
1133 my ($self,$buffer) = @_;
1134 my $length = length($buffer);
1135 my (@read,$counter);
1136 foreach (qw(c C s S i I l L n N v V)) {
1137 @read = unpack "$_$length", $buffer;
1138 for ($counter=0; $counter < 20; $counter++) {
1139 print("$read[$counter]\n");
1144 =head2 read_from_buffer($filehandle,$buffer,$length)
1146 Title : read_from_buffer($filehandle,$buffer,$length)
1147 Usage : $self->read_from_buffer($filehandle,$buffer,$length);
1148 Function: Read from the buffer.
1149 Returns : $buffer, containing a read of $length
1150 Args : a filehandle, a buffer, and a read length
1151 Notes : I just got tired of typing
1152 "unless (length($buffer) == $length)" so I put it here.
1154 =cut
1156 sub read_from_buffer {
1157 my ($self,$fh,$buffer,$length,$start_position) = @_;
1158 # print("Reading from a buffer!!! length($length) ");
1159 if ($start_position) {
1160 # print(" startposition($start_position)(".sprintf("%X", $start_position).")\n");
1162 # print("\n");
1163 if ($start_position) {
1164 # print("seeking to this position in the file: (".$start_position.")\n");
1165 seek ($fh,$start_position,0);
1166 # print("done. here is where I am now: (".tell($fh).")\n");
1168 else {
1169 # print("You did not specify a start position. Going from this position (the current position) (".tell($fh).")\n");
1171 read $fh, $buffer, $length;
1172 unless (length($buffer) == $length) {
1173 $self->warn("The read was incomplete! Trying harder.");
1174 my $missing_length = $length - length($buffer);
1175 my $buffer2;
1176 read $fh,$buffer2,$missing_length;
1177 $buffer .= $buffer2;
1178 if (length($buffer) != $length) {
1179 $self->throw("Unexpected end of file while reading from SCF file. I should have read $length but instead got ".length($buffer)."! Current file position is ".tell($fh).".");
1183 return $buffer;
1186 =head2 _dump_keys()
1188 Title : _dump_keys()
1189 Usage : &_dump_keys($a_reference_to_some_hash)
1190 Function: Dump out the keys in a hash.
1191 Returns : Nothing.
1192 Args : A reference to a hash.
1193 Notes : A debugging method.
1195 =cut
1197 sub _dump_keys {
1198 my $rhash = shift;
1199 if ($rhash !~ /HASH/) {
1200 print("_dump_keys: that was not a hash.\nIt was #$rhash# which was this reference:".ref($rhash)."\n");
1201 return;
1203 print("_dump_keys: The keys for $rhash are:\n");
1204 foreach (sort keys %$rhash) {
1205 print("$_\n");
1209 =head2 _dump_base_accuracies()
1211 Title : _dump_base_accuracies()
1212 Usage : $self->_dump_base_accuracies();
1213 Function: Dump out the v3 base accuracies in an easy to read format.
1214 Returns : Nothing.
1215 Args : None.
1216 Notes : A debugging method.
1218 =cut
1220 sub _dump_base_accuracies {
1221 my $self = shift;
1222 print("Dumping base accuracies! for v3\n");
1223 print("There are this many elements in a,c,g,t:\n");
1224 print(scalar(@{$self->{'text'}->{'v3_base_accuracy_a'}}).",".scalar(@{$self->{'text'}->{'v3_base_accuracy_c'}}).",".scalar(@{$self->{'text'}->{'v3_base_accuracy_g'}}).",".scalar(@{$self->{'text'}->{'v3_base_accuracy_t'}})."\n");
1225 my $number_traces = scalar(@{$self->{'text'}->{'v3_base_accuracy_a'}});
1226 for (my $counter=0; $counter < $number_traces; $counter++ ) {
1227 print("$counter\t");
1228 print $self->{'text'}->{'v3_base_accuracy_a'}->[$counter]."\t";
1229 print $self->{'text'}->{'v3_base_accuracy_c'}->[$counter]."\t";
1230 print $self->{'text'}->{'v3_base_accuracy_g'}->[$counter]."\t";
1231 print $self->{'text'}->{'v3_base_accuracy_t'}->[$counter]."\t";
1232 print("\n");
1236 =head2 _dump_peak_indices_incoming()
1238 Title : _dump_peak_indices_incoming()
1239 Usage : $self->_dump_peak_indices_incoming();
1240 Function: Dump out the v3 peak indices in an easy to read format.
1241 Returns : Nothing.
1242 Args : None.
1243 Notes : A debugging method.
1245 =cut
1247 sub _dump_peak_indices_incoming {
1248 my $self = shift;
1249 print("Dump peak indices incoming!\n");
1250 my $length = $self->{'bases'};
1251 print("The length is $length\n");
1252 for (my $count=0; $count < $length; $count++) {
1253 print("$count\t$self->{parsed}->{peak_indices}->[$count]\n");
1257 =head2 _dump_base_accuracies_incoming()
1259 Title : _dump_base_accuracies_incoming()
1260 Usage : $self->_dump_base_accuracies_incoming();
1261 Function: Dump out the v3 base accuracies in an easy to read format.
1262 Returns : Nothing.
1263 Args : None.
1264 Notes : A debugging method.
1266 =cut
1268 sub _dump_base_accuracies_incoming {
1269 my $self = shift;
1270 print("Dumping base accuracies! for v3\n");
1271 # print("There are this many elements in a,c,g,t:\n");
1272 # print(scalar(@{$self->{'parsed'}->{'v3_base_accuracy_a'}}).",".scalar(@{$self->{'text'}->{'v3_base_accuracy_c'}}).",".scalar(@{$self->{'text'}->{'v3_base_accuracy_g'}}).",".scalar(@{$self->{'text'}->{'v3_base_accuracy_t'}})."\n");
1273 my $number_traces = $self->{'bases'};
1274 for (my $counter=0; $counter < $number_traces; $counter++ ) {
1275 print("$counter\t");
1276 foreach (qw(A T G C)) {
1277 print $self->{'parsed'}->{'base_accuracies'}->{$_}->[$counter]."\t";
1279 print("\n");
1284 =head2 _dump_comments()
1286 Title : _dump_comments()
1287 Usage : $self->_dump_comments();
1288 Function: Debug dump the comments section from the scf.
1289 Returns : Nothing.
1290 Args : Nothing.
1291 Notes : None.
1293 =cut
1295 sub _dump_comments {
1296 my ($self) = @_;
1297 warn ("SCF comments:\n");
1298 foreach my $k (keys %{$self->{'comments'}}) {
1299 warn ("\t {$k} ==> ", $self->{'comments'}->{$k}, "\n");
1306 __END__