ó
‚™xTc           @   s˜   d  d l  m Z d  d l m Z m Z m Z d  d l Z d  d l Z d Z d
 d d d „ Z
 d d d „ Z d „  Z e d k r” e
 e j d	 ƒ n  d S(   iÿÿÿÿ(   t   division(   t   Sorryt   Usaget   null_outNs_  
split_models
  .short_caption = Split multi-model PDB file
  .caption = This utility will separate a multi-model PDB file (such as an     NMR ensemble) into individual files for each model.  The output files     will be named similarly to the input file but ending in _1.pdb, _2.pdb,     etc.
  .style = auto_align box caption_img:icons/custom/iotbx.pdb.join_fragment_files.png
{
  file_name = None
    .type = path
    .short_caption = PDB file
    .style = file_type:pdb input_file bold
  output_dir = None
    .type = path
    .short_caption = Output directory
    .style = directory default_cwd bold
}
c         C   så  | d  k r t j } n  | d  k r” t |  ƒ d k rE t d ƒ ‚ n  d d  l } | j j d |  d t d d d d	 ƒ } | j	 j
 ƒ  } t | ƒ n  d d
 l m } | j | j j d d ƒ} | j d ƒ | j j } t | j ƒ  ƒ d k r	t d | j j ƒ ‚ n  t j j | j j ƒ } | j d ƒ r=| d  } n | j d ƒ rY| d  } n  t j j | ƒ d }	 | j j d  k r–t j ƒ  | j _ n  t j j | j j |	 ƒ }
 t d | d | j j ƒ  d |
 d | j j d | ƒ S(   Ni    s   
iotbx.pdb.split_models ensemble.pdb [output_dir=/path/...]

Splits a multi-model PDB file into separate files for each model.
iÿÿÿÿt   argst   master_phil_stringt   pdb_file_defs   split_models.file_namet   directory_defs   split_models.output_dir(   t   file_readert
   force_typet   pdbi   s+   The PDB file %s already has a single model.s   .gziýÿÿÿs   .Ziþÿÿÿt	   hierarchyt   crystal_symmetryt   output_baset   original_filet   log(   t   Nonet   syst   stdoutt   lenR   t
   iotbx.philt   philt   process_command_line_with_filest   master_philt   workt   extractt   validate_paramst   iotbxR   t   any_filet   split_modelst	   file_namet   check_file_typet   file_objectR   t   modelsR   t   ost   patht   basenamet   endswitht   splitextt
   output_dirt   getcwdt   joinR   (   R   t   paramst   outR   t   cmdlineR   t   pdb_inR   t   pdb_rel_patht	   base_nameR   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/pdb_split_models.pyt   run   sH    	c         C   s  | d  k r t ƒ  } n  d d  l } t |  j ƒ  ƒ } g  } x<t |  j ƒ  ƒ D](\ } }	 | d 7} | j j j ƒ  }
 |
 j	 |	 j
 ƒ  ƒ |	 j d k r¨ t | ƒ } n |	 j j ƒ  } d | | f } t | d ƒ } | d  k	 r| | j j d | d t ƒ IJn  | d | | f IJ| d  k	 r?| d	 IJ| d
 | IJn  | j |
 j ƒ  ƒ | j ƒ  | j | ƒ | d | IJqO W| S(   Niÿÿÿÿi   t    s	   %s_%s.pdbt   wR   t   write_scale_recordss   REMARK Model %d of %ds   REMARK Original file:s   REMARK   %ss   Wrote %s(   R   R   t   iotbx.pdb.hierarchyR   R!   t	   enumerateR
   R   t   roott   append_modelt   detached_copyt   idt   strt   stript   opent   format_cryst1_and_scale_recordst   Truet   writet   as_pdb_stringt   closet   append(   R   R   R   R   R   R   t   n_modelst
   file_namest   kt   modelt   new_hierarchyt   model_idt   output_filet   f(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/pdb_split_models.pyR   E   s6     


c         C   s    |  j  j d  k r! t d ƒ ‚ n1 t j j |  j  j ƒ sR t d |  j  j ƒ ‚ n  |  j  j d  k	 rœ t j j |  j  j ƒ sœ t d d |  j  j ƒ ‚ qœ n  t	 S(   Ns#   Please specify a PDB file to split!s2   The PDB file '%s' does not exist or is not a file.s9   The specified output directory '%s' does not exist or is s   not a directory.(
   R   R   R   R   R"   R#   t   isfileR'   t   isdirR>   (   R*   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/pdb_split_models.pyR   f   s    t   __main__i   (    (   t
   __future__R    t   libtbx.utilsR   R   R   R"   R   R   R   R0   R   R   t   __name__t   argv(    (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/command_line/pdb_split_models.pyt   <module>   s   ,	