#!/usr/bin/perl 
use strict;
use warnings;
use DB_File;


# Copyright (C) 2013 by Pedro Faria 
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.




my $diretorio_atual = `pwd`;
chomp($diretorio_atual);
my @arquivos = `ls |grep fasta\$`;

    foreach my $arquivo (@arquivos) {
        chomp($arquivo);
#cria arquivo de configuracao que sera rodado pelo EGene
my $heredoc = <<"EOT";
#**********************************************************************************
#
PIPELINE=mascara_$arquivo
#
#**********************************************************************************

#==================================================================================
PHASE=upload_fasta
program = upload_fasta.pl
#----------------------------------------------------------------------------------
multifastafile = $diretorio_atual/$arquivo
sequence_type = n
#==================================================================================

#==================================================================================
#                                        ||                                       #
#                                       _||_                                      #
#                                       \\  /                                      #
#                                        \\/                                       #
#==================================================================================

#==================================================================================
PHASE=mask_cross_match
program = mask_cross_match.pl
#----------------------------------------------------------------------------------
maskLibrary = /usr/local/genome/databases/UniVec
matchArgumentList = -minmatch 15 -minscore 13
matchOut = /dev/null
minimum_primer_size = 0
#==================================================================================

#==================================================================================
#                                        ||                                       #
#                                       _||_                                      #
#                                       \\  /                                      #
#                                        \\/                                       #
#==================================================================================

#==================================================================================
PHASE=mask_cross_match_2
program = mask_cross_match.pl
#----------------------------------------------------------------------------------
maskLibrary = /home/alan/cana/montagem/canaBAC11sequence.fasta
matchArgumentList = -minmatch 15 -minscore 13
matchOut = /dev/null
minimum_primer_size = 0
#==================================================================================

#==================================================================================
#                                        ||                                       #
#                                       _||_                                      #
#                                       \\  /                                      #
#                                        \\/                                       #
#==================================================================================

#==================================================================================
PHASE=trimming
program = trimming.pl
#----------------------------------------------------------------------------------
output_all = false
minimum_quality = 10
verification_window_quality = 20
window_size = 30
verification_window_size = 15
quality_threshold = 80
invalid_letters = XxNn
#==================================================================================

#==================================================================================
#                                        ||                                       #
#                                       _||_                                      #
#                                       \\  /                                      #
#                                        \\/                                       #
#==================================================================================

#==================================================================================
PHASE=outsave
program = outsave.pl
#----------------------------------------------------------------------------------
savefile = $arquivo.masked
file_format = fasta
#==================================================================================

EOT
        my $nome_arq_pipeline = "pipeline_mascaramento_".$arquivo.".cnf";     
        open(PIPELINE, ">", $nome_arq_pipeline)  or die "cannot open > $nome_arq_pipeline: $!";
        print PIPELINE $heredoc;
        close(PIPELINE);

        #roda o EGene com o pipeline gerado pelo arquivo de configuração acima    
        system("/usr/local/genome/egene1/bin/bigou.pl -c $nome_arq_pipeline 1> $nome_arq_pipeline.stdout 2> $nome_arq_pipeline.stderr") or print STDERR "Erro na execucao do bigou: $!\n";



    }




