#!/usr/bin/env perl
use strict;



###########################################################################################################
#
#
#    trio2shape
#
#          created by Stephan Ripke, Broadinstitute, sripke@broadinstitute.org
#
#                                  12/01/10
#
#
#
#    converts triofams for shapeit usage
#
#
#
#
##########################################################################################################


my $version = "1.0.0";
my $progname = $0;
$progname =~ s!^.*/!!;
my $command_line = "$progname @ARGV";


##### help message
my $usage = "
Usage : $progname [options] famfile

version: $version


  --help           print this message and exit


 prepares a famfile from trios for usage in shapeit (meaning: bringing the FID into IID)

 created by Stephan Ripke 2012 at MGH, Boston, MA
 
";

#### evaluate options
use Getopt::Long;
GetOptions( 
    "help"=> \my $help,
    );


die "$usage\n" if ($help);

###################################################
###  system call with test if successfull
###################################################

sub mysystem(){
    my ($systemstr)="@_";
    system($systemstr);
    my $status = ($? >> 8);
    die "$systemstr\n->system call failed: $status" if ($status != 0);
}


##########################################
# subroutine to split a plink-output-line with references
##########################################

sub split_line_ref {
    my ($line)=${$_[0]};
    chomp($line);
    $line =~ s/^[\s]+//g;
    my @cols=  split /\s+/, $line;
    \@cols;
}



#################################################
#  BEGIN
#################################################



my $ffile = $ARGV[0];


my $in_name = $ffile;
my $out_name = $ffile.".shape";
my $out_name5 = $ffile.".shape5";
my $out_name5f = $ffile.".shape5.fam";

die $!."($in_name)" unless open IFILE, "< $in_name";
die $!."($out_name)" unless open OFILE, "> $out_name";
die $!."($out_name5)" unless open OFILE2, "> $out_name5";
die $!."($out_name5f)" unless open OFILE3, "> $out_name5f";

while (my $line = <IFILE>){
    my @cells = @{&split_line_ref(\$line)};
    my $fid = $cells[0];

    if ($cells[2] ne "0" || $cells[3] ne "0") {
	## SHAPE5
	my $sh5_str = "$fid"."____"."$cells[1]"."\t"."$fid"."____"."$cells[2]"."\t"."$fid"."____"."$cells[3]";
	print OFILE2 "$sh5_str\n";

	##shape5.fam
	my $sh5_str = "$fid"."_pca"."\t".$cells[1]."\t0\t0"."\t".$cells[4]."\t2\n";
	$sh5_str   .= "$fid"."_pco"."\t".$cells[1]."\t0\t0"."\t".$cells[4]."\t1\n";
	print OFILE3 "$sh5_str";
	
    }

    $fid =~ s/.*\*//;
    $cells[1] = "$fid...$cells[1]";
    if ($cells[2] ne "0") {
	$cells[2] = "$fid...$cells[2]";
    }
    if ($cells[3] ne "0") {
	$cells[3] = "$fid...$cells[3]";
    }

    print OFILE "@cells\n";
}

close IFILE;
close OFILE;
close OFILE2;
close OFILE3;




