# gene

The Gene object represents genes encoding the protein components of a Model. There is 2 kind of gene The `CoreGene` (`macsypy.gene.CoreGene`) which must be unique given a name. A `CoreGene` must have a corresponding HMM protein profile. A `ModelGene` encapsulate a CoreGene and is linked to a Model.

Warning

To optimize computation and to avoid concurrency problems when we search several models, each gene must be instantiated only once, and stored in gene_bank. gene_bank is a `macsypy.gene.GeneBank` object. The gene_bank and model_bank (`macsypy.model.ModelBank` object) are instantiated in `macsypy.scripts.macsyfinder.main()` function and filled by a definition_parser (`macsypy.defintion_parser.DefinitionParser`)

Example to get a CoreGene object:

    # get a model object
    model_a = model_bank("TXSS/model_a")
    model_b = model_bank("TXSS/model_b")

    # get of a <CoreGene> object
    t2ss =  gene_bank[("TXSS", "T2SS")]
    pilO = gene_bank[("TXSS", "pilO")]

to create a ModelGene

    modelA_t2ss(t2ss, model_A)
    modelA_pilO(pilO, model_a, loner=True, inter_gene_max_space=12)
    modelB_pilO(pilO, model_b, inter_gene_max_space=5)

There is only one instance of CoreGene with a given name (model family name, gene name) in one MSF run. But several instance of a ModelGene with the same name may exists. Above, there is 2 <ModelGene> representing pilO one in model_a the second in model_b with different properties.

Exchangeable inherits from ModelGene. Then a gene in some model is seen as a Gene, in some other models as an Exchangeable. But there only one instance of the corresponding CoreGene.:

    core_sctn = gene_bank(("TXSS", "sctN"))
    core_sctn_flg = gene_bank(("TXSS", "sctN_FLG"))
    model_sctn = ModelGene(core_sctn, model_a)
    ex_sctn_flg = Exchangeable(core_stn_flg, model_sctn)
    model_sctn.add_exchangeable(ex_sctn_flg)

    model_sctn_flg = ModelGene(core_sctn_flg, model_b)

which means that in model_a the gene sctn can be functionally replaced by sctn_flg. In Model_a it appear as an alternative to sctn but in model_B it appear as sctn_flg itself. In one MacSyFinder run several instances of ModelGene and/or Exchangeable with the same name may coexists . But in A whole macsyfinder run there is only one instance core_sctn_flg and core_sctn.

## gene API reference

## GeneBank

> class macsypy.gene.GeneBank

>   Store all Gene objects. Ensure that genes are instanced only once.

>   __contains__(gene: CoreGene) → bool

>     Implement the membership test operator

>     Parameters:

>       gene – the gene to test

>     Returns:

>       True if the gene is in, False otherwise

>     Return type:

>       boolean

>   __getitem__(key: tuple[str, str]) → CoreGene
>     Parameters:

>       key – The key to retrieve a gene. The key is composed of the name of models family and the gene name. for instance CRISPR-Cas/cas9_TypeIIB (‘CRISPR-Cas’ , ‘cas9_TypeIIB’) or TXSS/T6SS_tssH (‘TXSS’, ‘T6SS_tssH’)

>     Returns:

>       return the Gene corresponding to the key.

>     Raises:

>       KeyError – if the key does not exist in GeneBank.

>   __init__() → None
>   __iter__() → Iterator

>     Return an iterator object on the genes contained in the bank

>   __weakref__

>     list of weak references to the object

>   add_new_gene(model_location: ModelLocation, name: str, profile_factory: ProfileFactory) → None

>     Create a gene and store it in the bank. If the same gene (same name) is added twice, it is created only the first time.

>     Parameters:

>       - model_location – the location where the model family can be found.

>       - name – the name of the gene to add

>       - profile_factory – The Profile factory

>   genes_fqn() → list[str]
>     Returns:

>       the fully qualified name for all genes in the bank

## Gene

There is two classes to modelize a gene: `macsypy.gene.CoreGene` and `macsypy.gene.ModelGene`. The CoreGene are created using the `macsypy.gene.GeneBank` factory and there is only one instance of a CoreGene with a given name. Whereas several ModelGene with the same name can appear in different model and can have differents properties, loner in one model and not in an other, have different inter_gene_max_space … The ModelGene is attached to the model and is composed of a CoreGene.

Note

The `macsypy.hit.Hit` object are link to a CoreGene, whereas the `macsypy.hit.ValidHit` ref_gene attribute reference a `macsypy.gene.ModelGene`

## CoreGene

