ó
L¬ÃIc           @   s#  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z y d d l m	 Z	 Wn e d ƒ ‚ n Xd d l
 m Z d d l m Z d& d' d e	 j d f d e	 j d f d e	 j d f d e	 j d	 f d e	 j d	 f d e	 j d	 f d( f	 Z d) d* d e	 j d	 f d e	 j d	 f d e	 j d	 f d e	 j d f d e	 j d f d e	 j d f d+ d e	 j d f d e	 j d f d, d e	 j d	 f f Z d e	 j d f d e	 j d	 f d e	 j d	 f d e	 j d	 f d e	 j d	 f f Z d- d. d e	 j d f d e	 j d	 f d e	 j d f d e	 j d	 f d e	 j d	 f d e	 j d	 f f Z i e d 6e d 6e d 6e d 6Z d „  Z e	 j e	 j f Z d Z d Z d Z d Z d „  Z d „  Z d e j f d  „  ƒ  YZ  d! e j f d" „  ƒ  YZ! e" d# k re j# d$ d ƒ Z$ e j% d% e$ ƒ n  d S(/   sÕ   Test the creation of Chromosome Graphics.

Provide tests for creating and manipulating pdf pictures of chromosomes.

This tests the Graphics.BasicChromosome classes and the
Graphics.DisplayRepresentation classes.
iÿÿÿÿN(   t   MissingExternalDependencyError(   t   colorss2   Install reportlab if you want to use Bio.Graphics.(   t   BasicChromosome(   t   ChromosomeCountst    i   t   AC30001i   g      ø?t   AC12345t   AC23456t   AC00002t   AB0001t   AD00002t   AD11111t   AC22222t   It   IIt   IIIt   IVc         C   së   t  j |  ƒ } t |  } xÂ t t | ƒ ƒ D]® } | | \ } } } | d k r` t  j ƒ  } n4 | t | ƒ d k rˆ t  j d ƒ } n t  j ƒ  } | d k r¬ | | _ n  | d k	 rÄ | | _	 n  | | _
 | j | ƒ q, Wd | _ | S(   s/   Load a chromosome and all of its segments.
    i    i   R   i   N(   R   t
   Chromosomet   all_chr_infot   ranget   lent   TelomereSegmentt   ChromosomeSegmentt   labelt   Nonet
   fill_colort   scalet   addt	   scale_num(   t   chr_namet   cur_chromosomet   chr_segment_infot   seg_info_numR   R   R   t   cur_segment(    (    s   test_GraphicsChromosome.pyt   load_chromosomeK   s"    
		t$   0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZiô  g333333Ó?gš™™™™™™?c          C   s:   d }  x- t  d ƒ D] } t j t ƒ } |  | 7}  q W|  S(   s!   Generate a random id number.
    R   i   (   R   t   randomt   choicet   letter_choices(   t   idt   nt   letter(    (    s   test_GraphicsChromosome.pyt   get_random_idq   s
    c   	      C   sõ   t  j |  ƒ } t j t ƒ } xÊ t | ƒ D]¼ } | d k rL t  j ƒ  } n. | | d k rn t  j d ƒ } n t  j ƒ  } t j ƒ  } | t k r­ t j	 t
 ƒ } | | _ n  t j ƒ  } | t k rÚ t ƒ  } | | _ n  | j | ƒ q+ W| | f S(   s<   Generate a chromosome with random information about it.
    i    i   (   R   R   R$   t	   randranget   num_possible_segmentsR   R   R   t
   color_probR%   t   color_choicesR   t   id_probR*   R   R   (	   R   R   t   num_segmentst   segR!   t   color_chanceR   t	   id_chanceR'   (    (    s   test_GraphicsChromosome.pyt   load_random_chromosome{   s$    	t   OrganismGraphicTestc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s9   Test the creation of all chromosomes of an organism.
    c         C   s   t  j j d d ƒ |  _ d  S(   Nt   Graphicss   organism.pdf(   t   ost   patht   joint	   test_file(   t   self(    (    s   test_GraphicsChromosome.pyt   setUpœ   s    c         C   sY   t  j ƒ  } x3 d d d d g D] } t | ƒ } | j | ƒ q W| j |  j d ƒ d S(   s=   Test the basic functionality of drawing an organism.
        R   R   R   R   s   Test organismN(   R   t   OrganismR"   R   t   drawR:   (   R;   t   pdf_organismR   R   (    (    s   test_GraphicsChromosome.pyt   test_simple_organismŸ   s
    c         C   sn   t  j | ƒ } t j j d | ƒ } x3 d d d d g D] } t | ƒ } | j | ƒ q7 W| j | d ƒ d S(   s)   Output a simple organism to given format.R6   R   R   R   R   s   Test organismN(   R   R=   R7   R8   R9   R"   R   R>   (   R;   t   filenamet   formatt   test_organismR:   R   R   (    (    s   test_GraphicsChromosome.pyt   _simple_organism¬   s    c         C   s   |  j  d d ƒ d S(   s7   Output a simple organism to a postscript file.
        s   organism.epst   psN(   RD   (   R;   (    (    s   test_GraphicsChromosome.pyt   test_simple_organism_ps·   s    c         C   s   |  j  d d ƒ d S(   s0   Output a simple organism to a PDF file.
        s   organism.pdft   pdfN(   RD   (   R;   (    (    s   test_GraphicsChromosome.pyt   test_simple_organism_pdf¼   s    c         C   s   |  j  d d ƒ d S(   s1   Output a simple organism to an SVG file.
        s   organism.svgt   svgN(   RD   (   R;   (    (    s   test_GraphicsChromosome.pyt   test_simple_organism_svgÁ   s    c         C   sÏ   t  j j d d ƒ } t j ƒ  } g  } g  } t j d d ƒ } xF t | ƒ D]8 } t t	 | ƒ ƒ \ } } | j
 | ƒ | j
 | ƒ qL Wt | ƒ }	 x$ | D] }
 |	 |
 _ | j |
 ƒ q› W| j | d ƒ d S(   s:   Generate an organism with random chromosome info.
        R6   s   random_organism.pdfi   i   s   Randomly generated OrganismN(   R7   R8   R9   R   R=   R$   R+   R   R4   t   strt   appendt   maxR   R   R>   (   R;   t   random_fileR?   t   all_segst   all_chrst   num_chrsR   R   t   num_segst   max_segst   chr(    (    s   test_GraphicsChromosome.pyt   test_random_organismÆ   s    	c         C   sé   t  j ƒ  } d } t j } t j ƒ  } | t _ | j ƒ  | j ƒ  } | t _ | j | ƒ d k st t	 d | ƒ ‚ | j
 ƒ  } | j d ƒ rŸ | d d k s¯ t	 d | ƒ ‚ | j i d d 6ƒ | j d k så t	 d	 | j ƒ ‚ d
 S(   s*   Try widget derived functionality.
        s   chr_percent = 0.25i    s,   Unexpected results from dumpProperties: 
 %st
   label_sizei   s)   Unexpected results from getProperties: %si   t   start_x_positions,   setProperties doesn't seem to work right: %sN(   R   R   t   syst   stdoutt	   cStringIOt   StringIOt   dumpPropertiest   getvaluet   findt   AssertionErrort   getPropertiest   has_keyt   setPropertiesRW   (   R;   t   test_widgett   expected_stringt   save_stdoutt
   new_stdoutt
   properties(    (    s   test_GraphicsChromosome.pyRc   Ý   s$    		
	(   t   __name__t
   __module__t   __doc__R<   R@   RD   RF   RH   RJ   RU   Rc   (    (    (    s   test_GraphicsChromosome.pyR5   ™   s   							t   ChromosomeCountTestc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sG   Test the display representation for simple counts on a chromosome.
    c         C   s.   d d d d d g |  _  t |  j  ƒ |  _ d  S(   Nt   Bobt   Dylans   Doesn'tt   Liket   Spam(   t   namesR   t   count_display(   R;   (    (    s   test_GraphicsChromosome.pyR<   þ   s    c         C   si   |  j  j |  j d ƒ |  j  j |  j d d ƒ y  |  j  j d ƒ t d ƒ ‚ Wn t k
 rd n Xd S(   s4   Add counts to specific chromosome segments.
        i   i   i   s   Non-existents%   Didn't raise a KeyError on a fake keyN(   Rq   t	   add_countRp   R_   t   KeyError(   R;   (    (    s   test_GraphicsChromosome.pyt   test_add_count  s    c         C   sU   |  j  j |  j d d ƒ y# |  j  j d d ƒ t d ƒ ‚ Wn t k
 rP n Xd S(   s+   Add labels to chromosome segments.
        i   t   Ruless   Non-existentt   Elephants%   Didn't raise a KeyError on a fake keyN(   Rq   t	   add_labelRp   R_   Rs   (   R;   (    (    s   test_GraphicsChromosome.pyt   test_add_label  s    c         C   sU   |  j  j |  j d d ƒ y# |  j  j d d ƒ t d ƒ ‚ Wn t k
 rP n Xd S(   s0   Set the scale for a chromosome segment.
        i   g      ø?s   Non-existanti   s&   Didn't raise a KeyError on a fake key.N(   Rq   t	   set_scaleRp   R_   Rs   (   R;   (    (    s   test_GraphicsChromosome.pyt   test_set_scale  s    c         C   sš   |  j  j d ƒ } | t j k s1 t d | ƒ ‚ |  j  j d ƒ } | t j k sb t d | ƒ ‚ y  |  j  j d ƒ t d ƒ ‚ Wn t k
 r• n Xd S(   sL   Retrieve a color from a count number with the default color scheme.
        i   s   Unexpected color %si	   iÈ   s$   Didn't raise an error for bad count.N(   Rq   t   _color_from_countR   t   blueR_   t   redt
   ValueError(   R;   t
   test_color(    (    s   test_GraphicsChromosome.pyt   test_color_from_count$  s    c         C   ss   t  j d ƒ } |  j j |  j d d ƒ |  j j |  j d d ƒ |  j j |  j d d ƒ |  j j | ƒ } d S(   s:   Test filling out the information on a chromosome.
        t   1i   i   i   i   s
   Test-LabelN(   R   R   Rq   Rr   Rp   Rw   t   fill_chromosome(   R;   t   test_chrt   new_chr(    (    s   test_GraphicsChromosome.pyt   test_fill_chromosome3  s
    c         C   sŸ   d } d } d } d } |  j  j |  j | | ƒ |  j  j |  j | | ƒ |  j  j ƒ  } | | d | k s{ t d ƒ ‚ | | d | k s› t d ƒ ‚ d S(	   s/   Test retrieval of segment information.
        i   i   i   t   BigBirdi    s*   Did not set and retrieve counts correctly.s)   Did not set and retrieve label correctly.N(   Rq   Rr   Rp   Rw   t   get_segment_infoR_   (   R;   t   test_count_numt   test_count_valuet   test_label_numt   test_label_valuet   seg_info(    (    s   test_GraphicsChromosome.pyt   test_get_segment_info=  s    	(
   Rh   Ri   Rj   R<   Rt   Rx   Rz   R€   R…   R   (    (    (    s   test_GraphicsChromosome.pyRk   û   s   						
t   __main__t	   verbosityt
   testRunner(   R   Ni   (   R   Ni   (   R   Ni   (   R   Ni   (   R   Ni   (   R   Ng      ø?(   R	   Ni   (   R   Ng      ø?(   R   Ni   (&   Rj   R7   RX   R$   RZ   t   unittestt   BioR    t   reportlab.libR   t   Bio.GraphicsR   t"   Bio.Graphics.DisplayRepresentationR   R   R|   R}   t	   chr1_infot	   chr2_infot   greent	   chr3_infot	   chr4_infoR   R"   R.   R&   R,   R-   R/   R*   R4   t   TestCaseR5   Rk   Rh   t   TextTestRunnert   runnert   main(    (    (    s   test_GraphicsChromosome.pyt   <module>   s‚   	

	 	
	bW