#!/usr/bin/env perl
# PROJECT: CASAVA
# MODULE:  $RCSfile: notMapped2bam.pl,v $
# AUTHOR:  Tony Cox
#
# 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).
#
# this script creates SAM and BAM files out of a sorted or export file
#

use warnings FATAL => 'all';
use strict;

use File::Copy qw(move);
use File::Spec;
use File::Temp;
use Getopt::Long;
use Sys::Hostname;

use lib '/home/psgendb/local/pkg/CASAVA_v1.8.2-build/lib/CASAVA-1.8.2/perl';
use Casava::Common::Log;
use Casava::Common::IOLib qw(executeCmd);
use Casava::PostAlignment::Sequencing::Config
  qw(loadConfiguration %CONF_APP %CONF_PROJ readProjectParameters);
use Casava::PostAlignment::Sequencing::SamLib qw(getSamtoolsBin getSamHeaderFH);


#------------------------------------------------------------------------------

my $argvStr     = join ' ', @ARGV;
my $scriptName = (File::Spec->splitpath($0))[2];

my $taskId;
my $projectDir  = "";
my $help        = 0;

my $usage =
    "$scriptName [options]\n"
  . "\t--taskId=NUMBER   - task number out of NMNMTasks\n"
  . "\t--projectDir=DIR  - project directory\n"
  . "\t--help            - print this message\n";

my $result      = GetOptions(
    "taskId=i"       => \$taskId,
    "projectDir=s"  => \$projectDir,
    "help|h"        => \$help
);

if ((not $result) or $help) {
    errorExit "\n$usage";
}
errorExit "ERROR: Incorrect number of arguments\n$usage"
  if ( ($projectDir eq "") or (not defined $taskId) );

loadConfiguration($projectDir);

# Configuration:
my $libexecDir      = '/home/psgendb/local/pkg/CASAVA_v1.8.2-build/libexec/CASAVA-1.8.2';
my $timeStampFormat = $CONF_APP{formatTimeStamp};
my $confDir         = $CONF_APP{dirConf};
my $projConfFile    = $CONF_APP{projectConf};
my $NMNM_TAG        = $CONF_APP{TAG_NMNM};
my $isPaired        = ($CONF_PROJ{readMode} eq 'paired');
my $isSolexaQvals   = ($CONF_PROJ{qualityType} eq "Solexa64");
my $isSkipVariableMetadata = (defined $CONF_PROJ{skipVariableMetadata} and $CONF_PROJ{skipVariableMetadata});

my $sorted2Bam  = File::Spec->catfile($libexecDir, 'sortedToBam');

my %buildChromsBinSizes = ();
readProjectParameters( %buildChromsBinSizes, "BUILD_BIN_SIZES", $projectDir );
my @chroms = keys %buildChromsBinSizes;

#------------------------------------------------------------------------------

{
    my $hostname        = hostname();
    printLog("Running $hostname:[$0 $argvStr]\n", 0);
}

my $nmnmDir = File::Spec->catdir($CONF_PROJ{dirBuildParsed}, $NMNM_TAG);
my $nTasks = $CONF_APP{NMNMTasks};

my $filelist = File::Spec->catfile($nmnmDir, "tmp.list");

open(my $LFH,"<$filelist") or errorExit("ERROR: Can't open file: $filelist\n");
my $samHeaderFH = getSamHeaderFH(0,@chroms,$isSkipVariableMetadata);

my $i=-1;
while(<$LFH>) {
    $i++;
    next if(($i%$nTasks) != $taskId);
    chomp;
    my $exportFile = $_;

    my $bamFile = $exportFile;
    $bamFile =~ s/.txt$/.bam/;
    my $tmpBamFile = $bamFile . ".incomplete";

    my $nmnm2BamCmd = "$sorted2Bam --no-mapped --sorted-file $exportFile --bam-file $tmpBamFile --header-file $samHeaderFH";
    $nmnm2BamCmd .= " --paired" if($isPaired);
    $nmnm2BamCmd .= " --qlogodds" if($isSolexaQvals);

    executeCmd($nmnm2BamCmd, 5);

    move($tmpBamFile,$bamFile) or errorExit("ERROR: File move failed: $!\n");
    unlink($exportFile);
}

$samHeaderFH = undef;
close($LFH);

#------------------------------------------------------------------------------
