# definition_parser

The model definition parser object “DefinitionParser” instantiates Models and Genes objects from XML model definitions (see Macromolecular models). The parsing consists in three phases.

Phase 1.

> - For each model to parse

>   - create the Model

>   - add this Model to the model_bank

>   - findall genes defined in this model what are the level in the model definition.

>   - create the CoreGene (a Gene which is not bind to a model). For each gene name there is only one instance of CoreGene

>   - add these CoreGene in the gene_bank

Phase 2.

> - For each model to search

>   - For each Gene defined in this System:

>     > - link the gene to the model. Create a ModelGene by encapsulating CoreGene from the gene_bank It can exists at each run several ModelGene for one CoreGene

>     > - If a gene has exhangeables create them (an Exchangeable inherits from ModeleGene) and add them to the current ModelGene

For instance:

    Syst_1
    <system inter_gene_max_space="10">
        <gene name=”A” mandatory=”1” loner="1">
            <exchangeables>
                <gene name=”B”>
            </exchangeables>
        </gene>
    <system>

    Syst_2
    <system inter_gene_max_space="15">
        <gene name=”B” mandatory=”1”>
            <exchangeables>
                <gene name=”C”>
            </exchangeables>
        </gene>
    <system>

    Syst_3
    <system inter_gene_max_space="20">
        <gene name=”c” mandatory=”1” />
    <system>

With the example above:

- the CoreGene A, B, C will be created

- the ModelGene (Syst_1, A) (Syst_1, B), (Syst_2, B), (Syst_2, C), (Syst_3, C)

- The ModeleGene (Syst_1, A), (Syst_2, B) and (Syst_3, C) are directly link to their respective Models

- and where (Syst_1, B) (Syst_2, C) are exchangeables and link respectively to (Syst_1, A) and (Syst_2, B)

- the ModelGene has attributes defined in the model where they appear (Syst_1, B) inter_gene_max_space=”10” (Syst_2, B) inter_gene_max_space=”15”

Note

The only “full” Systems (i.e., with all corresponding Genes created) are those to detect.

## defintion_parser API reference

## DefinitionParser

Module use to parse XML model defintion and create a python Model and Genes, …

class `macsypy.definition_parser.``DefinitionParser`(cfg, model_bank, gene_bank, model_registry, profile_factory)

  Build a Model instance from the corresponding model definition described in the XML file.

  `__init__`(cfg, model_bank, gene_bank, model_registry, profile_factory)
    Parameters

      - cfg (`macsypy.config.Config` object) – the configuration object of this run

      - model_bank (`macsypy.model.ModelBank` object) – the model factory

      - gene_bank (`macsypy.gene.GeneBank` object) – the gene factory

      - model_registry (`macsypy.registry.ModelRegistry` object) – The registry with all model location

      - profile_factory (`macsypy.profil.ProfilFactory` object) – The profile factory

  `__weakref__`

    list of weak references to the object (if defined)

  `_check_syntax`(model_node, path)

    Check if the definition does not contains logical error which is allowed by syntax and absence of explicit grammar.

    Parameters

      - model_node (`Et.Element` object) – the node corresponding to the model

      - path (str) – the path of the definition.

    Returns

      None

    Raises

      ModelInconsistencyError – if an error is encountered in the document.

  `_create_model`(def_loc, model_node)
    Parameters

      - def_loc (`macsypy.registries.DefinitionLocation` object) – the definition location to parse.

      - model_node (`Et.ElementTree` object.) – the node corresponding to the model.

    Returns

      the model corresponding to the definition location.

    Return type

      `macsypy.model.Model` object.

  `_fill_gene_bank`(model_node, model_location, def_loc)

    find all gene node and add them to the gene_bank

    Parameters

      - model_node (`Et.ElementTree` object.) –

        param model_node

          the node corresponding to the model.

      - model_location (class:macsypy.registries.ModelLocation object.) –

      - def_loc (the node corresponding to the 'model' tag) – a definition location to parse.

    Returns

      None

  `_get_model_node`(def_loc)
    Parameters

      def_loc (return the node corresponding to the 'model' tag) – a definition location to parse.

  `_parse_exchangeable`(gene_node, gene_ref, curr_model)

    Parse a xml element gene child of exchangeable and build the corresponding object

    Parameters

      - gene_node (`xml.etree.ElementTree.Element` object.) – a “node” corresponding to the gene element in the XML hierarchy

      - gene_ref (class:macsypy.gene.ModelGene object) – the gene which this gene is homolog to

      - curr_model (`macsypy.model.Model` object) – the model being parsed .

    Returns

      the gene object corresponding to the node

    Return type

      `macsypy.gene.Exchangeable` object

  `_parse_genes`(model, model_node)

    Create genes belonging to the models. Each gene is directly added to the model in it’s right category (‘mandatory, accessory, …)

    Parameters

      - model (`macsypy.model.Model` object) – the Model currently parsing

      - model_node (`Et.ElementTree` object) – the element ‘model’

  `check_consistency`(models)

    Check the consistency of the co-localization features between the different values given as an input: between XML definitions, configuration file, and command-line options.

    Parameters

      models (list of class:macsypy.model.Model object) – the list of models to check

    Raise

      `macsypy.error.ModelInconsistencyError` if one test fails

    (see feature)

    In the different possible situations, different requirements need to be fulfilled (“mandatory_genes” and “accessory_genes” consist of lists of genes defined as such in the model definition):

    > - If: min_mandatory_genes_required = None ; min_genes_required = None

    > - Then: min_mandatory_genes_required = min_genes_required = len(mandatory_genes)

    > always True by Models design

    > - If: min_mandatory_genes_required = value ; min_genes_required = None

    > - Then: min_mandatory_genes_required <= len(mandatory_genes)

    > - AND min_genes_required = min_mandatory_genes_required

    > always True by design

    > - If: min_mandatory_genes_required = None ; min_genes_required = Value

    > - Then: min_mandatory_genes_required = len(mandatory_genes)

    > - AND min_genes_required >= min_mandatory_genes_required

    > - AND min_genes_required <= len(mandatory_genes+accessory_genes)

    > to be checked

    > - If: min_mandatory_genes_required = Value ; min_genes_required = Value

    > - Then: min_genes_required <= len(accessory_genes+mandatory_genes)

    > - AND min_genes_required >= min_mandatory_genes_required

    > - AND min_mandatory_genes_required <= len(mandatory_genes)

    > to be checked

  `parse`(models_2_detect)

    Parse models definition in XML format to build the corresponding Model objects, and add them to the model factory after checking its consistency. To get the model ask it to model_bank

    Parameters

      models_2_detect (list of `macsypy.registry.DefinitionLocation`) – a list of model definition to parse.