#!/usr/bin/perl -w 

# usage : perl RepeatMasker (fasta file) 
# Sarah Maman
# Copyright (C) 2012 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 $query        = $ARGV[0]; 
my $nolow        = $ARGV[1]; 
my $noint        = $ARGV[2]; 
my $norna        = $ARGV[3]; 
my $species      = $ARGV[4]; 
my $slow_search  = $ARGV[5]; 
my $quick_search = $ARGV[6]; 
my $rush_search  = $ARGV[7]; 
my $only_alus    = $ARGV[8]; 
my $output_std   = $ARGV[9]; 
my $output_mask  = $ARGV[10]; 


#Path du tool
my $REPEATMASKER = '/usr/local/bioinfo/bin/RepeatMasker'; 
my $cmd = ''; 


#exemple de commande test sur le terminal
#perl /usr/local/bioinfo/bin/RepeatMasker -parallel 8 sequences_for_CompleteMotifs.fasta -nolow -noint -norna -species cow -qq -alu

#Liste des fichiers generes par la commande RepeatMasker
#avec le fichier entrant: sequences_for_CompleteMotifs.fasta  
#rmblastdb.log  sequences_for_CompleteMotifs.fasta.masked  sequences_for_CompleteMotifs.fasta.out


#Commande RepeatMasker 
$cmd = qq/($REPEATMASKER -parallel 8 $query $nolow $noint $norna $species $slow_search $quick_search $rush_search $only_alus) >& repeatMasker.log 2>&1/; 
system $cmd; 


#Recuperation des fichiers sortants dans Galaxy
if (! -e "$query.masked") { print STDERR "Echec lors de la generation du fichier fasta.masked. \n"; } 
else { `(cp -a \"$query.masked\" $output_std) >& ./cp1.log 2>&1` } 


if (! -e "$query.out") {print STDERR "Echec lors de la generation du fichier fasta.out. \n";} 
else { `(cp -a \"$query.out\" $output_mask) >& ./cp2.log 2>&1` } 



## Copy the output files to galaxy
#if $adv_opts.adv_opts_selector=="advanced":

    #if "summary" in $output_files_list:
        ## Write out the summary file (default)
        #set $summary_file = $output_basename + '.tbl'
        #os.system("cp $summary_file $output_summary;")

    #end if

    #if "gff" in $output_files_list:
        ## Write out the gff file (default)
        #set $gff_file = $output_basename + '.out.gff'
        #os.system("cp $gff_file $output_gff");
    #end if

    #if "html" in $output_files_list:
        ## Write out the html file
        #set $html_file = $output_basename + '.out.html'
        #os.system("cp $html_file $output_html;")
    #end if

#else:

    ## Write out the summary file (default)
    #set $summary_file = $output_basename + '.tbl'
    #os.system("cp $summary_file $output_summary;")

    ## Write out the gff file (default)
    #set $gff_file = $output_basename + '.out.gff'
    #os.system("cp $gff_file $output_gff;")


## End of advanced options:
#end if

## Write out mask sequence file
#set $mask_sequence_file = $output_basename + '.masked'
#os.system("cp $mask_sequence_file $output_mask;")

## Write out standard file (default)
## The default '.out' file from RepeatMasker has a 3-line header and spaces rather
## than tabs. Remove the header and replace the whitespaces with tab
#set $standard_file = $output_basename + '.out'
#os.system("tail -n +4 $standard_file | tr -s ' ' '\t' > $output_std;")

## Delete all temporary files
#os.system("rm $dirname -r;")
