#!/usr/bin/env perl

=head1 NAME

getReadsInRepeat.pl

=head1 SYNOPSIS
  
  getReadsInRepeat.pl 
                      -iRead  <454Contigs.ace.1.pairs>    
                      -cName  <contig name>
	              -bStart <repeat boundry start>
	              -bEnd   <repeat boundry end>
                      -oRead  <path to read pair output file>
                      -help 
  

=head1 DESCRIPTION

This script takes read pair file, contig name, repeat boundary start,
repeat boundary end, path to output file of read ids and an optional 
file name to output a read pair subset file of read pair lines in the target region.

All unpaired reads in boundaries specified are examined.  
Reads are ouput if the entire read falls within specified
boundaries.  No overlap.

=head1 VERSION

$Revision: 1.4 $

$Date: 2009-08-26 17:18:15 $

=head1 HISTORY

=over

=item *

Brian Foster 2008/11/07 Creation

=back

=cut

# Includes

  
use strict;
use Carp;
use vars qw( $RealBin $optiRead $optcName $optbStart $optbEnd $optHelp $optoRept $optoRead);
use FindBin qw($RealBin);

use Getopt::Long;
use Pod::Usage;
use File::Path;
use File::Basename;


#============================================================================#
# INPUT VALIDATION
#============================================================================#

if( !GetOptions(
		"iRead=s" =>\$optiRead,
		"cName=s" =>\$optcName,
		"bStart=s"=>\$optbStart,
		"bEnd=s" =>\$optbEnd,
		"oRead=s" =>\$optoRead,
		"h"=>\$optHelp)
) {
    printHelp();
}

pod2usage(-verbose=>2) and exit 1 if defined $optHelp;

if( ! defined $optiRead || ! defined $optcName 
 || ! defined $optbStart|| ! defined $optbEnd){
    printHelp();
}
if (! -e $optiRead){
    print STDERR "Cannot find your read pair file $optiRead\n";
    printHelp();    
}
if ( ! -d dirname($optoRead)){
    mkpath (dirname($optoRead));
}
if ( ! -w dirname($optoRead)){
    print STDERR dirname($optoRead), "Not writable\n";
    printHelp();
}

#============================================================================#
# MAIN
#============================================================================#
# get read information from pair file
my (@F,$template);
my @result;
open (IN , $optiRead) or confess "can't open $optiRead\n";
while(<IN>){
    @F = (split/\t/,$_);

    #reject if paired or multiply placed
    next if ($F[6] =~/(?:M|P)/);

    #reject wrong contig
    next if ($F[4] ne $optcName); 
    
    # reject outside optb range no overlaps allowed
    next if ($F[1] <= $optbStart || $F[2] >= $optbEnd ); 

    #reformat ids
    ($template = $F[0]) =~ s/^(.*?)(?:\_|\.).*$/$1/;
    
    push (@result, $_);
}
close(IN);
if (@result){
    open(OUT, ">$optoRead") or confess "can't open $optoRead for writing\n";
    print OUT join("",@result);
    close(OUT);
}

#============================================================================#
sub printHelp {
    
    my $optVerbose = shift || 1;
    pod2usage(-verbose=>$optVerbose);
    exit 1;
}

#============================================================================#

exit 0;
