ó
©%Tc           @   sq   d  d l  Td   Z d   Z d   Z d a d   Z e e d  Z e e d  Z d	   Z	 d
   Z
 d   Z d S(   i’’’’(   t   *c         C   sE   d d  l  } | j j |   }  |  d } t |  |  t |  } | S(   Ni’’’’s   .cif(   t   ost   patht   normpatht   convert_shelx_fcf_to_cift   auto_read_cif_data_with_phases(   t   filenameR   t   output_mmCIF_file_namet   ret(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   handle_shelx_fcf_file_old   s    
c         C   s
   t  |   S(   N(   t   read_small_molecule_data_cif(   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   handle_shelx_fcf_file1   s    c   
      C   s=  d d  l  } d d  l } t } t } d } y t |  d  } Wn t k
 rY d G|  GHn Xy t | d  } Wn t k
 r d G| GHn X| r4| r4| j   } xx| D]p} d | k r¾ q© d | k rĶ q© d	 | k rÜ q© d
 | k rė q© d | k rś q© d | k r	q© d	 | k rq© d
 | k r'q© d | k r6q© d | k rW| j d d  } n  d | k rx| j d d  } n  d | k r| j d d  } n  d | k rŗ| j d d  } n  d | k rŪ| j d d  } n  d | k rü| j d d  } n  d | k r| j d d  } n  d | k r>| j d d  } n  d | k r_| j d d  } n  d | k r| j d d  } n  d | k r”| j d d  } n  d | k rĀ| j d d  } n  d | k rć| j d d  } n  d | k r| j d d   } n  d | k r%| j d d  } n  d | k rF| j d d  } n  d | k rg| j d d  } n  d | k r| j d d  } n  d | k r©| j d d  } n  d | k rŹ| j d d   } n  d! | k rė| j d! d"  } n  d# | k r| j d# d$  } n  d% | k r-| j d% d&  } n  d' | k rT| j d' d(  } d) } n  | d) k rt | j	 |   d* k r| j	 |  }	 t
 | j t |	 d+    |	 d+ <t |	 d+  d, k rt
 d- t |	 d.  d, t |	 d+   |	 d. <| j |	  d/ } qn  | j |  q© W| j   | j   n d0 GHd  S(1   Ni’’’’i    t   rs   BL ERROR:: Cannot read t   ws   BL ERROR:: Cannot write t   _shelx_titlet   _shelx_refln_list_codet   _shelx_F_calc_maximumt   _exptl_crystal_F_000t   _reflns_d_resolution_hight   _refln_F_squared_meass    _refln.F_meast   _refln_F_squared_sigmas    _refln.F_meas_sigmas    _symmetry_equiv_pos_as_xyzs   _symmetry_equiv.pos_as_xyzt   _cell_length_as   _cell.length_a     t   _symmetry_equiv_pos_as_xyzt   _cell_length_bs   _cell.length_b     t   _cell_length_cs   _cell.length_c     t   _cell_angle_alphas   _cell.angle_alpha  t   _cell_angle_betas   _cell.angle_beta   t   _cell_angle_gammas   _cell.angle_gamma  t   _refln_index_hs    _refln.index_ht   _refln_index_ks    _refln.index_kt   _refln_index_ls    _refln.index_lt   _refln_F_calcs    _refln.F_calct   _refln_phase_calcs    _refln.phase_calci   i   i   g        g      ą?i   s   
s"   BL ERROR:: IO error in conversion!(   t   stringt   matht   Falset   opent   IOErrort   filet	   readlinest   replacet   lent   splitt   strt   sqrtt   floatt   joint   writet   close(
   t   fcf_filenamet   cif_filenameR!   R"   t   fint   foutt   intablet   linest   linet   col(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyR   4   sø    	'#0
s   coot-shelxlc         C   s+   d d  l  } d } | j | d |   } | S(   Ni’’’’sD   -20[0-9][0-9]-[01][0-9]-[0-3][0-9]_[0-2][0-9][0-5][-0-9].[0-5][-0-9]t    (   t   ret   sub(   R+   R:   t   patternt   result(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   remove_time_extensions§   s    c            s&     f d   } t    | | |  d  S(   Nc            s   t    |   S(   N(   t   write_shelx_ins_file(   t   ins_file_name(   t   imol(    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   <lambda>½   s    (   t   shelxl_refine_inner(   RA   t   hkl_file_in_maybet   shelxh_flagt   func(    (   RA   sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   shelxl_refine»   s    c            s&     f d   } t  |  | | |  d  S(   Nc            s   t    |  t  S(   N(   t   save_string_to_filet   True(   R@   (   t   ins_text(    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyRB   Ä   s    (   RC   (   RA   RJ   RD   RE   RF   (    (   RJ   sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   shelxl_refine_primitiveĀ   s    c            s  d d  l    d d  l  | r0 t d d  } n t d d  } | sM d GHn2t } | rt   j j |  rt | } n    j j   j j d  t  }   j j	 |  sÅ y   j
 |  WqÅ qÅ Xn    j j	 |  sć d G| GHn  j j | t t t |     d t    } t |  d	 k r?d
 | t |  f GHn@| d } | d }	 | d }
 | d } | d } | d }    f d   } | rt } t |  d k r“t } nN | d d !d k sŚ| d d !d k rć| } n t |  rų| } n
 d | } | rĶ| | |  qĶn² t t |    } | d } d G| GH  j j |  rÄd | GH  j j |  }   j j |  } d G| G| GH| | k r“t |  } | | |  qĶ| | |  n	 d | GH  j j |  sėd | GHn | |  d G| d | d | GHt | | g g  |  } | rz  j j |	  rz  j j |  rzt |	 d  } t |  t |
 |  n d GHd  S(    Ni’’’’t   shelxht   PATHt   shelxls(   WARNING:: can't find executable shelxl/ht   .s    Failed to make shelxl directory t   -iP   sJ   BL WARNING:: filename %s too long! Has %s characters, only 80 are allowed!s   .inss   .ress   .lsts   .fcfs   .logs   .hklc            sg   y' d | |  f GH  j  |  |  t SWn9 y' d |  | f GH j |  |  t SWqc t SXn Xd  S(   Ns   make link from %s to %ss   copy %s to %s(   t   symlinkRI   t   copyfileR#   (   t   src_filet   dst_file(   R   t   shutil(    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   make_symlink   s    i    i   t   /s   \s   ../s   BL DEBUG:: looking fors   INFO:: hkl file %s founds   BL DEBUG:: comparing paths"   INFO:: hkl file %s does not exist!s2   data (hkl) file %s not found - not running shelxl!s   BL INFO:: Running shelxl as: t    s    > s8   BL WARNING:: shelxl didnt run ok! No output files found!(   R   RU   t   find_exeR#   R   t   isfileR.   t   abspatht   coot_shelxl_dirt   isdirt   mkdirt
   strip_patht   strip_extensiont   molecule_namet   unique_date_time_strR)   t   slash_start_qmt   popen_commandt'   handle_read_draw_molecule_with_recentreR   t   read_shelx_lst_file(   RA   RD   RF   RE   t
   shelxl_exet   orig_hkl_filet   dirt   stubt   ins_filenamet   res_filenamet   lst_filenameR1   t   log_filenamet   hkl_filenameRV   t   symlink_targett   trial_file_stubt   trial_hkl_file_namet   hkl_filename_dirt   trial_hkl_file_name_dirt
   new_targett   shelx_statust   imol_res(    (   R   RU   sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyRC   Ē   s    	!2






	&		
	
				

c            sN  d d  l  } d d  l  d    d     d        f d    d   }     f d   } | j j |   }  t   s d  GHn·| j j |   s± d	 |  GHnt } g  } y t |  d
  } Wn d G|  GHn X| rA| j   } g  } g  }	 d }
 x| D]} d | k r| j	   } t
 |  d k r#| d }  |  } d t | d  d | d d } | j |  g |  q#qd | k rŖd }
 q|
 d k rÅg  }	 d }
 q|
 d k r| |  } t |  t k r’|	 j |  n  t
 |  d k rd }
 q#d }
 qqW| j   | |	 |  n	 d G|  GHd  S(   Ni’’’’c         S   s)   |  r t  d |   n d GHt d  d  S(   Ns   Interesting Things from SHELXs(   INFO:: Nothing Interesting from LST files!   Nothing Interesting from LST file(   t   interesting_things_guit   add_status_bar_text(   t   interesting_list(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   gui_interesting_listf  s    c         S   s   |  d  S(   Ni’’’’(    (   t   s(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   chop_endo  s    c         S   s   |  d S(   Ni’’’’(    (   R|   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt	   last_chars  s    c   	         s9  g  } |  j  d d  } t |  d k r5| d } y\ t |  } t  |  } | sx d G| GHd | d | d d g S| | d | d d g SWq5| d }   |  }  |  } y t |  } Wn d d d d d g SXt  |  } | sd G| GHd | d | d | g S| | d | d | g Sq5Xn  d  S(   Nt   _i   i   s!   couldn't find chain id for resno R9   i    t   blank(   t   rsplitR)   t   intt"   chain_id_for_shelxl_residue_number(	   t   atom_idt   listt   lst   resnot   chain_idR|   t   pt   alt_conft   n(   R}   RA   R~   (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   make_atom_partsx  s0    
	
	c         S   sW  d d  l  } |  j   } t |  d k r. t Sy t | d  } Wn t } n Xy t | d  } Wn t } n Xy t | d  } Wn t } n Xy t | d  } Wn t } n X| rŠ | rŠ t S| r| rt |  d k rņ t S| | | | | d | d g } | Sn: t |  d k r/t St t | | | d | d g } | Sd  S(	   Ni’’’’i   i    i   i   i   i   i   (   R!   R*   R)   R#   R-   (   R7   R!   t   bitst   n0t   n1t   n2t   n3t   lst(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   parse_dr_line·  s>    



  c            s  g  } xz|  D]r} | d } i d d 6d d 6d d 6d d 6d d 6} | j  |  rb | | } n d }  | d	 |  } d
 } | d }	 | d }
  j |	  rõ  j |
  rõ |	 d k rõ |
 t |	  } d t |	  d t |  d } n d } | d k r| d } n | d | d	 d } d | d | d d t | d  d | d | } |  g | } | j |  q W  | |  d  S(   Ni   i    t   BUMPt   DANGt   FLATi   t   SIMUt   ISORi   s   not sure what?i   i   RX   s    [Z=t   ]R9   s   Disagreeable Restraint (   t   has_keyt   isNumberTypet   absR+   t   append(   t   disagreeable_restraints_listRz   t   dis_res_button_listt   dr_listt   restraint_typet   drl_dictt	   drl_indext
   atom_partst   stats_stringR   R   t   zt   rtt   button_labelt   interesting_thing(   R{   RA   R   t   operator(    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   do_guiä  s.    


*%8s&   WARNING:: Molecule number %i not valids+   WARNING:: shelx lst file: %s does not existR   s   BL ERROR:: Cannot read i    s   may be split intoi   i   s   Atom i   RX   s    may be split?s5      Observed   Target    Error     Sigma     Restrainti   s&   BL ERROR:: unknown error opening file (   R   RŖ   R   R[   t   valid_model_molecule_qmRZ   R#   R$   R'   R*   R)   R+   R   t   typet   ListTypeR0   (   t	   file_nameRA   R   R   R«   R3   Rz   R6   t
   split_listR   t   dr_countR7   t   partsR   R¤   RØ   t   dr_bits(    (   R}   R{   RA   R~   R   RŖ   sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyRf   _  s`    				?	-#
$				
c         C   s{  d d  l  } | j j |   }  t |   } | d k rE t |   } nØ | d k r` t |   } n | d k r{ t |   } nr | d k r t |   } nW | d k r± t |   } n< | d k rĢ t |   } n! | d k rē t |   } n |  } | d	 } | d
 } | d } | d } d G|  GHd G| GHd G| GH| j j |  r]d G| GHt | d  } n d G| GHd } t |  sd GHn÷ | j j |  r| j j |  sŗd G| GHt |  qCd d l	 m
 }	 | j	 |  |	 }
 | j	 |  |	 } |
 | k  r	t |  qCt |  n- | j j |  r>d G| GHt |  n d GH| j j |  rnd G| GHt | |  n	 d G| GHd  S(   Ni’’’’R   t   inst   insht   logt   hklt   rest   fcfs   .ress   .lsts   .fcfs   .fcf.cifs    file_name: s    file_stub: s    extension: s   Read res file i    s     No res file s*   WARNING:: Bad molecule from res file read.s      Read fcf-cif file (   t   ST_MTIMEs      Read fcf file s+   BL WARNING:: no fcf and or cif files found!s     ::Read lst file s      ::No lst file (   R   R   R[   t   file_name_extensiont   file_name_sans_extensionRZ   Re   R¬   R   t   statRŗ   R   t   handle_chelx_fcf_fileRf   (   RÆ   R   t	   extensiont	   file_stubt   res_file_namet   lst_file_namet   fcf_file_namet   fcf_cif_file_nameRw   Rŗ   t   fcf_datet   fcf_cif_date(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   read_shelx_projectC  sd    



								N(   t   typesR	   R   R   R\   R>   R#   RG   RK   RC   Rf   RĒ   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/shelx.pyt   <module>   s   
			q			ä