#!/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;
use Getopt::Long;
use Config::IniFiles;

my $cfg = Config::IniFiles->new( -file => "/usr/local/bioinfo/src/galaxy-test/galaxy-dist/PATH.ini" );
my $PATH = $cfg->val( 'workPath', 'FILEPATH_DEV' );
	
my $input_bank;		
my $ref_file;
my $database_type;
my $input_fasta;
my $output_blastn;
my $m_view;
my $evalue;
my $Filter;
my $wordsize;

Getopt::Long::Configure( 'no_ignorecase', 'bundling' );
GetOptions (
    'reffile=s' => \$ref_file,
    'inputbank=s' => \$input_bank,
    'databasetype=s' => \$database_type,
    'inputfasta=s' => \$input_fasta,
    'outputblastn=s' => \$output_blastn,
    'mview=s' => \$m_view,   
    'evalue=i' => \$evalue,
    'Filter=s' => \$Filter,
    'wordsize=i' => \$wordsize
) or die "Usage: Error in command line arguments\n";




#http://www.compbio.ox.ac.uk/analysis_tools/BLAST/formatdb.shtml
# formatdb -p F -i all_seqs.fasta -n customBLASTdb



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

`cd /$PATH/; mkdir blastall_$nb/; chmod 777 blastall_$nb/; cd blastall_$nb/;`;

#my $resultFile = "/work/galaxy-test/database/files/$nb";
my $resultFile = "$PATH/blastall_$nb/$nb";
#~ `cp $input_fasta $PATH/blastall_$nb/inputfasta.fasta; cp $ref_file $PATH/blastall_$nb/reffile.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

if (! $input_bank){
		`cp $input_fasta $PATH/blastall_$nb/inputfasta.fasta; cp $ref_file $PATH/blastall_$nb/reffile.fasta;`;
        my $cmd2 = "(/usr/local/bioinfo/bin/formatdb -p $database_type -i $PATH/blastall_$nb/reffile.fasta -n $PATH/blastall_$nb/reffile ; blastall -p blastn -d $ref_file -i $PATH/blastall_$nb/inputfasta.fasta -o $resultFile.out $m_view -e $evalue -F $Filter -W $wordsize) >& ./sm_blastall_index_$nb.log 2>&1";
        system $cmd2;
        #Info pour les biologistes
        print STDOUT "Blast command : \n\n $cmd2 \n\n ";    
    
    }
else{
		my $db=$input_bank;
		$db=~s/\.[pn]{1}al$//;
		`cp $input_fasta $PATH/blastall_$nb/inputfasta.fasta;`;
		
		my $cmd;
		
		if($input_bank =~ /\.nal$/){
			$cmd = "(blastall -p blastn -d $db -i $PATH/blastall_$nb/inputfasta.fasta -o $resultFile.out $m_view -e $evalue -F $Filter -W $wordsize) >& ./sm_blastall_$nb.log 2>&1";
		}
		else{
			$cmd = "(blastall -p blastp -d $db -i $PATH/blastall_$nb/inputfasta.fasta -o $resultFile.out $m_view -e $evalue -F $Filter -W $wordsize) >& ./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`;}
