#!/usr/bin/perl

if( $#ARGV eq -1 ) {
    print STDERR <<END ;
usage:
mask_cdf mask_file cdf_file result_cdf [+R|-R] 
         mask file should have 2 columns: probeset name, and list of comma-seperated masked probes.
         The resulting cdf file is written to stdout.
	 -R create the CDF library using R
	 +R create only the CDF library using R, so assume that the result_cdf already exists.
END
exit 0 ;
}

if( !($ARGV[3] eq "+R") ) {

open(MASK,$ARGV[0]) ;

my %mask ;

while(<MASK>) {
    chop ;
    /^\"*(\S*)\"*\s+\"*(.*)\"*/ ;
    $mask{$1} = $2 ;
}
close(MASK) ;


    
open(CDF,"$ARGV[1]") ;
open(OCDF,">$ARGV[2]") ;

while(<CDF>) {
    if( $found_probe == 0 ) {
	if( /Name=(.*)/ ) {
	    $gene = $1 ;
	    chop($gene) ;
	    $g =~ s/,$// ;
	    if( defined( $mask{$gene} ) ) {
		$found_probe = 1 ;
		$cur_cell = 0 ;
		@masked_probes = split( /,/, $mask{$gene}) ;
		print OCDF $_ ;
	    } else {
		print OCDF $_ ;
	    }
	} else {
	    print OCDF $_ ;
	}
    } else {
	if( /NumAtoms=(\d*)/ ) {
	    $total_num = $1 ;
	    $n = $1 - ($#masked_probes+1) ;
	    print OCDF "NumAtoms=$n\n" ;
	    @is_probe_masked = (0) x $total_num ;
	    @is_probe_masked[ @masked_probes ] = (1) x ($#masked_probes+1) ;
	} elsif( /NumCells=(\d*)/ ) {
	    $n = $1 - ($#masked_probes+1) * 2 ;
	    print OCDF "NumCells=$n\n" ;
	} elsif( /StartPosition=(\d*)/ ) {
	    $start = $1 ;
	    print OCDF $_ ;
	} elsif( /StopPosition=(\d*)/ ) {
	    $n = $1 ;
#	    if( $n != $start+$total_num-1 ) {
#		print STDERR "problem with gene $gene!!!!!!!!\n" ;
#	    } 
#	    print OCDF "StopPosition=".($start+$total_num-1- ($#mask_probes+1))."\n" ;
	    print OCDF $_ ;
	} elsif( /Cell(\d+)=(.*)/ ) {
	    $cur_cell++ ;
	    $rest = $2 ;
	    chop($rest) ;
	    @vals = split(/\s+/,$2) ;
	    if( !($vals[4] eq $gene ) ) {
		print STDERR "problem with gene $gene $vals[4]!!!!!!!!\n" ;
	    }
	    if( $is_probe_masked[$vals[5]+1] != 1 ) {
		print OCDF "Cell$cur_cell=$rest\n" ;
	    }
	} elsif( /\[.*\]/ ) {
	    $found_probe = 0 ;
	    print OCDF $_ ;
	} else {
	    print OCDF $_ ;
	}
    }
}
	
close(OCDF) ;
close(CDF) ;

}

if( ($ARGV[3] eq "+R") | ($ARGV[3] eq "-R" ) ) {
		
    open(R,"|R --vanilla") ;
    print R "library(makecdfenv)\n" ;
    print R "make.cdf.package(\"".$ARGV[2]."\")\n" ;
    close(R) ;
}
	    
	    
	    

