#!/usr/bin/env perl
# PROJECT: CASAVA
# AUTHOR:  Roman Petrovski
#
# Copyright (c) 2008 Illumina
# This software is covered by the "Illumina Genome Analyzer Software
# License Agreement" and the "Illumina Source Code License Agreement",
# and certain third party copyright/licenses, and any user of this
# source file is bound by the terms therein (see accompanying files
# Illumina_Genome_Analyzer_Software_License_Agreement.pdf and
# Illumina_Source_Code_License_Agreement.pdf and third party
# copyright/license notices).
#
# The scirpt creates the workflow for alignability computation
#
use warnings FATAL => 'all';
use strict;

use Getopt::Long;

use lib '/home/psgendb/local/pkg/CASAVA_v1.8.2-build/lib/CASAVA-1.8.2/perl';

$| = 1;

use Casava::Common::Log;
initLog( "", 0, 5 );

my $readLength     = undef; 
my $BASES_PER_LINE  = length("TAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCCTAACCC"); #TODO: get that from actual fa file
my $help = 0;

my $result = GetOptions(
    "readLength|rl=s"       => \$readLength, 
	"basesPerLine|bpl=i"    => \$BASES_PER_LINE,
    "help"                  => \$help
);


my $usage       =
    "computeAlignability.pl [options]\n"
  . "Prints alignability scores in CSF (Compact Scoring File) format\n"
  . "\t--readLength|-rl         - read length to compute the alignability on (default $readLength)\n"
  . "\t--basesPerLine|-bpl      - length of single output file (default $BASES_PER_LINE)\n"
  . "\t--help                   - Print this help\n";

if ($help) {
    print $usage;
    exit(0);
}    # if

errorExit "ERROR: readLength not specified\n$usage" if ( !defined($readLength));


my $z = 0; #global position in all aln files
my $j = 0; #position in output read
my @a = ();

while (<>)
{
    my $inc = $_ =~ /[^\t]*\t[^\t]*\t1:/ ? 1 : 0;
    $z++;
    for ( my $i = 0 ; $i < $readLength ; ++$i )
    {
        $a[$i] += $inc;
    }
    print chr( 64 + $a[$j] );
    print "\n" if ( ( $z % $BASES_PER_LINE ) == 0 );
    $a[ $j++ ] = 0;
    $j = 0 if ( $j == $readLength );
}

for ( my $i = 1 ; $i < $readLength ; ++$i ) 
{
    $z++;
    print chr( 64 + $a[$j++] );
    print "\n" if ( ( $z % $BASES_PER_LINE ) == 0 );
    $j = 0 if ( $j == $readLength );
}

