o
    Rŀg=                     @   sv   d Z zddlmZ W n ey   dZY nw ddlmZ ddlmZ ddlm	Z	 ddl
mZ d	d
 ZG dd dZdS )a  Provides a container for information concerning the tracks to be drawn in a diagram.

It also provides the interface for defining the diagram (possibly split these
functions in later version?).

For drawing capabilities, this module uses reportlab to draw and write the
diagram:

http://www.reportlab.com

For dealing with biological information, the package expects BioPython
objects - namely SeqRecord objects containing SeqFeature objects.
    )renderPMN)_write   )CircularDrawer)LinearDrawer)Trackc                  G   s   | D ]
}|dur|  S qdS )z-Return the first non-null argument (PRIVATE).N )argsargr   r   W/var/www/html/myenv/lib/python3.10/site-packages/Bio/Graphics/GenomeDiagram/_Diagram.py_first_defined&   s
   r   c                   @   s   e Zd ZdZ																		
	d0ddZdd Z																		d1ddZd2ddZd3ddZdd Z	dd Z
dd Zdd  Zd!d" Zd4d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ ZdS )5Diagrama  Diagram container.

    Arguments:
     - name           - a string, identifier for the diagram.
     - tracks         - a list of Track objects comprising the diagram.
     - format         - a string, format of the diagram 'circular' or
       'linear', depending on the sort of diagram required.
     - pagesize       - a string, the pagesize of output describing the ISO
       size of the image, or a tuple of pixels.
     - orientation    - a string describing the required orientation of the
       final drawing ('landscape' or 'portrait').
     - x              - a float (0->1), the proportion of the page to take
       up with even X margins t the page.
     - y              - a float (0->1), the proportion of the page to take
       up with even Y margins to the page.
     - xl             - a float (0->1), the proportion of the page to take
       up with the left X margin to the page (overrides x).
     - xr             - a float (0->1), the proportion of the page to take
       up with the right X margin to the page (overrides x).
     - yt             - a float (0->1), the proportion of the page to take
       up with the top Y margin to the page (overrides y).
     - yb             - a float (0->1), the proportion of the page to take
       up with the bottom Y margin to the page (overrides y).
     - circle_core    - a float, the proportion of the available radius to
       leave empty at the center of a circular diagram (0 to 1).
     - start          - an integer, the base/aa position to start the diagram at.
     - end            - an integer, the base/aa position to end the diagram at.
     - tracklines     - a boolean, True if track guidelines are to be drawn.
     - fragments      - and integer, for a linear diagram, the number of equal
       divisions into which the sequence is divided.
     - fragment_size  - a float (0->1), the proportion of the space
       available to each fragment that should be used in drawing.
     - track_size     - a float (0->1), the proportion of the space
       available to each track that should be used in drawing with sigils.
     - circular       - a boolean, True if the genome/sequence to be drawn
       is, in reality, circular.

    NcircularA3	landscape皙?F
         ?T        c                 C   s   i | _ || _|| _|| _|| _|| _|| _|| _|| _|	| _	|
| _
|| _|| _|| _|| _|dur5|| _n| jdkr>d| _nd| _|| _|| _|| _g | _d| _dS )z6Initialize.

        gdd = Diagram(name=None)
        Nr   g?)tracksnameformatpagesizeorientationxyxlxrytybstartend
tracklines	fragmentsfragment_size
track_sizer   circle_corecross_track_linksdrawing)selfr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r   r&   r   r   r   __init__V   s2   

zDiagram.__init__c                 C   s*   | j  D ]}t||rt||| qdS )zSet the passed attribute of all tracks in the set to the passed value.

        Arguments:
         - attr    - An attribute of the Track class.
         - value   - The value to set that attribute.

        set_all_tracks(self, attr, value)
        N)r   valueshasattrsetattr)r)   attrvaluetrackr   r   r   set_all_tracks   s
   	
zDiagram.set_all_tracksc                 C   sZ  |dkrTt | t|| jt|| jt|| jt|| jt|| jt|| jt|| jt|	| j	t|
| j
t|| jt|| jt|| jt|| jt|| jt|| j}nOt| t|| jt|| jt|| jt|| jt|| jt|| jt|| jt|	| j	t|
| j
t|| jt|| jt|| jt|| jt|| jt|| j}|  |j| _dS )zvDraw the diagram, with passed parameters overriding existing attributes.

        gdd.draw(format='circular')
        linearN)r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r'   r   r   r&   drawr(   )r)   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r   r&   r'   drawerr   r   r   r3      sN   





























