#!/usr/bin/perl

# First order mundge of ALL AML data
# Simply get the first N rows in a form we can import into MATLAB

$in = $ARGV[0];
$maxrows = $ARGV[1] || 0;
$hdrskip = 3;
$colskip = 2;
if ($maxrows > 0) { $maxrows+=$hdrskip-1; }

die "USAGE: $0 <input> <numrows>\n" unless $in;

open(IN, "<$in") or die "Could not open file: $in";

$numrows = 0;
while (<IN>) {
	$numrows++;
	chomp;
	@fields = split /\t/, $_; 
	for ($i=0;$i<$colskip;$i++) {
		shift @fields;
	}
	$i=0;
	@out = ();
	foreach $f (@fields) {
		# take every other field
		if (($i % 2)==0) {
			push @out, $f."\t";
		}
		$i++;
	}
	$maxvar = 0;
	$keep = 0;
	$elements = scalar(@out);
	for ($i=0;$i<$elements;$i++) {
		for ($j=0;$j<$elements;$j++) {
			$var = abs($out[$i] - $out[$j]);
			if ($var > $maxvar) {
				$maxvar = $var;
			}
		}
	}
	if ($maxvar > 100) {
		$keep = 1;
	}

	if ( ($numrows > $hdrskip) && ($keep == 1)) {
		print "@out\n";
	}
	last if (($numrows > $maxrows) && ($maxrows > 0));
}
close(IN);

