package OntologyMap;

# Copyright INRA

# Sebastien.Carrere@toulouse.inra.fr
# Jerome.Gouzy@toulouse.inra.fr

# This software is a computer program whose purpose is to provide
# a code generator framework for BioMoby web-services.

# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software.  You can  use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".

# As a counterpart to the access to the source code and  rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty  and the software's author,  the holder of the
# economic rights,  and the successive licensors  have only  limited
# liability.

# In this respect, the user's attention is drawn to the risks associated
# with loading,  using,  modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate,  and  that  also
# therefore means  that it is reserved for developers  and  experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and,  more generally, to use and operate it in the
# same conditions as regards security.

# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.

use vars qw(@ISA @EXPORT);
require Exporter;

@ISA = qw(Exporter);

@EXPORT = qw(

use strict;
use Carp;
use BiomobyUtils;
use General qw(:DEFAULT GetDirectoryFile);
use XML::Twig;

sub BiomobyToMobyle
    my $biomoby_term = shift;
    my $dictionnary  = GetDirectoryFile('OntologyMap.pm') . '/../data/BioMobyMobyleDictionnary.xml';

    Carp::confess("$dictionnary not found") if (!-e $dictionnary);

    my $default_biomoby_term = 'text-formatted';
    my $o_mobyle_elt;
    while ($biomoby_term !~ /object/i)
        my ($code, $result) = &IsBiomobyTermDefined($biomoby_term, $dictionnary);

        if ($code < 0)

            $biomoby_term = &BiomobyUtils::GetParentObject($biomoby_term);
        elsif ($code == 0)
            die("ERROR: $biomoby_term - $result");
            $o_mobyle_elt = $result;

    if (!defined $o_mobyle_elt)
        (my $code, $o_mobyle_elt) = &IsBiomobyTermDefined($default_biomoby_term, $dictionnary);

    return $o_mobyle_elt;


sub GetMobyleTypeXml
    my $o_mobyle_elt      = shift;
    my $mobyle_class      = $o_mobyle_elt->{att}->{class};
    my $mobyle_superclass = $o_mobyle_elt->{att}->{superclass};
    my $mobyle_biotype    = $o_mobyle_elt->{att}->{biotype};
    my $mobyle_dataformat = $o_mobyle_elt->{att}->{dataformat};

    my $mobyle_xml = '';
    $mobyle_xml .= "<biotype>$mobyle_biotype</biotype>\n"            if ($mobyle_biotype    ne '');
    $mobyle_xml .= "<datatype>\n";
    $mobyle_xml .= "\t<class>$mobyle_class</class>\n"                if ($mobyle_class      ne '');
    $mobyle_xml .= "\t<superclass>$mobyle_superclass</superclass>\n" if ($mobyle_superclass ne '');
    $mobyle_xml .= "</datatype>\n";
    $mobyle_xml .= "<acceptedDataFormats>\n\t<dataFormat>$mobyle_dataformat</dataFormat>\n</acceptedDataFormats>\n"
      if ($mobyle_dataformat ne '');

    return $mobyle_xml;

sub IsBiomobyTermDefined
    my $biomoby_term = shift;
    my $dictionnary  = shift;

    #xpath: /ontology_mapping/biomoby_term/term[@id="$biomoby"]
    my $o_xml = new XML::Twig();
    my $o_root_elt        = $o_xml->root;
    my @a_o_biomoby_terms = $o_root_elt->get_xpath("/ontology_mapping/biomoby_term/term[\@id='$biomoby_term']/mobyle");

    if ($#a_o_biomoby_terms < 0)
        return -1, 'not found';
    elsif ($#a_o_biomoby_terms > 0)
        return 0, 'duplicated term';
        return 1, $a_o_biomoby_terms[0];