| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # A package for parsing CDF files | ||
| 4 | |||
| 5 | # Docs come before the code | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Bio::Affymetrix::CDF- parse Affymetrix CDF files | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Bio::Affymetrix::CDF; | ||
| 14 | |||
| 15 | # Parse the CDF file | ||
| 16 | |||
| 17 | my $cdf=new Bio::Affymetrix::CDF({"probemode"=>0}); | ||
| 18 | |||
| 19 | $cdf->parse_from_file("foo.cdf"); | ||
| 20 | |||
| 21 | # Find some fun facts about this chip type | ||
| 22 | |||
| 23 | print $cdf->rows().",".$cdf->cols()."\n"; | ||
| 24 | |||
| 25 | print $cdf->version()."\n"; | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | # Print out all of the probeset names on this chip type | ||
| 30 | |||
| 31 | foreach my $i (keys %{$chp->probesets}) { | ||
| 32 | print $chp->probesets->{$i}->name()."\n"; | ||
| 33 | } | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | The Affymetrix microarray system produces files in a variety of | ||
| 38 | formats. If this means nothing to you, these modules are probably not | ||
| 39 | for you :). This module parses CDF files. Use this module if you want | ||
| 40 | to find out about the design of an Affymetrix GeneChip, or you need the | ||
| 41 | object for another one of the modules in this package. | ||
| 42 | |||
| 43 | All of the Bio::Affymetrix modules parse a file entirely into | ||
| 44 | memory. You therefore need enough memory to hold these objects. For | ||
| 45 | some applications, parsing as a stream may be more appropriate- | ||
| 46 | hopefully the source to these modules will give enough clues to make | ||
| 47 | this an easy task. This module in particular takes a lot of memory if | ||
| 48 | probe information is also stored (about 150Mb). Memory usage is not too | ||
| 49 | onorous (about 15Mb) if probe level information is omitted. You | ||
| 50 | can.control this by setting probemode=>1 or probemode=>0 in the constructor. | ||
| 51 | |||
| 52 | You can also use these modules to write CDF files (using the | ||
| 53 | write_to_filehandle method). See COMPATIBILITY for some important caveats. | ||
| 54 | |||
| 55 | =head2 HINTS | ||
| 56 | |||
| 57 | You fill the object filled with data using the | ||
| 58 | parse_from_filehandle, parse_from_string or parse_from_file | ||
| 59 | routines. You can get/set various statistics using methods on the | ||
| 60 | object. | ||
| 61 | |||
| 62 | The key method is probesets. This returns a reference to a hash of | ||
| 63 | Bio::Affymetrix::CDF::Probeset objects. The keys of this hash are unit | ||
| 64 | number - if you are looking for a specific probeset you will have to | ||
| 65 | search for it yourself. Each Bio::Affymetrix::CDF::Probeset object | ||
| 66 | contains information about the probesets. | ||
| 67 | |||
| 68 | |||
| 69 | =head1 NOTES | ||
| 70 | |||
| 71 | =head2 REFERENCE | ||
| 72 | |||
| 73 | Modules were written with the official Affymetrix documentation, which | ||
| 74 | can be located at http://www.affymetrix.com/support/developer/AffxFileFormats.ZIP | ||
| 75 | |||
| 76 | =head2 COMPATIBILITY | ||
| 77 | |||
| 78 | This module can parse the CDF files used with the Affymetrix software | ||
| 79 | MAS 5 and GCOS. These files have QC information in them (such as the | ||
| 80 | information of the location of the QC probesets), which is not parsed. | ||
| 81 | |||
| 82 | This module can also write CDF files. The support is currently pretty | ||
| 83 | limited .Currently the software can only write MAS5 files (not XDA | ||
| 84 | format files), and will only write files that have been parsed in | ||
| 85 | previously- it cannot create CDF files from scratch. So if you have | ||
| 86 | any way of making Affymetrix chips, you will just have to look | ||
| 87 | elsewhere :). These limitations are caused through not parsing the QC | ||
| 88 | information. | ||
| 89 | |||
| 90 | =head1 TODO | ||
| 91 | |||
| 92 | Parsing QC information? Rearrange probe information to make it more usable? | ||
| 93 | |||
| 94 | =head1 COPYRIGHT | ||
| 95 | |||
| 96 | Copyright (C) 2005 by Nick James, David J Craigon, NASC, The | ||
| 97 | University of Nottingham | ||
| 98 | |||
| 99 | This module is free software. You can copy or redistribute it under the same terms as Perl itself. | ||
| 100 | |||
| 101 | Affymetrix is a registered trademark of Affymetrix Inc., Santa | ||
| 102 | Clara, California, USA. | ||
| 103 | |||
| 104 | =head1 AUTHORS | ||
| 105 | |||
| 106 | Nick James (nick at arabidopsis.info) | ||
| 107 | |||
| 108 | David J Craigon (david at arabidopsis.info) | ||
| 109 | |||
| 110 | Nottingham Arabidopsis Stock Centre (http://arabidopsis.info), University of Nottingham. | ||
| 111 | |||
| 112 | =head1 METHODS | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | |||
| 117 | package Bio::Affymetrix::CDF; | ||
| 118 | |||
| 119 | |||
| 120 | use Carp; | ||
| 121 | use warnings; | ||
| 122 | use strict; | ||
| 123 | our $VERSION=0.5; | ||
| 124 | |||
| 125 | use Bio::Affymetrix::CDF::Probeset; | ||
| 126 | |||
| 127 | =head2 new | ||
| 128 | |||
| 129 | Arg [1] : hashref of parameters (optional) | ||
| 130 | Example : my $cdf=new Bio::Affymetrix::CDF({probemode=>1); | ||
| 131 | Description: constructor for CDF object. Turn probemode on and off (default off) by supplying named parameters as a hash reference | ||
| 132 | Returntype : new Bio::Affmetrix::CDF object | ||
| 133 | Exceptions : none | ||
| 134 | Caller : general | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub new { | ||
| 139 | my $class=shift; | ||
| 140 | my $q=shift; | ||
| 141 | my $self={}; | ||
| 142 | |||
| 143 | if (ref $q eq "HASH" && exists $q->{"probemode"}) { | ||
| 144 | $self->{"_PROBEMODE"}=$q->{"probemode"}; | ||
| 145 | } else { | ||
| 146 | $self->{"_PROBEMODE"}=undef; | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->{"PROBESETS"}={}; | ||
| 150 | |||
| 151 | bless $self,$class; | ||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | # Getter/setters | ||
| 156 | |||
| 157 | # CDF file trivia | ||
| 158 | =head2 original_version | ||
| 159 | Arg [0] : none | ||
| 160 | Example : my $version=$cdf->original_version() | ||
| 161 | Description: Returns the version of the CDF file parsed. Encoded in file. | ||
| 162 | Returntype : string | ||
| 163 | Exceptions : none | ||
| 164 | Caller : general | ||
| 165 | =cut | ||
| 166 | |||
| 167 | sub original_version { | ||
| 168 | my $self=shift; | ||
| 169 | return $self->{"VERSION"}; | ||
| 170 | } | ||
| 171 | |||
| 172 | =head2 original_format | ||
| 173 | Arg [0] : none | ||
| 174 | Example : my $format=$cdf->original_format() | ||
| 175 | Description: Returns the format of the CDF file parsed. Currently | ||
| 176 | MAS5 or XDA. | ||
| 177 | Returntype : string | ||
| 178 | Exceptions : none | ||
| 179 | Caller : general | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | |||
| 184 | sub original_format { | ||
| 185 | my $self=shift; | ||
| 186 | return $self->{"FORMAT"}; | ||
| 187 | } | ||
| 188 | |||
| 189 | # Chip name | ||
| 190 | |||
| 191 | =head2 name | ||
| 192 | Arg [1] : string $name (optional) | ||
| 193 | Example : my $name=$cdf->name() | ||
| 194 | Description: Get/set the name of this chip type | ||
| 195 | (e.g. ATH1-121501). Only supplied by MAS5 version files. | ||
| 196 | Returntype : string | ||
| 197 | Exceptions : none | ||
| 198 | Caller : general | ||
| 199 | =cut | ||
| 200 | |||
| 201 | sub name { | ||
| 202 | my $self=shift; | ||
| 203 | if (my $q=shift) { | ||
| 204 | $self->{"NAME"}=$q; | ||
| 205 | } | ||
| 206 | return $self->{"NAME"}; | ||
| 207 | } | ||
| 208 | |||
| 209 | =head2 resequencing_reference_sequence | ||
| 210 | Arg [1] : string $refseq (optional) | ||
| 211 | Example : my $refseq=$cdf->resequencing_reference_sequence() | ||
| 212 | Description: Get/set the name of resequencing_reference_sequence. | ||
| 213 | Only available in GCOS format files | ||
| 214 | Returntype : string | ||
| 215 | Exceptions : none | ||
| 216 | Caller : general | ||
| 217 | =cut | ||
| 218 | |||
| 219 | sub resequencing_reference_sequence { | ||
| 220 | my $self=shift; | ||
| 221 | if (my $q=shift) { | ||
| 222 | $self->{"NAME"}=$q; | ||
| 223 | } | ||
| 224 | return $self->{"NAME"}; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | =head2 rows | ||
| 230 | Arg [1] : integer $rows (optional) | ||
| 231 | Example : my $name=$cdf->rows() | ||
| 232 | Description: Get/set the number of rows in this chip | ||
| 233 | Returntype : integer | ||
| 234 | Exceptions : none | ||
| 235 | Caller : general | ||
| 236 | =cut | ||
| 237 | |||
| 238 | sub rows { | ||
| 239 | my $self=shift; | ||
| 240 | if (my $q=shift) { | ||
| 241 | $self->{"ROWS"}=$q; | ||
| 242 | } | ||
| 243 | return $self->{"ROWS"}; | ||
| 244 | } | ||
| 245 | |||
| 246 | =head2 cols | ||
| 247 | Arg [1] : integer $cols (optional) | ||
| 248 | Example : my $name=$cdf->cols() | ||
| 249 | Description: Get/set the number of cols in this chip | ||
| 250 | Returntype : integer | ||
| 251 | Exceptions : none | ||
| 252 | Caller : general | ||
| 253 | =cut | ||
| 254 | |||
| 255 | sub cols { | ||
| 256 | my $self=shift; | ||
| 257 | if (my $q=shift) { | ||
| 258 | $self->{"COLS"}=$q; | ||
| 259 | } | ||
| 260 | return $self->{"COLS"}; | ||
| 261 | } | ||
| 262 | |||
| 263 | =head2 probesets | ||
| 264 | Arg [1] : hashref $probesets | ||
| 265 | Example : my %probesets=%{$cdf->probesets()} | ||
| 266 | Description: Get the probesets on the array | ||
| 267 | Returntype : an reference to an hash of | ||
| 268 | Bio::Affymetrix::CDF::Probeset objects (q.v.), keyed on unit number | ||
| 269 | Exceptions : none | ||
| 270 | Caller : general | ||
| 271 | =cut | ||
| 272 | |||
| 273 | |||
| 274 | sub probesets { | ||
| 275 | my $self=shift; | ||
| 276 | |||
| 277 | if (my $q=shift) { | ||
| 278 | $self->{"PROBESETS"}=$q; | ||
| 279 | } | ||
| 280 | |||
| 281 | return $self->{"PROBESETS"}; | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | =head2 probe_grid | ||
| 286 | Arg [1] : arrayref $probelist | ||
| 287 | Example : my $probe=$ps->probe_grid()->[500][500]; #Return probe at 500,500 | ||
| 288 | Description: Get/set the grid of probes making up this array. Only available if | ||
| 289 | with probes mode is used. | ||
| 290 | |||
| 291 | Returns an reference to a two dimensional array of | ||
| 292 | Bio::Affymetrix::CDF::Probe objects. | ||
| 293 | Returntype : reference to two-dimensional array of Bio::Affymetrix::CDF::Probe objects | ||
| 294 | Exceptions : none | ||
| 295 | Caller : general | ||
| 296 | =cut | ||
| 297 | |||
| 298 | |||
| 299 | sub probe_grid { | ||
| 300 | my $self=shift; | ||
| 301 | if (!$self->{"_PROBEMODE"}) { | ||
| 302 | croak "probe_grid is not available when not in probemode"; | ||
| 303 | } | ||
| 304 | |||
| 305 | if (my $q=shift) { | ||
| 306 | $self->{"PROBEGRID"}=$q; | ||
| 307 | } | ||
| 308 | return $self->{"PROBEGRID"}; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | # These are all named "original_" because they aren't calculated, they are what a parsed file claims | ||
| 313 | |||
| 314 | =head2 original_number_of_probes | ||
| 315 | Arg [0] : none | ||
| 316 | Example : my $number_of_probes=$cdf->original_number_of_probes() | ||
| 317 | Description: Get the number of probesets on the array, as listed | ||
| 318 | originally in the file. A better way is to do my | ||
| 319 | $q=scalar(@{$cdf->probesets()}); if you want a current count. Should really be called original_number_of_probesets. | ||
| 320 | Returntype : integer | ||
| 321 | Exceptions : none | ||
| 322 | Caller : general | ||
| 323 | =cut | ||
| 324 | |||
| 325 | |||
| 326 | sub original_number_of_probes { | ||
| 327 | my $self=shift; | ||
| 328 | return $self->{"NUMBEROFUNITS"}; | ||
| 329 | } | ||
| 330 | |||
| 331 | =head2 original_max_unit | ||
| 332 | Arg [0] : none | ||
| 333 | Example : my $max_units=$cdf->original_max_units() | ||
| 334 | Description: Get the max unit number in the CDF file. Fairly useless. Only available in MAS5 files | ||
| 335 | Returntype : integer | ||
| 336 | Exceptions : none | ||
| 337 | Caller : general | ||
| 338 | =cut | ||
| 339 | |||
| 340 | sub original_max_unit { | ||
| 341 | my $self=shift; | ||
| 342 | return $self->{"MAXUNIT"}; | ||
| 343 | } | ||
| 344 | |||
| 345 | =head2 original_num_qc_units | ||
| 346 | Arg [0] : none | ||
| 347 | Example : my $max_units=$cdf->original_num_qc_units() | ||
| 348 | Description: Get the number of QC units in the CDF file. Only piece | ||
| 349 | of QC information obtainable using this piece of software. | ||
| 350 | Returntype : integer | ||
| 351 | Exceptions : none | ||
| 352 | Caller : general | ||
| 353 | =cut | ||
| 354 | |||
| 355 | |||
| 356 | sub original_num_qc_units { | ||
| 357 | my $self=shift; | ||
| 358 | return $self->{"NUMQCUNITS"}; | ||
| 359 | } | ||
| 360 | |||
| 361 | =head2 original_file_name | ||
| 362 | |||
| 363 | Arg [0] : none | ||
| 364 | Example : my $cdf_file_name=$cdf->original_file_name(); | ||
| 365 | Description: If this object was created using parse_from_file, the original filename. Otherwise undef. | ||
| 366 | Returntype : string | ||
| 367 | Exceptions : none | ||
| 368 | Caller : general | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | |||
| 373 | sub original_file_name { | ||
| 374 | my $self=shift; | ||
| 375 | |||
| 376 | return $self->{"file_name"}; | ||
| 377 | } | ||
| 378 | |||
| 379 | |||
| 380 | # PARSING ROUTINES | ||
| 381 | |||
| 382 | =head2 parse_from_string | ||
| 383 | |||
| 384 | Arg [1] : string | ||
| 385 | Example : $cdf->parse_from_string($cdf_file_in_a_string); | ||
| 386 | Description: Parse a CDF file from a buffer in memory | ||
| 387 | Returntype : none | ||
| 388 | Exceptions : none | ||
| 389 | Caller : general | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | |||
| 394 | sub parse_from_string { | ||
| 395 | my $self=shift; | ||
| 396 | my $string=shift; | ||
| 397 | |||
| 398 | |||
| 399 | open CDF,"<",\$string or carp "Cannot open string stream"; | ||
| 400 | |||
| 401 | $self->parse_from_filehandle(\*CDF); | ||
| 402 | |||
| 403 | close CDF; | ||
| 404 | } | ||
| 405 | |||
| 406 | =head2 parse_from_file | ||
| 407 | |||
| 408 | Arg [1] : string | ||
| 409 | Example : $cdf->parse_from_file($cdf_filename); | ||
| 410 | Description: Parse a CDF file from a file | ||
| 411 | Returntype : none | ||
| 412 | Exceptions : dies if cannot open file | ||
| 413 | Caller : general | ||
| 414 | |||
| 415 | =cut | ||
| 416 | |||
| 417 | sub parse_from_file { | ||
| 418 | my $self=shift; | ||
| 419 | my $filename=shift; | ||
| 420 | |||
| 421 | $self->{"file_name"}=$filename; | ||
| 422 | open CDF,"<".$filename or carp "Cannot open file ".$filename; | ||
| 423 | |||
| 424 | $self->parse_from_filehandle(\*CDF); | ||
| 425 | |||
| 426 | close CDF; | ||
| 427 | } | ||
| 428 | |||
| 429 | =head2 parse_from_filehandle | ||
| 430 | |||
| 431 | Arg [1] : reference to filehandle | ||
| 432 | Example : $cdf->parse_from_filehandle(\*STDIN); | ||
| 433 | Description: Parse a CDF file from a filehandle | ||
| 434 | Returntype : none | ||
| 435 | Exceptions : none | ||
| 436 | Caller : general | ||
| 437 | |||
| 438 | =cut | ||
| 439 | |||
| 440 | |||
| 441 | sub parse_from_filehandle { | ||
| 442 | my $self=shift; | ||
| 443 | $self->{"FH"}=shift; | ||
| 444 | |||
| 445 | binmode $self->{"FH"}; | ||
| 446 | |||
| 447 | # First step- detect whether it's a GCOS or MAS5 file | ||
| 448 | |||
| 449 | # A buffer for reading things into | ||
| 450 | my $buffer; | ||
| 451 | |||
| 452 | # XDA files have their first feature as a magic number, 65 | ||
| 453 | |||
| 454 | read ($self->{"FH"}, $buffer, 4) or die "Cannot read from file"; | ||
| 455 | my $magic_number = unpack("V", $buffer); | ||
| 456 | |||
| 457 | |||
| 458 | if ($magic_number==67) { | ||
| 459 | # It's a GCOS v1.2 "v4 file", XDA file! Hurrah! | ||
| 460 | $self->_parse_xda($self->{"FH"}); | ||
| 461 | delete $self->{"FH"}; | ||
| 462 | return; | ||
| 463 | } | ||
| 464 | |||
| 465 | binmode $self->{"FH"},":crlf"; | ||
| 466 | |||
| 467 | $buffer.=readline($self->{"FH"}); | ||
| 468 | |||
| 469 | if ($buffer eq "[CDF]\n") { | ||
| 470 | # It's a MAS5/GCOS v1.0 "v3 file"! Yippee! | ||
| 471 | $self->_parse_mas5($self->{"FH"}); | ||
| 472 | return; | ||
| 473 | delete $self->{"FH"}; | ||
| 474 | } | ||
| 475 | |||
| 476 | die "This doesn't look like a CDF file to me."; | ||
| 477 | } | ||
| 478 | |||
| 479 | sub _parse_xda { | ||
| 480 | my ($self,$fh) = @_; | ||
| 481 | |||
| 482 | $self->{"FORMAT"}="XDA"; | ||
| 483 | |||
| 484 | my $buffer; | ||
| 485 | |||
| 486 | # First some trivia | ||
| 487 | |||
| 488 | (read ($fh, $buffer, 4)==4) or croak "Can no longer read from file"; | ||
| 489 | $self->{"VERSION"}= unpack ("V", $buffer); | ||
| 490 | |||
| 491 | if ($self->{"VERSION"}!=1) { | ||
| 492 | carp "This CDF file is newer than the software parsing them. Results may be suspect."; # die here, perhaps? | ||
| 493 | } | ||
| 494 | |||
| 495 | # CDF file trivia | ||
| 496 | |||
| 497 | (read ($fh, $buffer, 12)==12) or die "Can no longer read from file"; | ||
| 498 | ($self->{"COLS"},$self->{"ROWS"},$self->{"NUMBEROFUNITS"},$self->{"NUMQCUNITS"})= unpack ("S2V2", $buffer); | ||
| 499 | |||
| 500 | $self->{"RESEQREFSEQ"}=$self->_unpack_length_string($fh); #What's this? | ||
| 501 | |||
| 502 | # Probe names | ||
| 503 | |||
| 504 | $self->{"PROBESETS"}={}; | ||
| 505 | |||
| 506 | for (my $i=1;$i<=$self->{"NUMBEROFUNITS"};$i++) { | ||
| 507 | (read ($fh, $buffer, 64)==64) or die "Can no longer read from file"; | ||
| 508 | # my $name=unpack("Z64",$buffer); | ||
| 509 | # $self->{"PROBESETS"}->{$i}=new Bio::Affymetrix::CDF::Probeset; | ||
| 510 | # $self->{"PROBESETS"}->{$i}->name($name); | ||
| 511 | } | ||
| 512 | |||
| 513 | # File offsets that are not useful for us | ||
| 514 | read ($fh, $buffer, 4*($self->{"NUMBEROFUNITS"}+$self->{"NUMQCUNITS"})); | ||
| 515 | |||
| 516 | # QC information is just stored, for now | ||
| 517 | |||
| 518 | for (my $i=0;$i<$self->{"NUMQCUNITS"};$i++) { | ||
| 519 | (read ($fh, $buffer, 6)==6) or die "Can no longer read from file"; | ||
| 520 | my ($junk,$numprobe)=unpack("SV",$buffer); | ||
| 521 | $self->{"_qcinfo"}.=$buffer; | ||
| 522 | (read ($fh, $buffer, 7*$numprobe)==7*$numprobe) or die "Can no longer read from file"; | ||
| 523 | $self->{"_qcinfo"}.=$buffer; | ||
| 524 | } | ||
| 525 | |||
| 526 | # Probe information | ||
| 527 | for (my $i=1;$i<=$self->{"NUMBEROFUNITS"};$i++) { | ||
| 528 | my $ps=new Bio::Affymetrix::CDF::Probeset(); | ||
| 529 | $ps->_parse_from_filehandle_bin($fh,$self->{"_PROBEMODE"}); | ||
| 530 | $ps->{"CDF"}=$self; | ||
| 531 | $self->{"PROBESETS"}->{$ps->{"UNITNUMBER"}}=$ps; | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | |||
| 536 | sub _parse_mas5 { | ||
| 537 | my $self=shift; | ||
| 538 | |||
| 539 | binmode $self->{"FH"},":crlf"; | ||
| 540 | # Obtain file version, and do some rudimentary checking of information | ||
| 541 | { | ||
| 542 | $self->{"FORMAT"}="MAS5"; | ||
| 543 | |||
| 544 | my $i=$self->_next_line(); | ||
| 545 | |||
| 546 | my ($name,$value)=$self->_split_line($i); | ||
| 547 | |||
| 548 | if ($name ne "Version") { | ||
| 549 | die "File does not look like a CDF file to me"; | ||
| 550 | } | ||
| 551 | |||
| 552 | if ($value ne "GC3.0") { | ||
| 553 | die "Can't understand any other type of CDF file other than GC3.0"; | ||
| 554 | } | ||
| 555 | |||
| 556 | $self->{"VERSION"}=$value; | ||
| 557 | } | ||
| 558 | |||
| 559 | # Parse the rest of the file | ||
| 560 | my $i=$self->_next_line(); | ||
| 561 | |||
| 562 | while (!eof $self->{"FH"}) { | ||
| 563 | if ($i eq "[Chip]") { | ||
| 564 | $i=$self->_parse_chip_section($i); | ||
| 565 | } elsif ($i=~/\[QC\d+\]/) { | ||
| 566 | $i=$self->_parse_qc_section($i); | ||
| 567 | } elsif ($i=~/\[Unit\d+\]/) { | ||
| 568 | $i=$self->_parse_unit_section($i); | ||
| 569 | } else { | ||
| 570 | $i=$self->_next_line(); | ||
| 571 | } | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | # Parsing bits of the CDF file | ||
| 576 | |||
| 577 | sub _parse_chip_section { | ||
| 578 | my $self=shift; | ||
| 579 | my $i=shift;; | ||
| 580 | while (!(($i=$self->_next_line())=~/^\[.*\]$/)) { | ||
| 581 | my ($name,$value)=$self->_split_line($i); | ||
| 582 | $self->{uc $name}=$value; | ||
| 583 | } | ||
| 584 | |||
| 585 | return $i; | ||
| 586 | } | ||
| 587 | |||
| 588 | sub _parse_qc_section { | ||
| 589 | my $self=shift; | ||
| 590 | |||
| 591 | my $i=shift; | ||
| 592 | |||
| 593 | my $fh=$self->{"FH"}; | ||
| 594 | |||
| 595 | $self->{"_qcinfo"}.=$i."\n"; | ||
| 596 | |||
| 597 | while (!(($i=<$fh>)=~/^^\s*\[.*\]\s*$/)) { | ||
| 598 | # QC sections still bore | ||
| 599 | # us, but we're now going to store them for later | ||
| 600 | |||
| 601 | $self->{"_qcinfo"}.=$i; | ||
| 602 | |||
| 603 | |||
| 604 | } | ||
| 605 | chomp $i; | ||
| 606 | return $i; | ||
| 607 | } | ||
| 608 | |||
| 609 | |||
| 610 | sub _parse_unit_section { | ||
| 611 | my $self=shift; | ||
| 612 | shift; | ||
| 613 | my $i=new Bio::Affymetrix::CDF::Probeset; | ||
| 614 | $i->CDF($self); | ||
| 615 | my $ret=$i->_parse_from_filehandle($self->{"FH"},$self->{"_PROBEMODE"}); | ||
| 616 | |||
| 617 | |||
| 618 | |||
| 619 | $self->{"PROBESETS"}->{$i->original_unit_number()}=$i; | ||
| 620 | |||
| 621 | return $ret; | ||
| 622 | } | ||
| 623 | |||
| 624 | |||
| 625 | # General INI utility functions | ||
| 626 | |||
| 627 | sub _split_line { | ||
| 628 | my $self=shift; | ||
| 629 | my $line=shift; | ||
| 630 | |||
| 631 | my @q=split /=/,$line,2; | ||
| 632 | |||
| 633 | if (scalar(@q)!=2) { | ||
| 634 | die "Can't parse line ".$line; | ||
| 635 | } | ||
| 636 | |||
| 637 | return @q; | ||
| 638 | } | ||
| 639 | |||
| 640 | # Sub that ignores blank lines | ||
| 641 | |||
| 642 | sub _next_line { | ||
| 643 | my $self=shift; | ||
| 644 | my $q; | ||
| 645 | |||
| 646 | my $fh=$self->{"FH"}; | ||
| 647 | |||
| 648 | do { | ||
| 649 | $q=<$fh>; | ||
| 650 | chomp $q; | ||
| 651 | } while (!eof $fh&&$q=~/^\s*$/); | ||
| 652 | |||
| 653 | if (!eof $fh) { | ||
| 654 | return $q; | ||
| 655 | } else { | ||
| 656 | return undef; | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | |||
| 661 | # Writing | ||
| 662 | |||
| 663 | =head2 write_to_file | ||
| 664 | |||
| 665 | Arg [1] : string $filename | ||
| 666 | Arg [2] : string $format | ||
| 667 | Arg [3] : string $version | ||
| 668 | Example : $cdf->write_to_file($cdf_filename); | ||
| 669 | Description: Writes a CDF file to a file. See write_to_filehandle for descriptions of format and version | ||
| 670 | Returntype : none | ||
| 671 | Exceptions : dies if cannot open file | ||
| 672 | Caller : general | ||
| 673 | |||
| 674 | =cut | ||
| 675 | |||
| 676 | sub write_to_file { | ||
| 677 | my $self=shift; | ||
| 678 | my $filename=shift; | ||
| 679 | |||
| 680 | open CDF,">".$filename or croak "Cannot open file for writing ".$filename; | ||
| 681 | |||
| 682 | $self->write_to_filehandle(\*CDF,@_); | ||
| 683 | |||
| 684 | close CDF; | ||
| 685 | } | ||
| 686 | |||
| 687 | =head2 write_to_filehandle | ||
| 688 | |||
| 689 | Arg [1] : filehandle $filehandle | ||
| 690 | Arg [2] : string $format | ||
| 691 | Arg [3] : string $version | ||
| 692 | Example : $cdf->write_to_filehandle($cdf_filename); | ||
| 693 | Description: Writes a CDF file to a filehandle. Takes arguments of | ||
| 694 | the filehandle, the desired format, and the desired version of that | ||
| 695 | format. | ||
| 696 | |||
| 697 | Currently, format defaults to MAS5, and version defaults to | ||
| 698 | GC3.0. These are the only formats the software is capable of | ||
| 699 | producing currently. Also, this software cannot write files that | ||
| 700 | were read in using the GCOS file format. The original CDF file must | ||
| 701 | have been parsed in probe mode. | ||
| 702 | |||
| 703 | Returntype : none | ||
| 704 | Exceptions : dies if cannot open file | ||
| 705 | Caller : general | ||
| 706 | |||
| 707 | =cut | ||
| 708 | |||
| 709 | |||
| 710 | sub write_to_filehandle { | ||
| 711 | my $self=shift; | ||
| 712 | my $filehandle=shift; | ||
| 713 | my $format=shift; | ||
| 714 | my $version=shift; | ||
| 715 | |||
| 716 | if (!$self->{"_PROBEMODE"}) { | ||
| 717 | croak "No writing when not in probemode"; | ||
| 718 | } | ||
| 719 | |||
| 720 | if (!defined $version) { | ||
| 721 | $version="GC3.0"; | ||
| 722 | } | ||
| 723 | |||
| 724 | if (!defined $format) { | ||
| 725 | $format="MAS5"; | ||
| 726 | } | ||
| 727 | |||
| 728 | |||
| 729 | binmode $filehandle,":crlf"; | ||
| 730 | |||
| 731 | |||
| 732 | |||
| 733 | if (defined $version && $version ne "GC3.0") { | ||
| 734 | croak "Bio::Affymetrix::CDF module cannot do any other version than GC3.0 currently"; | ||
| 735 | } | ||
| 736 | |||
| 737 | $version="GC3.0"; | ||
| 738 | |||
| 739 | if ($format ne "MAS5") { | ||
| 740 | croak "Bio::Affymetrix::CDF module can only produce MAS5 CDF files currently"; | ||
| 741 | } | ||
| 742 | |||
| 743 | if ($self->original_format() eq "XDA") { | ||
| 744 | carp "You cannot produce authentic MAS5 CDF files starting from an XDA CDF file (yet). Output will continue, but resulting CDF file will be unusable"; | ||
| 745 | } | ||
| 746 | |||
| 747 | # Chipfile trivia | ||
| 748 | |||
| 749 | my $maxunit=-3000; | ||
| 750 | |||
| 751 | foreach my $i (keys %{$self->probesets()}) { | ||
| 752 | if ($i>$maxunit) { | ||
| 753 | $maxunit=$i; | ||
| 754 | } | ||
| 755 | } | ||
| 756 | |||
| 757 | print $filehandle "[CDF]\nVersion=".$version."\n\n"; | ||
| 758 | # Uses $self->probesets as an arrayref (rather than hashref) for | ||
| 759 | # calculating NumberOfUnits here | ||
| 760 | |||
| 761 | if (!defined($self->name())) { | ||
| 762 | carp("GCOS format files do not have a name. Use the name() method if you want to set a name for this chip in the output."); | ||
| 763 | } | ||
| 764 | |||
| 765 | print $filehandle "[Chip]\nName=".$self->name()."\nRows=".$self->rows()."\nCols=".$self->cols()."\nNumberOfUnits=".(scalar(keys %{$self->probesets()}))."\nMaxUnit=".$maxunit."\nNumQCUnits=".$self->original_num_qc_units()."\nChipReference=\n\n"; | ||
| 766 | |||
| 767 | |||
| 768 | print $filehandle $self->{"_qcinfo"} unless ($self->original_format() eq "XDA"); | ||
| 769 | |||
| 770 | foreach my $i (sort {int($a)<=>int($b)} keys %{$self->probesets()}) { | ||
| 771 | |||
| 772 | my $numcells=0; | ||
| 773 | |||
| 774 | my $z=$self->probesets()->{$i}; | ||
| 775 | |||
| 776 | # Calulate number of cells (or probes, in English) | ||
| 777 | |||
| 778 | foreach my $v (@{$z->probe_pairs()}) { | ||
| 779 | $numcells+=scalar (@$v) | ||
| 780 | } | ||
| 781 | |||
| 782 | print $filehandle "[Unit".$i."]\n"; | ||
| 783 | print $filehandle "Name=".$z->unit_name()."\n"; | ||
| 784 | print $filehandle "Direction=".($z->is_sense()?1:2)."\n"; | ||
| 785 | print $filehandle "NumAtoms=".scalar(@{$z->probe_pairs()})."\n"; | ||
| 786 | print $filehandle "NumCells=".$numcells."\n"; | ||
| 787 | print $filehandle "UnitNumber=".$i."\n"; | ||
| 788 | print $filehandle "UnitType=".$z->{"UNITTYPE"}."\n"; | ||
| 789 | print $filehandle "NumberBlocks=1\n\n"; | ||
| 790 | |||
| 791 | print $filehandle "[Unit".$i."_Block1]\n"; | ||
| 792 | print $filehandle "Name=".$z->name()."\nBlockNumber=1\n"; | ||
| 793 | print $filehandle "NumAtoms=".scalar(@{$z->probe_pairs()})."\n"; | ||
| 794 | print $filehandle "NumCells=".$numcells."\n"; | ||
| 795 | print $filehandle "StartPosition=0\nStopPosition=".(scalar(@{$z->probe_pairs()})-1)."\n"; | ||
| 796 | print $filehandle "CellHeader=X Y PROBE FEAT QUAL EXPOS POS CBASE PBASE TBASE ATOM INDEX CODONIND CODON REGIONTYPE REGION\n"; | ||
| 797 | |||
| 798 | my $count=1; | ||
| 799 | |||
| 800 | for (my $x=0;$x<scalar(@{$z->probe_pairs()});$x++) { | ||
| 801 | foreach my $i (@{$z->probe_pairs()->[$x]}) { | ||
| 802 | print $filehandle "Cell".($count)."=".join("\t",($i->x(),$i->y(),"N","control",$z->name(),$x,$i->mismatch_position(),$i->probe_target_base(),$i->probe_mismatch_base(),$i->probe_target_base(),$x,$i->index(),-1,-1,99,""))."\n"; | ||
| 803 | $count++; | ||
| 804 | } | ||
| 805 | } | ||
| 806 | |||
| 807 | print $filehandle "\n\n"; | ||
| 808 | } | ||
| 809 | } | ||
| 810 | |||
| 811 | # binary file utility to read a length-defined string | ||
| 812 | |||
| 813 | sub _unpack_length_string { | ||
| 814 | my $self=shift; | ||
| 815 | my $fh=shift; | ||
| 816 | |||
| 817 | my $buffer; | ||
| 818 | |||
| 819 | (read ($fh, $buffer, 4)==4) or die "Can no longer read from file";; | ||
| 820 | my $len = unpack ("V", $buffer); | ||
| 821 | (read ($fh, $buffer, $len)==$len) or die "Can no longer read from file"; | ||
| 822 | |||
| 823 | return unpack("a".$len,$buffer); | ||
| 824 | |||
| 825 | } | ||
| 826 | |||
| 827 | |||
| 828 | 1; |