#!/usr/bin/perl -w

# usage : perl sm_blastall.pl
# 15/11/2013 - 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 File::Basename;

my $input_bank     = $ARGV[0];
my $input_fasta    = $ARGV[1];
my $output_blastn  = $ARGV[2];
my $m_view         = $ARGV[3];
my $evalue         = $ARGV[4];
my $Filter         = $ARGV[5];
my $wordsize       = $ARGV[6];

#recuperation de l id de l output
my ($nb) = ($output_blastn=~/galaxy_dataset_(\d+)\.\S+$/);

#my $resultFile = "/work/galaxy-test/database/files/$nb";
my $resultFile = "/work/galaxy-test/database/files/$nb";
`cp $input_fasta $input_fasta.fasta`;

#TESTS commande : perl sm_blastall.pl /bank/blastdb/ensembl_bos_taurus test.fasta
#blastall -p blastn -d /bank/blastdb/ensembl_bos_taurus -i transcrits_Cuffmerge.fasta -o blastn_porc_sur_bovin.out
my $cmd = "(blastall -p blastn -d $input_bank -i $input_fasta.fasta -o $resultFile.out $m_view -e $evalue -F $Filter -W $wordsize) >& /work/galaxy-test/logs/sm_blastall_$nb.log 2>&1";
system $cmd;

#Info pour les biologistes
print STDOUT "Blast command : \n\n $cmd \n\n ";


#Recuperation des fichiers par Galaxy
if (! -e "$resultFile.out"){print STDERR "out file not found. \n";}else{`cp -a $resultFile.out $output_blastn`;}
