ó
¤>dc           @   sL   d  Z  d d l Z d d l m Z d d l m Z d e f d     YZ d S(   s;    Helper class for working with segmentation type analyses.
i˙˙˙˙N(   t   BaseTool(   t   EventDetectionToolst   SegmentationToolsc           B   sV   e  Z d  Z d Z d Z d   Z e d  Z e d  Z d   Z	 d   Z
 d   Z RS(	   s@    Provides helper methods specific to segmentation analyses.
    t   Segmentationt   segmentationc         C   s  |  j    } | d k r/ i t d 6t d 6} nęi  } d | k rX t | d  | d <n  | d d k rn t n t | d <d | k r t | d  | d <n  | d d k rą t n t | d <t } | d rd | k rö | d | d <| d | d	 <n  d
 | k rt } qn  | d r[d | k rC| d | d <| d | d <n  d | k r[t } q[n  | rq|  j |  n  | d rĹ| d
 | d
 <| d | d <d | k rĹ| d | d <| d	 | d	 <qĹn  | d r| d | d <| d | d <d | k r| d | d <| d | d <qn  | S(   sŻ   Returns the segmentation summary data.
        
        This data is normalized, to eliminate differences in what is stored
        for different types of segmentation analyses.
        
        The following fields are output:

        * has_template - True if the segmentation found template data.
        * has_complement - True if the segmentation found complement data.
        * first_sample_template - The first sample of the template data in
            the raw data. Only present if has_template is True.
        * duration_template - The duration (in samples) of the template
            data. Only present if has_template is True.
        * first_sample_complement - The first sample of the complement data
            in the raw data. Only present if has_complement is True.
        * duration_complement - The duration (in samples) of the complement
            data. Only present if has_complement is True.
            
        t   has_templatet   has_complementt   num_tempi    t   num_compt   start_index_tempt   start_event_templatet   end_index_tempt   end_event_templatet   first_sample_templatet   start_index_compt   start_event_complementt   end_index_compt   end_event_complementt   first_sample_complementt   duration_templatet   duration_complementN(   t   _get_summary_datat   Nonet   Falset   boolt   Truet   _get_raw_info(   t   selft   summaryt   resultst   need_raw_info(    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyt   get_results   sN    
  



c         C   s[  | d k r$ t  d j |    n  |  j   } | d k rX d | d k rQ d n d f S| d k rs d d g } n	 | g } |  j   \ } } t |  j d |  } | j d |  \ } } Wd QXd d g }	 xj t |  D]\ \ }
 } | d j |  sqÝ n  | d	 j |  } | d
 j |  } | | | !|	 |
 <qÝ W| d k rSt	 |	  S|	 d S(   s   Get the template or complement event data.
        
        :param section: Either template, complement, or both.
        :param time_in_seconds: Return the start and length fields
            in seconds, rather than samples.
        :return: The event dataset for the section. If section=both
            then it returns a tuple with both sections. Returns None
            if the section does not exist.
        t   templatet
   complementt   bothsE   Unrecognized section: {} Expected: "template", "complement" or "both"t
   group_namet   time_in_secondsNs   has_{}s   start_event_{}s   end_event_{}i    (   R    R!   R"   (
   t	   Exceptiont   formatR   R   t   _find_event_dataR   t   handlet   get_event_datat	   enumeratet   tuple(   R   t   sectionR$   R   t   sectionst   evdet_groupt   _t   evdett
   event_datat   datasetst   nt   this_sectiont   ev1t   ev2(    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyR)   N   s*    
	
c   
      C   sß   |  j    } d d g } | d k r3 d d g } n	 | g } x t |  D]t \ } } | d j |  sn qI n  | d j |  } | d j |  }	 |  j j d | d | |	 d	 |  | | <qI W| d k r× t |  S| d
 S(   sY   Get the template or complement raw data.
        
        :param section: Either template, complement, or both.
        :param scale: Scale the raw data to pA.
        :return:  The raw data for the section. If section=both
            then it returns a tuple with both sections. Returns None
            if the section does not exist.
        R"   R    R!   s   has_{}s   first_sample_{}s   duration_{}t   startt   endt   scalei    N(   R   R   R*   R&   R(   t   get_raw_dataR+   (
   R   R,   R9   R   R2   R-   R3   R4   R7   t   dur(    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyR:   o   s    		-
c         C   sa   |  j  j |  j  } | d  k r% d  Sd | k r> | d } n d | k rW | d } n d  } | S(   NR   t   split_hairpin(   R(   t   get_summary_dataR#   R   (   R   R   R   (    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyR      s    c         C   s˛   |  j  j |  j  } | j d  } | d  k rE |  j  j d  } n
 | d } | d  k r_ d  S|  j  j j d } | j } d j	 | |  } |  j  j
 | d d t } | | f S(   Nt   event_detectiont   EventDetectioni	   i    s   {}/Reads/Read_{}t   Eventst   skip_decoding(   R(   t   get_analysis_attributesR#   t   getR   t   get_latest_analysist   statust	   read_infot   read_numberR&   t   get_analysis_datasetR   (   R   t   attrsR.   RF   RG   t   event_table_groupt   dataset(    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyR'      s    
	c         C   sg  |  j    \ } } |  j j j d } | d  k r] d  | d <d  | d <d  | d <d  | d <d  S| j d d  d k rŕ | d } | d } | | d	 | j | d <| | d
 d	 | | d
 d | j } | | d | d <n  | j d d  d k rc| d } | d } | | d	 | j | d <| | d
 d	 | | d
 d | j } | | d | d <n  d  S(   Ni    R   R   R   R   R
   i˙˙˙˙R   R7   i   t   lengthR   R   (   R'   R(   RE   RF   R   RC   t
   start_time(   R   R   R/   RK   RF   R5   R6   R8   (    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyR   ­   s(    





)

)(   t   __name__t
   __module__t   __doc__t   group_idt   analysis_idR   R   R)   R:   R   R'   R   (    (    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyR   	   s   	?! 		(   RP   t   numpyt   npt&   ont_fast5_api.analysis_tools.base_toolR    t,   ont_fast5_api.analysis_tools.event_detectionR   R   (    (    (    sR   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/analysis_tools/segmentation.pyt   <module>   s   