CEL.pm untested

filter: Statementstmt 0.0% 0 / 260 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 420 CC 27 SCAR 24.6
CC
27
Cov
0%
_parse_mas5
56.0
parse_from_filehandle
12.0
_parse_xda
12.0
CRAP 11.7
1 #!/usr/bin/perl
2  
3 # A package for parsing CEL files
4  
5 # Docs come before the code
6  
7 =head1 NAME
8  
9 Bio::Affymetrix::CEL- parse Affymetrix CEL files
10  
11 =head1 SYNOPSIS
12  
13 use Bio::Affymetrix::CEL;
14  
15 # Parse the CDF file
16  
17 my $cel=new Bio::Affymetrix::CEL();
18  
19 $cel->parse_from_file("foo.cel");
20  
21 # Print out all of the intensities for each square
22  
23 for (my $x=0;$x<scalar(@{$cel->intensity_map()});$x++) {
24 for (my $y=0;$y<scalar(@{cel->intensity_map()->[$x]});$y++) {
25 print join(",",($x,$y,$cel->intensity_map->[$x][$y]->[0])."\n";
26 }
27 }
28  
29  
30  
31 =head1 DESCRIPTION
32  
33 The Affymetrix microarray system produces files in a variety of
34 formats. If this means nothing to you, these modules are probably not
35 for you :). This module parses CEL files. Use this module if you want
36 to find out about the results for individual probes on an array.
37  
38 All of the Bio::Affymetrix modules parse a file entirely into
39 memory. You therefore need enough memory to hold these objects. For
40 some applications, parsing as a stream may be more appropriate-
41 hopefully the source to these modules will give enough clues to make
42 this an easy task. This module takes lots of memory due to the way
43 Perl stores arrays. If you are writing a program that uses many CEL
44 files, delete each CEL file once you have the information you need
45 from it.
46  
47 =head2 HINTS
48  
49 You fill the object filled with data using the
50 parse_from_filehandle, parse_from_string or parse_from_file
51 routines. You can get/set various statistics using methods on the
52 object.
53  
54 The key method is intensity_map. This returns a reference to a 2D
55 array of parameters for each square.
56  
57  
58 =head1 NOTES
59  
60 =head2 REFERENCE
61  
62 Modules were written with the official Affymetrix documentation, which
63 can be located at http://www.affymetrix.com/support/developer/AffxFileFormats.ZIP
64  
65 =head2 COMPATIBILITY
66  
67 This module can parse the CEL files produced by the Affymetrix software
68 MAS 5 and GCOS.
69  
70 =head1 TODO
71  
72 Writing CEL files as well as reading them.
73  
74 =head1 COPYRIGHT
75  
76 Copyright (C) 2005 by Nick James, David J Craigon, NASC, The
77 University of Nottingham
78  
79 This module is free software. You can copy or redistribute it under the same terms as Perl itself.
80  
81 Affymetrix is a registered trademark of Affymetrix Inc., Santa
82 Clara, California, USA.
83  
84 =head1 AUTHORS
85
86 Nick James (nick at arabidopsis.info)
87  
88 David J Craigon (david at arabidopsis.info)
89  
90 Nottingham Arabidopsis Stock Centre (http://arabidopsis.info), University of Nottingham.
91  
92 =head1 METHODS
93  
94 =cut
95  
96  
97 package Bio::Affymetrix::CEL;
98  
99  
100 use Carp;
101 use warnings;
102 use strict;
103 our $VERSION=0.5;
104  
105 =head2 new
106  
107 Arg [0] : none
108 Example : my $cel=new Bio::Affymetrix::CEL();
109 Description: constructor for CEL object
110 Returntype : new Bio::Affmetrix::CEL object
111 Exceptions : none
112 Caller : general
113  
114 =cut
115  
116 sub new {
117 my $class=shift;
118 my $q=shift;
119 my $self={};
120  
121 bless $self,$class;
122 return $self;
123 }
124  
125 # Getter/setters
126  
127 # CEL file trivia
128 =head2 original_version
129 Arg [0] : none
130 Example : my $version=$cel->original_version()
131 Description: Returns the version of the CEL file parsed. Encoded in file.
132 Returntype : string
133 Exceptions : none
134 Caller : general
135 =cut
136  
137 sub original_version {
138 my $self=shift;
139 return $self->{"VERSION"};
140 }
141  
142 =head2 original_format
143 Arg [0] : none
144 Example : my $format=$cel->original_format()
145 Description: Returns the format of the CEL file parsed. Currently MAS5 or XDA
146 Returntype : string ("MAS5" or "XDA")
147 Exceptions : none
148 Caller : general
149  
150 =cut
151  
152  
153 sub original_format {
154 my $self=shift;
155 return $self->{"FORMAT"};
156 }
157  
158 =head2 original_rows
159 Arg [0] : none
160 Example : my $rows=$cel->orignal_rows()
161 Description: Get the number of rows originally reported on this chip
162 Returntype : integer
163 Exceptions : none
164 Caller : general
165 =cut
166  
167 sub original_rows {
168 my $self=shift;
169  
170 return $self->{"ROWS"};
171 }
172  
173 =head2 original_cols
174 Arg [0] : none
175 Example : my $cols=$cel->original_cols()
176 Description: Get the number of cols originally reported on this chip
177 Returntype : integer
178 Exceptions : none
179 Caller : general
180 =cut
181  
182 sub original_cols {
183 my $self=shift;
184  
185 return $self->{"COLS"};
186 }
187  
188 =head2 x_offset
189 Arg [1] : integer $x_offset (optional)
190 Example : my $x_offset=$cel->x_offset()
191 Description: Get/Set the x offset. This is always 0 when parsed
192 from a genuine CEL file. Only available in MAS5 CEL files
193 Returntype : integer
194 Exceptions : none
195 Caller : general
196 =cut
197  
198 sub x_offset {
199 my $self=shift;
200 if (my $q=shift) {
201 $self->{"OFFSETX"}=$q;
202 }
203 return $self->{"OFFSETX"};
204 }
205  
206  
207 =head2 y_offset
208 Arg [1] : integer $y_offset (optional)
209 Example : my $y_offset=$cel->y_offset()
210 Description: Get/Set the y offset. This is always 0 when parsed
211 from a genuine CEL file. Only available in MAS5 CEL files.
212 Returntype : integer
213 Exceptions : none
214 Caller : general
215 =cut
216  
217 sub y_offset {
218 my $self=shift;
219 if (my $q=shift) {
220 $self->{"OFFSETY"}=$q;
221 }
222 return $self->{"OFFSETY"};
223 }
224  
225  
226 =head2 grid_corner_ul
227 Arg [1] : integer $grid_corner_ul (optional)
228 Example : my $grid_corner_ul=$cel->grid_corner_ul()
229 Description: Get/set the XY coordinates of the upper left grid corner in pixel coordinates. Only available in MAS5 CEL files.
230 Returntype : integer
231 Exceptions : none
232 Caller : general
233 =cut
234  
235 sub grid_corner_ul {
236 my $self=shift;
237 if (my $q=shift) {
238 $self->{"GRIDCORNERUL"}=$q;
239 }
240 return $self->{"GRIDCORNERUL"};
241 }
242  
243 =head2 grid_corner_ur
244 Arg [1] : integer $grid_corner_ur (optional)
245 Example : my $grid_corner_ur=$cel->grid_corner_ur()
246 Description: Get/set the XY coordinates of the upper right grid corner in pixel coordinates. Only available in MAS5 CEL files.
247 Returntype : integer
248 Exceptions : none
249 Caller : general
250 =cut
251  
252 sub grid_corner_ur {
253 my $self=shift;
254 if (my $q=shift) {
255 $self->{"GRIDCORNERUR"}=$q;
256 }
257 return $self->{"GRIDCORNERUR"};
258 }
259  
260 =head2 grid_corner_lr
261 Arg [1] : integer $grid_corner_lr (optional)
262 Example : my $grid_corner_lr=$cel->grid_corner_lr()
263 Description: Get/set the XY coordinates of the lower right grid corner in pixel coordinates. Only available in MAS5 CEL files.
264 Returntype : integer
265 Exceptions : none
266 Caller : general
267 =cut
268  
269 sub grid_corner_lr {
270 my $self=shift;
271 if (my $q=shift) {
272 $self->{"GRIDCORNERLR"}=$q;
273 }
274 return $self->{"GRIDCORNERLR"};
275 }
276  
277  
278 =head2 grid_corner_ll
279 Arg [1] : integer $grid_corner_ll (optional)
280 Example : my $grid_corner_ll=$cel->grid_corner_ll()
281 Description: Get/set the XY coordinates of the lower left grid corner in pixel coordinates. Only available in MAS5 CEL files.
282 Returntype : integer
283 Exceptions : none
284 Caller : general
285 =cut
286  
287 sub grid_corner_ll {
288 my $self=shift;
289 if (my $q=shift) {
290 $self->{"GRIDCORNERLL"}=$q;
291 }
292 return $self->{"GRIDCORNERLL"};
293 }
294  
295 =head2 axis_invert_x
296 Arg [1] : boolean $axis_invert_x (optional)
297 Example : my $axis_invert_x=$cel->axis_invert_x()
298 Description: Get/set whether the X axis is inverted. Always false in genuine CEL files Only available in MAS5 CEL files.
299 Returntype : boolean
300 Exceptions : none
301 Caller : general
302 =cut
303  
304 sub axis_invert_x {
305 my $self=shift;
306 if (my $q=shift) {
307 $self->{"AXIS-INVERTX"}=$q;
308 }
309 return $self->{"AXIS-INVERTX"};
310 }
311  
312 =head2 axis_invert_x
313 Arg [1] : boolean $axis_invert_y (optional)
314 Example : my $axis_invert_y=$cel->axis_invert_y()
315 Description: Get/set whether the Y axis is inverted. Always false in genuine CEL files Only available in MAS5 CEL files.
316 Returntype : boolean
317 Exceptions : none
318 Caller : general
319 =cut
320  
321 sub axis_invert_y {
322 my $self=shift;
323 if (my $q=shift) {
324 $self->{"AXISINVERTY"}=$q;
325 }
326 return $self->{"AXISINVERTY"};
327 }
328  
329  
330  
331 =head2 swap_xy
332 Arg [1] : boolean $swap_xy (optional)
333 Example : my $swap_xy=$cel->swap_xy()
334 Description: Get/set whether the X and Y axis are swapped. Always false in genuine CEL files Only available in MAS5 CEL files.
335 Returntype : boolean
336 Exceptions : none
337 Caller : general
338 =cut
339 sub swap_xy {
340 my $self=shift;
341 if (my $q=shift) {
342 $self->{"SWAPXY"}=$q;
343 }
344 return $self->{"SWAPXY"};
345 }
346  
347 =head2 original_file_name
348  
349 Arg [0] : none
350 Example : my $cel_file_name=$cel->original_file_name();
351 Description: If this object was created using parse_from_file, the original filename. Otherwise undef.
352 Returntype : string
353 Exceptions : none
354 Caller : general
355  
356 =cut
357  
358 sub original_file_name {
359 my $self=shift;
360  
361 return $self->{"file_name"};
362 }
363  
364  
365 # Algorithm name
366  
367 =head2 algorithm_name
368  
369 Arg [1] : string $algorithm_name (optional)
370 Example : my $algorithm_name=$cel->algorithm_name();
371 Description: Get/set the algorithm name that created this CEL file.
372 Returntype : string
373 Exceptions : none
374 Caller : general
375  
376 =cut
377 sub algorithm_name {
378 my $self=shift;
379 if (my $q=shift) {
380 $self->{"ALGORITHM"}=$q;
381 }
382  
383 return $self->{"ALGORITHM"};
384 }
385  
386 =head2 algorithm_params
387  
388 Arg [1] : hashref $algorithm_params (optional)
389 Example : my %algorithm_params=%{$cel->algorithm_params()};
390  
391 # Print margin of cells ignored for calculating signal
392  
393 print $algorithm_params("CellMargin");
394  
395 Description: Get/set the algorithm parameters for the CEL file creation
396 Returntype : hashref
397 Exceptions : none
398 Caller : general
399  
400 =cut
401  
402  
403 sub algorithm_params {
404 my $self=shift;
405 if (my $q=shift) {
406 $self->{"ALGORITHMPARAMS"}=$q;
407 }
408  
409 return $self->{"ALGORITHMPARAMS"};
410 }
411  
412  
413 =head2 intensity_map
414 Arg [1] : optional arrayref
415 Example : print $cel->intensity_map()->[0][0]->[0]
416 Description: Returns an reference to a 2D array. Each value in this array is another arrayref containing (in order):
417 Mean intensity, Standard Deviation of intensity, number of pixels
418 used in the calculation, a flag stating whether the user has masked
419 this square, a flag stating whether the software has called this
420 square an outlier.
421 Returntype : 2d arrayref
422 Exceptions : none
423 Caller : general
424 =cut
425  
426 sub intensity_map {
427 my $self=shift;
428 if (my $q=shift) {
429 $self->{"_INTENSITY"}=$q;
430 }
431  
432 return $self->{"_INTENSITY"};
433 }
434  
435  
436 =head2 parse_from_string
437  
438 Arg [1] : string
439 Example : $cel->parse_from_string($cel_file_in_a_string);
440 Description: Parse a CEL file from a buffer in memory
441 Returntype : none
442 Exceptions : none
443 Caller : general
444  
445 =cut
446  
447  
448 sub parse_from_string {
449 my $self=shift;
450 my $string=shift;
451  
452  
453 open CEL,"<",\$string or die "Cannot open string stream";
454  
455 $self->parse_from_filehandle(\*CEL);
456  
457 close CEL;
458 }
459  
460 =head2 parse_from_file
461  
462 Arg [1] : string
463 Example : $cel->parse_from_file($cel_filename);
464 Description: Parse a CEL file from a file
465 Returntype : none
466 Exceptions : dies if cannot open file
467 Caller : general
468  
469 =cut
470  
471 sub parse_from_file {
472 my $self=shift;
473 my $filename=shift;
474  
475 $self->{"file_name"}=$filename;
476  
477 open CDF,"<".$filename or die "Cannot open file ".$filename;
478  
479 $self->parse_from_filehandle(\*CDF);
480  
481 close CDF;
482 }
483  
484 =head2 parse_from_filehandle
485  
486 Arg [1] : reference to filehandle
487 Example : $cel->parse_from_filehandle(\*STDIN);
488 Description: Parse a CEL file from a filehandle
489 Returntype : none
490 Exceptions : none
491 Caller : general
492  
493 =cut
494  
495 sub parse_from_filehandle {
496 my $self=shift;
497 $self->{"FH"}=shift;
498  
499 binmode $self->{"FH"};
500  
501 # First step- detect whether it's a GCOS or MAS5 file
502
503 # A buffer for reading things into
504 my $buffer;
505  
506 # XDA files have their first feature as a magic number, 64
507  
508 read ($self->{"FH"}, $buffer, 4) or die "Cannot read from file";
509 my $magic_number = unpack("V", $buffer);
510  
511  
512 if ($magic_number==64) {
513 # It's a GCOS v1.2 "v4 file", XDA file! Hurrah!
514 $self->_parse_xda($self->{"FH"});
515 delete $self->{"FH"};
516 return;
517 }
518  
519 binmode $self->{"FH"},":crlf";
520  
521 $buffer.=readline($self->{"FH"});
522  
523 if ($buffer eq "[CEL]\n") {
524 # It's a MAS5/GCOS v1.0 "v3 file"! Yippee!
525 $self->_parse_mas5();
526 delete $self->{"FH"};
527  
528 return;
529 }
530  
531 croak "This doesn't look like a CEL file to me.";
532 }
533  
534 sub _parse_xda {
535 my $self=shift;
536
537 $self->{"FORMAT"}="XDA";
538  
539 my $buffer;
540  
541 # First some trivia
542  
543 (read ($self->{"FH"}, $buffer, 4)==4) or croak "Can no longer read from file";
544 $self->{"VERSION"}= unpack ("V", $buffer);
545
546 if ($self->{"VERSION"}!=4) {
547 carp "This CEL file is newer than the software parsing them. Results may be suspect."; # die here, perhaps?
548 }
549  
550 # CEL file trivia
551  
552 (read ($self->{"FH"}, $buffer, 12)==12) or die "Can no longer read from file";
553 ($self->{"COLS"},$self->{"ROWS"},undef)= unpack ("V3", $buffer);
554  
555 {
556 # Header, if you can be bothered
557 my $headerstring=$self->_unpack_length_string($self->{"FH"});
558 }
559  
560 # Algorithm name
561 $self->{"ALGORITHM"}=$self->_unpack_length_string($self->{"FH"});
562  
563 {
564 $self->{"ALGORITHMPARAMS"}={};
565 my $algorithmparamsstr=$self->_unpack_length_string($self->{"FH"});
566 foreach my $i (split /[; ]/,$algorithmparamsstr) {
567 my ($k,$v)=split /[\=\-\:]/,$i;
568 $self->{"ALGORITHMPARAMS"}->{$k}=$v;
569 }
570
571 }
572  
573  
574 (read ($self->{"FH"}, $buffer, 16)==16) or die "Can no longer read from file";
575  
576 my $outliercount;
577 my $maskedcount;
578 my $subgrids;
579  
580 # Cell margin is put in Algorithm Parameters in MAS5 files. We'll emulate this behaviour here.
581  
582 ($self->{"ALGORITHMPARAMS"}->{"CellMargin"},$outliercount,$maskedcount,$subgrids)=unpack("V4",$buffer);
583  
584 if ($subgrids!=0) {
585 croak "Bio::Affymetrix::CEL cannot do subgrids";
586 }
587  
588  
589 # Intensities
590  
591 my @intensitymap;
592  
593 for (my $y=0;$y<$self->{"ROWS"};$y++) {
594 for (my $x=0;$x<$self->{"COLS"};$x++) {
595  
596 (read ($self->{"FH"}, $buffer, 10)==10) or croak "Can no longer read from file";
597 my @q= unpack ("f2S", $buffer);
598  
599 push @q,(undef,undef);
600  
601 $intensitymap[$x][$y]=\@q;
602  
603 }
604 }
605  
606 # Masks
607  
608 for (my $i=0;$i<$maskedcount;$i++) {
609 (read ($self->{"FH"}, $buffer, 4)==4) or croak "Can no longer read from file";
610 my ($x,$y)= unpack ("S2", $buffer);
611
612 $intensitymap[$x][$y]->[3]=1;
613
614 }
615  
616  
617 for (my $i=0;$i<$outliercount;$i++) {
618 (read ($self->{"FH"}, $buffer, 4)==4) or croak "Can no longer read from file";
619 my ($x,$y)= unpack ("S2", $buffer);
620
621 $intensitymap[$x][$y]->[4]=1;
622
623 }
624  
625 $self->{"_INTENSITY"}=\@intensitymap;
626 }
627  
628 sub _parse_mas5 {
629 my $self=shift;
630  
631 # Obtain file version, and do some rudimentary checking of information
632
633 $self->{"FORMAT"}="MAS5";
634
635 my $i=$self->_next_line();
636
637 my ($name,$value)=$self->_split_line($i);
638
639 if ($name ne "Version") {
640 croak "File does not look like a CEL file to me";
641 }
642
643 if ($value ne "3") {
644 croak "Can't understand any version of MAS5 CEL files other than 3";
645 }
646
647 $self->{"VERSION"}=$value;
648  
649  
650 # Parse the rest of the file
651 $i=$self->_next_line();
652  
653 while (!eof $self->{"FH"}) {
654 if ($i eq "[HEADER]") {
655 $i=$self->_parse_header_section($i);
656 } elsif ($i eq "[INTENSITY]") {
657 $i=$self->_parse_intensity_section($i);
658 } elsif ($i eq "[MASKS]") {
659 $i=$self->_parse_mask_section($i);
660 } elsif ($i eq "[OUTLIERS]") {
661 $i=$self->_parse_outlier_section($i);
662 } else {
663 $i=$self->_next_line();
664 }
665 }
666 }
667  
668  
669 # Parsing bits of the CDF file
670  
671 sub _parse_header_section {
672 my $self=shift;
673 my $i=shift;;
674 while (!(($i=$self->_next_line())=~/^\[.*\]$/)) {
675 my ($name,$value)=$self->_split_line($i);
676 if ($name eq "TotalX" || $name eq "TotalY") {
677 ; # Do nothing- this contains no information
678 } elsif ($name eq "AlgorithmParameters") {
679 # Apparently, this file can have = dilimited too
680 my @list=split /;/,$value;
681 my %ap;
682 foreach my $q (@list) {
683 my ($a,$b)=split /:/,$q;
684 $ap{$a}=$b;
685 }
686  
687 $self->{"ALGORITHMPARAMS"}=\%ap;
688  
689 } else {
690 $self->{uc $name}=$value;
691 }
692 }
693  
694 return $i;
695 }
696  
697 sub _parse_intensity_section {
698 my $self=shift;
699  
700 my $i=shift;
701  
702 my $fh=$self->{"FH"};
703  
704 my @intensitymap;
705  
706 $i=<$fh>; # Two lines of junk
707 $i=<$fh>;
708  
709  
710 while ((($i=<$fh>)=~/^\s*(\d+)\s+(\d+)\s+([\d\.]+)\s+([\d\.]+)\s+(\d+)/o)) {
711 $intensitymap[$1][$2]=[$3,$4,$5,undef,undef];
712
713 }
714  
715  
716 $self->{"_INTENSITY"}=\@intensitymap;
717  
718  
719 chomp $i;
720 return $i;
721 }
722  
723  
724 sub _parse_mask_section {
725 my $self=shift;
726 shift;
727  
728 my $i;
729 $i=readline $self->{"FH"}; # Two lines of junk
730 $i=readline $self->{"FH"};
731  
732 while ((($i=readline $self->{"FH"})=~/^\s*(\d+)\s+(\d+)\s*$/o)) {
733 $self->{"_INTENSITY"}->[$1][$2]->[3]=1;
734 }
735  
736  
737 chomp $i;
738 return $i;
739 }
740  
741  
742 sub _parse_outlier_section {
743 my $self=shift;
744 shift;
745  
746 my $i;
747 $i=readline $self->{"FH"}; # Two lines of junk
748 $i=readline $self->{"FH"};
749  
750 while ((($i=readline $self->{"FH"})=~/^\s*(\d+)\s+(\d+)\s*$/o)) {
751 $self->{"_INTENSITY"}->[$1][$2]->[4]=1;
752 }
753  
754 chomp $i;
755 return $i;
756 }
757  
758  
759  
760 # General INI utility functions
761  
762 sub _split_line {
763 my $self=shift;
764 my $line=shift;
765
766 my @q=split /=/,$line,2;
767  
768 if (scalar(@q)!=2) {
769 die "Can't parse line ".$line;
770 }
771  
772 return @q;
773 }
774  
775 # Sub that ignores blank lines
776  
777 sub _next_line {
778 my $self=shift;
779 my $q;
780
781 my $fh=$self->{"FH"};
782  
783 do {
784 $q=<$fh>;
785 chomp $q;
786 } while (!eof $fh&&$q=~/^\s*$/);
787  
788 if (!eof $fh) {
789 return $q;
790 } else {
791 return undef;
792 }
793 }
794  
795  
796 # Writing
797  
798 sub write_to_filehandle {
799 my $self=shift;
800 my $filehandle=shift;
801 my $format=shift;
802 my $version=shift;
803  
804 binmode $filehandle,":crlf";
805 }
806  
807 # binary file utility to read a length-defined string
808  
809 sub _unpack_length_string {
810 my $self=shift;
811 my $fh=shift;
812
813 my $buffer;
814  
815 (read ($fh, $buffer, 4)==4) or die "Can no longer read from file";;
816 my $len = unpack ("V", $buffer);
817 (read ($fh, $buffer, $len)==$len) or die "Can no longer read from file";
818  
819 return unpack("a".$len,$buffer);
820  
821 }
822  
823  
824 1;