filter.pm

Code Index:


package filter;

use strict;

#$PATH est a editer.
my $PATH = '/bioinfo/www/bioinfo/services/biomobylipm/ws2007/iant';

my $LOCAL_DIR = $PATH . '/services/dev/aa_dna/';

my $UNIX_CMD = $PATH . '/cfg/redhat_cmd.cfg';


BioMOBY::iANT_filter

        Description : Program to filter iANT-xml files.The filter command is a string describing the filtering process.  You can specify more than one filter step (coma separated). In this case the AND logical operator is applied to compound the full filter. example: "score > 1500, sequence/begin > 200, sequence/end <= 850 , hit/definition =~ antigen"
        Contact     : Sebastien.Carrere@toulouse.inra.fr
sub iANT_filter
{
    my ($caller, $message) = @_;
    my $moby_response;
    my %h_input_data = ();

    my $service_name = 'iANT_filter';

    my $o_param = New ParamParser("$LOCAL_DIR/$service_name/default.cfg");
    $o_param->Update($UNIX_CMD, 'A');
    my $service_doc = $o_param->Get('service_documentation');

    #
    # Initialisation
    #
    my $rh_param_service                  = &BiomobyUtils::InitializeServiceParameters($o_param);
    my $rh_registered_input_article_names = &BiomobyUtils::GetRegisteredInputArticleNames($o_param);

    #
    # Analyse du message entrant
    #

    my ($service_notes, $ra_queries) = &MOBYXSLT::getInputs($message);
    foreach my $query (@{$ra_queries})
    {
        my $query_id         = &MOBYXSLT::getInputID($query);
        my @a_input_articles = &MOBYXSLT::getArticles($query);

        my $input_data = '';
        foreach my $input_article (@a_input_articles)
        {
            my ($article_name, $article) = @{$input_article};
            my $article_param_root_key = $rh_registered_input_article_names->{$article_name};

            if (&MOBYXSLT::isSimpleArticle($article))
            {
                my $moby_object_type_for_input      = $o_param->Get($article_param_root_key . '_otp');
                my $moby_object_namespace_for_input = $o_param->Get($article_param_root_key . '_ons');
                my $object_type                     = &MOBYXSLT::getObjectType($article);

                if (&BiomobyUtils::IsTheCorrectObject($object_type, $moby_object_type_for_input))
                {
                    my @a_hasa_elements = &MOBYXSLT::getObjectHasaElements($article);
                    if ($#a_hasa_elements >= 0)
                    {
                        foreach my $hasa_element (@a_hasa_elements)
                        {
                            $h_input_data{$article_param_root_key} = &MOBYXSLT::getObjectContent($hasa_element)
                              if (&MOBYXSLT::getObjectName($hasa_element) eq 'content');
                        }
                    }
                    else    #pour les objets primaires (string,float,integer ...)
                    {
                        $h_input_data{$article_param_root_key} = &MOBYXSLT::getObjectContent($article);
                    }
                }
            }

            elsif (&MOBYXSLT::isSecondaryArticle($article))
            {
                ($rh_param_service->{$article_name}) =
                  &MOBYXSLT::getNodeContentWithArticle($query, 'Parameter', $article_name);
            }
        }

        foreach my $article_key (keys %h_input_data)
        {
            &_Log_iANT_filter("$service_name $article_key ");
            if ((!defined $h_input_data{$article_key}) || ($h_input_data{$article_key} eq ''))
            {

                my $exception =
                  &MOBYXSLT::encodeException(
                                             -refElement       => $article_key,
                                             -refQueryID       => $query_id,
                                             -severity         => 'error',
                                             -exceptionCode    => '226',
                                             -exceptionMessage => "empty input"
                                             );
                return
                  SOAP::Data->type(
                           'base64' => (
                               &MOBYXSLT::responseHeader(
                                   -authority => $o_param->Get('AUTH_URI'),
                                   -note => "$! : Documentation available at " . $o_param->Get('service_documentation'),
                                   -exception => $exception
                               )
                             )
                             . &MOBYXSLT::responseFooter()
                             );
            }
        }

        #
        # Preparation des fichiers et commandes
        #
        my $file_root = $service_name . "." . $$ . time;
        my $tmpdir    = $o_param->Get('tmp_dir') . "/$file_root";
        mkdir("$tmpdir");
        chmod(0777, "$tmpdir");
        chdir($tmpdir);

        my $cmd = $o_param->Get("bin_cmd");

        (my $code, $cmd) =
          &CreateInputFiles_iANT_filter($query_id, $tmpdir, $file_root, $cmd, \%h_input_data, $o_param);

        return $cmd if (!$code);
        $cmd = &NameOutputFiles_iANT_filter($tmpdir, $file_root, $cmd, $o_param);

        while ($cmd =~ m/(maskparam#\w+)/g)
        {
            my $param_name = $1;
            $param_name =~ s/^maskparam#//;

            if (($param_name =~ /redundancy/i) && ($rh_param_service->{$param_name} eq ''))
            {
                $cmd =~ s/--redundancy=maskparam#\w+//;
            }
            elsif ($param_name =~ /entrydb/i)
            {
                $cmd =~ s/--entrydb=maskparam#\w+// if ($rh_param_service->{$param_name} eq 'no');
                $cmd =~ s/=maskparam#\w+//          if ($rh_param_service->{$param_name} eq 'yes');
            }
            elsif ($param_name =~ /filter/i)
            {
                use HTML::Entities;
                $rh_param_service->{$param_name} = HTML::Entities::decode($rh_param_service->{$param_name});
                $cmd =~ s/maskparam#\w+/$rh_param_service->{$param_name}/;
            }
            else
            {
                $cmd =~ s/maskparam#\w+/$rh_param_service->{$param_name}/;
            }

        }

        #
        # Execution
        #
        &_Log_iANT_filter("$service_name cmd:$cmd");
        my $error = `$cmd 2>&1 1>/dev/null`;

        if ($error =~ /ERROR/)
        {
            use HTML::Entities;
            $error = HTML::Entities::decode($error);

            my $exception =
              &MOBYXSLT::encodeException(
                                         -refElement       => '',
                                         -refQueryID       => $query_id,
                                         -severity         => 'error',
                                         -exceptionCode    => '222',
                                         -exceptionMessage => "$error"
                                         );
            return
              SOAP::Data->type(
                           'base64' => (
                               &MOBYXSLT::responseHeader(
                                   -authority => $o_param->Get('AUTH_URI'),
                                   -note => "$! : Documentation available at " . $o_param->Get('service_documentation'),
                                   -exception => $exception
                               )
                             )
                             . &MOBYXSLT::responseFooter()
                             );
        }

        #
        # Preparation des resultats
        #
        $moby_response = &CatOutputFiles_iANT_filter($query_id, $tmpdir, $file_root, $moby_response, $o_param);

    }

    #
    # Message sortant
    #
    my $exception =
      &MOBYXSLT::encodeException(
                                 -refElement       => '',
                                 -refQueryID       => '',
                                 -severity         => 'information',
                                 -exceptionCode    => '700',
                                 -exceptionMessage => "OK"
                                 );

    return
      SOAP::Data->type(
                       'base64' => (
                                    &MOBYXSLT::responseHeader(
                                                              -authority => $o_param->Get('AUTH_URI'),
                                                              -note      => "Documentation available at $service_doc",
                                                              -exception => $exception
                                    )
                         )
                         . $moby_response
                         . &MOBYXSLT::responseFooter()
                         );
}

function CreateInputFiles_iANT_filter

        Title   : CreateInputFiles_iANT_filter
        Usage   : ($code, $cmd) = CreateInputFiles_iANT_filter($tmpdir, $file_root, $cmd, $rh_input_data, $o_param)
        Function: Creates input files for each input article to be used in the shell command line.
                          Makes substitutions in the command line with the temporary file names.
                          If input file can not be created, returns 0 and SOAP message
                          Else, return 1 and the command line.
        Args    : $query_id Moby query ID
                          $tmpdir temporary dir
                          $file_root temporary file name
                          $cmd original masked command line
                          $rh_input_data hash reference of input articles data
                          $o_param ParamParser object
        Returns : code (0|1)
                          SOAP message|command
sub CreateInputFiles_iANT_filter
{
    my ($query_id, $tmpdir, $file_root, $cmd, $rh_input_data, $o_param) = (@_);

    foreach my $input_key (keys %{$rh_input_data})
    {
        my $input_data = $rh_input_data->{$input_key};
        $input_data =~ s/\r/\n/g;
        my @a_input_data_lines = split(/\n/, $input_data);
        $input_data = '';

        foreach my $input_data_line (@a_input_data_lines)
        {
            chomp $input_data_line;
            if (($input_data_line ne '') && ($input_data_line ne "\n"))
            {
                $input_data .= $input_data_line . "\n";
            }
        }

        use IO::File;

        my $input_fh = new IO::File(">$tmpdir/$file_root.$input_key.input");

        if (!defined $input_fh)
        {

#Exception codes @ http://biomoby.open-bio.org/CVS_CONTENT/moby-live/Perl/docs/html/MOBY/Client/Exception/MobyExceptionCodes.html
            my $exception =
              &MOBYXSLT::encodeException(
                                         -refElement       => $o_param->Get($input_key . '_nm'),
                                         -refQueryID       => $query_id,
                                         -severity         => 'error',
                                         -exceptionCode    => '600',
                                         -exceptionMessage => "$tmpdir/$file_root.$input_key.input can not be written"
                                         );
            return (
                    0,
                    SOAP::Data->type(
                           'base64' => (
                               &MOBYXSLT::responseHeader(
                                   -authority => $o_param->Get('AUTH_URI'),
                                   -note => "$! : Documentation available at " . $o_param->Get('service_documentation'),
                                   -exception => $exception
                               )
                             )
                             . &MOBYXSLT::responseFooter()
                    )
                    );

        }
        elsif ($input_data eq '')
        {

#Exception codes @ http://biomoby.open-bio.org/CVS_CONTENT/moby-live/Perl/docs/html/MOBY/Client/Exception/MobyExceptionCodes.html
            my $exception =
              &MOBYXSLT::encodeException(
                                         -refElement       => $o_param->Get($input_key . '_nm'),
                                         -refQueryID       => $query_id,
                                         -severity         => 'error',
                                         -exceptionCode    => '226',
                                         -exceptionMessage => "empty object"
                                         );
            return (
                    0,
                    SOAP::Data->type(
                           'base64' => (
                               &MOBYXSLT::responseHeader(
                                   -authority => $o_param->Get('AUTH_URI'),
                                   -note => "$! : Documentation available at " . $o_param->Get('service_documentation'),
                                   -exception => $exception
                               )
                             )
                             . &MOBYXSLT::responseFooter()
                    )
                    );

        }
        else
        {
            print $input_fh $input_data;
            $input_fh->close;
            chmod 0777, "$tmpdir/$file_root.$input_key.input";
            $cmd =~ s/mask#$input_key/$tmpdir\/$file_root.$input_key.input/;
        }
    }
    return (1, $cmd);

}

function NameOutputFiles_iANT_filter

        Title   : NameOutputFiles_iANT_filter
        Usage   : ($cmd) = NameOutputFiles_iANT_filter($cmd, $o_param)
        Function: Makes substitutions in the command line with the temporary file names.
        Args    : $tmpdir temporary dir
                          $file_root temporary file name
                          $cmd original masked command line
                          $o_param ParamParser object
        Returns : command
sub NameOutputFiles_iANT_filter
{
    my ($tmpdir, $file_root, $cmd, $o_param) = (@_);
    my @a_output_article_name_keys = $o_param->GetKeys('oar\d+_nm');
    foreach my $output_article_name_key (@a_output_article_name_keys)
    {
        my ($article_root_key) = ($output_article_name_key =~ /^(oar\d+)/);
        if ($o_param->Get($article_root_key . '_tp') =~ /simple/i)
        {
            $cmd =~ s/mask#$article_root_key/$tmpdir\/$file_root.$article_root_key.output/g;
        }
        else
        {

#collection ...
#on peut faire la meme substitution et exploser le fichier dans un post-traintement dans  la sub CatOutputFiles_iANT_filter
        }
    }
    return $cmd;
}

function CatOutputFiles_iANT_filter

        Title   : CatOutputFiles_iANT_filter
        Usage   : $moby_response = CreateInputFiles_iANT_filter($tmpdir, $file_root, $moby_response, $o_param)
        Function: Get content  of generated output files and add to moby response message
        Args    : $query_id Moby query ID
                          $tmpdir temporary dir
                          $file_root temporary file name
                          $moby_response original moby response to be completed
                          $o_param ParamParser object
        Returns : $moby_response completed moby response
sub CatOutputFiles_iANT_filter
{
    my ($query_id, $tmpdir, $file_root, $moby_response, $o_param) = (@_);
    my $unix_cat = $o_param->Get('UNIX_CAT');

    my @a_output_article_name_keys = $o_param->GetKeys('oar\d+_nm');
    foreach my $output_article_name_key (@a_output_article_name_keys)
    {
        my ($article_root_key) = ($output_article_name_key =~ /^(oar\d+)/);
        if ($o_param->Get($article_root_key . '_tp') =~ /simple/i)
        {
            chmod 0777, "$tmpdir/$file_root.$article_root_key.output";
            my $data = `$unix_cat $tmpdir/$file_root.$article_root_key.output`;

            #binary encoding pour les images
            #use MIME::Base64;
            #$data = MIME::Base64::encode($data);

            #
            #A faire uniquement si on renvoie autre chose qu'un objet de base (String, Object, Integer ...)
            #
            $data = "<moby:String id=\"\" namespace=\"\" articleName=\"content\"><![CDATA[$data]]></moby:String>";

            my $output_object_type      = $o_param->Get($article_root_key . '_otp');
            my $output_object_namespace = $o_param->Get($article_root_key . '_ons');
            my $output_article_name     = $o_param->Get($article_root_key . '_nm');
            $moby_response .=
              &MOBYXSLT::simpleResponse(
                   "<moby:$output_object_type  namespace=\"$output_object_namespace\">$data</moby:$output_object_type>",
                   $output_article_name, $query_id);
        }
        else
        {

#Retour de collection: ...
#my $output_article_name = $o_param->Get($article_root_key . '_nm');
#my @a_files = ();
#foreach my $file (@a_files)
#{
#	my $object_content = "<moby:$output_object_type id=\"\" namespace=\"$output_object_namespace\"><moby:String id=\"\" namespace=\"\" articleName=\"content\">$data</moby:String></moby:$output_object_type>";
#	push(@a_objects, $object_content);
#}
#$moby_response .= &MOBYXSLT::collectionResponse(\@a_objects, $output_article_name, $query_id);
        }

    }
    return $moby_response;
}

procedure _Log_iANT_filter

        Title   : _Log_iANT_filter
        Usage   : _Log_iANT_filter($message)
        Function: Write execution log message into iANT_filter/log file
        Args    : $message
        Returns : none
sub _Log_iANT_filter
{
    my $message = shift();

    if ((-w "$LOCAL_DIR/iANT_filter/log") || (-W "$LOCAL_DIR/iANT_filter/log"))
    {
        open(LOG, ">> $LOCAL_DIR/iANT_filter/log") || die("$! $LOCAL_DIR/iANT_filter/log");
        print LOG "$message\n";
        close LOG;
    }
    chmod 0777, "$LOCAL_DIR/iANT_filter/log";

    return;
}

1;