CHP.pm untested

filter: Statementstmt 0.0% 0 / 449 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 679 CC 62 SCAR 45.8
CC
62
Cov
0%
_parse_mas5
272.0
_write_xda
182.0
_parse_xda
156.0
CRAP 97.9
1 #!/usr/bin/perl
2  
3 package Bio::Affymetrix::CHP;
4  
5 # Docs come before the code
6  
7 =head1 NAME
8  
9 Bio::Affymetrix::CHP- parse Affymetrix CHP files
10  
11 =head1 SYNOPSIS
12  
13 use Bio::Affymetrix::CHP;
14  
15 use Bio::Affymetrix::CDF;
16  
17 # Parse the CDF file
18  
19 my $cdf=new Bio::Affymetrix::CDF();
20  
21 $cdf->parse_from_file("foo.cdf");
22  
23  
24 # Make a new CHP object, using the CDF file
25  
26 my $chp=new Bio::Affymetrix::CHP($cdf);
27  
28 # Parse CHP file
29  
30 $chp->parse_from_file("foo.chp");
31  
32 # Find some fun facts about this chip
33  
34 print $chp->algorithm_name()."\n";
35  
36 print $chp->version()."\n";
37  
38  
39 # Print out all of the signal values for this chip
40  
41 while (my ($probename,$results)=each %{$chp->probe_set_results()}) {
42 print $probename.",".$results->{"Signal"}."\n";
43 }
44  
45  
46  
47 =head1 DESCRIPTION
48  
49 The Affymetrix microarray system produces files in a variety of
50 formats. If this means nothing to you, these modules are probably not
51 for you :). This module parses CHP files.
52  
53 This module requires a Bio::Affymetrix::CDF object before it can do
54 anything. This must be supplied to the constructor. See the perldoc
55 for that to see how to use that module. The module can parse various
56 types of CHP file transparently. You can find out what type you have
57 by using the original_version() method.
58
59 All of the Bio::Affymetrix modules parse a file entirely into
60 memory. You therefore need enough memory to hold these objects. For
61 some applications, parsing as a stream may be more appropriate-
62 hopefully the source to these modules will give enough clues to make
63 this an easy task.
64  
65 You can also use this software to write CHP files (see the
66 write_to_file and write_to_filehandle methods).
67  
68 =head2 HINTS
69  
70 You fill the object filled with data using the
71 parse_from_filehandle, parse_from_string or parse_from_file
72 routines. You can get/set various statistics using methods on the
73 object. Data is retrieved as a giant hash from
74 probe_set_results. Subroutines marked "original_" give values as they
75 are claimed in the original file, not as they are now (for instance if
76 you modify the value)
77  
78 =head1 NOTES
79  
80 =head2 REFERENCE
81  
82 Modules were written with the official Affymetrix documentation, which
83 can be located at http://www.affymetrix.com/support/developer/AffxFileFormats.ZIP
84  
85 =head2 COMPATIBILITY
86  
87 This module can parse the CHP files produced by the Affymetrix software
88 MAS 5 and GCOS v1.2. It can also process files produced by GCOS v1.0
89 in theory. However the authors of this module have never actually
90 seen an actual GCOS v1.0 file, and so we rely on the specification
91 supplied by Affymetrix only. If you have GCOS v1.0 files, feedback
92 as to whether the code actually works is welcome.
93  
94 Whatever file format you use the module should work transparently.
95  
96 These modules are focused on GCOS v1.2 CHP files. The MAS5 CHP
97 files actually contain a lot of extra information that is not
98 displayed in MAS5 or GCOS. This information is thrown away by the
99 parser.
100  
101 This module can only do expression arrays.
102  
103 Writing CHP files should work no matter what kind of CHP file was
104 parsed originally. In other words, you can use these modules as a CHP
105 file converter.
106  
107 As stated above, some of the information that is in a MAS5 file is not
108 present in an GCOS file. This module throws away all of this
109 additional information. This means that if you try and write a MAS5
110 file, there will be large blank sections. This should not put off any
111 programs that use them- this tactic was based on the Affymetrix
112 conversion.
113  
114 =head2 KNOWN BUG
115  
116 Change P Value always comes out wrong when using comparison CHP files
117 from MAS5. If you have a solution for this, please send email to the
118 addresses below.
119  
120 =head1 COPYRIGHT
121  
122 Copyright (C) 2005 by Nick James, David J Craigon, NASC (arabidopsis.info), The
123 University of Nottingham
124  
125 This module is free software. You can copy or redistribute it under the same terms as Perl itself.
126  
127 Affymetrix is a registered trademark of Affymetrix Inc., Santa
128 Clara, California, USA.
129  
130 =head1 AUTHORS
131
132 Nick James (nick at arabidopsis.info)
133  
134 David J Craigon (david at arabidopsis.info)
135  
136 Nottingham Arabidopsis Stock Centre (http://arabidopsis.info), University of Nottingham.
137  
138 =head1 METHODS
139  
140 =cut
141  
142  
143 use strict;
144 use warnings;
145 use Carp;
146  
147 our $VERSION=0.5;
148  
149 # Module for processing CHP files
150  
151  
152 =head2 new
153  
154 Arg [1] : Bio::Affymetrix::CDF $db_file
155 Example : my $chp=new Bio::Affymetrix::CHP($cdf);
156 Description: constructor for CHP object
157 Returntype : new Bio::Affmetrix::CHP object
158 Exceptions : none
159 Caller : general
160  
161 =cut
162  
163 sub new {
164 my $proto = shift;
165 my $class = ref($proto)||$proto;
166 my $self = {};
167  
168 $self->{"cdf"}=shift;
169  
170 if ((!defined($self->{"cdf"}))||(!$self->{"cdf"}->isa("Bio::Affymetrix::CDF"))) {
171 croak "Need to supply a Bio::Affymetric::CDF file to Bio::Affymetrix::CHP constructor";
172 }
173  
174 bless ($self, $class); # reconsecrate
175 return $self;
176 }
177  
178 # Getter/setter routines
179  
180 # CDF object associated with this file
181  
182  
183 =head2 CDF
184  
185 Arg [1] : Bio::Affymetrix::CDF $db_file
186 Example : none
187 Description: get/set for CDF file associated with this CHP file. The db is the one that
188 belongs to the external_name.
189 Returntype : Bio::Affymetrix::CDF object
190 Exceptions : none
191 Caller : general
192  
193 =cut
194  
195 sub CDF {
196 my $self=shift;
197  
198 if (my $q=shift) {
199 $self->{"cdf"}=$q;
200 }
201 return $self->{"cdf"};
202 }
203  
204  
205 # File format. Either XDA or MAS5 at the moment
206  
207 =head2 original_format
208  
209 Arg [0] : none
210 Example : my $format=$chp->original_format()
211 Description: Returns the format of the CHP file parsed
212 (currently) either "XDA" (which is a GCOS v1.2 format,
213 also known as version 4) or "MAS5" (which is produced
214 either by MAS 5 or GCOS v1.0, also known as version 3)
215 Returntype : string ("XDA" or "MAS5")
216 Exceptions : none
217 Caller : general
218  
219 =cut
220  
221 sub original_format {
222 my $self=shift;
223 return $self->{"format"};
224 }
225  
226 # Version of file (encoded in file)
227  
228 =head2 original_version
229  
230 Arg [0] : none
231 Example : my $version=$chp->original_version()
232 Description: Returns the version of the CHP file parsed
233 Best used in conjunction with original_format,
234 above. For XDA files, the version is always
235 (currently) 1. For MAS5 files the version is either 12
236 for a file produced by MAS5, or 13 for a GCOS v1.0
237 file. Code is written for parsing version 13 files,
238 but we\'ve no idea if it works.
239  
240 Returntype : string
241 Exceptions : none
242 Caller : general
243  
244 =cut
245  
246  
247 sub original_version {
248 my $self=shift;
249 return $self->{"version"};
250 }
251  
252 # Number of columns in the array
253  
254 =head2 cols
255  
256 Arg [0] : none
257 Example : my $x=$chp->cols()
258 Description: Numbers of columns in the array
259 Returntype : integer
260 Exceptions : none
261 Caller : general
262  
263 =cut
264  
265 sub cols {
266 my $self=shift;
267  
268 if (my $q=shift) {
269 $self->{"no_cols"}=$q;
270 }
271 return $self->{"no_cols"};
272 }
273  
274  
275 # Number of rows in the array
276  
277 =head2 rows
278  
279 Arg [0] : none
280 Example : my $y=$chp->rows()
281 Description: Get/set numbers of rows in the array
282 Returntype : integer
283 Exceptions : none
284 Caller : general
285  
286 =cut
287  
288 sub rows {
289 my $self=shift;
290  
291 if (my $q=shift) {
292 $self->{"no_rows"}=$q;
293 }
294 return $self->{"no_rows"};
295 }
296  
297  
298 # Number of probes claimed
299  
300  
301 =head2 original_number_of_probes
302  
303 Arg [0] : none
304 Example : my $original_probes=$chp->original_number_of_probes()
305 Description: Gets the original number of probes reported in the
306 array.
307  
308 The CHP files have the number of probes stored in
309 them, and this function lets you read this number as
310 it was stored in the file originally.
311  
312 A better way of finding out the current number of
313 probes is to count the number in the probe_set_results
314 hash, like so: scalar(keys %{$chp->probe_set_results()});
315
316 Returntype : integer
317 Exceptions : none
318 Caller : general
319  
320 =cut
321  
322  
323 sub original_number_of_probes {
324 my $self=shift;
325 return $self->{"no_units"};
326 }
327  
328 # Number of qc units
329  
330 =head2 original_number_qc_units
331  
332 Arg [0] : none
333 Example : my $original_qc=$chp->original_number_qc_units()
334 Description: Gets the original number of QC units in the file.
335 Returntype : integer
336 Exceptions : none
337 Caller : general
338  
339 =cut
340  
341  
342 sub original_number_qc_units {
343 my $self=shift;
344 return $self->{"no_qc_units"};
345 }
346  
347 # MS COM prog ID
348  
349 =head2 original_com_progid
350  
351 Arg [0] : none
352 Example : my $com_id=$chp->original_com_progid()
353 Description: Gets the progid of the original Microsoft COM object that made
354 this CHP file
355 Returntype : string
356 Exceptions : none
357 Caller : general
358  
359 =cut
360  
361 sub original_com_progid {
362 my $self=shift;
363 return $self->{"com_progid"};
364 }
365  
366 # CEL file name this CHP file originated from
367  
368 =head2 CEL_file_name
369  
370 Arg [1] : string $cel_file_name (optional)
371 Example : my $cel_file_name=$chp->CEL_file_name();
372 Description: Get/set the CEL file this CHP file was made from
373 Returntype : string
374 Exceptions : none
375 Caller : general
376  
377 =cut
378  
379  
380 sub CEL_file_name {
381 my $self=shift;
382  
383 if (my $q=shift) {
384 $self->{"cel_file_name"}=$q;
385 }
386 return $self->{"cel_file_name"};
387 }
388  
389 =head2 original_file_name
390  
391 Arg [0] : none
392 Example : my $chp_file_name=$chp->original_file_name();
393 Description: If this object was created using parse_from_file, the original filename. Otherwise undef.
394 Returntype : string
395 Exceptions : none
396 Caller : general
397  
398 =cut
399  
400  
401 sub original_file_name {
402 my $self=shift;
403  
404 return $self->{"file_name"};
405 }
406  
407  
408  
409 # ATH1, etc.
410  
411 =head2 probe_array_type
412  
413 Arg [1] : string $array_type (optional)
414 Example : my $probe_array_type=$chp->probe_array_type();
415 Description: Get/set the Affymetrix chip type used in the
416 production of this CHP file. String is same format as CDF file name,
417 for example ATH1-121501
418 Returntype : string
419 Exceptions : none
420 Caller : general
421  
422 =cut
423  
424  
425 sub probe_array_type {
426 my $self=shift;
427  
428 if (my $q=shift) {
429 $self->{"probe_array_type"}=$q;
430 }
431 return $self->{"probe_array_type"};
432 }
433  
434 # Algorithm name
435  
436 =head2 algorithm_name
437  
438 Arg [1] : string $algorithm_name (optional)
439 Example : my $algorithm_name=$chp->algorithm_name();
440 Description: Get/set the algorithm name that turned the CEL file
441 into this CHP file
442 Returntype : string
443 Exceptions : none
444 Caller : general
445  
446 =cut
447  
448 sub algorithm_name {
449 my $self=shift;
450  
451 if (my $q=shift) {
452 $self->{"algorithm_name"}=$q;
453 }
454 return $self->{"algorithm_name"};
455 }
456  
457  
458 =head2 algorithm_version
459  
460 Arg [1] : string $algorithm_version (optional)
461 Example : my $algorithm_version=$chp->algorithm_version();
462 Description: Get/set the algorithm version that turned the CEL file
463 into this CHP file
464 Returntype : string
465 Exceptions : none
466 Caller : general
467  
468 =cut
469  
470  
471 sub algorithm_version {
472 my $self=shift;
473  
474 if (my $q=shift) {
475 $self->{"algorithm_version"}=$q;
476 }
477 return $self->{"algorithm_version"};
478 }
479  
480  
481 # Algorithm params- returns hashref
482 =head2 algorithm_params
483  
484 Arg [1] : hashref $algorithm_params (optional)
485 Example : my %algorithm_params=%{$chp->algorithm_params()};
486  
487 # Print scale factor
488  
489 print $algorithm_params("SF");
490  
491 Description: Get/set the algorithm parameters for turning the CEL
492 file into this CHP file. Returns a reference to a hash, keyed on
493 parameter name.
494  
495 Parameters include:
496 SF (scale factor)
497 Alpha1
498 Alpha2
499 Tau
500 NF (normalisation factor)
501  
502  
503 Returntype : hashref
504 Exceptions : none
505 Caller : general
506  
507 =cut
508  
509 sub algorithm_params {
510 my $self=shift;
511  
512 if (my $q=shift) {
513 $self->{"algorithm_params"}=$q;
514 }
515 return $self->{"algorithm_params"};
516 }
517  
518 # Summary Statistics- returns hashref
519  
520  
521 =head2 summary_statistics
522  
523 Arg [1] : hashref $summary_statistics (optional)
524 Example : my %summary_statistics=%{$chp->summary_statistics()};
525  
526 # Print RawQ
527  
528 print $chp->summary_statistics()->{"RawQ"};
529  
530 Description: Get/set the summary statistics for turning the CEL
531 file into this CHP file. Returns a reference to a hash, keyed on
532 parameter name.
533  
534 Parameters include:
535 RawQ
536 Noise
537 Background
538  
539  
540 Returntype : hashref
541 Exceptions : none
542 Caller : general
543  
544 =cut
545  
546  
547 sub summary_statistics {
548 my $self=shift;
549  
550 if (my $q=shift) {
551 $self->{"summary_statistics"}=$q;
552 }
553 return $self->{"summary_statistics"};
554 }
555  
556 # Used in calculating background
557  
558 =head2 smooth_factor
559  
560 Arg [1] : float $smooth_factor (optional)
561 Example : my $smooth_factor=$chp->smooth_factor();
562 Description: Get/set the smooth factor. Your guess is as good as
563 mine as to what this actually is, although it is used in calculating
564 the background. Not available in MAS5 files.
565 Returntype : float
566 Exceptions : none
567 Caller : general
568  
569 =cut
570  
571  
572  
573 sub smooth_factor {
574 my $self=shift;
575  
576 if (my $q=shift) {
577 $self->{"smooth_factor"}=$q;
578 }
579 return $self->{"smooth_factor"};
580 }
581  
582 # An arrayref of zones
583  
584 =head2 background_zones
585  
586 Arg [1] : arrayref of arrayrefs $background_zones (optional)
587 Example : my @background_zones=@{$chp->background_zones()};
588  
589 # Print "X", "Y", "Background Value" for background zone 0
590 print $background_zones[0]->[0],$background_zones[0]->[1],$background_zones[0]->[2];
591  
592 Description: Get/set an array of background zones. Again, your guess is as good as
593 mine as to what this actually is, although it is used in calculating
594 the background. Not available in MAS5 files.
595  
596 Returns an arrayref to an arrayref. Each zone has an array three
597 values long, which are X,Y and background value respectively.
598 Returntype : arrayref of arrayrefs
599 Exceptions : none
600 Caller : general
601  
602 =cut
603  
604  
605 sub background_zones {
606 my $self=shift;
607  
608 if (my $q=shift) {
609 $self->{"zones"}=$q;
610 }
611  
612 return $self->{"zones"};
613 }
614  
615 # The hash of results
616  
617 =head2 probe_set_results
618  
619 Arg [1] : hashref of hashrefs $probe_set_results (optional)
620 Example : my %results=%{$chp->probe_set_results()};
621 e
622 # Print "Signal", "Detection Call", "StatPairsUsed" for my favourite probe
623  
624 print ($results{"246310_at"}->{"Signal"}."\n".$results{"246310_at"}->{"DetectionCall"}."\n".$results{"246310_at"}->{"StatPairsUsed"}."\n");
625  
626 Description: Gain access to the actual data. Returns a reference to
627 hash, keyed on probe name. Each value contains another reference
628 to a hash with the following keys:
629  
630 Signal
631 DetectionCall (detection call- one of P M A N)
632 DetectionPValue
633 StatPairs
634 StatPairsUsed
635 Probeset (a Bio::CDF::ProbeSet object, q.v.)
636  
637 and optionally:
638 Change
639 ChangePValue
640 SignalLogRatio
641 SignalLogRatioHigh
642 SignalLogRatioLow
643 CommonPairs
644  
645 when it is a CHP files with a comparison in it.
646  
647  
648 Returntype : hashref of hashrefs
649 Exceptions : none
650 Caller : general
651  
652 =cut
653  
654  
655 sub probe_set_results {
656 my $self=shift;
657  
658 if (my $q=shift) {
659 $self->{"probe_set_results"}=$q;
660 }
661  
662 return $self->{"probe_set_results"};
663 }
664  
665  
666 # Parsing
667 =head2 parse_from_string
668  
669 Arg [1] : string
670 Example : $chp->parse_from_string($chp_file_in_a_string);
671 Description: Parse a CHP file from a buffer in memory
672 Returntype : none
673 Exceptions : none
674 Caller : general
675  
676 =cut
677  
678 sub parse_from_string {
679 my $self=shift;
680 my $string=shift;
681  
682  
683 open CHP,"<",\$string or croak "Cannot open string stream";
684  
685 $self->parse_from_filehandle(\*CHP);
686  
687 close CHP;
688 }
689  
690 =head2 parse_from_file
691  
692 Arg [1] : string
693 Example : $chp->parse_from_file($chp_filename);
694 Description: Parse a CHP file from a file
695 Returntype : none
696 Exceptions : dies if can\'t open file
697 Caller : general
698  
699 =cut
700  
701  
702 sub parse_from_file {
703 my $self=shift;
704 my $filename=shift;
705  
706 open CHP,"<".$filename or croak "Cannot open file ".$filename;
707 $self->{"file_name"}=$filename;
708  
709 $self->parse_from_filehandle(\*CHP);
710  
711 close CHP;
712 }
713  
714 =head2 parse_from_filehandle
715  
716 Arg [1] : reference to filehandle
717 Example : $chp->parse_from_filehandle(\*STDIN);
718 Description: Parse a CHP file from a filehandle
719 Returntype : none
720 Exceptions : none
721 Caller : general
722  
723 =cut
724  
725  
726 sub parse_from_filehandle {
727 my $self=shift;
728 my $fh=shift;
729  
730 binmode $fh;
731
732 # First step- detect whether it's a GCOS or MAS5 file
733
734 # A buffer for reading things into
735 my $buffer;
736  
737 # XDA files have their first feature as a magic number, 65
738  
739 read ($fh, $buffer, 4);
740 my $magic_number = unpack("V", $buffer);
741  
742 if ($magic_number==65) {
743 $self->_parse_xda($fh);
744 return;
745 }
746  
747 # MAS5 files have their first feature as a string
748  
749 read ($fh, $buffer, 18, 4);
750
751 $magic_number=unpack("A22",$buffer);
752  
753 if ($magic_number eq "GeneChip Sequence File") {
754 # It's a MAS5/GCOS v1.0 "v3 file"! Yippee!
755 $self->_parse_mas5($fh);
756 return;
757 }
758  
759 croak "This doesn't look like a CHP file to me. I can only understand certain CHP filetypes, however\n";
760 }
761  
762 sub _parse_xda {
763 my ($self,$fh) = @_;
764  
765 $self->{"format"}="XDA";
766
767 my $buffer;
768  
769 # First some trivia
770  
771 read ($fh, $buffer, 4);
772 $self->{"version"}= unpack ("V", $buffer);
773
774 if ($self->{"version"}!=1) {
775 carp "This CHP file is newer than the software parsing them. Results may be suspect."; # die here, perhaps?
776 }
777  
778 read ($fh, $buffer, 12);
779 ($self->{"no_cols"},$self->{"no_rows"},$self->{"no_units"},$self->{"no_qc_units"})= unpack ("S2V2", $buffer);
780  
781 read ($fh, $buffer, 4);
782
783 $self->{"chip_type"}=unpack ("V", $buffer);
784  
785 if ($self->{"chip_type"}!=0) {
786 croak "This software does not process non-expression arrays";
787 }
788
789 $self->{"com_progid"}=$self->unpack_length_string($fh);
790
791 $self->{"cel_file_name"}=$self->unpack_length_string($fh);
792  
793 $self->{"probe_array_type"}=$self->unpack_length_string($fh);
794  
795 $self->{"algorithm_name"}=$self->unpack_length_string($fh);
796  
797 $self->{"algorithm_version"}=$self->unpack_length_string($fh);
798  
799 # Algorithm parameters
800
801 {
802 read ($fh, $buffer, 4);
803 my $no_algorithm_params = unpack ("V", $buffer);
804  
805 # get varying number of parameter names and values and read into hash
806 my %algorithm_params;
807 for (my $i=0;$i<$no_algorithm_params;$i++) {
808 my $name=$self->unpack_length_string($fh);
809 my $value=$self->unpack_length_string($fh);
810 $algorithm_params{$name}=$value;
811 }
812  
813 $self->{"algorithm_params"}=\%algorithm_params;
814 }
815  
816 # Summary statistics
817  
818 {
819 read ($fh, $buffer, 4);
820 my $no_stats = unpack ("V", $buffer);
821  
822 # get varying number of parameter names and values and read into hash
823 my %h;
824 for (my $i=0;$i<$no_stats;$i++) {
825 my $name=$self->unpack_length_string($fh);
826 my $value=$self->unpack_length_string($fh);
827 $h{$name}=$value;
828 }
829  
830 $self->{"summary_statistics"}=\%h;
831 }
832  
833 # Background calculation
834  
835 {
836 read ($fh, $buffer, 8);
837 my $m;
838 ($m,$self->{"smooth_factor"}) = unpack ("Vf", $buffer);
839
840 $self->{"zones"}=[];
841  
842 my %zone_info;
843 for (my $i=0;$i<$m;$i++) {
844 read ($fh, $buffer, 12);
845 my @zone = unpack ("f3", $buffer);
846 push @{$self->{"zones"}},\@zone;
847 }
848 }
849  
850 # Actual data. This is the bit that would need to be added to, if we did SNP etc. arrays
851  
852 {
853 my $size;
854 read ($fh, $buffer, 5);
855 ($self->{"analysis_type"},$size)=unpack("CV", $buffer);
856  
857 my %data;
858  
859 my $probesetlist=$self->{"cdf"}->probesets();
860  
861 foreach my $i (sort {int($a)<=>int($b)} keys(%$probesetlist)) {
862 read ($fh, $buffer, $size);
863
864 # Non-comparison analysis
865
866 if ($self->{"analysis_type"}==0 || $self->{"analysis_type"}==2) {
867 my @results=unpack("Cf2S2",$buffer);
868 my %h;
869 if ($results[0]==0) {
870 $h{"DetectionCall"}="P";
871 } elsif ($results[0]==1) {
872 $h{"DetectionCall"}="M";
873 } elsif ($results[0]==2) {
874 $h{"DetectionCall"}="A";
875 } elsif ($results[0]==3) {
876 $h{"DetectionCall"}="N";
877 }
878 $h{"DetectionPValue"}=$results[1];
879 $h{"Signal"}=$results[2];
880 $h{"StatPairs"}=$results[3];
881 $h{"StatPairsUsed"}=$results[4];
882  
883 $h{"Probeset"}=$probesetlist->{$i};
884  
885 $data{$probesetlist->{$i}->name()}=\%h;
886  
887 } else {
888 # Comparison analysis
889 $self->{"comparison"}=1;
890 my @results=unpack("Cf2S2Cf4S",$buffer);
891 my %h;
892 if ($results[0]==0) {
893 $h{"DetectionCall"}="P";
894 } elsif ($results[0]==1) {
895 $h{"DetectionCall"}="M";
896 } elsif ($results[0]==2) {
897 $h{"DetectionCall"}="A";
898 } elsif ($results[0]==3) {
899 $h{"DetectionCall"}="N";
900 }
901 $h{"DetectionPValue"}=$results[1];
902 $h{"Signal"}=$results[2];
903 $h{"StatPairs"}=$results[3];
904 $h{"StatPairsUsed"}=$results[4];
905 $h{"Change"}=$results[6];
906 $h{"ChangePValue"}=$results[7];
907 $h{"SignalLogRatio"}=$results[8];
908 $h{"SignalLogRatioLow"}=$results[9];
909 $h{"SignalLogRatioHigh"}=$results[10];
910 $h{"CommonPairs"}=$results[11];
911  
912 $h{"Probeset"}=$probesetlist->[$i];
913  
914 $data{$probesetlist->{$i}->name()}=\%h;
915 }
916 }
917 $self->{"probe_set_results"}=\%data;
918 }
919 }
920  
921  
922  
923  
924  
925 sub _parse_mas5 {
926 my ($self,$fh) = @_;
927  
928 $self->{"format"}="MAS5";
929  
930 my $buffer;
931
932 # Get version from file
933 read ($fh, $buffer, 4);
934 $self->{"version"}=unpack ("V", $buffer);
935  
936 if ($self->{"version"}!=12&&$self->{"version"}!=13) {
937 carp "This CHP file has a version number unrecognised by the software parsing them. Results may be suspect."; # die here, perhaps?
938 }
939  
940 if ($self->{"version"}==13) {
941 carp "The authors of this module have never seen a genuine GCOS v1.0 CHP file. This program can parse them, but we're only relying on the specification supplied by Affymetrix- we've not tested this at all. Suspect results therefore are highly likely.";
942 }
943 # Trivia section
944  
945 $self->{"algorithm_name"}=$self->unpack_length_string($fh);
946  
947 $self->{"algorithm_version"}=$self->unpack_length_string($fh);
948  
949
950 # Parse algorithm parameters to maintain compatability with XDA format
951  
952 {
953 my %algorithm_params;
954 my $parse_me=$self->unpack_length_string($fh);
955 foreach my $i (split / /,$parse_me) {
956 my ($name,$value)=split /=/,$i;
957 $algorithm_params{$name}=$value;
958 }
959  
960 $self->{"algorithm_params"}=\%algorithm_params;
961 }
962  
963 # Summary statistics
964  
965 {
966 my %summary_stats;
967 my $parse_me=$self->unpack_length_string($fh);
968 foreach my $i (split / /,$parse_me) {
969 my ($name,$value)=split /=/,$i;
970 $summary_stats{$name}=$value;
971 }
972  
973 $self->{"summary_statistics"}=\%summary_stats;
974 }
975  
976 read ($fh, $buffer, 20);
977 my $max_probeset_num;
978 ($self->{"no_rows"},$self->{"no_cols"},$self->{"no_units"},$max_probeset_num,$self->{"no_qc_units"})= unpack ("V5", $buffer);
979
980 # THROW AWAY PROBESET NUMBER FOR EACH PROBESET
981 read ($fh, $buffer, 4*$self->{"no_units"});
982  
983 # THROW AWAY NUMBER OF PROBE PAIRS FOR EACH PROBESET
984 read ($fh, $buffer, 4*$max_probeset_num);
985  
986 # THROW AWAY PROBESETTYPE FOR EACH PROBESET
987 read ($fh, $buffer, 4*$max_probeset_num); # Should test
988  
989 # THROW AWAY PROBESET NUMBER FOR EACH PROBESET
990 read ($fh, $buffer, 4*$self->{"no_units"});
991  
992 read ($fh, $buffer, 512);
993 ($self->{"probe_array_type"},$self->{"cel_file_name"})=unpack ("Z256Z256",$buffer);
994  
995 if ($self->{"probe_array_type"} ne $self->{"cdf"}->name()) {
996 carp "The CDF object you have supplied does not have the same name as the CDF file used to make this CHP file. Results may be dubious";
997 }
998  
999 $self->{"com_progid"}=$self->unpack_length_string($fh);
1000  
1001 # Actual data. This is the bit that would need to be added to, if we did SNP etc. arrays
1002 {
1003 my %data;
1004
1005 if ($self->{"version"}==12) {
1006
1007 my %results;
1008
1009 my $probesetlist=$self->{"cdf"}->probesets();
1010
1011 foreach my $i (sort {int($a)<=>int($b)} keys(%$probesetlist)) {
1012  
1013 # Non-comparison analysis
1014  
1015 read ($fh, $buffer, 44);
1016
1017 my @results=unpack("V7f3V", $buffer);
1018  
1019 my %h;
1020  
1021 if ($results[10]==0) {
1022 $h{"DetectionCall"}="P";
1023 } elsif ($results[10]==1) {
1024 $h{"DetectionCall"}="M";
1025 } elsif ($results[10]==2) {
1026 $h{"DetectionCall"}="A";
1027 } elsif ($results[10]==3) {
1028 $h{"DetectionCall"}="N";
1029 }
1030  
1031 $h{"DetectionPValue"}=$results[7];
1032 $h{"Signal"}=$results[9];
1033 $h{"StatPairs"}=$results[0];
1034 $h{"StatPairsUsed"}=$results[1];
1035 $h{"Probeset"}=$probesetlist->{$i};
1036  
1037 # Blimey-o-reilly! There's an entire CEL file in here! Let's ditch that.
1038
1039 read ($fh, $buffer, $h{"StatPairs"}*52); #52 bytes of junk per probeset?
1040  
1041 read ($fh, $buffer, 4);
1042
1043 # Comparison analysis
1044  
1045 if (unpack("V",$buffer)==1) {
1046 $self->{"comparison"}=1;
1047 read ($fh, $buffer, 58);
1048 my @results=unpack("V5c2l9",$buffer);
1049  
1050 $h{"Change"}=$results[4];
1051 $h{"ChangePValue"}=$results[15]/1000;
1052 $h{"SignalLogRatio"}=$results[12]/1000;
1053 $h{"SignalLogRatioLow"}=$results[14]/1000;
1054 $h{"SignalLogRatioHigh"}=$results[9]/1000;
1055 $h{"CommonPairs"}=$results[0];
1056 $h{"BaselineAbsent"}=$results[5];
1057 $h{"Probeset"}=$probesetlist->{$i};
1058 }
1059
1060 # $probesetlist->[$i] or die "Suspect CDF file! We have more data in the CHP file than expected. Are you using the right CDF file?";
1061
1062 $data{$probesetlist->{$i}->name()}=\%h;
1063
1064 }
1065 } elsif ($self->{"version"}==13) {
1066
1067 my %results;
1068
1069 my $probesetlist=$self->{"cdf"}->probesets();
1070
1071 for (my $i=0;$i<$self->{"no_units"};$i++) {
1072  
1073 # Non-comparison analysis
1074  
1075 read ($fh, $buffer, 24);
1076
1077 my @results=unpack("V3f2V", $buffer);
1078  
1079 my %h;
1080 if ($results[5]==0) {
1081 $h{"DetectionCall"}="P";
1082 } elsif ($results[5]==1) {
1083 $h{"DetectionCall"}="M";
1084 } elsif ($results[5]==2) {
1085 $h{"DetectionCall"}="A";
1086 } elsif ($results[5]==3) {
1087 $h{"DetectionCall"}="N";
1088 }
1089  
1090  
1091 $h{"DetectionPValue"}=$results[3];
1092 $h{"Signal"}=$results[4];
1093 $h{"StatPairs"}=$results[0];
1094 $h{"StatPairsUsed"}=$results[1];
1095 $h{"Probeset"}=$probesetlist->[$i];
1096  
1097 # Blimey-o-reilly! There's still-enough-of-a-CEL-file in here to be annoying! Nurse! The screens!
1098
1099 read ($fh, $buffer, $h{"StatPairs"}*20); #20 bytes of junk per probeset?
1100  
1101 read ($fh, $buffer, 4);
1102
1103 # Comparison analysis
1104  
1105 if (unpack("V",$buffer)==1) {
1106 read ($fh, $buffer, 27); #????????
1107 my @results=unpack("V2cV4",$buffer);
1108  
1109 my %h;
1110 $h{"Change"}=$results[1];
1111 $h{"ChangePValue"}=$results[6];
1112 $h{"SignalLogRatio"}=$results[3]/1000;
1113 $h{"SignalLogRatioLow"}=$results[4]/1000;
1114 $h{"SignalLogRatioHigh"}=$results[5]/1000;
1115 $h{"CommonPairs"}=$results[0];
1116 $h{"BaselineAbsent"}=$results[2];
1117  
1118 $h{"Probeset"}=$probesetlist->[$i];
1119 }
1120
1121 $probesetlist->[$i] or die "Suspect CDF file! We have more data in the CHP file than expected. Are you using the right CDF file?";
1122
1123 $data{$probesetlist->[$i]->name()}=\%h;
1124
1125 }
1126 }
1127  
1128 $self->{"probe_set_results"}=\%data;
1129 }
1130 }
1131  
1132  
1133  
1134 # binary file utility to read a length-defined string
1135  
1136 sub unpack_length_string {
1137 my $self=shift;
1138 my $fh=shift;
1139
1140 my $buffer;
1141  
1142 read ($fh, $buffer, 4);
1143 my $len = unpack ("V", $buffer);
1144 read ($fh, $buffer, $len);
1145  
1146 return unpack("a".$len,$buffer);
1147  
1148 }
1149  
1150  
1151 =head2 write_to_file
1152  
1153 Arg [1] : string $filename
1154 Arg [2] : string $format
1155 Arg [3] : string $version
1156 Example : $cdf->write_to_file($cdf_filename);
1157 Description: Writes a CDF file to a file. See write_to_filehandle for descriptions of format and version
1158 Returntype : none
1159 Exceptions : dies if cannot open file
1160 Caller : general
1161  
1162 =cut
1163  
1164 sub write_to_file {
1165 my $self=shift;
1166 my $filename=shift;
1167  
1168 open CDF,">".$filename or die "Cannot open file for writing".$filename;
1169  
1170 $self->write_to_filehandle(\*CDF,@_);
1171  
1172 close CDF;
1173 }
1174  
1175 =head2 write_to_filehandle
1176  
1177 Arg [1] : filehandle $filehandle
1178 Arg [2] : string $format
1179 Arg [3] : string $version
1180 Example : $chp->write_to_filehandle(\*STDOUT);
1181 Description: Writes a CDF file to a filehandle. Takes arguments of
1182 the filehandle, the desired format, and the desired version of that
1183 format.
1184 Currently, format defaults to MAS5, and version defaults to
1185 GC3.0.
1186 Returntype : none
1187 Exceptions : dies if cannot open file
1188 Caller : general
1189  
1190 =cut
1191  
1192  
1193 sub write_to_filehandle {
1194 my $self=shift;
1195 my $filehandle=shift;
1196 my $format=shift;
1197 my $version=shift;
1198 my $fake=shift;
1199  
1200 if (!defined $format) {
1201 $format="MAS5";
1202 $version=12;
1203 }
1204  
1205 if ($format eq "XDA" && !defined $version) {
1206 $version=1;
1207 }
1208  
1209 if ($format eq "XDA") {
1210 $self->_write_xda($filehandle,$version,$fake);
1211 } elsif ($format eq "MAS5") {
1212 $self->_write_mas5($filehandle,$version,$fake);
1213 } else {
1214 croak "Format must be XDA or MAS5";
1215 }
1216 }
1217  
1218 sub _write_xda {
1219 my $self=shift;
1220
1221 my $filehandle=shift;
1222 my $version=shift;
1223  
1224 my $fake=shift;
1225  
1226 if ($version!=1) {
1227 croak "Bio::Affymetrix can only write version 1 XDA files";
1228 }
1229  
1230 # Static stuff- always the same
1231  
1232 print $filehandle pack ("V2S2V3",65,$version,$self->{"no_cols"},$self->{"no_rows"},$self->{"no_units"},$self->{"no_qc_units"},0); # 0 at the end means expression array. Check no_units, no_qc_units!
1233
1234 if (!$fake) {
1235 print $filehandle pack ("V/a*","GeneChip.CallGEBaseCall.1");
1236 } else {
1237 print $filehandle pack ("V/a*","Bio::Affymetrix version ".$VERSION);
1238 }
1239 print $filehandle pack ("V/a*",$self->{"cel_file_name"});
1240  
1241 print $filehandle pack ("V/a*",$self->{"probe_array_type"});
1242 print $filehandle pack ("V/a*",$self->{"algorithm_name"});
1243 print $filehandle pack ("V/a*",$self->{"algorithm_version"});
1244  
1245 print $filehandle pack ("V",scalar(keys %{$self->{"algorithm_params"}}));
1246
1247 while (my ($key,$value)=each %{$self->{"algorithm_params"}}) {
1248 print $filehandle pack ("V/a*V/a*",$key,$value);
1249 }
1250  
1251 print $filehandle pack ("V",scalar(keys %{$self->{"summary_statistics"}}));
1252
1253 while (my ($key,$value)=each %{$self->{"summary_statistics"}}) {
1254 print $filehandle pack ("V/a*V/a*",$key,$value);
1255 }
1256  
1257 print $filehandle pack ("Vf",scalar(@{$self->{"zones"}}),$self->{"smooth_factor"});
1258  
1259 foreach my $i (@{$self->{"zones"}}) {
1260 print $filehandle pack ("f3",@$i);
1261 }
1262
1263 print $filehandle pack("C", $self->{"analysis_type"});
1264  
1265 # Hand calculated size of objects
1266  
1267 if ($self->{"analysis_type"}==0 || $self->{"analysis_type"}==2) {
1268 print $filehandle pack("V", 13);
1269 } else {
1270 print $filehandle pack("V", 32);
1271 }
1272 # Resequencing - BLANKED
1273
1274 my $probesetlist=$self->{"cdf"}->probesets();
1275
1276 foreach my $i (sort {int($a)<=>int($b)} keys(%$probesetlist)) {
1277 if ($self->{"analysis_type"}==0 || $self->{"analysis_type"}==2) {
1278 my $result=$self->{"probe_set_results"}->{$probesetlist->{$i}->name()};
1279  
1280 my $dt;
1281  
1282 if ($result->{"DetectionCall"} eq "P") {
1283 $dt=0;
1284 } elsif ($result->{"DetectionCall"} eq "M") {
1285 $dt=1;
1286 } elsif ($result->{"DetectionCall"} eq "A") {
1287 $dt=2;
1288 } elsif ($result->{"DetectionCall"} eq "N") {
1289 $dt=3;
1290 }
1291  
1292 print $filehandle pack("Cf2S2",$dt,$result->{"DetectionPValue"},$result->{"Signal"},$result->{"StatPairs"},$result->{"StatPairsUsed"});
1293  
1294 } else {
1295 # Comparison analysis
1296 my $result=$self->{"probe_set_results"}->{$probesetlist->{$i}->name()};
1297  
1298 my $dt;
1299  
1300 if ($result->{"DetectionCall"} eq "P") {
1301 $dt=0;
1302 } elsif ($result->{"DetectionCall"} eq "M") {
1303 $dt=1;
1304 } elsif ($result->{"DetectionCall"} eq "A") {
1305 $dt=2;
1306 } elsif ($result->{"DetectionCall"} eq "N") {
1307 $dt=3;
1308 }
1309  
1310 print $filehandle pack("Cf2S2Cf4S",$dt,$result->{"DetectionPValue"},$result->{"Signal"},$result->{"StatPairs"},$result->{"StatPairsUsed"},$result->{"Change"},$result->{"ChangePValue"},$result->{"SignalLogRatio"},$result->{"SignalLogRatioLow"},$result->{"SignalLogRatioHigh"},$result->{"CommonPairs"});
1311 }
1312 }
1313 }
1314  
1315 sub _write_mas5 {
1316 my $self=shift;
1317
1318 my $filehandle=shift;
1319 my $version=shift;
1320  
1321 my $fake=shift;
1322  
1323 if (!defined $version) {
1324 $version=12;
1325 }
1326  
1327 if ($version!=12) {
1328 croak "Unfortuantely, these modules can only write version 12 MAS5 files at present. If you've got software that can make or read version 13 files, please write to the authors of Bio::Affymetrix";
1329 }
1330  
1331  
1332 print $filehandle pack("a22VV/a*V/a*","GeneChip Sequence File",$version,$self->{"algorithm_name"},$self->{"algorithm_version"});
1333  
1334 {
1335 # Make parameters and summary stats
1336
1337 my $algoparams;
1338
1339 while (my ($key,$value)=each %{$self->{"algorithm_params"}}) {
1340 $algoparams.=$key."=".$value." ";
1341 }
1342
1343 my $algosummary;
1344
1345 while (my ($key,$value)=each %{$self->{"summary_statistics"}}) {
1346 $algosummary.=$key."=".$value." ";
1347 }
1348  
1349 print $filehandle pack("V/a*V/a*",$algoparams,$algosummary);
1350 }
1351  
1352
1353 # Rows,columns come from CDF file
1354  
1355 print $filehandle pack("V2",(scalar(@{$self->CDF()->probe_grid()})+1),(scalar(@{$self->CDF()->probe_grid()->[1]})+1)); ##Maybe?
1356
1357 my @probelist=sort {int($a)<=>int($b)} (keys %{$self->CDF()->probesets()});
1358  
1359 # Number of probes trivia
1360
1361 print $filehandle pack("V3",scalar(@probelist),$probelist[scalar(@probelist)-1],$self->{"no_qc_units"});
1362  
1363 print $filehandle pack("V*",@probelist);
1364  
1365  
1366 # This bit is surely a mistake. They want the number of probe pairs per probeset, until probeset number is greater than the number of probesets
1367  
1368 foreach my $i (@probelist) {
1369 if ($i<scalar(@probelist)) {
1370 print $filehandle pack("V",scalar(@{$self->CDF()->probesets()->{$i}->probe_pairs()}));
1371 } else {
1372 print $filehandle pack("V",0);
1373 }
1374 }
1375  
1376 foreach my $i (@probelist) {
1377 print $filehandle pack("V",(3));
1378 }
1379  
1380 foreach my $i (@probelist) {
1381 print $filehandle pack("V",scalar(@{$self->CDF()->probesets()->{$i}->probe_pairs()->[0]}));
1382 }
1383  
1384
1385 print $filehandle pack("a256a256",$self->{"probe_array_type"},$self->{"cel_file_name"});
1386  
1387 if (!$fake) {
1388 print $filehandle pack ("V/a*","GeneChip.CallGEBaseCall.1");
1389 } else {
1390 print $filehandle pack ("V/a*","Bio::Affymetrix version ".$VERSION);
1391 }
1392  
1393
1394  
1395 }
1396  
1397 1;