zDiagram.drawtest1.psPSH   c                 C   s   t | j|||dS )a  Write the drawn diagram to a specified file, in a specified format.

        Arguments:
            - filename   - a string indicating the name of the output file,
              or a handle to write to.
            - output     - a string indicating output format, one of PS, PDF,
              SVG, or provided the ReportLab renderPM module is installed, one
              of the bitmap formats JPG, BMP, GIF, PNG, TIFF or TIFF.  The
              format can be given in upper or lower case.
            - dpi        - an integer. Resolution (dots per inch) for bitmap formats.

        Returns:
            No return value.

        write(self, filename='test1.ps', output='PS', dpi=72)

        )dpi)r   r(   )r)   filenameoutputr8   r   r   r   write   s   zDiagram.writec                 C   s(   ddl m} | }| ||| | S )a  Return a byte string containing the diagram in the requested format.

        Arguments:
            - output    - a string indicating output format, one of PS, PDF,
              SVG, JPG, BMP, GIF, PNG, TIFF or TIFF (as specified for the write
              method).
            - dpi       - Resolution (dots per inch) for bitmap formats.

        Returns:
            Return the completed drawing as a bytes string in a prescribed
            format.

        r   )BytesIO)ior<   r;   getvalue)r)   r:   r8   r<   handler   r   r   write_to_string   s   zDiagram.write_to_stringc                 C   s|   |du rt d|| jvr|| j|< n#t|  }|  |D ]}||jkr0| j| | j|d < q|| j|< || j| _dS )a  Add a Track object to the diagram.

        It also accepts instructions to place it at a particular level on the
        diagram.

        Arguments:
            - track          - Track object to draw.
            - track_level    - an integer. The level at which the track will be
              drawn (above an arbitrary baseline).

        add_track(self, track, track_level)
        NzMust specify trackr   )
ValueErrorr   sorted
get_levelsreversetrack_level)r)   r0   rE   occupied_levelsvalr   r   r   	add_track  s   


zDiagram.add_trackc                 K   s   t  }|D ]
}t||||  q|| jvr|| j|< n"t|  }|  |D ]}||kr7| j| | j|d < q'|| j|< || j| _|S )a3  Add a new Track to the diagram at a given level.

        The track is returned for further user manipulation.

        Arguments:
            - track_level   - an integer. The level at which the track will be
              drawn (above an arbitrary baseline).

        new_track(self, track_level)
        r   )r   r-   r   rB   rC   rD   rE   )r)   rE   r	   newtrackkeyrF   rG   r   r   r   	new_track+  s    

zDiagram.new_trackc                 C   s   | j |= dS )zRemove the track to be drawn at a particular level on the diagram.

        Arguments:
            - track_level   - an integer. The level of the track on the diagram
              to delete.

        del_track(self, track_level)
        Nr   )r)   rE   r   r   r   	del_trackH  s   	zDiagram.del_trackc                 C   s   t | j S )z5Return a list of the tracks contained in the diagram.)listr   r+   r)   r   r   r   
get_tracksS  s   zDiagram.get_tracksc                 C   s"   | j | }| j |= | || dS )zMove a track from one level on the diagram to another.

        Arguments:
         - from_level   - an integer. The level at which the track to be
           moved is found.
         - to_level     - an integer. The level to move the track to.

        N)r   rH   )r)   
from_levelto_levelauxr   r   r   
move_trackW  s   
	zDiagram.move_trackr   c                 C   sD   |}|   }i }|D ]}| j| ||< ||| _||7 }q
|| _dS )a  Renumber all tracks consecutively.

        Optionally from a passed lowest number.

        Arguments:
         - low     - an integer. The track number to start from.
         - step    - an integer. The track interval for separation of
           tracks.

        N)rC   r   rE   )r)   lowstepr0   levels
conversionlevelr   r   r   renumber_tracksd  s   


zDiagram.renumber_tracksc                 C   s
   t | jS )zAReturn a sorted list of levels occupied by tracks in the diagram.rB   r   rO   r   r   r   rC   y     
zDiagram.get_levelsc                    s   t  fdd jD S )zlReturn a sorted list of levels occupied by tracks.

        These tracks are not explicitly hidden.
        c                 3   s     | ]} j | js|V  qd S )N)r   hide).0rJ   rO   r   r   	<genexpr>  s    z+Diagram.get_drawn_levels.<locals>.<genexpr>r[   rO   r   rO   r   get_drawn_levels}  s   zDiagram.get_drawn_levelsc                 C   sJ   g g }}| j  D ]}| \}}|| || q
t|t|fS )zgReturn lowest and highest base numbers from track features.

        Returned type is a tuple.
        )r   r+   rangeappendminmax)r)   lowshighsr0   rU   highr   r   r   ra     s   

zDiagram.rangec                 C   s
   | j | S )z:Return the track contained at the level of the passed key.rL   )r)   rJ   r   r   r   __getitem__  r\   zDiagram.__getitem__c                 C   s`   d| j  d| j dg}|dt| j  |  D ]}|d|| j| f  qd|}|S )z1Return a formatted string describing the diagram.z
<z: >z	%d trackszTrack %d: %s

)	__class__r   rb   lenr   rC   join)r)   outstrrY   r   r   r   __str__  s   
zDiagram.__str__)Nr   r   r   r   r   NNNNNNFr   Nr   Tr   )NNNNNNNNNNNNNNNNNN)r5   r6   r7   )r6   r7   )r   r   )__name__
__module____qualname____doc__r*   r1   r3   r;   r@   rH   rK   rM   rP   rT   rZ   rC   r`   ra   rh   ro   r   r   r   r   r   .   sl    )
8

F

r   )rs   reportlab.graphicsr   ImportErrorBio.Graphicsr   _CircularDrawerr   _LinearDrawerr   _Trackr   r   r   r   r   r   r   <module>   s   
