#!/usr/bin/perl -w

# Jamie Bean  4/27/01  Gaasterland Lab   bean@genomes.rockefeller.edu

# INPUT: .cel file .psc file (PM only)
# OUTPUT: .raw file (user specified) 
# matches probe set name with mean intensity at given cell 
coordinates
# outputs: min  max
# outputs: # at max with sd=0
# outputs: probeset    PM   x   y   mean intensity   standard 
deviation
# over     probeset    MM   x   y   mean intensity   standard 
deviation
# assumes MM for each PM is directly below on chip i.e. has y 
coordinate value PM + 1

use strict;
use Getopt::Long;
my ($cel_file, $out_file, $psc_file);
&GetOptions("cel=s"    => \$cel_file,
	    "out=s"    => \$out_file,
	    "psc=s"    => \$psc_file);             
(($cel_file) && ($psc_file) && ($out_file)) || die "Usage: cel2raw 
OPTIONS

where OPTIONS are:
                  -cel   .CEL FILE
                  -out   output file (.RAW FILE)
                  -psc   .PSC FILE                 
\n";

open (CEL, $cel_file) || die "Can't open $cel_file: $!\n";
my $max_mean = 0;
my $min_mean = 100000000;
my $count = 0;
my @cell_int;
while () {
    if (!/[a-zA-Z]/ && /\d/) {
	my @cel_line = split (/\t/,$_);
	if (@cel_line == 5) {
	    my ($cel_x,$cel_y,$mean_int,$sd) = (split 
/\t/,$_)[0,1,2,3];
	    if ($mean_int < $min_mean) {
		$min_mean = $mean_int;
	    }
	    if ($mean_int > $max_mean) {
		$count = 0;
		$max_mean = $mean_int;
		if ($sd == 0) {
		    $count++;
		}
	    } 
	    elsif ($mean_int == $max_mean && $sd == 0) {
		$count++;
	    }
	    $cel_x =~s/\s*//;
	    $cel_y =~s/\s*//;
	    $cell_int[$cel_x][$cel_y] = "$mean_int\t$sd";
	}
    }
}
close (CEL) || die "Can't close $cel_file: $!\n";
# filters out .cel headers and matches cell intensity and standard 
deviation with probeset id by x,y coordinates
# also finds min and max intensity values present on chip and counts 
number of 
# times standard deviation = 0 occurs at max. intensity

open (OUT, ">$out_file") || die "Can't create $out_file: $!\n";
print OUT "#Min-max\t$min_mean\t$max_mean\n";
print OUT "#SDzero\t$count\n";

open (PSC, $psc_file) || die "Can't open $psc_file: $!\n";
while () {
    my ($probe,$x,$y) = (split /\t/,$_)[0,1,2];
    chomp $y;
    print OUT $probe, "\tPM\t", $x, "\t", $y, "\t", 
$cell_int[$x][$y], "\n";
    print OUT $probe, "\tMM\t", $x, "\t", $y+1, "\t", 
$cell_int[$x][$y+1], "\n";
}
close (PSC) || die "Can't close $psc_file: $!\n";

close (OUT) || die "Can't close $out_file: $!\n";
# creates file out_file:            probeset id     PM     x     y      
intensity      standard deviation
#                                       "           MM     "     y+1    
intensity      standard deviation
#                                      ...
#                                      ...













