#!/usr/bin/perl -w

# MACHINES CONFIGURATION; ONLY ONE OF THESE TWO -- OR NONE
$SGE_CLUSTER = 0;
$ABACUS_CPU = 0;
$MySplusDir = 1;

##################################################################################
# Oct. 2002 - Bioinformatics Research Center - North Carolina State University   #
#                                                                                #
# Comments to sarisbro at uottawa dot ca - Thanks.                                #
##################################################################################

if($SGE_CLUSTER){
	$curdir = "/home/stephane/work/Pandit62";
}elsif($ABACUS_CPU){
	$curdir = "/home/stephane/work/Pandit62/FlatFiles";
}elsif($MySplusDir){
	$curdir = "/home/stephane/MySwork";
}else{
	$curdir = "D:/data/Pandit62";
}

########################### MAIN ###############################################

$infile = "$curdir/pwise.dat";
$b4_2nd_line = 1;
open(OUTFILE,">$curdir/parsed_pwise.dat") || warn "\ncannot create parsed_pwise: $!";
open(OUTFILE1,">$curdir/parsed_pwise1.dat") || warn "\ncannot create parsed_pwise1: $!";
open(OUTFILE2,">$curdir/parsed_pwise2.dat") || warn "\ncannot create parsed_pwise2: $!";
@IN_FILE = get_file_data($infile);
foreach $line (@IN_FILE) {
	if($b4_2nd_line){
		$b4_2nd_line = 0;
		print OUTFILE "$line";
		print OUTFILE1 "$line";
		#print OUTFILE2 "PF_id,PF_name,SP_pair,lnL_M0,t_M0,S_M0,N_M0,omega_M0,dN_M0,dS_M0,lnL_M1,t_M1,delta\n";
		print OUTFILE2 "PF_id  PF_name  ns  ls  SP_pair  lnL_M0  t_M0  S_M0  N_M0  omega_M0  dN_M0  dS_M0  lnL_M1  t_M1  delta\n";
		next;
	}
	else{
		#row.names,PF.name,SP.pair,lnL.M0,t.M0,S.M0,N.M0,omega.M0,dN.M0,dS.M0,ns,ls,lnL.M1,PFname,t.M1,S.M1,N.M1,omega.M1,dN.M1,dS.M1
		($row,$PF_name,$SP_pair,$lnL_M0,$t_M0,$S_M0,$N_M0,$omega_M0,$dN_M0,$dS_M0,$ns,$ls,$lnL_M1,$PFreal_name,$t_M1,$S_M1,$N_M1,$omega_M1,$dN_M1,$dS_M1) = split(/,/,$line);
		if(($t_M0<39.0)&&($t_M1<39.0)&&($dN_M0<10)&&($dS_M0<10)&&($dN_M1<10)&&($dS_M1<10)){# convergence problem? branch definitely too long and estimates possibly flawed
			print OUTFILE "$line";
			if($omega_M0<99.0){# problem due to zero synonymous changes
				print OUTFILE1 "$line";
				if(($lnL_M0-$lnL_M1)>0){# this is obviousely a convergence problem
					$delta = 2.0*($lnL_M0-$lnL_M1);
					#print OUTFILE2 "$PF_name,$PFreal_name,$ns,$ls,$SP_pair,$lnL_M0,$t_M0,$S_M0,$N_M0,$omega_M0,$dN_M0,$dS_M0,$lnL_M1,$t_M1,$delta\n";
					print OUTFILE2 "$PF_name  $PFreal_name  $ns  $ls  $SP_pair  $lnL_M0  $t_M0  $S_M0  $N_M0  $omega_M0  $dN_M0  $dS_M0  $lnL_M1  $t_M1  $delta\n";
				}
			}
		}
	}
}
close OUTFILE;
close OUTFILE1;
close OUTFILE2;
#################################################################################
# The following routine is adapted from:
# Beginning Perl for Bioinformatics
# by James Tisdall
# published by O'Reilly & Associates
# (c) 2001 James Tisdall
#################################################################################
sub get_file_data {
    my($filename) = @_;
    # Initialize variables
    my @filedata = (  );
    unless( open(GET_FILE_DATA, $filename) ) {
        print STDERR "Cannot open file \"$filename\"\n\n";
        exit;
    }
    @filedata = <GET_FILE_DATA>;
    close GET_FILE_DATA;
    return @filedata;
}
#################################################################################
