#!/usr/bin/perl

use strict;

use Bio::SeqIO;
use Getopt::Long;
use Pod::Usage;

my ($seq_obj, $id, $desc, $line, $gap_start, $gap_len, $target, $oripos, $gen_pos, $sequence);

our ($input, $prod_size, $help);

## Get options for input file, product size, and help from command line.

GetOptions(
    'input|i=s' => \$input,
    'product_size|p=s' => \$prod_size,
    'help|h|?' => \$help,
);
 
pod2usage(-verbose => 2) if ($help);

## Check input file, product_size option, and output files.

pod2usage(-message => "-i input file not supplied.\n") if (!$input);
pod2usage(-message => "-p product size was not supplied.\n") if (!$prod_size);

my $output = $input . ".p3.in";
open (OUT, ">$output") || die "Cannot open $output: $!\n";

## Read input fasta file.

my $in = Bio::SeqIO->new(
    -file => $input,
    -format => 'Fasta'
    );

## Get id, gap coordinates, sequence and put in file.

while ($seq_obj = $in->next_seq){
    $id = $seq_obj->display_id;
    $desc = $seq_obj->desc();
    if ($desc =~ m/(allelepos)(\=)(\d*)(\.\.)(\d*)/){
	$gap_start = $3;
	$gap_len = $5 - $3;
	$target = ($gap_start - 11).",".($gap_len + 20);
    }
    if ($desc =~ m/(oripos)(\=)(\d*)(\.\.)(\d*)/){
	$gen_pos = $desc;

    }
    
    $sequence = $seq_obj->seq;

    print OUT <<PRIMER3;
PRIMER_SEQUENCE_ID=$id, $gen_pos
SEQUENCE=$sequence
TARGET=$target
PRIMER_OPT_TM=55
PRIMER_MIN_TM=54
PRIMER_MAX_TM=56
PRIMER_PRODUCT_SIZE_RANGE=$prod_size
PRIMER_OPT_SIZE=20
PRIMER_MIN_SIZE=18
PRIMER_MAX_SIZE=22
PRIMER_NUM_NS_ACCEPTED=1
PRIMER_NUM_RETURN=1
PRIMER_FILE_FLAG=1
PRIMER_EXPLAIN_FLAG=1
=
PRIMER3
}

close OUT;



=head1 NAME                                                                                                               

flanks2primer3.pl                                                                             

This script changes the output of flanks.pl to a format that can be used directly as primer3 input.                          
                                                                 
=head1 SYNOPSIS                                                                                                  

flanks2primer3.pl [--h] --i <input_file> --p <product_size>

    options:
    --help                help message
    --input_file          output from flanks.pl
    --product_size        PCR product size, can be a range
                        
=head1 OPTIONS                                                                                                     

=over 8                                                                                                              

=item --h

B<help>                   Prints a help message.

=item --i

B<input>                   Input file (mandatory)
 
=item --p

B<product_size>            Accepts a PCR product size argument (mandatory)

=back 

=head1 DESCRIPTION

This script accepts output from flanks.pl (included in Bioperl, written by Heikki Lehvaslaiho) and converts it to a format acceptable for input in the primer3 primer design software.  The following defaults are used:  
PRIMER_OPT_TM=55
PRIMER_MIN_TM=54
PRIMER_MAX_TM=56
PRIMER_OPT_SIZE=20
PRIMER_MIN_SIZE=18
PRIMER_MAX_SIZE=22
PRIMER_NUM_NS_ACCEPTED=1
PRIMER_NUM_RETURN=1
PRIMER_FILE_FLAG=1
PRIMER_EXPLAIN_FLAG=1

Illegal characters, such as /, are not removed by this script and must be removed before primer3 usage.

=head1 AUTHOR

Susan Strickler
srs57@cornell.edu

=cut
