ó
ůxTc           @   se   d  d l  m Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d	   Z	 d
   Z
 d S(   i˙˙˙˙(   t   divisionc         C   s  d d l  } d d d g } d d d g } g  | D] } | j |  ^ q1 } |  d d d  } d } d	 } xt |  D] \ }	 }
 |
 j |  } | s qx n  | j   } t |  d
 k rd | d	 d d d  } | d d d d  } | d d d d  | |	 } n< d } | d	 d d d  } | d d d d  | |	 } | d j g  | D] } d ^ q] | } Pqx W| t |  f S(   s   Try a bunch of templates to work out the most sensible. N.B. assumes
  that the image index will be the last digits found in the file name.i˙˙˙˙Ns   ([0-9]+)\.(.*)s   (.*)\.([0-9]+)_(.*)s   (.*)\.([0-9]+)(.*)t   .t   _t    i    i   i   i   t   #(	   t   ret   compilet   Nonet	   enumeratet   matcht   groupst   lent   joint   int(   t   filenameR   t   patternst   joinerst   patternt   compiled_patternst	   rfilenamet   templatet   digitst   jt   cpR	   R
   t   extent   prefixt   d(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   template_regex   s0    	""*c         C   s    d d l  m } g  } xM |  D]E } t |  } | d d k rU | j | d f  q | j |  q W| t  } x' | D] } | | d j | d  qy W| S(   s  Group filenames by supposed imageset.

  Get the template for each file in the list. Then add to a dictionary
  indexed by template containing a list of indices within that template.
  For files that do not match a template, these are added by filename
  instead.

  i˙˙˙˙(   t   defaultdicti    i   N(   t   collectionsR   R   R   t   appendt   list(   t	   filenamesR   R   t   ft   tt   matched(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   group_files_by_imageset.   s    	c         C   s  d d l  } | j j |   \ } } | d k s< | d k rE d } n  t |  \ } } | r| j d  } | j d  d } | j d  d } | d | | }	 | j |  }
 g  } xZ t d d |  D]9 } |	 | |
 k rÍ | j | j j	 | |	 |   qÍ qÍ Wn	 |  g } | S(	   sl   Search in the directory in which this image is for images which share
  the same template: return this list.i˙˙˙˙NR   R   R   i    s   %%0%ddi
   (
   t   ost   patht   splitR   R   t   countt   listdirt   rangeR   R   (   t
   image_nameR%   t	   directoryR   R   R   t
   len_digitst   pfxt   sfxt   template_strt   files_in_directoryt   matching_imagesR   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   find_matching_imagesN   s"    	*	c         C   s   d t  |  j d  d  S(   s$   Replace the format match with hashesR   i    (   R   t   group(   R	   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt!   replace_template_format_with_hashm   s    c         C   s   d d l  } | j d t |   S(   s3   Convert the template format to the template string.i˙˙˙˙Ns	   %0[0-9]+d(   R   t   subR5   (   R   R   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   template_format_to_stringq   s    c         C   sD   |  j  d  d } |  j  d  d } d | d |  j d  | f S(   s*   Convert the template to a glob expression.R   i    i˙˙˙˙s   %s%s%ss   [0-9](   R'   R(   (   R   R.   R/   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   template_string_to_glob_exprv   s    c         C   sI   |  j  d  d } |  j  d  d } t |  t |  |  j d  f S(   s$   Get the number idex of the template.R   i    i˙˙˙˙(   R'   R   R(   (   R   R.   R/   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   template_string_number_index|   s    c         C   s    d d l  m  } | t |    S(   s#   Return all files matching template.i˙˙˙˙(   t   glob(   R:   R8   (   R   R:   (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt%   locate_files_matching_template_string   s    c         C   s   t  |   } t |  } t |  d k rB t d j |     n  t t |     } t | d |  } t | d |  } | | f S(   s3   Return the image range of files with this template.i    s%   Template {0} doesn't match any files.i˙˙˙˙(   R;   t   sortedR   t
   ValueErrort   formatt   sliceR9   R   (   R   R    t   indext   firstt   last(    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   template_image_range   s    	N(   t
   __future__R    R   R$   R3   R5   R7   R8   R9   R;   RC   (    (    (    sC   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/dxtbx/sweep_filenames.pyt   <module>   s   	+	 						