#!/usr/bin/perl -w

# usage : perl sm_search_names.pl final_count_file.csv BIOMART.txt 
# 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_count_file          = $ARGV[0];
my $input_biomart             = $ARGV[1];                         	
my $output_correspondance_txt = $ARGV[2];



my $cmd = '';
my $SEARCH = "/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tools/sm_RNAseq/SEARCH_NAMES.pl";
#my $path = dirname ($input_count_file);
my $path = "/galaxydata/database/files/workspace/";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."secBIOMARTsearch"; 
my $nb1 = ($output_correspondance_txt=~/galaxy_dataset_(\d+)\.\S+$/);
my $nb = $ALL."_".$nb1;
`cd $path/; mkdir $nb/; cd $nb/;`;

$cmd = "(cd $path/$nb/; perl $SEARCH $input_count_file $input_biomart) >& ./search_biomart_names.log 2>&1";
system $cmd;

#Info pour les biologistes
print STDOUT "Search BIOMART names : \n\n$cmd \n\n ";


# Recuperation du fichier sortant dans Galaxy
if (! -e "$path/$nb/TEST_TABLEAU_CORRESPONDANCE.csv"){print STDERR "$path/$nb/TEST_TABLEAU_CORRESPONDANCE.csv not found. \n";}
else {`mv "$path/$nb/TEST_TABLEAU_CORRESPONDANCE.csv" $output_correspondance_txt`;}

