#!/usr/bin/perl -w

#    godack10.pl
#    Copyright 2002 Charlie Kim
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    For a copy of the GNU General Public License, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# This program uses the following modules

# Perl/Tk 800.023  Copyright (c) 1995-1996 Nick Ing-Simmons. 
# http://starbase.neosoft.com/%7Eclaird/comp.lang.perl.tk/ptkFAQ.html


use Tk;
use Tk::Label;
use Tk::Button;
use Tk::Checkbutton;
use Tk::Frame;
use Tk::Entry;
use Tk::Radiobutton;
use Tk::Scale;
use Tk::Scrollbar;
use Tk::Text;

$mw = MainWindow->new(-title=>'GODACK');

$mw->Label(-text=>'Good Data filtering by Charlie Kim',
	   )->pack();
$mw->Label(-text=>"Copyright 2002 Charlie Kim GNU General Public License\n")->pack();

# Input #
$inputfr = $mw->Frame(-relief=>'groove',
		      -label=>'PCL Input File',
		      -borderwidth=>2,
		      )->pack(-side=>'top',
			      -anchor=>'nw',
			      -fill=>'both',
			      );
$single_e = $inputfr->Entry(-state=>'normal',
			    -textvariable=> \$singlefile,
			    -width=>30,
			    )->pack(-side=>'left',
				    -pady=>3,
				    );
$inputfr->Button(-text=>'Browse',
		 -command=> \&selectsinglefile,
		 -borderwidth=>1,
		 -padx=> 0,
		 -pady=> 0,
		 )->pack(-side=>'left',
			 );

$filterfr = $mw->Frame(-relief=>'groove',
		       -label=>'Percent Good Data Points Filtering Value',
		       -borderwidth=>2,
		       )->pack(-side=>'top',
			       -anchor=>'nw',
			       -fill=>'both',
			       );
$filterval = 80;
$filterfr->Entry(-state=>'normal',
		 -textvariable=> \$filterval,
		 -width=>4,
		 )->pack(-side=>'top',
			 -pady=>3,
			 );



# Run #
$mw->Button(-text=>"Remove Lines with Insufficient Data Points",
	    -command=> \&main,
	    -state=> 'normal',
	    )->pack();

$mw->Button(-text=>"Exit",
	    -command=> sub { exit },
	    )->pack();

MainLoop;

sub selectsinglefile {
    my $opentypes = [
		     "{All files}     *           ",
		     "{PCL files}     {.pcl}      ",
		     "{Text files}    {.txt}      ",
		     ];
    my $file = $mw->getOpenFile(-filetypes=>$opentypes);
    if (defined $file and $file ne '') {
	$single_e->delete(0, 'end');
	$single_e->insert(0, $file);
	$single_e->xview('end');
    }
}

sub errormsg {
    my $msg = shift;
    my $errorwin = $mw->Toplevel();
    $errorwin->Label(-text=> $msg,
		     )->pack();
    $errorwin->Button(-text=>'Close',
		      -command=> sub { $errorwin->destroy },
		      )->pack();
    $errorwin->waitWindow();
}

sub main {
    my $cutoff = $filterval / 100;
    open(INFILE,$singlefile) or die "Can't open input file\n";

    my $outfile = $mw->getSaveFile(-title=>'Save Results As');
    open(OUTFILE,">$outfile") or die "Can't open output file\n";

    chomp($header=<INFILE>);
    my $gweight = 0;
    $gweight++ if $header =~ /GWEIGHT/i;
    print OUTFILE "$header\n";

    my @temp = split(/\t/,$header);
    my $datacol = $#temp - $gweight - 1;

    while (<INFILE>) {
	chomp;
	if (/EWEIGHT/i) {
	    print OUTFILE "$_\n";
	    next;
	}

	my @tempdata = split(/\t/);
	my $counter = 0;
	for (my $c = 2 + $gweight; $c <= $#tempdata; $c++) {
	    $counter++ if $tempdata[$c];
	}

	print OUTFILE "$_\n" if $counter / $datacol >= $cutoff;
    }

    close INFILE;
    close OUTFILE;
    $mw->bell;
    &errormsg("Program complete");
}
