#!/usr/bin/perl

=pod

=head1  NAME

join_annot_results.pl
 
=head1  SYNOPSIS

join_annot_results.pl

=head1  OPTIONS

  --mitoprot
  --netphos
  --yingoyang
  --netcglyc
  --netoglyc
  --netnglyc
  --gpi

=head1 DESCRIPTION

join_annot_results.pl

=head1 VERSION

Version 1

=head1 DATE

19/03/12

=head1 AUTHORS

Sabrina Rodriguez (sabrina.rodriguez@jouy.inra.fr)

=cut


use strict;
use Getopt::Long;
use IO::File;

#use ParamParser;

############################ OPTIONS / PARAMETERS ############################

my @getopt_args = (
					'-mitoprot=s',
					'-netphos=s',
					'-yingoyang=s',
					'-netcglyc=s',
					'-netoglyc=s',
					'-netnglyc=s',
					'-gpi=s'					
		  		);
		  

my %options = ();

unless ( GetOptions( \%options, @getopt_args ) ) {
  usage();
}

sub usage {
  exec "pod2text $0";
  exit( 1 );
}

usage() if ( !exists $options{'snpList'} );

############################ VARIABLES ############################

# check which options are present
# be carefull not to make an error in spelling the option name

if ( exists $options{'mitoprot'} ) { my $mitoprot_file = $options{'mitoprot'}; };
if ( exists $options{'netphos'} ) { my $netphos_file = $options{'netphos'}; };
if ( exists $options{'yingoyang'} ) { my $yingoyang_file = $options{'yingoyang'}; };
if ( exists $options{'netcglyc'} ) { my $netcglyc_file = $options{'netcglyc'}; };
if ( exists $options{'netoglyc'} ) { my $netoglyc_file = $options{'netoglyc'}; };
if ( exists $options{'netnglyc'} ) { my $netnglyc_file = $options{'netnglyc'}; };
if ( exists $options{'gpi'} ) { my $gpi_file = $options{'gpi'}; };


my $h_title_list = {};
my $h_seq_list = {};

my $h_mitoprot = {};
my $h_netphos = {};
my $h_yingoyang = {};
my $h_netcglyc = {};
my $h_netoglyc = {};
my $h_netnglyc = {};
my $h_gpi = {};


############################ PROGRAM ############################

# mitoprot
($h_mitoprot,$h_seq_list,$h_title_list) = getAnnotInformation($mitoprot,$h_seq_list,$h_title_list);

# netphos
($h_netphos,$h_seq_list,$h_title_list) = getAnnotInformation($netphos,$h_seq_list,$h_title_list);

# yinoyang
($h_yingoyang,$h_seq_list,$h_title_list) = getAnnotInformation($yingoyang,$h_seq_list,$h_title_list);

# netcglyc
($h_netcglyc,$h_seq_list,$h_title_list) = getAnnotInformation($netcglyc,$h_seq_list,$h_title_list);

# netoglyc
($h_netoglyc,$h_seq_list,$h_title_list) = getAnnotInformation($netoglyc,$h_seq_list,$h_title_list);

# netnglyc
($h_netnglyc,$h_seq_list,$h_title_list) = getAnnotInformation($netnglyc,$h_seq_list,$h_title_list);

# gpi
($h_gpi,$h_seq_list,$h_title_list) = getAnnotInformation($gpi,$h_seq_list,$h_title_list);














# Get information for the programm
sub getAnnotInformation($$$){
	
	my ($resultfile,$seq_list,$title_list) = @_;
	
	my ($line,$path);
	
	my $prog_results = {};
	
	# Get the path to that program
	$path = $resultfile;
	$path =~ s/\/([^\/]+)$//;
	
	# get the program name
	my ($prog) = $resultfile =~/\/([^_]+)$/;		
	
	# Get the title for that program	
	$titlefile = $path."/".$prog."_title.txt";
	my $fh_in = new IO::File "r", $titlefile or die "cannot open file $titlefile:  $! \n";
	$line = <$fh_in>;			
	$fh_in->close();	
	$line =~ s/^[^\t]*\t//;
	
	$title_list->{$prog} = $line;
	
	
	# Get the results for that program
	my $fh_in = new IO::File "r", $resultfile or die "cannot open file $resultfile:  $! \n";
	while($line =<$fh_in>){
	
		($name) = $line =~ /^([^\t]*)/;
		
		$line =~ s/^[^\t]*\t//;
		$seq_list->{$name} = $name;
		$prog_results->{$name} = $line;				
	}
	$fh_in->close();
	
	
	# send the results
	return($prog_results,$seq_list,$title_list);
}



