#!/usr/bin/env perl

#####################################################################################################
#                                                                                                   #
#                                   PriSM License                                                   #
#                                                                                                   #
# PriSM is distributed under the following BSD-style license:                                       #
# Copyright  2011-2014 Broad Institute, Inc.  All rights reserved.                                 #
#                                                                                                   #
# Redistribution and use in source and binary forms, with or without modification, are              #
# permitted provided that the following conditions are met:                                         #
#                                                                                                   #
#  1. Redistributions of source code must retain the above copyright notice, this list              #
#     of conditions and the following disclaimer.                                                   #
#                                                                                                   #
#  2. Redistributions in binary form must reproduce the above copyright notice, this list           #
#     of conditions and the following disclaimer in the documentation and/or other materials        #
#     provided with the distribution.                                                               #
#                                                                                                   #
#  3. Neither the name of the Broad Institute, Inc. nor the names of its contributors may be        #
#     used to endorse or promote products derived from this software without specific prior         #
#     written permission.                                                                           #
#                                                                                                   #
# THIS SOFTWARE IS PROVIDED AS IS.  BROAD MAKES NO EXPRESS OR IMPLIED REPRESENTATIONS OR          #
# WARRANTIES OF ANY KIND REGARDING THE SOFTWARE AND COPYRIGHT, INCLUDING, BUT NOT LIMITED TO,       #
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, CONFORMITY WITH ANY              #
# DOCUMENTATION, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT         #
# DISCOVERABLE. IN NO EVENT SHALL BROAD, THE COPYRIGHT HOLDERS, OR CONTRIBUTORS BE LIABLE FOR       #
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,        #
# BUT NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    #
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,     #
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE       #
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF, HAVE REASON TO KNOW, OR IN FACT SHALL KNOW OF THE       #
# POSSIBILITY OF SUCH DAMAGE.                                                                       #
#                                                                                                   #
# If, by operation of law or otherwise, any of the aforementioned warranty disclaimers are          #
# determined inapplicable, your sole remedy, regardless of the form of action, including, but       #
# not limited to, negligence and strict liability, shall be replacement of the Software with        #
# an updated version if one exists.                                                                 #
#                                                                                                   #
# Development of PriSM has been funded in whole or in part with federal funds from the National     #
# Institute of Allergy and Infectious Diseases, National Institutes of Health, Department of        #
# Health and Human Services, under Contract No. HHSN266200400001C.                                  #
#                                                                                                   #
# In addition, PriSM is distributed, in part, under and subject to the provisions of licenses for:  #
# Perl, version 5.20.0,  1997-2010 Tom Christiansen, Nathan Torkington (all rights reserved).      #
#####################################################################################################

# ===================================================================================================
# Description:
#   To call "make_all_consensus_web.pl" and "primer_design_web.pl"
#  
# Usage: 
#    generate_primer -f aln_file -n genome_name
#       aln_file-- alignment file generated by CLUSTALW or MUSCLE
#       genome_name -- the name of a genome, for example: den9
#       You can edit multiprimer.conf to customize settings
#
# Creation Date: 2007-03-02
# Author: Qing Yu
# ===================================================================================================

use strict;
use PrimerDesign;

my $usage = <<USAGE;
Usage:
	$0 alnFile genomeName genoFastaFile outfile1 outfile2 configureFile

For example:
    $0 -f den9.aln den9 den9.fasta consense.out primerDesign.out multiprimer.conf

USAGE
;

my $aln_file =$ARGV[0];
my $genoName = $ARGV[1];
my $genoFasta =$ARGV[2];
my $outfile1 =$ARGV[3];
my $outfile2 =$ARGV[4];
my $conf_file = $ARGV[5];

if ( scalar(@ARGV) != 6)
{
	print "Wrong Input!!\n\n$usage\n";
	exit(0);
}

my $exe_dir = "/bit/data01/projects/prod_scripts/PriSM/";

unless (-e $aln_file)
{
  my $cmd0 = $exe_dir."muscle3.8.31_i86linux64  -in $genoFasta  -out $aln_file";
  print "\n<br>$cmd0<br><br>\n\n";
  &doSystem($cmd0);
}

my $cmd1 = "perl ".$exe_dir."make_all_consensus_web.pl $aln_file $conf_file >$outfile1";
print "\n<br>$cmd1<br><br>\n\n";
&doSystem($cmd1);

my $cmd2 = "perl ".$exe_dir."primer_design_web.pl $aln_file $genoName $genoFasta $conf_file>$outfile2";
print "\n<br>$cmd2<br><br>\n\n";
&doSystem($cmd2);

exit;

################
sub doSystem 
{
    my ($string) = @_;
    
    my $rc = system($string);
    if ($rc) 
    {
		return "system call '$string' terminated with exit status ($rc)\n";
    }
    return undef;
}
