o
    RÅ€g*  ã                   @   sX   d Z ddlmZ ddlmZ G dd„ dƒZG dd„ dƒZG dd	„ d	ƒZG d
d„ dƒZdS )a‹  BioPython Pathway module.

Bio.Pathway is a lightweight class library designed to support the following tasks:

 - Data interchange and preprocessing between pathway databases and analysis software.
 - Quick prototyping of pathway analysis algorithms

The basic object in the Bio.Pathway model is Interaction, which represents an arbitrary
interaction between any number of biochemical species.

Network objects are used to represent the connectivity between species in pathways
and reaction networks.

For applications where it is not necessary to explicitly represent network connectivity,
the specialized classes Reaction and System should be used in place of Interacton and
Network.

The Bio.Pathway classes, especially Interaction, are intentionally
designed to be very flexible. Their intended use are as wrappers around database
specific records, such as BIND objects. The value-added in this module is a
framework for representing collections of reactions in a way that supports
graph theoretic and numeric analysis.

Note: This module should be regarded as a prototype only. API changes are likely.
      Comments and feature requests are most welcome.

é    )Úreduce)Ú
MultiGraphc                   @   sJ   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚReactiona"  Abstraction for a biochemical transformation.

    This class represents a (potentially reversible) biochemical
    transformation of the type::

      a S1 + b S2 + ... --> c P1 + d P2 + ...

    where:
     - a, b, c, d ... are positive numeric stochiometric coefficients,
     - S1, S2, ... are substrates
     - P1, P2, ... are products

    A Reaction should be viewed as the net result of one or more individual
    reaction steps, where each step is potentially facilitated by a different
    catalyst. Support for 'Reaction algebra' will be added at some point in
    the future.

    Attributes:
     - reactants   -- dict of involved species to their stochiometric coefficients:
       reactants[S] = stochiometric constant for S
     - catalysts   -- list/tuple of tuples of catalysts required for this reaction
     - reversible  -- true iff reaction is reversible
     - data        -- reference to arbitrary additional data

    Invariants:
     - for all S in reactants: reactants[S] != 0
     - for all C in catalysts: catalysts[C] != 0

    N© r   c                 C   sZ   |du ri | _ n| ¡ | _ | ¡ D ]\}}|dkr| j |= qtt|ƒƒ| _|| _|| _dS )z!Initialize a new Reaction object.Nr   )Ú	reactantsÚcopyÚitemsÚsortedÚsetÚ	catalystsÚdataÚ
reversible)Úselfr   r   r   r   ÚrÚvaluer   r   úH/var/www/html/myenv/lib/python3.10/site-packages/Bio/Pathway/__init__.pyÚ__init__H   s   
€
zReaction.__init__c                 C   s:   t |tƒo| j|jko| j|jko| j|jko| j|jkS )z#Return true iff self is equal to r.)Ú
isinstancer   r   r   r   r   )r   r   r   r   r   Ú__eq__W   s   

ÿ
þ
ý
ûzReaction.__eq__c                 C   s   t |  ¡ ƒ}t|ƒS ©zReturn a hashcode for self.)ÚtupleÚspeciesÚhash)r   Útr   r   r   Ú__hash__a   s   zReaction.__hash__c                 C   s   d| j | j| j| jf S )ú1Return a debugging string representation of self.zReaction(%r, %r, %r, %r))r   r   r   r   ©r   r   r   r   Ú__repr__f   s   üzReaction.__repr__c                 C   sÊ   d}d}t | jƒ}|D ]J}| j| }|dk r3|dkr|d }|dkr,|tt|ƒƒ d }|t|ƒ }q|dkrR|dkr?|d }|dkrK|t|ƒ d }|t|ƒ }qtdƒ‚| jr_|d | S |d	 | S )
ú'Return a string representation of self.Ú r   z + éÿÿÿÿú é   z+Invalid 0 coefficient in Reaction.reactantsz <=> z --> )r	   r   ÚstrÚabsÚAttributeErrorr   )r   Ú
substratesÚproductsÚall_speciesr   Ústochr   r   r   Ú__str__o   s*   

zReaction.__str__c                 C   s4   i }| j D ]
}| j |  ||< qt|| j| j| jƒS )z2Return a new Reaction that is the reverse of self.)r   r   r   r   r   )r   r   r   r   r   r   Úreverse‹   s   
zReaction.reversec                 C   ó
   t | jƒS )z.Return a list of all Species involved in self.)Úlistr   r   r   r   r   r   ’   ó   
zReaction.species)Nr   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r*   r+   r   r   r   r   r   r   )   s    

	r   c                   @   sR   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚSystemzÚAbstraction for a collection of reactions.

    This class is used in the Bio.Pathway framework to represent an arbitrary
    collection of reactions without explicitly defined links.

    Attributes:
     - None

    r   c                 C   ó   t |ƒ| _dS )zInitialize a new System object.N)r
   Ú_System__reactions)r   Ú	reactionsr   r   r   r   ¢   ó   zSystem.__init__c                 C   s   dd  tt| jƒ¡ d S )r   zSystem(ú,ú))ÚjoinÚmapÚreprr5   r   r   r   r   r   ¦   s   zSystem.__repr__c                 C   s*   dt t| jƒƒ d t t|  ¡ ƒƒ d S )r   z