class macsypy.gene.CoreGene(model_location: ModelLocation, name: str, profile_factory: ProfileFactory)

  Modeling gene attached to a profile. It can be only one instance with the same name (familly name, gene name)

  __hash__() → int

    Return hash(self).

  __init__(model_location: ModelLocation, name: str, profile_factory: ProfileFactory) → None
  __weakref__

    list of weak references to the object

  property model_family_name: str

    The name of the model family for instance ‘CRISPRCas’ or ‘TXSS’

  property name: str

    The name of the gene a hmm profile with the same name must exist.

  property profile: Profile

    The HMM protein Profile corresponding to this gene

## ModelGene

class macsypy.gene.ModelGene(gene: CoreGene, model: Model, loner: bool = False, multi_system: bool = False, inter_gene_max_space: int = None, multi_model: bool = False)

  Handle Gene described in a Model

  __hash__() → int

    Return hash(self).

  __init__(gene: CoreGene, model: Model, loner: bool = False, multi_system: bool = False, inter_gene_max_space: int = None, multi_model: bool = False)

    Handle gene described in a Model

    Parameters:

      - gene – a gene link to a profile

      - model – the model that owns this Gene

      - loner – True if the Gene can be isolated on the genome (with no contiguous genes), False otherwise.

      - multi_system – True if this Gene can belong to different occurrences of this System.

      - inter_gene_max_space – the maximum space between this Gene and another gene of the System.

      - multi_model – True if this Gene is allowing to appear in several system occurrence from different model.

  __str__() → str

    Print the name of the gene and of its exchangeable genes.

  __weakref__

    list of weak references to the object

  add_exchangeable(exchangeable: Exchangeable)

    Add an exchangeable gene to this Gene

    Parameters:

      exchangeable – the exchangeable to add

  alternate_of() → ModelGene
    Returns:

      the gene to which this one is an exchangeable to (reference gene), or itself if it is a first level gene.

  property core_gene: CoreGene
    Returns:

      The CoreGene associated to this ModelGene

  property exchangeables: list[ModelGene]
    Returns:

      the list of genes which can replace this one without any effect on the model

  property inter_gene_max_space: int | None
    Returns:

      The maximum distance allowed between this gene and another gene for them to be considered co-localized. If the value is not set at the Gene level, return None.

  is_accessory(model: Model) → bool
    Returns:

      True if the gene is within the accessory genes of the model, False otherwise.

    Parameters:

      model – the query of the test

  property is_exchangeable: bool
    Returns:

      True if this gene is described in the model as an exchangeable. False if it is described as first level gene.

  is_forbidden(model: Model) → bool
    Returns:

      True if the gene is within the forbidden genes of the model, False otherwise.

    Parameters:

      model – the query of the test

  is_mandatory(model: Model) → bool
    Returns:

      True if the gene is within the mandatory genes of the model, False otherwise.

    Parameters:

      model – the query of the test

  property loner: bool
    Returns:

      True if the gene can be isolated on the genome, False otherwise

  property model: Model
    Returns:

      the Model that owns this Gene

  property multi_model: bool
    Returns:

      True if this Gene can belong to different occurrences of systems from different

        model `macsypy.model.Model`

      (and can be used for multiple System assessments), False otherwise.

    Return type:

      boolean.

  property multi_system: bool
    Returns:

      True if this Gene can belong to different occurrences of the model (and can be used for multiple System assessments), False otherwise.

  set_status(status: GeneStatus) → None

    Set the status for this gene

    Parameters:

      status – the status of this gene

  property status: GeneStatus
    Returns:

      The status of this gene

## Exchangeable

class macsypy.gene.Exchangeable(c_gene: CoreGene, gene_ref: ModelGene, loner: bool | None = None, multi_system: bool | None = None, multi_model: bool | None = None, inter_gene_max_space: int | None = None)

  Handle Exchangeables. Exchangeable are ModelGene which can replaced functionally another ModelGene. Biologically it can be Homolog or Analog

  __init__(c_gene: CoreGene, gene_ref: ModelGene, loner: bool | None = None, multi_system: bool | None = None, multi_model: bool | None = None, inter_gene_max_space: int | None = None) → None
    Parameters:

      - c_gene – the gene

      - gene_ref – the gene to which the current can replace it.

  add_exchangeable(exchangeable: Exchangeable) → None

    This method should never be called, it’s a security to avoid to add exchangeable to an exchangeable.

    Parameters:

      exchangeable – the exchangeable gene to add

    Raises:

      MacsypyError –

  alternate_of() → ModelGene
    Returns:

      the gene to which this one is an exchangeable to (reference gene)

  property is_exchangeable: bool
    Returns:

      True

  property status: GeneStatus
    Returns:

      The status of this gene. if the status is not define for this gene itself, return the status of the reference gene.

## GeneStatus

class macsypy.gene.GeneStatus(*values)

  Handle status of Gene GeneStatus can take 4 value:

  - MANDATORY

  - ACCESSORY

  - FORBIDDEN

  - NEUTRAL

  __str__() → str

    Return str(self).

Fork me on GitHub