#!/usr/bin/perl
# Chris Seidel, February 2003
#
# Program to make sure that the gal file is completely
# rectangular with regard to the number of tab characters.
# When a GAL file is modified in Excel and saved, I find
# that Excel does not preserve the rectangular nater of the file
# and instead trims out empty trailing columns. Also Joe's GAL 
# filemaker program builds the GAL file header
# as if it only has the standard 5 columns: Block, Row, Col, ID, Name
# thus if there are more columns (allowed in GenePix > v4)
# the file must be edited to reflect the actual number of columns,
# and for some reason different numbers of tabs get added to the
# right edge of the file. This program reads the GP Header
# to determine the correct number of fields, and makes sure
# the right edge is padded accordingly.
#
# Usage: $0 infile > outfile

$_ = <>; #skip first line
print $_;
$_ = <>; #get the second row, which contains header/fields pair
print $_;
s/[\r\n]//g; # get rid of end of line chars before splitting
($header_rows,$fields) = split(/\t/);

for($i=0; $i < $header_rows; ++$i){
    $_ = <>;
    s/\t//g; #there are spurious tab chars in the header lines!
             #there shouldn't be any!
    print $_;
}
    $_ = <>; #the column header row. Leave tabs in place.
    print $_;

while(<>){
    @list = split(/\t/);
    $l_len = @list;
    if( $l_len < $fields ){
        s/(\s+)$//;
        $cr = $1;
        print $_;
        for($i=$l_len;$i<$fields;++$i){
            print "\t";
        }
        print $cr;
    }
    else{
        print $_;
    }
}