#!/usr/bin/perl

#
# Command line client for the Jaspar database
#
# 
#


use warnings;
use strict;
use XML::Compile::WSDL11;
use XML::Compile::Transport::SOAPHTTP;
use Getopt::Long;
use Data::Dumper;

# Parsing command line options
my ( $get_all, $get_by_id, $get_by_name, $search_string, $format, $db , $help);
GetOptions(
	"all"      => \$get_all,
	"id:s"     => \$get_by_id,
	"name:s"   => \$get_by_name,
	"search:s" => \$search_string,
	"db=s"     => \$db,
	"format=s" => \$format,
	"help|?"   => \$help
);

if ($help){
	print "\n\tJasparClient.pl query -db jaspar_sub_database -format matrix_format \n\n";
	print "\tquery can be: -all, -id matrix_id, -name matrix_name or -search tagname=tagvalue\n";
	print "\tjaspar_sub_database can be: CORE, FAM, PHYLOFACTS, POLII, CNE or SPLICE\n";
	print "\tmatrix_format can be PFM, PWM or ICM\n\n";
}

# Defaults
unless ($db)     { $db     = 'CORE'; }
unless ($format) { $format = 'PFM'; }

# Retriving and processing the WSDL
my $wsdl  = XML::LibXML->new->parse_file('http://api.bioinfo.no/wsdl/JasparDB.wsdl');
my $proxy = XML::Compile::WSDL11->new($wsdl);                                           

# 
if ($get_all) {
	my $getAllMatrices = $proxy->compileClient('getAllMatrices');
	my $answer         = $getAllMatrices->( Format => $format, Database => $db );
	my @matrices       = @{ $answer->{parameters}{Matrix} };
	for my $matrix (@matrices) {
		_print_matrix($matrix);
	}
}

if ($get_by_id) {
	my $getMatrixById = $proxy->compileClient('getMatrixById');
	my $answer        = $getMatrixById->( Id => $get_by_id, Format => $format, Database => $db );
	my $matrix        = $answer->{parameters}{Matrix};
	_print_matrix($matrix);
}

if ($get_by_name) {
	my $getMatrixByName = $proxy->compileClient('getMatrixByName');
	my $answer          = $getMatrixByName->( Name => $get_by_name, Format => $format, Database => $db );
	my $matrix          = $answer->{parameters}{Matrix};
	_print_matrix($matrix);
}

if ($search_string) {
	my $searchByTag = $proxy->compileClient('searchByTag');
	my ( $tn, $tv ) = split( /=/, $search_string );
	my %tag;
	$tag{'Name'}  = $tn;
	$tag{'Value'} = [$tv];
	my $answer = $searchByTag->( Tag => [ \%tag ], Format => $format, Database => $db );
	if ( defined $answer->{parameters}{Matrix} ) {
		my @matrices = @{ $answer->{parameters}{Matrix} };
		for my $matrix (@matrices) {
			_print_matrix($matrix);
		}
	}
}

sub _print_matrix {
	my $matrix = $_[0];
	print "\nMatrix ID       $matrix->{ID}\n";

	my @tags = @{ $matrix->{Tag} };
	for my $tag (@tags) {
		print sprintf( "%-16s", $tag->{Name} );
		my @values = @{ $tag->{Value} };
		for my $value (@values) {
			print $value . ' ';
		}
		print "\n";
	}
	print "Matrix:\n";
	print 'A ';
	for my $col ( @{ $matrix->{A}->{col} } ) {
		print sprintf( "%4d", $col );
	}
	print "\nC ";
	for my $col ( @{ $matrix->{C}->{col} } ) {
		print sprintf( "%4d", $col );
	}
	print "\nG ";
	for my $col ( @{ $matrix->{G}->{col} } ) {
		print sprintf( "%4d", $col );
	}
	print "\nT ";
	for my $col ( @{ $matrix->{T}->{col} } ) {
		print sprintf( "%4d", $col );
	}
	print "\n\n";
}
