#!/usr/bin/env perl

# runs step 1 on all chromosomes
# produces the input for step 2 results
# submits a pbs job for every chromosome specified

use strict;

# create and store distributions for the top 25 models from permutation testing
# for both the model p-value and interaction p-value

my $PRGM_NAME = "run_step1.pl";

my $pbs = '/scratch/username/impute/impute_1000genomes.pbs'; #CHANGE
my $pbs_himemory = '/scratch/username/impute/impute_1000genomes_himem.pbs'; #CHANGE

# Subroutine: usage
# Displays current version date and usage
sub usage{
  print "\nusage:\t\t$PRGM_NAME <basename>\n\n";
  print "example:\t$PRGM_NAME hdlmarsh \n\n";
}

# subroutine: check_args
# Checks number of arguments passed in and returns name of pedigree file.
sub check_args{
  if(@ARGV != 1)
  { usage();
    exit;}

  return @ARGV;
}

my ($basename) = check_args();

for(my $i=1; $i<=22; $i++){
  # check that tarball exists for this chromosome
  my @files = glob("*chr${i}_step0.tar.gz");
  if(@files > 0){
    print "submitting job for chromosome $i\n";
    system("qsub $pbs_himemory -v PREFIX=$basename,CHR=$i") if $i<=2;
    system("qsub $pbs -v PREFIX=$basename,CHR=$i") if $i>2;
  }
}