#!/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";
open(RESFILE,"$infile") || die "\ncannot open $infile: $!";
$b4_2nd_line = 1;
open(OUTNAMES,">$curdir/pf_names.dat") || warn "\ncannot create pf_names.dat: $!";
open(OUTFILE0,">$curdir/pwise_names_1w.at.least.dat") || warn "\ncannot create pwise_names_1w.at.least.dat: $!";
open(OUTFILE1,">$curdir/pwise_names_50PCw.at.least.dat") || warn "\ncannot create pwise_names_50PCw.at.least.dat: $!";
open(OUTFILE2,">$curdir/pwise_names_1wSIG1pc.at.least.dat") || warn "\ncannot create pwise_names_1wSIG.at.least.dat: $!";
open(OUTFILE3,">$curdir/pwise_names_50PCwSIG1pc.at.least.dat") || warn "\ncannot create pwise_names_50PCwSIG.at.least.dat: $!";

$PSS_thisPF = $PSS_thisPFsig = 0;
$new_PF_100 = 0;
$new_PF_50 = 0;
$new_PF_100sig = 0;
$new_PF_50sig = 0;
$this_PF = 0;

while(<RESFILE>){
	chomp;
	($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(/,/);
	if($PF_name != $this_PF){
		$this_PF = $PF_name;
		$PSS_thisPF = $PSS_thisPFsig = 0;
		$new_PF_100 = $new_PF_50 = $new_PF_100sig = $new_PF_50sig = 1;
		print OUTNAMES "$PF_name  $ns  $ls  $PFreal_name\n";

	}
	if(($omega_M0>1)&&($t_M0<39.0)&&($t_M1<39.0)&&($dN_M0<10)&&($dS_M0<10)&&($dN_M1<10)&&($dS_M1<10)){
		$delta = 2.0*($lnL_M0-$lnL_M1);
		if($delta>0){
			#PFs with at least one omega ratio > 1
			if($new_PF_100){
				#print OUTFILE0 "$PF_name  $ns  $ls  $PFreal_name\n";
				print OUTFILE0 "$PF_name  $ns  $ls  $PFreal_name\n";
				$new_PF_100 = 0;
			}
			#PFs with at least 50% omega ratios > 1
			$PSS_thisPF ++;
			if(($PSS_thisPF > 0.5*($ns*($ns-1)/2))&&($new_PF_50)){
				#print OUTFILE1 "$PF_name  $ns  $ls  $PFreal_name\n";
				print OUTFILE1 "$PF_name  $ns  $ls  $PFreal_name\n";
				$new_PF_50 = 0;
			}
			#PFs with at least one omega ratio > 1 at level 1%
			if($new_PF_100sig&&($delta>6.64)){
				#print OUTFILE2 "$PF_name  $ns  $ls  $PFreal_name\n";
				print OUTFILE2 "$PF_name  $ns  $ls  $PFreal_name\n";
				$new_PF_100sig = 0;
			}
			#PFs with at least 50% omega ratios > 1 at level 1%
			$PSS_thisPFsig ++;
			if(($PSS_thisPFsig > 0.5*($ns*($ns-1)/2))&&($new_PF_50sig)&&($delta>6.64)){
				#print OUTFILE3 "$PF_name  $ns  $ls  $PFreal_name\n";
				print OUTFILE3 "$PF_name  $ns  $ls  $PFreal_name\n";
				$new_PF_50sig = 0;
			}
		}
	}
}

close OUTNAMES;
close OUTFILE0;
close OUTFILE1;
close OUTFILE2;
close OUTFILE3;
