ó
¤>dc           @   sè   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m	 Z	 d d l
 m Z d d l m Z m Z d d l m Z m Z d d l m Z m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s8    Basic API for reading/writing single-read fast5 files.
iÿÿÿÿN(   t   CURRENT_FAST5_VERSION(   t   VBZt   raise_missing_vbz_error_read(   t	   Fast5Read(   t	   Fast5Infot   ReadInfo(   t   supported_modest   mode_docstring(   t   _sanitize_data_for_readingt   _sanitize_data_for_writingt   Fast5FileTypeErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR
      s   t	   Fast5Filec           B   sÔ   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z	 e d „  ƒ Z
 e d	 „  ƒ Z e d
 „ Z d d d e d „ Z d e d „ Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z RS(   s;   This object encapsulates a read fast5 file. It can be used
    instead of directly using the h5py API in order to help maintain
    consistency in fast5 file format, and simplify common tasks.

    The object will contain a field called **status** that is a
    Fast5Status object with details about the file.
    t   rc         C   sY   d |  _  | t k r0 t d j | t ƒ ƒ ‚ n  | |  _ d |  _ | |  _ |  j	 ƒ  d S(   s“    Constructor. Opens the specified file.

        :param fname: Filename to open.
        :param mode: File open mode (r, r+, w, w-, x, a).
        s   UniqueGlobalKey/s&   Unsupported file handle mode : '{}' {}N(
   t
   global_keyR   t   IOErrort   formatR   t   filenamet   Nonet   handlet   modet   _initialise_file(   t   selft   fnameR   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   __init__   s    				c         c   s   |  j  |  j ƒ  ƒ Vd  S(   N(   t   get_readt   get_read_id(   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt	   get_reads,   s    c         C   s   |  j  ƒ  g S(   N(   R   (   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   get_read_ids0   s    c         C   s7   | |  j  ƒ  k r3 t d j | |  j  ƒ  ƒ ƒ ‚ n  |  S(   Ns4   read_id given: {} does not match read_id in file: {}(   R   t   KeyErrorR   (   R   t   read_id(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR   3   s    	c         C   s   |  j  j d j S(   Ni    (   t   statust	   read_infoR   (   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR   9   s    c         C   s   t  d ƒ |  j S(   NsP   'read.get_read_id()' will be deprecated. Use the property 'read.read_id' instead(   t   DeprecationWarningR   (   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR   =   s    
c         C   s   |  j  ƒ  } d j | ƒ S(   Ns   Raw/Reads/Read_{}(   t   _get_only_read_numberR   (   R   t   read_number(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   raw_dataset_group_nameA   s    c         C   s   d |  j  |  j d  k S(   Nt   context_tagsiÿÿÿÿ(   R   R   (   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   has_context_tagsF   s    c         C   s(   |  j  ƒ  |  j |  j d | | ƒ d S(   s	   Add channel info data to the channel_id group.

        :param data: A dictionary of key/value pairs. Keys must be strings.
            Values can be strings or numeric values.
        :param clear: If set, any existing channel info data will be removed. 
        t
   channel_idN(   t   assert_writeablet   _add_attributesR   (   R   t   datat   clear(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   add_channel_infoJ   s    
c         C   sY   |  j  ƒ  |  j |  j k r@ d j | |  j ƒ } t | ƒ ‚ n  |  j |  j | | | ƒ S(   s†   Pull raw data from the file.
        
        :param read_number: The read number you want raw data from. Pass None
            if there is only one read, and will grab that read's raw data.
        :param start: The first sample to pull. Default is to pull from the
            beginning.
        :param end: One past the last sample to pull. Default is to pull until
            the end.
        :param scale: If set, the returned data will be scaled floating point
            values, in pA. Otherwise raw DAQ values are returned as 16 bit
            integers.
        :returns: Raw data as either 32 bit floats, or 16 bit integers.
        s,   Fast5 file has no raw data for read {} in {}(   t   assert_opent   raw_dataset_nameR   R   R   R   t	   _load_raw(   R   R$   t   startt   endt   scalet   msg(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   get_raw_dataT   s
    
c         C   s  |  j  ƒ  |  j |  j k rC d } t | j |  j |  j ƒ ƒ ‚ n  |  j } | |  j k rn |  j j | ƒ n  y- |  j | j d d | d d t	 | ƒ Wn t
 k
 rº } t | ƒ n X|  j j |  j ƒ  } t |  j j | _ | d k	 r	|  j | | d t ƒn  d S(   s   Add raw data for a read.
        
        :param read_number: The number of the read the raw data is for.
        :param data: The raw data DAQ values (16 bit integers).
        
        The read must already exist in the file. It must not already
        have raw data.
        s1   Fast5 file already has raw data for read {} in {}t   SignalR+   t   dtypet   i2R,   N(   R)   R/   R   R   R   R   R%   t   create_groupt   create_datasett   varst
   ValueErrorR   R    t   read_number_mapR#   t   TrueR!   t   has_raw_dataR   R*   (   R   R+   t   attrst   compressionR4   t
   group_namet   et
   read_index(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   add_raw_datah   s    	
!	-c      	   C   sÁ   |  j  ƒ  t | | | | d | d | ƒ} |  j j j | ƒ t |  j j ƒ d } | |  j j | <| |  j j | <|  j }	 i | d 6| d 6| d 6| d 6| d 6| d 6}
 |  j	 |	 |
 ƒ d	 S(
   sm   Add a new read to the file.

        :param read_number: The read number to assign to the read.
        :param read_id: The unique read-id for the read.
        :param start_time: The start time (in samples) of the read.
        :param duration: The duration (in samples) of the read.
        :param mux: The mux set at the time of the read.
        :param median_before: The median level of the data before the read.

        Note that most tools assume a file contains only one read.
        Putting multiple reads into a file severely limits the
        ability to operate on those reads with standard tools.
        t   muxt   median_beforei   R$   R   t
   start_timet   durationt	   start_muxN(
   R)   R   R    R!   t   appendt   lenR=   t   read_id_mapR%   t
   _add_group(   R   R$   R   RH   RI   RF   RG   R!   t   nRB   R@   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   add_read…   s    
!	

c         C   s“  t  |  ƒ } | j s- t d j |  ƒ ƒ ‚ n  t j |  d ƒ N} x› | j D] } d j | j ƒ } | | k r} | | } n | j | ƒ } | j | j	 d <| j
 | j	 d <| j | j	 d <| j | j	 d <| j | j	 d <qL Wd	 | k rü | j d	 ƒ n  d
 | d k r| j d ƒ n  | j d k  r|d | k r|| d } | j ƒ  } x)| D]} | | }	 |	 j	 d }
 | j | j |
 } | j
 |	 j	 d <d |	 k rT|	 d } d | j j k rr|	 d d } t j | j d d t f d t f d t f d t f g ƒ} | d | d <t j | d ƒ | d <| d | d <| d | d <|	 d =|	 j d d | d d ƒqrqTqTWq|n  t | j	 d <Wd QXd S(   s‡    Update a fast5 file from an older version to the new standard.
        
        :param fname: The filename of the fast5 file.
        s   Cannot update invalid file: {}s   r+s   Raw/Reads/Read_{}R$   R   RI   RH   RJ   t   Analysest   tracking_idt   UniqueGlobalKeys   UniqueGlobalKey/tracking_idgš™™™™™ñ?s   Analyses/EventDetection_000s!   Analyses/EventDetection_000/Readst   Eventst   varianceR7   t   meant   stdvR1   t   lengthR+   RA   t   gzipt   file_versionN(    (   R   t   validR   R   t   h5pyt   FileR!   R$   R9   R@   R   RI   RH   RJ   t   versiont   keysR=   R7   t   namest   npt   emptyt   sizet   floatt   intt   sqrtR:   R    (   R   R    R   R!   t   read_group_namet   rght   reads_groupt   data_group_namest   data_group_namet
   read_groupR$   t   datasett   old_datat   new_data(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   update_legacy_file¨   sP    	


!)c      	   C   s6  i  } t  |  d d ƒ} | j ƒ  | d <| j ƒ  | d <| j j } g  } xM | D]E } | j i | j d 6| j d 6| j d 6| j	 d 6| j
 d	 6ƒ qT W| | d
 <| j | ƒ } t | Œ  \ } }	 t |	 ƒ }	 |	 d }
 | j |
 ƒ | d <|
 d  | d d <| j |
 ƒ | d <t j j |  ƒ | d <Wd QX| S(   s	   Read summary data suitable to encode as a json packet.

        :param fname: The fast5 file to pull the summary data from.
        :param component: The component name to pull summary data for.

        :returns: A dictionary containing the summary data.
        R   R   RR   R(   R$   R   RH   RI   RJ   t   readsiÿÿÿÿt   softwareiüÿÿÿt	   componentR+   R   N(   R   t   get_tracking_idt   get_channel_infoR    R!   RK   R$   R   RH   RI   RJ   t   list_analysest   zipt   sortedt   get_analysis_attributest   get_summary_datat   ost   patht   basename(   R   Rs   t   summaryt   fhR!   t   read_summaryt   readt   analyses_listt   _t   group_namest   group(    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   read_summary_dataß   s,    	




c         C   s   |  j  j d j } | S(   Ni    (   R    R!   R$   (   R   R$   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR#     s    c         C   s  yß |  j  d k r¢ t j |  j |  j  ƒ j } t | j d <| j d ƒ | j d ƒ | j |  j d ƒ | j |  j d ƒ | j |  j d	 ƒ Wd  QXd
 |  _  n  t |  j ƒ |  _	 |  j	 j
 rÞ t j |  j |  j  ƒ |  _ n  Wn) t k
 r
t d j |  j ƒ ƒ ‚ n Xd  S(   Nt   ws   w-t   xRZ   RQ   s	   Raw/ReadsR(   R&   RR   s   r+s0   Failed to initialise single-read Fast5File: '{}'(   R‡   s   w-Rˆ   (   R   R\   R]   R   R    R@   R9   R   R   R    R[   R   t	   ExceptionR
   R   (   R   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR   
  s    "N(   R   R   t   __doc__R   R   R   R   t   propertyR   R   R%   R'   t   FalseR-   R   R5   R   RE   RP   t   staticmethodRp   R†   R#   R   (    (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR      s"   				
	#7'	t
   EmptyFast5c           B   s   e  Z d  „  Z RS(   c         C   s/   t  j |  j |  j ƒ |  _ t |  j j d <d  S(   NRZ   (   R\   R]   R   R   R   R    R@   (   R   (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyR     s    (   R   R   R   (    (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyRŽ     s   (   RŠ   R{   R\   t   numpyRa   t   ont_fast5_apiR    t"   ont_fast5_api.compression_settingsR   R   t   ont_fast5_api.fast5_readR   t   ont_fast5_api.fast5_infoR   R   t   ont_fast5_api.static_dataR   R   t   ont_fast5_api.data_sanitisationR   R	   R<   R
   R   RŽ   (    (    (    sA   /tmp/pip-install-m8tpmx/ont-fast5-api/ont_fast5_api/fast5_file.pyt   <module>   s   ÿ 