System of z reactions involving z species)r#   Úlenr5   r   r   r   r   r   r*   ª   s   ÿþýüÿzSystem.__str__c                 C   ó   | j  |¡ dS )zAdd reaction to self.N)r5   Úadd©r   Úreactionr   r   r   Úadd_reaction´   ó   zSystem.add_reactionc                 C   r>   )zRemove reaction from self.N)r5   Úremover@   r   r   r   Úremove_reaction¸   rC   zSystem.remove_reactionc                 C   r,   )z]Return a list of the reactions in this system.

        Note the order is arbitrary!
        )r-   r5   r   r   r   r   r6   ¼   s   
zSystem.reactionsc                 C   s&   t ttdd„ dd„ |  ¡ D ƒg ƒƒƒS )z,Return a list of the species in this system.c                 S   s   | | S ©Nr   )ÚsÚxr   r   r   Ú<lambda>Ç   s    z System.species.<locals>.<lambda>c                 S   s   g | ]}|  ¡ ‘qS r   )r   )Ú.0rH   r   r   r   Ú
<listcomp>Ç   s    z"System.species.<locals>.<listcomp>)r	   r
   r   r6   r   r   r   r   r   Ä   s    ÿzSystem.speciesc                 C   sr   |   ¡ }|  ¡ }g t|ƒ }tt|ƒƒD ]}dt|ƒ ||< ||   ¡ D ]}|| j| || |¡< q$q|||fS )a‹  Compute the stoichiometry matrix for self.

        Returns (species, reactions, stoch) where:
         - species    = ordered list of species in this system
         - reactions  = ordered list of reactions in this system
         - stoch      = 2D array where stoch[i][j] is coef of the
           jth species in the ith reaction, as defined
           by species and reactions above

        r   )r   r6   r=   Úranger   Úindex)r   r   r6   r)   ÚirG   r   r   r   ÚstochiometryÊ   s   ÿ
zSystem.stochiometryN©r   )r/   r0   r1   r2   r   r   r*   rB   rE   r6   r   rO   r   r   r   r   r3   —   s    


r3   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚInteractiona!  An arbitrary interaction between any number of species.

    This class definition is intended solely as a minimal wrapper interface that should
    be implemented and extended by more specific abstractions.

    Attributes:
     - data      -- reference to arbitrary additional data

    c                 C   s
   || _ d S rF   )r   )r   r   r   r   r   Ú__init_ð   s   
zInteraction.__init_c                 C   r,   r   )r   r   r   r   r   r   r   ó   r.   zInteraction.__hash__c                 C   ó   dt | jƒ d S )r   zInteraction(r9   )r<   r   r   r   r   r   r   ÷   ó   zInteraction.__repr__c                 C   rS   )r   ú<ú>)r#   r   r   r   r   r   r*   û   rT   zInteraction.__str__N)r/   r0   r1   r2   Ú_Interaction__init_r   r   r*   r   r   r   r   rQ   å   s    
rQ   c                   @   sj   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚNetworkaf  A set of species that are explicitly linked by interactions.

    The network is a directed multigraph with labeled edges. The nodes in the graph
    are the biochemical species involved. The edges represent an interaction between
    two species, and the edge label is a reference to the associated Interaction
    object.

    Attributes:
     - None

    r   c                 C   r4   )z Initialize a new Network object.N)r   Ú_Network__graph©r   r   r   r   r   r     r7   zNetwork.__init__c                 C   rS   )z9Return a debugging string representation of this network.z<Network: __graph: rV   )r<   rY   r   r   r   r   r     rT   zNetwork.__repr__c                 C   s   dt |  ¡ ƒt |  ¡ ƒf S )z/Return a string representation of this network.z*Network of %i species and %i interactions.)r=   r   Úinteractionsr   r   r   r   r*     s   

þzNetwork.__str__c                 C   r>   )zAdd species to this network.N)rY   Úadd_noderZ   r   r   r   Úadd_species  rC   zNetwork.add_speciesc                 C   s   | j  |||¡ dS )z Add interaction to this network.N)rY   Úadd_edge)r   ÚsourceÚsinkÚinteractionr   r   r   Úadd_interaction   s   zNetwork.add_interactionc                 C   ó   | j  |¡S )z*Return list of unique sources for species.)rY   ÚparentsrZ   r   r   r   r_   $  ó   zNetwork.sourcec                 C   rc   )z7Return list of (source, interaction) pairs for species.)rY   Úparent_edgesrZ   r   r   r   Úsource_interactions(  re   zNetwork.source_interactionsc                 C   rc   )z(Return list of unique sinks for species.)rY   ÚchildrenrZ   r   r   r   r`   ,  re   zNetwork.sinkc                 C   rc   )z5Return list of (sink, interaction) pairs for species.)rY   Úchild_edgesrZ   r   r   r   Úsink_interactions0  re   zNetwork.sink_interactionsc                 C   ó
   | j  ¡ S )z+Return list of the species in this network.)rY   Únodesr   r   r   r   r   4  r.   zNetwork.speciesc                 C   rk   )z7Return list of the unique interactions in this network.)rY   Úlabelsr   r   r   r   r[   8  r.   zNetwork.interactionsNrP   )r/   r0   r1   r2   r   r   r*   r]   rb   r_   rg   r`   rj   r   r[   r   r   r   r   rX      s    
rX   N)	r2   Ú	functoolsr   ÚBio.Pathway.Rep.MultiGraphr   r   r3   rQ   rX   r   r   r   r   Ú<module>   s   nN