#!/usr/bin/env perl

=head1 LICENSE

Copyright (c) 2007-2009 Illumina, Inc.

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).

This file is part of the Consensus Assessment of Sequence And VAriation
(CASAVA) software package.

=head1 NAME

normalizeRnaCounts.pl - Recalculate normalized value colmn for RNA feature counts.

=head1 SYNOPSIS

 normalizeRnaCounts.pl --factor=500000000000 RawCountsFile >NormalizedCountsFile

Options:

=over 4

=item *

--factor=f

The raw counts will be divided by this factor.

=back

=head1 DESCRIPTION

The script takes input data from STDIN. The input lines are expected to have the 
following format:

chromosomeName\tfeatureStartPos\tfeatureEndPos\tfeatureName\tnormalizedValue\trawValue\n

The script adjusts values in normalizedValue column using the following formula:

(10^9 * $illuminaNormalizedValue / $factor)

This is required to convert the legacy Illumina normalized feature count values to RPKM 

The result lines are printed to STDOUT.

=head1 DIAGNOSTICS

=head2 Exit status

0: successful completion
1: abnormal completion
2: fatal error

=head2 Errors

All error messages are prefixed with "ERROR: ".

=head2 Warnings

All warning messages generated by CASAVA are prefixed with "WARNING: ".

=head1 CONFIGURATION AND ENVIRONMENT

=head1 DEPENDENCIES

=over 4

=item Standard perl modules

strict, warnings, Getopt::Long, Pod::Usage

=item External perl modules

=item CASAVA perl modules

Casava::Sequencing::GenomicIOLib, 
Casava::Common::Log

=back

=head1 BUGS AND LIMITATIONS

There are no known bugs in this module.

All documented features are fully implemented.

Please report problems to Illumina Technical Support (support@illumina.com)

Patches are welcome.

=head1 AUTHOR

Roman Petrovski

=cut


use strict;
use warnings 'all';
use Getopt::Long;
use Pod::Usage;

use lib '@CASAVA_FULL_PERL_LIBDIR@';

use Casava::Common::Log qw(initLog printLog);
use Casava::PostAlignment::Sequencing::GenomicIOLib qw(%featureCounts);

#############################################################
# Defaults
my $factor = undef;
my $help = 0;
my $man = 0;

initLog( undef, 4, 0 );
printLog ("running $0 " . (join ' ', @ARGV) . "\n", 1);

GetOptions('factor=f'  => \$factor,
           'help|?'           => \$help,
           'man'              => \$man) or pod2usage(2);

pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2, -input => $1) if ($man and $0 =~ /(.*)/);
pod2usage("$0: --factor not specified.\n") unless defined $factor;


############################################################
while(<>)
{
    chomp;
    my @fields = split ("\t", $_);
    $fields[$featureCounts{count}] *= (10**9 / $factor);

    print ((join "\t", @fields) . "\n");    
}
1;
__END__

