#!/usr/bin/env perl
#
#   Usage: ./mrsclustal -h
#
#   martin.senger@gmail.com
#   February 2010
#
# ABSTRACT: program running BLAST using the MRS Retrieval System
# PODNAME: MRS::Client
#-----------------------------------------------------------------------------
use warnings;
use strict;

our $VERSION = '1.0.1'; # VERSION

use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../lib/perl5";
use MRS::Client;
use Data::Dumper;

sub say { print @_, "\n"; }

sub get_usage {
    return <<"END_OF_USAGE";
Usage:
   [Available ONLY for MRS server version 5 and lower!]
   mrsclustal [options] -i <multiple_fasta_file>

where
   "multiple_fasta_file" is a file with two or more protein
      sequences in FASTA format that will be aligned

where 'options' are:
   -e <endpoint>  URL of the MRS clustal service
   -S <name>      service name of the MRS clustal service
   -H <hostname>  endpoint host (with standard ports)
   -E             show what endpoint and service name are used

   -a <cost>      gap opening cost (positive integer)
   -z <cost>      gap extension cost (float)

   -d             show only diagnostics

   -h             this help
   -v             show version

END_OF_USAGE
}

# be prepare for command-line options/arguments
my @all_args = @ARGV;
use Getopt::Std;

use vars qw/ $opt_h $opt_v /;               # general
use vars qw/ $opt_e $opt_S $opt_H $opt_E /; # endpoints
use vars qw/ $opt_i $opt_a $opt_z /;        # clustal run
use vars qw/ $opt_d /;                      # results
use vars qw/ $opt_D /;
my $switches = 'aeHiSz';   # switches taking an argument
getopt ($switches);

# help wanted?
if ($opt_h or @all_args == 0) {
    print get_usage;
    exit 0;
}

# print version and exit
if ($opt_v) {
    ## no critic
    no strict;    # because the $VERSION will be added only when
    no warnings;  # the distribution is fully built up
    print "$0 using MRS::Client version $MRS::Client::VERSION\n";
    exit 0;
}

# use UTF8 for output
binmode STDOUT, ":encoding(UTF-8)";

# create the main worker
my @args = ();
push (@args, clustal_url => $opt_e) if defined $opt_e;
push (@args, host => $opt_H) if defined $opt_H;
push (@args, clustal_service => $opt_S) if defined $opt_S;
push (@args, debug => $opt_D) if defined $opt_D;
push (@args, mrs_version => 5);
our $client = MRS::Client->new (@args);

# print environment (where to find server, etc.)
if (defined $opt_E) {
    say 'Clustal URL:            ' . $client->clustal_url       if $client->clustal_url;;
    say 'Clustal service name:   ' . $client->clustal_service   if $client->clustal_service;
    say 'Clustal WSDL:           ' . $client->clustal_wsdl      if $client->clustal_wsdl;
}

# collect parameters for a clustal run
my @run_args = ();
push (@run_args, open_cost => $opt_a) if defined $opt_a;
push (@run_args, extend_cost => $opt_z) if defined $opt_z;
push (@run_args, fasta_file => $opt_i) if $opt_i;

# run Clustal
my $result = $client->clustal->run (@run_args);
if (defined $result) {
    say "ERROR: " . $result->failed if $result->failed;
    if ($opt_d) {
        say $result->diagnostics;
    } else {
        my $output = "$result";
        if ($output) {
            print $output;
        } else {
            say $result->diagnostics;
        }
    }
} else {
    say "ERROR: No result available from the ClustalW service. Perhaps you are trying to connect to MRS 6?";
}



=pod

=head1 NAME

MRS::Client - program running BLAST using the MRS Retrieval System

=head1 VERSION

version 1.0.1

=head1 AUTHOR

Martin Senger <martin.senger@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Martin Senger, CBRC - KAUST (Computational Biology Research Center - King Abdullah University of Science and Technology) All Rights Reserved..

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut


__END__
