#!/usr/bin/perl -w
use strict;

my $infile_ref = $ARGV[0];
my $infile_avedist1k = $ARGV[1];
my $infile_corrgsea1k = $ARGV[2];
my $outfile_all_positive = $ARGV[3];
#my ( $para1, $para2 ) = split( /:/, $ARGV[3] );
my $para1=0.6;
my $para2=0.9;
my $libsvm_train_para = "-t 0"; # "-t 0" / "-t 2 -g 0.5"
#my $outfile1 = $ARGV[5];
#my $outfile_ref_positive = $ARGV[6];
my $n_bagging = 100;
#my $svmtrain = "\"libsvm-3.0\/windows\/svm-train.exe\"";
#my $svmpredict = "\"libsvm-3.0\/windows\/svm-predict.exe\"";
my $svmtrain = "libsvm-3.0\/svm-train";
my $svmpredict = "libsvm-3.0\/svm-predict";
my $pos_pos = 0;
my $pos_all = 0;
srand(time());

my %feature = ();
my @positive = ();
my %hash_positive = ();
my @negative = ();
my @all = ();
my %n_predict = ();

open( in1, "<$infile_ref" );
while( my $line1 = <in1> ) {
	chomp( $line1 );
	my ( $id1, $desc1 ) = split( /\t/, $line1 );
	if( !exists( $feature{ $id1 } ) ) {
		$feature{ $id1 } = "positive";
		push( @positive, $id1 );
		$hash_positive{ $id1 } = "yes";
	}
}
close( in1 );

open( in2, "<$infile_avedist1k" );
while( my $line2 = <in2> ) {
	chomp( $line2 );
	my ( $id1, $dist, $zscore, $desc1 ) = split( /\t/, $line2, 4 );
	if( !exists( $feature{ $id1 } ) ) {
		push( @negative, $id1 );
	}
	
	$feature{ $id1 } = "1:$zscore";
}
close( in2 );

open( out_test1, ">tmp_libsvm_test.txt" );
open( in3, "<$infile_corrgsea1k" );
while( my $line3 = <in3> ) {
	chomp( $line3 );
	my ( $id1, $es, $zscore, $desc1 ) = split( /\t/, $line3, 4 );
	
	if( !exists( $feature{ $id1 } ) ) {
		print "Features do not match!\n";
		exit;
	}
	
	$feature{ $id1 } = join( " ", $feature{ $id1 }, "2:$zscore" );
	
	if( exists( $hash_positive{ $id1 } ) ) {
		print out_test1 join( " ", "+1", $feature{ $id1 } );
	}
	else {
		print out_test1 join( " ", "-1", $feature{ $id1 } );
	}
	
	push( @all, $id1 );
	print out_test1 "\n";
}
close( in3 );
close( out_test1 );


open( out3, ">$outfile_all_positive" );
for( my $n_run = 1; $n_run <= $n_bagging; $n_run++ ) {
	open( out_svm1, ">tmp_libsvm_input.txt" );
	
	for( my $i = 0; $i <= $#positive; $i++ ) {
		print out_svm1 join( " ", "+1", $feature{ $positive[$i] } );
		print out_svm1 "\n";
	}
	
	my @rand_negative = ();
	rand_sampling( $#positive + 1, \@rand_negative, @negative );
	
	for( my $i = 0; $i <= $#rand_negative; $i++ ) {
		print out_svm1 join( " ", "-1", $feature{ $rand_negative[$i] } );
		print out_svm1 "\n";
	}
	
	close( out_svm1 );
	#system( "$svmtrain -t 0 -b 1 -h 0 tmp_libsvm_input.txt tmp_libsvm.model > tmp_out.txt" );
	system( "$svmtrain $libsvm_train_para -b 1 -h 0 tmp_libsvm_input.txt tmp_libsvm.model > tmp_out.txt" );
	system( "$svmpredict -b 1 tmp_libsvm_test.txt tmp_libsvm.model tmp_libsvm_test.predict.txt > tmp_out.txt" );
	#print "$svmtrain -t 0 tmp_libsvm_input.txt tmp_libsvm.model";
	
	open( in_svmpredict, "<tmp_libsvm_test.predict.txt" );
	my @predict = <in_svmpredict>;
	shift( @predict );
	chomp( @predict );
	close( in_svmpredict );
	
	for( my $i = 0; $i <= $#predict; $i++ ) {
		my ( $label, $prob1, $prob2 ) = split( /\s/, $predict[$i] );
		
		#if( $label == 1 ) {
		if( $prob1 >= $para1 ) {
			if( !exists( $n_predict{ $all[$i] } ) ) {
				$n_predict{ $all[$i] } = 1;
			}
			else {
				$n_predict{ $all[$i] }++;
			}
		}
		else {
			if( !exists( $n_predict{ $all[$i] } ) ) {
				$n_predict{ $all[$i] } = 0;
			}
		}
	}
	
	#exit;
}




#open( out1, ">$outfile1" );
#open( out2, ">$outfile_ref_positive" );


for( my $i = 0; $i <= $#all; $i++ ) {
	
	
		print out3 join( "\t", $i,$#all );
		print out3 "\n";
}

#close( out1 );
#close( out2 );
close( out3 );


sub rand_sampling {
	my ( $n_sampling, $rand_samples, @all_samples ) = @_;
	
	for( my $i = 0; $i < $n_sampling; $i++ ) {
	
	my $j = int( rand( $#all_samples + 1 ) );
		$rand_samples->[$i] = splice( @all_samples, $j, 1 );
	}
}
