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
15 Bio::SeqIO::scf - .scf file input/output stream
19 Do not use this module directly. Use it via the Bio::SeqIO class, see
20 L<Bio::SeqIO> for more information.
24 This object can transform .scf files to and from Bio::Seq::SequenceTrace
25 objects. Mechanisms are present to retrieve trace data from scf
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
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.
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
56 http://bugzilla.open-bio.org/
58 =head1 AUTHOR Chad Matsalla
61 bioinformatics@dieselwurks.com
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
72 The rest of the documentation details each of the object
73 methods. Internal methods are usually preceded with a _
77 # Let the code begin...
79 package Bio
::SeqIO
::scf
;
80 use vars
qw($DEFAULT_QUALITY);
82 use Bio::Seq::SeqFactory;
83 use Bio::Seq::SequenceTrace;
84 use Bio::Annotation::Comment;
87 my $dumper = new Dumpvalue();
88 $dumper->veryCompact(1);
94 use base qw(Bio::SeqIO);
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'));
109 Usage : $scf = $stream->next_seq()
110 Function: returns the next scf sequence in the stream
111 Returns : a Bio::Seq::SequenceTrace object
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().
126 my ($seq, $seqc, $fh, $buffer, $offset, $length, $read_bytes, @read,
128 # set up a filehandle to read in the scf
129 return if $self->{_readfile
};
131 unless ($fh) { # simulate the <> function
132 if ( !fileno(ARGV
) or eof(ARGV
) ) {
133 return unless my $ARGV = shift;
135 $self->throw("Could not open $ARGV for SCF stream reading $!");
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
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);
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);
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);
184 if ($creator->{header
}->{sample_size
} == 1) {
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
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);
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.
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
};
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');
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'},
250 my $returner = Bio
::Seq
::SequenceTrace
->new(
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;
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.
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;
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.
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.
327 sub _get_v3_base_accuracies
{
328 my ($self,$buffer) = @_;
329 my $length = length($buffer);
330 my $qlength = $length/4;
332 my (@qualities,@sorter,$counter,$round,$last_base,$accuracies,$currbase);
333 foreach $currbase (qw(a c g t)) {
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;
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
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.
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) {
372 my ($tagname, $text) = ($1, $2);
373 my $comment_obj = Bio
::Annotation
::Comment
->new(
375 -tagname
=> $tagname);
377 $comments->add_Annotation('comment', $comment_obj);
381 $self->{'comments'} = $comments;
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
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.
400 my ($self,$buffer) = @_;
403 $header->{'samples'},
404 $header->{'sample_offset'},
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;
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
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.
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
470 sub _parse_v2_traces
{
471 my ($self,$buffer,$sample_size) = @_;
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
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];
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});
501 sub _deprecated_get_peak_indices_deprecated_use_the_sequencetrace_object_instead
{
503 my @temp = split(' ',$self->{'parsed'}->{'peak_indices'});
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.
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
537 return $self->{'comments'};
540 sub _dump_traces_outgoing_deprecated_use_the_sequencetrace_object
{
541 my ($self,$transformed) = @_;
542 my (@sA,@sT,@sG,@sC);
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'}};
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");
562 sub _dump_traces_incoming_deprecated_use_the_sequencetrace_object
{
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");
582 Title : write_seq(-target => $swq, <comments>)
583 Usage : $obj->write_seq(
586 -CONV => "Bioperl-Chads Mighty SCF writer.");
587 Function: Write out an scf.
589 Args : Requires: a reference to a Bio::Seq::Quality object to form the
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.
598 For best results, use a SequenceTrace object.
599 The things that you need to write an scf:
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
611 my ($self,%args) = @_;
614 my ($swq) = $self->_rearrange([qw(TARGET)], %args);
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(
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(); }
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";
647 $writer_fodder->{header
}->{version
} = "2";
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);
677 if ($writer_fodder->{'header'}->{'version'} == 2) {
678 $writer_fodder->{bases
} = $self->_get_binary_bases(
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");
706 ($writer_fodder->{peak_indices
},
707 $writer_fodder->{accuracies
},
708 $writer_fodder->{bases
},
709 $writer_fodder->{reserved
} ) =
710 $self->_get_binary_bases(
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'});
720 # bases_offset + base_offsets + accuracies + called_bases +
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
756 $self->flush if $self->_flush_on_write && defined $self->_fh;
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
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!).
779 sub _get_binary_header
{
780 my ($self,$header) = @_;
781 my $binary = pack "a4 NNNNNNNN a4 NN N20",
784 $header->{'samples'},
785 $header->{'samples_offset'},
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'}}
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.
814 sub _get_binary_traces
{
815 my ($self,$version,$ref,$sample_size) = @_;
816 # ref _should_ be a Bio::Seq::SequenceTrace, but might be a
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(
828 $traceobj->_synthesize_traces();
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)};
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);
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
});
871 sub _get_binary_bases
{
872 my ($self,$version,$trace,$sample_size) = @_;
874 if ($sample_size == 1) { $byte = "c"; } else { $byte = "n"; }
875 my ($returner,@current_row,$current_base,$string,$binary);
876 my $length = $trace->length();
878 $returner->{'version'} = "2";
879 for (my $current_base =1; $current_base <= $length; $current_base++) {
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;
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
});
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
}});
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
},
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"
949 sub _make_trace_string
{
950 my ($self,$version) = @_;
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'}};
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);
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
986 Args : A reference to an array containing comments.
991 sub _get_binary_comments
{
992 my ($self,$rcomments) = @_;
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;
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
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
1017 # Args : A reference to a Bio::Seq::Quality
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);
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
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
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
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){
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;
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;
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){
1095 for ($i=0; $i < $num_samples; $i++) {
1096 $samples[$i] = $samples[$i] + $p_sample;
1097 $p_sample = $samples[$i];
1100 for ($i=0; $i < $num_samples; $i++) {
1101 $samples[$i] = $samples[$i] + $p_sample;
1102 $p_sample = $samples[$i];
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];
1115 $self->warn("Bad direction. Use \"forward\" or \"backward\".");
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.
1126 Args : A buffer containing arbitrary binary data.
1127 Notes : Eliminate the ambiguity and the guesswork. Used in the
1128 adaptation of _delta(), mostly.
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.
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");
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");
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);
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).".");
1188 Title : _dump_keys()
1189 Usage : &_dump_keys($a_reference_to_some_hash)
1190 Function: Dump out the keys in a hash.
1192 Args : A reference to a hash.
1193 Notes : A debugging method.
1199 if ($rhash !~ /HASH/) {
1200 print("_dump_keys: that was not a hash.\nIt was #$rhash# which was this reference:".ref($rhash)."\n");
1203 print("_dump_keys: The keys for $rhash are:\n");
1204 foreach (sort keys %$rhash) {
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.
1216 Notes : A debugging method.
1220 sub _dump_base_accuracies
{
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";
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.
1243 Notes : A debugging method.
1247 sub _dump_peak_indices_incoming
{
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.
1264 Notes : A debugging method.
1268 sub _dump_base_accuracies_incoming
{
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";
1284 =head2 _dump_comments()
1286 Title : _dump_comments()
1287 Usage : $self->_dump_comments();
1288 Function: Debug dump the comments section from the scf.
1295 sub _dump_comments
{
1297 warn ("SCF comments:\n");
1298 foreach my $k (keys %{$self->{'comments'}}) {
1299 warn ("\t {$k} ==> ", $self->{'comments'}->{$k}, "\n");