#!/usr/bin/perl -w

# usage : perl sm_e-PCR.pl <FASTA file>
#Sarah Maman
# Copyright (C) 2013 INRA
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use Config::IniFiles;

my $cfg = Config::IniFiles->new( -file => "/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/PATH.ini" );
my $ePCR = $cfg->val( 'toolsPath', 'ePCR' );


my $input_fasta   = $ARGV[0];
my $input_sts     = $ARGV[1];
my $wordsize      = $ARGV[2];
my $margin        = $ARGV[3];
my $default       = $ARGV[4];
my $maxmismatches = $ARGV[5];
my $maxindel      = $ARGV[6];
my $outputformat  = $ARGV[7];
my $output        = $ARGV[8];
my $cmd           = '';

#Test en ligne de commande
#/usr/local/bioinfo/src/e-PCR/current/e-PCR -w9 -f 1 -m100 tests/genethon.sts D=100-400 tests/file.fasta N=1 G=1 T=3
#Test depuis la vm-galaxy
#perl sm_e-PCR.pl tests/file.fasta tests/genethon.sts 9 100 100-400 1 1 3 tests/TOTO

$cmd = "($ePCR  -w$wordsize -f 1 -m$margin $input_sts D=$default $input_fasta N=$maxmismatches G=$maxindel T=$outputformat > ${input_fasta}.txt ) >& ./ePCR.log 2>&1";
#Info pour les biologistes
print STDOUT "e-PCR command : \n\n $cmd \n\n ";
system $cmd;

if (! -e "${input_fasta}.txt"){print STDERR "e-PCR TXT FILE NOT FOUND\n";}else{`mv "${input_fasta}.txt" $output`;}
