sync w/ main trunk
[bioperl-live.git] / Bio / SeqIO / scf.pm
blobe7576ea35e7f53a4022463f6bce2d324ef3af471
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(-Quality => $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 SequenceWithQuality 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 my $swq2 = Bio::Seq::SequenceTrace->new(
621 -swq => $swq
623 $swq2->_synthesize_traces();
624 $swq2->set_accuracies();
625 $swq = $swq2;
628 else {
629 $self->throw("You must pass a Bio::Seq::Quality or a Bio::Seq::SequenceTrace object to write_seq as a parameter named \"target\"");
631 # all of the rest of the arguments are comments for the scf
632 foreach $arg (sort keys %args) {
633 next if ($arg =~ /target/i);
634 ($label = $arg) =~ s/^\-//;
635 $writer_fodder->{comments}->{$label} = $args{$arg};
637 if (!$comments{'NAME'}) { $comments{'NAME'} = $swq->id(); }
638 # HA! Bwahahahaha.
639 $writer_fodder->{comments}->{'CONV'} = "Bioperl-Chads Mighty SCF writer." unless defined $comments{'CONV'};
640 # now deal with the version of scf they want to write
641 if ($writer_fodder->{comments}->{version}) {
642 if ($writer_fodder->{comments}->{version} != 2 && $writer_fodder->{comments}->{version} != 3) {
643 $self->warn("This module can only write version 2.0 or 3.0 scf's. Writing a version 2.0 scf by default.");
644 $writer_fodder->{header}->{version} = "2.00";
646 elsif ($writer_fodder->{comments}->{'version'} > 2) {
647 $writer_fodder->{header}->{'version'} = "3.00";
649 else {
650 $writer_fodder->{header}->{version} = "2";
653 else {
654 $writer_fodder->{header}->{'version'} = "3.00";
656 # set a few things in the header
657 $writer_fodder->{'header'}->{'magic'} = ".scf";
658 $writer_fodder->{'header'}->{'sample_size'} = "2";
659 $writer_fodder->{'header'}->{'bases'} = length($swq->seq());
660 $writer_fodder->{'header'}->{'bases_left_clip'} = "0";
661 $writer_fodder->{'header'}->{'bases_right_clip'} = "0";
662 $writer_fodder->{'header'}->{'sample_size'} = "2";
663 $writer_fodder->{'header'}->{'code_set'} = "9";
664 @{$writer_fodder->{'header'}->{'spare'}} = qw(0 0 0 0 0 0 0 0 0 0
665 0 0 0 0 0 0 0 0 0 0);
666 $writer_fodder->{'header'}->{'samples_offset'} = "128";
667 $writer_fodder->{'header'}->{'samples'} = $swq->trace_length();
668 # create the binary for the comments and file it in writer_fodder
669 $writer_fodder->{comments} = $self->_get_binary_comments(
670 $writer_fodder->{comments});
671 # create the binary and the strings for the traces, bases,
672 # offsets (if necessary), and accuracies (if necessary)
673 $writer_fodder->{traces} = $self->_get_binary_traces(
674 $writer_fodder->{'header'}->{'version'},
675 $swq,$writer_fodder->{'header'}->{'sample_size'});
676 my ($b_base_offsets,$b_base_accuracies,$samples_size,$bases_size);
678 # version 2
680 if ($writer_fodder->{'header'}->{'version'} == 2) {
681 $writer_fodder->{bases} = $self->_get_binary_bases(
683 $swq,
684 $writer_fodder->{'header'}->{'sample_size'});
685 $samples_size = CORE::length($writer_fodder->{traces}->{'binary'});
686 $bases_size = CORE::length($writer_fodder->{bases}->{binary});
687 $writer_fodder->{'header'}->{'bases_offset'} = 128 + $samples_size;
688 $writer_fodder->{'header'}->{'comments_offset'} = 128 +
689 $samples_size + $bases_size;
690 $writer_fodder->{'header'}->{'comments_size'} =
691 length($writer_fodder->{'comments'}->{binary});
692 $writer_fodder->{'header'}->{'private_size'} = "0";
693 $writer_fodder->{'header'}->{'private_offset'} = 128 +
694 $samples_size + $bases_size +
695 $writer_fodder->{'header'}->{'comments_size'};
696 $writer_fodder->{'header'}->{'binary'} =
697 $self->_get_binary_header($writer_fodder->{header});
698 $dumper->dumpValue($writer_fodder) if $self->verbose > 0;
699 $self->_print ($writer_fodder->{'header'}->{'binary'})
700 or print("Could not write binary header...\n");
701 $self->_print ($writer_fodder->{'traces'}->{'binary'})
702 or print("Could not write binary traces...\n");
703 $self->_print ($writer_fodder->{'bases'}->{'binary'})
704 or print("Could not write binary base structures...\n");
705 $self->_print ($writer_fodder->{'comments'}->{'binary'})
706 or print("Could not write binary comments...\n");
708 else {
709 ($writer_fodder->{peak_indices},
710 $writer_fodder->{accuracies},
711 $writer_fodder->{bases},
712 $writer_fodder->{reserved} ) =
713 $self->_get_binary_bases(
715 $swq,
716 $writer_fodder->{'header'}->{'sample_size'}
718 $writer_fodder->{'header'}->{'bases_offset'} = 128 +
719 length($writer_fodder->{'traces'}->{'binary'});
720 $writer_fodder->{'header'}->{'comments_size'} =
721 length($writer_fodder->{'comments'}->{'binary'});
722 # this is:
723 # bases_offset + base_offsets + accuracies + called_bases +
724 # reserved
725 $writer_fodder->{'header'}->{'private_size'} = "0";
727 $writer_fodder->{'header'}->{'comments_offset'} =
728 128+length($writer_fodder->{'traces'}->{'binary'})+
729 length($writer_fodder->{'peak_indices'}->{'binary'})+
730 length($writer_fodder->{'accuracies'}->{'binary'})+
731 length($writer_fodder->{'bases'}->{'binary'})+
732 length($writer_fodder->{'reserved'}->{'binary'});
733 $writer_fodder->{'header'}->{'private_offset'} =
734 $writer_fodder->{'header'}->{'comments_offset'} +
735 $writer_fodder->{'header'}->{'comments_size'};
736 $writer_fodder->{'header'}->{'spare'}->[1] =
737 $writer_fodder->{'header'}->{'comments_offset'} +
738 length($writer_fodder->{'comments'}->{'binary'});
739 $writer_fodder->{header}->{binary} =
740 $self->_get_binary_header($writer_fodder->{header});
741 $self->_print ($writer_fodder->{'header'}->{'binary'})
742 or print("Couldn't write header\n");
743 $self->_print ($writer_fodder->{'traces'}->{'binary'})
744 or print("Couldn't write samples\n");
745 $self->_print ($writer_fodder->{'peak_indices'}->{'binary'})
746 or print("Couldn't write peak offsets\n");
747 $self->_print ($writer_fodder->{'accuracies'}->{'binary'})
748 or print("Couldn't write accuracies\n");
749 $self->_print ($writer_fodder->{'bases'}->{'binary'})
750 or print("Couldn't write called_bases\n");
751 $self->_print ($writer_fodder->{'reserved'}->{'binary'})
752 or print("Couldn't write reserved\n");
753 $self->_print ($writer_fodder->{'comments'}->{'binary'})
754 or print ("Couldn't write comments\n");
757 # kinda unnecessary, given the close() below, but maybe that'll go
758 # away someday.
759 $self->flush if $self->_flush_on_write && defined $self->_fh;
761 $self->close();
768 =head2 _get_binary_header()
770 Title : _get_binary_header();
771 Usage : $self->_get_binary_header();
772 Function: Provide the binary string that will be used as the header for
773 a scfv2 document.
774 Returns : A binary string.
775 Args : None. Uses the entries in the $self->{'header'} hash. These
776 are set on construction of the object (hopefully correctly!).
777 Notes :
779 =cut
781 sub _get_binary_header {
782 my ($self,$header) = @_;
783 my $binary = pack "a4 NNNNNNNN a4 NN N20",
785 $header->{'magic'},
786 $header->{'samples'},
787 $header->{'samples_offset'},
788 $header->{'bases'},
789 $header->{'bases_left_clip'},
790 $header->{'bases_right_clip'},
791 $header->{'bases_offset'},
792 $header->{'comments_size'},
793 $header->{'comments_offset'},
794 $header->{'version'},
795 $header->{'sample_size'},
796 $header->{'code_set'},
797 @{$header->{'spare'}}
799 return $binary;
802 =head2 _get_binary_traces($version,$ref)
804 Title : _set_binary_tracesbases($version,$ref)
805 Usage : $self->_set_binary_tracesbases($version,$ref);
806 Function: Constructs the trace and base strings for all scfs
807 Returns : Nothing. Alters self.
808 Args : $version - "2" or "3"
809 $sequence - a scalar containing arbitrary sequence data
810 $ref - a reference to either a SequenceTraces or a
811 SequenceWithQuality object.
812 Notes : This is a really complicated thing.
814 =cut
816 sub _get_binary_traces {
817 my ($self,$version,$ref,$sample_size) = @_;
818 # ref _should_ be a Bio::Seq::SequenceTrace, but might be a
819 # Bio::Seq::Quality
820 my $returner;
821 my $sequence = $ref->seq();
822 my $sequence_length = length($sequence);
823 # first of all, do we need to synthesize the trace?
824 # if so, call synthesize_base
825 my ($traceobj,@traces,$current);
826 if ( ref($ref) eq "Bio::Seq::Quality" ) {
827 $traceobj = Bio::Seq::Quality->new(
828 -target => $ref
830 $traceobj->_synthesize_traces();
832 else {
833 $traceobj = $ref;
834 if ($version eq "2") {
835 my $trace_length = $traceobj->trace_length();
836 for ($current = 1; $current <= $trace_length; $current++) {
837 foreach (qw(a c g t)) {
838 push @traces,$traceobj->trace_value_at($_,$current);
842 elsif ($version == 3) {
843 foreach my $current_trace (qw(a c g t)) {
844 my @trace = @{$traceobj->trace($current_trace)};
845 foreach (@trace) {
846 if ($_ > 30000) {
847 $_ -= 65536;
850 my $transformed = $self->_delta(\@trace,"forward");
851 if($sample_size == 1){
852 foreach (@{$transformed}) {
853 $_ += 256 if ($_ < 0);
856 push @traces,@{$transformed};
860 $returner->{version} = $version;
861 $returner->{string} = \@traces;
862 my $length_of_traces = scalar(@traces);
863 my $byte;
864 if ($sample_size == 1) { $byte = "c"; } else { $byte = "n"; }
865 # an unsigned integer should be I, but this is too long
867 $returner->{binary} = pack "n${length_of_traces}",@traces;
868 $returner->{length} = CORE::length($returner->{binary});
869 return $returner;
873 sub _get_binary_bases {
874 my ($self,$version,$trace,$sample_size) = @_;
875 my $byte;
876 if ($sample_size == 1) { $byte = "c"; } else { $byte = "n"; }
877 my ($returner,@current_row,$current_base,$string,$binary);
878 my $length = $trace->length();
879 if ($version == 2) {
880 $returner->{'version'} = "2";
881 for (my $current_base =1; $current_base <= $length; $current_base++) {
882 my @current_row;
883 push @current_row,$trace->peak_index_at($current_base);
884 push @current_row,$trace->accuracy_at("a",$current_base);
885 push @current_row,$trace->accuracy_at("c",$current_base);
886 push @current_row,$trace->accuracy_at("g",$current_base);
887 push @current_row,$trace->accuracy_at("t",$current_base);
888 push @current_row,$trace->baseat($current_base);
889 push @current_row,0,0,0;
890 push @{$returner->{string}},@current_row;
891 $returner->{binary} .= pack "N C C C C a C3",@current_row;
893 return $returner;
895 else {
896 $returner->{'version'} = "3.00";
897 $returner->{peak_indices}->{string} = $trace->peak_indices();
898 my $length = scalar(@{$returner->{peak_indices}->{string}});
899 $returner->{peak_indices}->{binary} =
900 pack "N$length",@{$returner->{peak_indices}->{string}};
901 $returner->{peak_indices}->{length} =
902 CORE::length($returner->{peak_indices}->{binary});
903 my @accuracies;
904 foreach my $base (qw(a c g t)) {
905 $returner->{accuracies}->{$base} = $trace->accuracies($base);
906 push @accuracies,@{$trace->accuracies($base)};
908 $returner->{sequence} = $trace->seq();
909 $length = scalar(@accuracies);
910 # this really is "c" for samplesize == 2
911 $returner->{accuracies}->{binary} = pack "c${length}",@accuracies;
912 $returner->{accuracies}->{length} =
913 CORE::length($returner->{accuracies}->{binary});
914 $length = $trace->seq_obj()->length();
915 for (my $count=0; $count< $length; $count++) {
916 push @{$returner->{reserved}->{string}},0,0,0;
919 $length = scalar(@{$returner->{reserved}->{string}});
920 # this _must_ be "c"
921 $returner->{'reserved'}->{'binary'} =
922 pack "c$length",@{$returner->{reserved}->{string}};
923 $returner->{'reserved'}->{'length'} =
924 CORE::length($returner->{'reserved'}->{'binary'});
925 # $returner->{'bases'}->{'string'} = $trace->seq();
926 my @bases = split('',$trace->seq());
927 $length = $trace->length();
928 $returner->{'bases'}->{'binary'} = $trace->seq();
929 # print("Returning this:\n");
930 # $dumper->dumpValue($returner);
931 return ($returner->{peak_indices},
932 $returner->{accuracies},
933 $returner->{bases},
934 $returner->{reserved});
939 =head2 _make_trace_string($version)
941 Title : _make_trace_string($version)
942 Usage : $self->_make_trace_string($version)
943 Function: Merges trace data for the four bases to produce an scf
944 trace string. _requires_ $version
945 Returns : Nothing. Alters $self.
946 Args : $version - a version number. "2" or "3"
947 Notes :
949 =cut
951 sub _make_trace_string {
952 my ($self,$version) = @_;
953 my @traces;
954 my @traces_view;
955 my @as = @{$self->{'text'}->{'samples_a'}};
956 my @cs = @{$self->{'text'}->{'samples_c'}};
957 my @gs = @{$self->{'text'}->{'samples_g'}};
958 my @ts = @{$self->{'text'}->{'samples_t'}};
959 if ($version == 2) {
960 for (my $curr=0; $curr < scalar(@as); $curr++) {
961 $as[$curr] = $DEFAULT_QUALITY unless defined $as[$curr];
962 $cs[$curr] = $DEFAULT_QUALITY unless defined $cs[$curr];
963 $gs[$curr] = $DEFAULT_QUALITY unless defined $gs[$curr];
964 $ts[$curr] = $DEFAULT_QUALITY unless defined $ts[$curr];
965 push @traces,($as[$curr],$cs[$curr],$gs[$curr],$ts[$curr]);
968 elsif ($version == 3) {
969 @traces = (@as,@cs,@gs,@ts);
971 else {
972 $self->throw("No idea what version required to make traces here. You gave #$version# Bailing.");
974 my $length = scalar(@traces);
975 $self->{'text'}->{'samples_all'} = \@traces;
979 =head2 _get_binary_comments(\@comments)
981 Title : _get_binary_comments(\@comments)
982 Usage : $self->_get_binary_comments(\@comments);
983 Function: Provide a binary string that will be the comments section of
984 the scf file. See the scf specifications for detailed
985 specifications for the comments section of an scf file. Hint:
986 CODE=something\nBODE=something\n\0
987 Returns :
988 Args : A reference to an array containing comments.
989 Notes : None.
991 =cut
993 sub _get_binary_comments {
994 my ($self,$rcomments) = @_;
995 my $returner;
996 my $comments_string = '';
997 my %comments = %$rcomments;
998 foreach my $key (sort keys %comments) {
999 $comments{$key} ||= '';
1000 $comments_string .= "$key=$comments{$key}\n";
1002 $comments_string .= "\n\0";
1003 my $length = CORE::length($comments_string);
1004 $returner->{length} = $length;
1005 $returner->{string} = $comments_string;
1006 $returner->{binary} = pack "A$length",$comments_string;
1007 return $returner;
1010 #=head2 _fill_missing_data($swq)
1012 # Title : _fill_missing_data($swq)
1013 # Usage : $self->_fill_missing_data($swq);
1014 # Function: If the $swq with quality has no qualities, set all qualities
1015 # to 0.
1016 # If the $swq has no sequence, set the sequence to N's.
1017 # Returns : Nothing. Modifies the Bio::Seq::Quality that was passed as an
1018 # argument.
1019 # Args : A reference to a Bio::Seq::Quality
1020 # Notes : None.
1022 #=cut
1025 #sub _fill_missing_data {
1026 # my ($self,$swq) = @_;
1027 # my $qual_obj = $swq->qual_obj();
1028 # my $seq_obj = $swq->seq_obj();
1029 # if ($qual_obj->length() == 0 && $seq_obj->length() != 0) {
1030 # my $fake_qualities = ("$DEFAULT_QUALITY ")x$seq_obj->length();
1031 # $swq->qual($fake_qualities);
1033 # if ($seq_obj->length() == 0 && $qual_obj->length != 0) {
1034 # my $sequence = ("N")x$qual_obj->length();
1035 # $swq->seq($sequence);
1039 =head2 _delta(\@trace_data,$direction)
1041 Title : _delta(\@trace_data,$direction)
1042 Usage : $self->_delta(\@trace_data,$direction);
1043 Function:
1044 Returns : A reference to an array containing modified trace values.
1045 Args : A reference to an array containing trace data and a string
1046 indicating the direction of conversion. ("forward" or
1047 "backward").
1048 Notes : This code is taken from the specification for SCF3.2.
1049 http://www.mrc-lmb.cam.ac.uk/pubseq/manual/formats_unix_4.html
1051 =cut
1054 sub _delta {
1055 my ($self,$rsamples,$direction) = @_;
1056 my @samples = @$rsamples;
1057 # /* If job == DELTA_IT:
1058 # * change a series of sample points to a series of delta delta values:
1059 # * ie change them in two steps:
1060 # * first: delta = current_value - previous_value
1061 # * then: delta_delta = delta - previous_delta
1062 # * else
1063 # * do the reverse
1064 # */
1065 # int i;
1066 # uint_2 p_delta, p_sample;
1068 my ($i,$num_samples,$p_delta,$p_sample,@samples_converted,$p_sample1,$p_sample2);
1069 my $SLOW_BUT_CLEAR = 0;
1070 $num_samples = scalar(@samples);
1071 # c-programmers are funny people with their single-letter variables
1073 if ( $direction eq "forward" ) {
1074 if($SLOW_BUT_CLEAR){
1075 $p_delta = 0;
1076 for ($i=0; $i < $num_samples; $i++) {
1077 $p_sample = $samples[$i];
1078 $samples[$i] = $samples[$i] - $p_delta;
1079 $p_delta = $p_sample;
1081 $p_delta = 0;
1082 for ($i=0; $i < $num_samples; $i++) {
1083 $p_sample = $samples[$i];
1084 $samples[$i] = $samples[$i] - $p_delta;
1085 $p_delta = $p_sample;
1087 } else {
1088 for ($i = $num_samples-1; $i > 1; $i--){
1089 $samples[$i] = $samples[$i] - 2*$samples[$i-1] + $samples[$i-2];
1091 $samples[1] = $samples[1] - 2*$samples[0];
1094 elsif ($direction eq "backward") {
1095 if($SLOW_BUT_CLEAR){
1096 $p_sample = 0;
1097 for ($i=0; $i < $num_samples; $i++) {
1098 $samples[$i] = $samples[$i] + $p_sample;
1099 $p_sample = $samples[$i];
1101 $p_sample = 0;
1102 for ($i=0; $i < $num_samples; $i++) {
1103 $samples[$i] = $samples[$i] + $p_sample;
1104 $p_sample = $samples[$i];
1106 } else {
1107 $p_sample1 = $p_sample2 = 0;
1108 for ($i = 0; $i < $num_samples; $i++){
1109 $p_sample1 = $p_sample1 + $samples[$i];
1110 $samples[$i] = $p_sample1 + $p_sample2;
1111 $p_sample2 = $samples[$i];
1116 else {
1117 $self->warn("Bad direction. Use \"forward\" or \"backward\".");
1119 return \@samples;
1122 =head2 _unpack_magik($buffer)
1124 Title : _unpack_magik($buffer)
1125 Usage : $self->_unpack_magik($buffer)
1126 Function: What unpack specification should be used? Try them all.
1127 Returns : Nothing.
1128 Args : A buffer containing arbitrary binary data.
1129 Notes : Eliminate the ambiguity and the guesswork. Used in the
1130 adaptation of _delta(), mostly.
1132 =cut
1134 sub _unpack_magik {
1135 my ($self,$buffer) = @_;
1136 my $length = length($buffer);
1137 my (@read,$counter);
1138 foreach (qw(c C s S i I l L n N v V)) {
1139 @read = unpack "$_$length", $buffer;
1140 for ($counter=0; $counter < 20; $counter++) {
1141 print("$read[$counter]\n");
1146 =head2 read_from_buffer($filehandle,$buffer,$length)
1148 Title : read_from_buffer($filehandle,$buffer,$length)
1149 Usage : $self->read_from_buffer($filehandle,$buffer,$length);
1150 Function: Read from the buffer.
1151 Returns : $buffer, containing a read of $length
1152 Args : a filehandle, a buffer, and a read length
1153 Notes : I just got tired of typing
1154 "unless (length($buffer) == $length)" so I put it here.
1156 =cut
1158 sub read_from_buffer {
1159 my ($self,$fh,$buffer,$length,$start_position) = @_;
1160 # print("Reading from a buffer!!! length($length) ");
1161 if ($start_position) {
1162 # print(" startposition($start_position)(".sprintf("%X", $start_position).")\n");
1164 # print("\n");
1165 if ($start_position) {
1166 # print("seeking to this position in the file: (".$start_position.")\n");
1167 seek ($fh,$start_position,0);
1168 # print("done. here is where I am now: (".tell($fh).")\n");
1170 else {
1171 # print("You did not specify a start position. Going from this position (the current position) (".tell($fh).")\n");
1173 read $fh, $buffer, $length;
1174 unless (length($buffer) == $length) {
1175 $self->warn("The read was incomplete! Trying harder.");
1176 my $missing_length = $length - length($buffer);
1177 my $buffer2;
1178 read $fh,$buffer2,$missing_length;
1179 $buffer .= $buffer2;
1180 if (length($buffer) != $length) {
1181 $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).".");
1185 return $buffer;
1188 =head2 _dump_keys()
1190 Title : _dump_keys()
1191 Usage : &_dump_keys($a_reference_to_some_hash)
1192 Function: Dump out the keys in a hash.
1193 Returns : Nothing.
1194 Args : A reference to a hash.
1195 Notes : A debugging method.
1197 =cut
1199 sub _dump_keys {
1200 my $rhash = shift;
1201 if ($rhash !~ /HASH/) {
1202 print("_dump_keys: that was not a hash.\nIt was #$rhash# which was this reference:".ref($rhash)."\n");
1203 return;
1205 print("_dump_keys: The keys for $rhash are:\n");
1206 foreach (sort keys %$rhash) {
1207 print("$_\n");
1211 =head2 _dump_base_accuracies()
1213 Title : _dump_base_accuracies()
1214 Usage : $self->_dump_base_accuracies();
1215 Function: Dump out the v3 base accuracies in an easy to read format.
1216 Returns : Nothing.
1217 Args : None.
1218 Notes : A debugging method.
1220 =cut
1222 sub _dump_base_accuracies {
1223 my $self = shift;
1224 print("Dumping base accuracies! for v3\n");
1225 print("There are this many elements in a,c,g,t:\n");
1226 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");
1227 my $number_traces = scalar(@{$self->{'text'}->{'v3_base_accuracy_a'}});
1228 for (my $counter=0; $counter < $number_traces; $counter++ ) {
1229 print("$counter\t");
1230 print $self->{'text'}->{'v3_base_accuracy_a'}->[$counter]."\t";
1231 print $self->{'text'}->{'v3_base_accuracy_c'}->[$counter]."\t";
1232 print $self->{'text'}->{'v3_base_accuracy_g'}->[$counter]."\t";
1233 print $self->{'text'}->{'v3_base_accuracy_t'}->[$counter]."\t";
1234 print("\n");
1238 =head2 _dump_peak_indices_incoming()
1240 Title : _dump_peak_indices_incoming()
1241 Usage : $self->_dump_peak_indices_incoming();
1242 Function: Dump out the v3 peak indices in an easy to read format.
1243 Returns : Nothing.
1244 Args : None.
1245 Notes : A debugging method.
1247 =cut
1249 sub _dump_peak_indices_incoming {
1250 my $self = shift;
1251 print("Dump peak indices incoming!\n");
1252 my $length = $self->{'bases'};
1253 print("The length is $length\n");
1254 for (my $count=0; $count < $length; $count++) {
1255 print("$count\t$self->{parsed}->{peak_indices}->[$count]\n");
1259 =head2 _dump_base_accuracies_incoming()
1261 Title : _dump_base_accuracies_incoming()
1262 Usage : $self->_dump_base_accuracies_incoming();
1263 Function: Dump out the v3 base accuracies in an easy to read format.
1264 Returns : Nothing.
1265 Args : None.
1266 Notes : A debugging method.
1268 =cut
1270 sub _dump_base_accuracies_incoming {
1271 my $self = shift;
1272 print("Dumping base accuracies! for v3\n");
1273 # print("There are this many elements in a,c,g,t:\n");
1274 # 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");
1275 my $number_traces = $self->{'bases'};
1276 for (my $counter=0; $counter < $number_traces; $counter++ ) {
1277 print("$counter\t");
1278 foreach (qw(A T G C)) {
1279 print $self->{'parsed'}->{'base_accuracies'}->{$_}->[$counter]."\t";
1281 print("\n");
1286 =head2 _dump_comments()
1288 Title : _dump_comments()
1289 Usage : $self->_dump_comments();
1290 Function: Debug dump the comments section from the scf.
1291 Returns : Nothing.
1292 Args : Nothing.
1293 Notes : None.
1295 =cut
1297 sub _dump_comments {
1298 my ($self) = @_;
1299 warn ("SCF comments:\n");
1300 foreach my $k (keys %{$self->{'comments'}}) {
1301 warn ("\t {$k} ==> ", $self->{'comments'}->{$k}, "\n");
1308 __END__