#!/usr/bin/perl -w

use strict;
use IO::File;
use Pod::Usage;
use Getopt::Long;

=pod

=head1  NAME
split_VCF_files.pl
 
=head1  SYNOPSIS

split_VCF_files.pl -arg1 <> -arg2 <> -arg3 <> -arg4 <>

=head1  OPTIONS

  --infile string, the path of the input file VCF
  --snp_number_per_file string, number of lines per splitted files
  --splitted_filename_size string, the size of the filenames splitted files
  --working_dir string, directory to split the files

=head1 DESCRIPTION

split_VCF_files.pl - This program splits a vcf file according to criteria

WARNING:
THE FASTA FILES SHOULD HAVE FORMAT: name.fasta
ALL FILES MUST HAVE ONLY 1 EXTENSION: .txt or .tab....


=head1 VERSION


=head1 DATE

05/03/2012

=head1 AUTHORS

Sabrina Rodriguez

=cut

############################ OPTIONS / PARAMETERS ############################

my @getopt_args = (
                    '-infile=s'  ,
                    '-snp_number_per_file=s'  ,
                    '-splitted_filename_size=s',
                    '-working_dir=s' 
                  );

my %options = ();

unless ( GetOptions( \%options, @getopt_args ) ) {
  usage();
}

sub usage {
  exec "pod2text $0";
  exit( 1 );
}

usage() if ( !exists $options{'infile'} );
usage() if ( !exists $options{'snp_number_per_file'} );
usage() if ( !exists $options{'splitted_filename_size'} );
usage() if ( !exists $options{'working_dir'} );


############################ PROGRAM ############################

# Set options
my $snp_number_per_file = $options{'snp_number_per_file'};
my $infile = $options{'infile'};
my $splitted_filename_size = $options{'splitted_filename_size'};
my $working_dir = $options{'working_dir'};


my ($filename);
($filename) = $infile =~ /\/([^\/]+)$/;


my @args = ();

@args = ("cp", $infile, "$working_dir/.");
unless (-e "$working_dir/$filename") {system(@args) == 0 or die "system @args failed: $?"};
	
chdir "$working_dir" or die "cannot change dir split_VCF_files chdir $working_dir : $?";
	
@args = ("split", "-l", $snp_number_per_file, "-a", $splitted_filename_size, "-d", "$working_dir/$filename");
system(@args) == 0 or die "system @args failed: $?";

@args = ("rm", "$working_dir/$filename");
system(@args) == 0 or die "system @args failed: $?";
