ó
Ø™xTc           @   sÒ  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l	 m
 Z
 d  d l	 m Z d  d l m Z d  d l m Z d  d	 l m Z d  d l m Z d  d l Z e j j d
 d e ƒZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z  d d „ Z! d d „ Z" d  „  Z# d! „  Z$ d" „  Z% d# „  Z& i  d$ „ Z' d% „  Z( d d& „ Z) d d e j* d' „ Z+ d d e j* d( „ Z, d d) d d e j* d* „ Z- d d) d d e j* d+ „ Z. d, e j* d- „ Z/ e j* d. „ Z0 d e j* d/ „ Z1 e j* d0 „ Z2 e j* d1 „ Z3 d d) d d e j* d2 „ Z4 d d d3 „ Z5 d d d4 „ Z6 d d5 „ Z7 d6 „  Z8 d S(7   iÿÿÿÿ(   t   divisionN(   t   pdb(   t   file_reader(   t
   cablam_res(   t   cablam_math(   t	   NDimTable(   t   easy_pickle(   t   easy_runsÖ  
cablam_validate {
  pdb_infile = None
    .type = path
    .help = '''input PDB file or dirpath'''
  output = *text kin ca_kin full_kin markup_no_ribbons points records records_plus_pdb oneline
    .type = choice
    .help = '''choose output type,
    =text for comma-separated outlier summary
    =kin print markup kinemage to screen
    =ca_kin print CA geo markup kinemage to screen
    =full_kin open in King a multi-crit-type kinemage with markup
    =markup_no_ribbons print allowed, outlier, and ca geom outlier markup kin
    =points print cablam-space points to sys.stdout, in kinemage dotlist format
    =records print HELIX and SHEET-style records based on CaBLAM analysis
    =records_plus_pdb print HELIX and SHHET-style records as part of a pdb file
    =oneline print one-line validation to sys.stdout: percent of cablam outliers
    '''
  outlier_cutoff = 0.05
    .type = float
    .help = '''sets the contour level for detecting outliers, e.g. outlier_cutoff=0.01 for accepting the top 99%, defaults to 0.05'''
  check_prolines = False
    .type = bool
    .help = '''test of a function to differentiate cis vs trans prolines'''
  help = False
    .type = bool
    .help = '''help and data interpretation messages'''
}
t   process_includest   cablam_validationc           B   s   e  Z d d d d  „ Z RS(   c         C   s‚   | |  _  | |  _ | |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _	 d |  _
 |  j  ru |  j  j |  _ n	 d  |  _ d  S(   Nt    (   t   residuet   outlier_levelt   ca_geom_outlier_levelt   Nonet   loose_alphat   regular_alphat
   loose_betat   regular_betat   loose_threetent   regular_threetent
   suggestiont   rg(   t   selfR   R   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   __init__k   s    											N(   t   __name__t
   __module__R   R   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR	   j   s   t   motif_valuec           B   s   e  Z d  „  Z RS(   c         C   s:   d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  S(   N(   R   R   R   R   R   R   R   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR   }   s    					(   R   R   R   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR   |   s   t   motif_guessc           B   s   e  Z d  „  Z RS(   c         C   s:   t  |  _ t  |  _ t  |  _ t  |  _ t  |  _ t  |  _ d  S(   N(   t   FalseR   R   R   R   R   R   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR   †   s    					(   R   R   R   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR   …   s   t   motif_chunkc           B   s_   e  Z e j d  „ Z d e j d „ Z d e j d „ Z d d d d e j d „ Z d „  Z RS(   c         C   s„   |  j  d k r" |  j d | ƒ n^ |  j  d k rD |  j d | ƒ n< |  j  d k rf |  j d | ƒ n t j j d ƒ t j ƒ  d  S(   Nt   helixt   writetot   threetent   sheets9   
tried to print a record that was not 'helix' or 'sheet'
(   t
   motif_typet   print_helix_recordt   print_threeten_recordt   print_sheet_recordt   syst   stderrt   writet   exit(   R   R    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   print_record�   s    	i   c         C   s¬   |  j  j ƒ  } |  j j ƒ  } |  j j |  j  j d } | d k r¨ | j d d | d d | d | d  d | d d | d  d | d d d | d	 ƒ n  d  S(
   Ni   i   s   HELIX  s   %3it    i   s!    1                               s   %5is   
(   t   motif_startt   id_with_resnamet	   motif_endt   resnumR)   (   R   t	   helix_numR    t   motif_start_idt   motif_end_idt	   motif_len(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR$   ›   s    Sc         C   s¬   |  j  j ƒ  } |  j j ƒ  } |  j j |  j  j d } | d k r¨ | j d d | d d | d | d  d | d d | d  d | d d d | d ƒ n  d  S(	   Ni   s   HELIX  s   %3iR,   i   s!    5                               s   %5is   
(   R-   R.   R/   R0   R)   (   R   R1   R    R2   R3   R4   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR%   ¦   s    St   Ui    c         C   sg   |  j  j ƒ  } |  j j ƒ  } | j d d | d d | d | d | d | d | d ƒ d  S(   Ns   SHEET  s   %3iR,   s   %3ss   %2is   
(   R-   R.   R/   R)   (   R   t   sheet_idt
   strand_numt   strand_countt   strand_senseR    R2   R3   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR&   ±   s    c         C   s   d  |  _ d  |  _ d  |  _ d  S(   N(   R   R-   R/   R#   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR   ¹   s    		(	   R   R   R'   t   stdoutR+   R$   R%   R&   R   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR   Ž   s   	c           C   s   t  j j d ƒ d  S(   Nsv  
phenix.cablam_validate file.pdb [options ...]

Options:

  pdb_infile=filename      input PDB file
                             some output formats support a dir of files in this
                             field
  outlier_cutoff=0.05      sets the contour level for detecting outliers,
                             e.g. outlier_cutoff=0.01 for accepting the top 99%,
                             defaults to 0.05
  output=
                           kin : prints markup kinemage for a single outlier
                             cutoff to screen
                           text : prints machine-readable columnated and comma-
                             separated data to screen
                             (default output)
                           points : prints cablam-space points to screen,
                             in kinemage dotlist format
                           records : prints HELIX and SHEET-style records to
                             screen, based on CaBLAM secondary structure
                             analysis
                           records_plus_pdb : prints HELIX and SHEET records and
                             the associated pdb file to screen
                           ca_kin : prints a kinemage with ca-contour outliers
                             marked
                           full_kin : opens a phenix.king window with multicrit
                             style markup for the submitted structure. Also
                             saves a .pdb with HELIX and SHEET records and a
                             .kin with other markup to working dir
                           markup_no_ribbons : prints 'allowed', 'outlier', and
                             'ca geom outlier' kinemage markup to screen
                           oneline : prints oneline-style output to screen.
                             Supports printing for multiple files

  help=False               prints this usage text, plus notes on data
                             interpretation to screen

Example:

phenix.cablam_validate file.pdb output=full_kin
--------------------------------------------------------------------------------
(   R'   R(   R)   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   usageÂ   s    	*c           C   s   t  j j d ƒ d  S(   Ns€
  
cablam_validate data interpretation:

Text:
  Text output is provided in comma-separated format:
  residue,contour_level,loose_alpha,regular_alpha,loose_beta,regular_beta

  'residue' is a residue identifier formatted as pdb columns 18 to 27 (1 index)

  'contour_level' is the 3D outlier contour at which this residue was found.
    Smaller values are worse outliers.  Residues with values <=0.05 are worth
    concern.

  'loose_alpha' is a 2D percentile contour for this residue's similarity to
    alpha helix.  A high value indicates some amount of helix character, but not
    necessarily the presence of a regular helix.
    >0.001 is meaningful, >0.01 is likely

  'regular_alpha' is a 2D percentile contour for this residue's similarity to
    regular alpha helix.  The contours for regular helix are very tight.
    >0.00001 is meaningful, >0.0001 is likely

  'loose_beta' is a 2D percentile contour for this residue's similarity to
    beta strand.  A high value indicates some amount of strand character, but
    not necessarily the presence of a regular strand.
    >0.01 is meaningful, >0.1 is likely

  'regular_beta' is a 2D percentile contour for this residue's similarity to
    regular beta sheet.  Beta structure contours are generally more permissive
    than helix contours, and the contour values should not be judged on the same
    scale.
    >0.001 is meaningful, >0.01 is likely

    ('meaningful' and 'likely' scores are subject to change as the system is
    refined)

Kin:
  Kin output is provided in a kinemage format and should be appended to an open
    kinemage of the structure of interest.  This provides validation markup of
    the structure.  Markup takes the form of purple lines, drawn to follow the
    peptide plane diherdrals used to determine outliers.  Clicking on the
    vertices or on a point in the middle of the center line will bring up
    validation numbers.

  The first value corresponds to 'contour_level' in the text output.

  The values preceded by 'a' and 'rega' correspond to 'loose_alpha' and
    'regular_alpha', respectively.

  The values preceded by 'b' and 'regb' correspond to 'loose_beta' and
    'regular_beta', respectively.

Points:
  Points output is provided in kinemage dotlist format.  Each point corresponds
    to one outlier residue and is plotted in the 3D cablam space used to
    determine outliers (dimensions are CA_pseudodihedral_in,
    CA_pseudodihedral_out, and peptide_plane_psedudodihedral).  These points may
    be appended to contour kinemages to visualize outliers in that space.
  This output is intended primarily for developer and exploratory use, rather
    than for validation per se.

(   R'   R(   R)   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   interpretationô   s    	=t   pdbidc         C   s8   t  j |  d d d d d g d | ƒ} t j | ƒ | S(   Nt   targetatomst   CAt   Ct   Nt   OR=   (   R   t   construct_linked_residuesR   t   cablam_measures(   t	   hierarchyR=   t   resdata(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   setup:  s    c          C   s    d d d g }  i  } x„ |  D]| } t  j j d d | d d t j j ƒ } | d  k r| t j j	 d | d	 ƒ t j
 ƒ  n  t j d
 | ƒ } | | | <q W| S(   Nt   generalt   glyt   prot   relative_paths+   chem_data/cablam_data/cablam.8000.expected.s   .picklet   tests2   
Could not find a needed pickle file for category s    in chem_data.
Exiting.
t	   file_name(   t   libtbxt   envt   find_in_repositoriest   ost   patht   isfileR   R'   R(   R)   R*   R   t   load(   t
   categoriest	   unpickledt   categoryt
   picklefilet   ndt(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   fetch_peptide_expectationsH  s    	c          C   s    d d d g }  i  } x„ |  D]| } t  j j d d | d d t j j ƒ } | d  k r| t j j	 d | d	 ƒ t j
 ƒ  n  t j d
 | ƒ } | | | <q W| S(   NRH   RI   RJ   RK   s+   chem_data/cablam_data/cablam.8000.expected.s
   _CA.pickleRL   s2   
Could not find a needed pickle file for category s    in chem_data.
Exiting.
RM   (   RN   RO   RP   RQ   RR   RS   R   R'   R(   R)   R*   R   RT   (   RU   RV   RW   RX   RY   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   fetch_ca_expectationsY  s    	c          C   s©   d d d d d d g }  i  } x„ |  D]| } t  j j d d | d	 d
 t j j ƒ } | d  k r… t j j	 d | d ƒ t j
 ƒ  n  t j d | ƒ } | | | <q% W| S(   NR   R   R   R   R   R   RK   s(   chem_data/cablam_data/cablam.8000.motif.s   .pickleRL   s/   
Could not find a needed pickle file for motif s    in chem_data.
Exiting.
RM   (   RN   RO   RP   RQ   RR   RS   R   R'   R(   R)   R*   R   RT   (   t   motifsRV   t   motifRX   RY   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   fetch_motifsq  s    	c          C   s�   d d g }  i  } x„ |  D]| } t  j j d d | d d t j j ƒ } | d  k ry t j j	 d | d ƒ t j
 ƒ  n  t j d	 | ƒ } | | | <q W| S(
   Nt   cist   transRK   s*   chem_data/cablam_data/cablam.8000.proline.s   .pickleRL   s)   
Could not find a needed pickle file for s     proline in chem_data.
Exiting.
RM   (   RN   RO   RP   RQ   RR   RS   R   R'   R(   R)   R*   R   RT   (   t   confsRV   t   confRX   RY   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   fetch_cis_trans_proline‰  s    	gš™™™™™©?c   
      C   s4  i  } |  j  ƒ  } | j ƒ  x| D]	} |  | } d | j k r# d | j k r# d | j k r# | j d | j d | j d g } | j | j d ƒ d } | j ƒ  d k rÆ | d j | ƒ }	 n; | j ƒ  d k rî | d	 j | ƒ }	 n | d
 j | ƒ }	 |	 | k  r,t d | d |	 ƒ | | <q,q# q# W| S(   Nt   CA_d_int   CA_d_outt   CO_d_inR?   t   resnamet   GLYRI   t   PRORJ   RH   R   R   (   t   keyst   sortt   measurest   altst   firstaltt   uppert   valueAtR	   (
   RF   t   expectationst   cutofft   outlierst   reskeyst   residR   t   cablam_pointRg   t
   percentile(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   find_peptide_outliers¢  s     

-$#g{®Gázt?c   
      C   s4  i  } |  j  ƒ  } | j ƒ  x| D]	} |  | } d | j k r# d | j k r# d | j k r# | j d | j d | j d g } | j | j d ƒ d } | j ƒ  d k rÆ | d j | ƒ }	 n; | j ƒ  d k rî | d	 j | ƒ }	 n | d
 j | ƒ }	 |	 | k  r,t d | d |	 ƒ | | <q,q# q# W| S(   NRd   Re   t   CA_aR?   Rg   Rh   RI   Ri   RJ   RH   R   R   (   Rj   Rk   Rl   Rm   Rn   Ro   Rp   R	   (
   RF   Rq   Rr   Rs   Rt   Ru   R   Rv   Rg   Rw   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   find_ca_outliers¶  s     

-$#c         C   s—  i  } |  j  ƒ  } | j ƒ  xt| D]l} |  | } d | j k r# d | j k r# d | j k r# d | j k r# | j d | j d | j d g } | j d | j d | j d g } | j | j d ƒ d }	 |	 j ƒ  d k r| d j | ƒ }
 | d j | ƒ } na |	 j ƒ  d	 k rG| d
 j | ƒ }
 | d
 j | ƒ } n& | d j | ƒ }
 | d j | ƒ } t d | d |
 d | ƒ | | <q# q# W| S(   NRd   Re   Rf   Ry   R?   Rg   Rh   RI   Ri   RJ   RH   R   R   R   (   Rj   Rk   Rl   Rm   Rn   Ro   Rp   R	   (   RF   t   peptide_expectationst   ca_geom_expectationst   residue_statsRt   Ru   R   t   peptide_pointt   ca_geom_pointRg   t   peptide_percentilet   ca_geom_percentile(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   find_all_residue_statsÊ  s&    

<$$&c         C   sœ   d } d } d } xƒ |  j  ƒ  D]u } | j } t | | ƒ } | r | d | _ | d | _ | d | _ | d | _ | d | _ | d | _ q q Wd  S(	   Ngü©ñÒMbP?g-Cëâ6?R   R   R   R   R   R   (	   t   valuesR   t   helix_or_sheet_resR   R   R   R   R   R   (   R}   R\   t   alpha_cutofft   beta_cutofft   threeten_cutofft   res_statR   t   contours(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   helix_or_sheetæ  s    	c         C   s�   i |  d 6} d |  j  k ry d |  j  k ry |  j  d |  j  d g } x- | j ƒ  D] \ } } | j | ƒ | | <qR W| Sd  Sd  S(   NR   Rd   Re   (   Rl   t   itemsRp   R   (   R   R\   R‰   Rv   t	   motifnamet   contour(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR„   ø  s    c         C   s±  d } d } d } d } x’|  D]Š} |  | } d | _  | j | k  rM q n  | j | k r» yH |  | j j j j | k r£ |  | j j j j | k r£ d | _  n  Wq» t k
 r· q» Xn  | j | k r)yH |  | j j j j | k r|  | j j j j | k rd | _  n  Wq)t k
 r%q)Xn  | j	 | k r | j	 | j k r yH |  | j j j j	 | k s…|  | j j j j	 | k r‘d | _  n  Wq©t k
 r¥q©Xq q Wd  S(   Ngü©ñÒMbP?g-Cëâ6?g{®Gázt?s                    s    try alpha helix s    try beta sheet  s    try threeten    (
   R   R   R   R   t   prevresRu   t   nextrest   KeyErrorR   R   (   t   residuesR…   R†   R‡   t   ca_geom_cutoffRu   t   res(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   assign_sec_struc  s6    
	88!8c         C   s;  d } d } d } d } t  ƒ  } t ƒ  } t |  | d d ƒ} t | | ƒ x |  D] }	 t ƒ  |  |	 _ qS WxÇ|  D]¿}	 |	 | k rŒ qt n  |  |	 }
 |	 | j ƒ  k r® qt n  | |	 j | k ryE | |
 j j j | k r| |
 j	 j j | k rt
 |
 j _ n  Wqt k
 rqXn  | |	 j | k rªyc | |
 j j j | k r’| |
 j	 j j | k r’t
 |
 j _ t
 |
 j	 j _ t
 |
 j j _ n  Wqªt k
 r¦qªXn  | |	 j | k rt | |	 j | |	 j k rt yE | |
 j j j | k s| |
 j	 j j | k rt
 |
 j _ n  Wq3t k
 r/q3Xqt qt Wd  S(   Ngü©ñÒMbP?g-Cëâ6?Rr   g      ð?(   RZ   R^   Rx   RŠ   R   Rj   R   R�   Ru   RŽ   t   TrueR�   R   R   (   RF   t   ca_outlierst   loose_alpha_cutofft   reg_beta_cutofft   loose_threeten_cutofft   reg_threeten_cutoffRq   R\   t   allresRu   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   find_partial_sec_struc%  sH    		
22-2c         C   sÐ  |  j  ƒ  } | j ƒ  g  } d  } x‹ | D]ƒ } |  | } | j j r� | rW | | _ n1 t ƒ  } | j | ƒ d | _ | | _	 | | _ | j
 s¬ d  } q¬ q) | r) d  } q) q) Wx‹ | D]ƒ } |  | } | j j r+| rå | | _ n1 t ƒ  } | j | ƒ d | _ | | _	 | | _ | j
 s:d  } q:q· | r· d  } q· q· Wx‹ | D]ƒ } |  | } | j j r¹| rs| | _ n1 t ƒ  } | j | ƒ d | _ | | _	 | | _ | j
 sÈd  } qÈqE| rEd  } qEqEW| S(   NR   R!   R"   (   Rj   Rk   R   R   R   R/   R   t   appendR#   R-   R�   R   R   (   RF   Rt   R\   t   current_motifRu   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   find_whole_sec_strucf  sd    

						
						
						c         C   sÑ  d } d } t  |  | ƒ } t j | ƒ t ƒ  } | j ƒ  } | j ƒ  xƒ| D]{} | | } | j ƒ  d d !j ƒ  d k rN d | j k rÉd | j k rÉd | j k rÉ| j d }	 | j d | j d g }
 | d j	 |
 ƒ } | d	 j	 |
 ƒ } |	 d
 k r]|	 d k r]| | k  rÃd G| G| j ƒ  Gd G| GH| d k rNd G| GHqZd G| GHqÃqÆ|	 d k su|	 d k rÆ| | k  rÃd G| G| j ƒ  Gd G| GH| d k r´d G| GHqÀd G| GHqÃqÆqÉqN qN Wd  S(   Ng{®Gázt?i    i   Ri   t   omegaRd   Re   R_   R`   iÄÿÿÿi<   s   bad CIS at s   value:gš™™™™™¹?s     try TRANS. value:s     no suggestion. trans value:ix   iˆÿÿÿs   bad TRANS at s     try CIS. value:s     no suggestion. cis value:(
   RG   R   t	   omegacalcRc   Rj   Rk   R.   Ro   Rl   Rp   (   RE   R=   t
   cis_cutofft   trans_cutoffRF   t   pro_contourRt   Ru   R   R    Rv   t   cislevelt
   translevel(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   check_prolines°  s6    	

-c   
      C   sw   t  |  | ƒ } t ƒ  } t ƒ  } t | | d | ƒ} t | d | ƒt | ƒ } x | D] }	 |	 j d | ƒ qY Wd  S(   NRr   R–   R    (   RG   R[   R^   Rz   Rœ   RŸ   R+   (
   RE   t	   ca_cutoffR=   R    RF   t   ca_expectationst   motif_contoursR–   R\   R]   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   print_helix_sheet_recordsÕ  s    		c         C   s*   t  |  | | | ƒ | j |  j ƒ  ƒ d  S(   N(   R«   R)   t   as_pdb_string(   RE   R¨   R=   R    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt&   print_pdb_with_new_helix_sheet_recordså  s    g{®Gáz„?c         C   s·   t  |  | ƒ } t ƒ  } t ƒ  } t ƒ  }	 t | | d | ƒ}
 t |
 |	 ƒ t |
 | d d d | ƒt |
 | d d d | ƒt | | d | ƒ} t | |	 ƒ t | | d | ƒd  S(   NRr   t   colort   purpleR    t   magenta(	   RG   RZ   R[   R^   Rx   RŠ   t   give_kinRz   t   give_ca_kin(   RE   t   peptide_cutofft   peptide_bad_cutoffR¨   R=   R    RF   R{   R©   Rª   t   peptide_outliersR–   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   print_cablam_markup_kinï  s    			c         C   sd  t  | d d ƒ } t  | d d ƒ } t |  | ƒ } t ƒ  }	 t ƒ  }
 t ƒ  } t | |	 d | ƒ} t | | ƒ t | | d d d | ƒt | | d d d | ƒt | |
 d | ƒ} t | | ƒ t	 | | d | ƒt
 | d	 | ƒt | ƒ } x | D] } | j d | ƒ qü W| j |  j ƒ  ƒ | j ƒ  | j ƒ  d
 | d | d } t j | ƒ d  S(   Ns   _cablam_multi.kint   ws   _cablam_multi.pdbRr   R®   R¯   R    R°   R–   s   phenix.king -m s   _cablam_multi.pdb (   t   openRG   RZ   R[   R^   Rx   RŠ   R±   Rz   R²   Rœ   RŸ   R+   R)   R¬   t   closeR   t   fully_buffered(   RE   R³   R´   R¨   R=   R    t   outfile_kint   outfile_pdbRF   R{   R©   Rª   Rµ   R–   R\   R]   t   king_command(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   cablam_multicrit_kin  s,    			

R¯   c      	   C   s  | j  d t | ƒ d ƒ | j  d | d t | ƒ d ƒ |  j ƒ  } | j ƒ  x²| D]ª} |  | } | j | k r€ q[ n  | j } | j } | j }	 | j d ƒ | j d ƒ }
 } | j d ƒ | j d ƒ } } |	 j d ƒ } t	 j
 |
 | | ƒ } t	 j
 | | | ƒ } | d | d d	 | d
 | d
 d	 | d | d d	 g } d j d | j d d | j d d | j d d | j d d | j g ƒ } | j  d | d t | d ƒ d t | d
 ƒ d t | d ƒ ƒ | j  d | d t | d ƒ d t | d
 ƒ d t | d ƒ ƒ | j  d | d t | d ƒ d t | d
 ƒ d t | d ƒ ƒ | j  d | d t | d ƒ d t | d
 ƒ d t | d ƒ ƒ | j  d | d t | d ƒ d t | d
 ƒ d t | d ƒ ƒ q[ W| j  d ƒ d  S(   Ns   
@subgroup {cablam out s   } dominant
s%   @vectorlist {cablam outliers} color= s    width= 4 master={cablam out t   }R?   RB   i    g       @i   i   R,   s   %.5ft   at   regat   bt   regbs   
{s   } P s   } s   
(   R)   t   strRj   Rk   R   R   RŽ   R�   t
   getatomxyzR   t   perptersectt   joinR   R   R   R   (   Rs   t   outlier_cutoffR®   R    Rt   Ru   t   outlierR   RŽ   R�   t   CA_1t   O_1t   CA_2t   O_2t   CA_3t	   pseudoC_1t	   pseudoC_2t   midpointt   stats(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR±   *  s0    #

			?QGGGGKc      	   C   sP  | j  d t | ƒ d ƒ | j  d ƒ |  j ƒ  } | j ƒ  xþ| D]ö} |  | } | j } | j } | j } | j d ƒ }	 | j d ƒ }
 | j d ƒ } d j d | j	 d d | j
 d d | j d	 d | j d
 d | j g ƒ } | j  d | d t |
 d |
 d |	 d d ƒ d t |
 d |
 d |	 d d ƒ d t |
 d |
 d |	 d d ƒ ƒ | j  d | d t |
 d ƒ d t |
 d ƒ d t |
 d ƒ ƒ | j  d | d t |
 d |
 d | d d ƒ d t |
 d |
 d | d d ƒ d t |
 d |
 d | d d ƒ ƒ qE W| j  d ƒ d  S(   Ns   
@subgroup {ca out s   } dominant
sG   @vectorlist {ca outliers} color= red width= 4 master={ca geom outliers}R?   R,   s   %.5fRÀ   RÁ   RÂ   RÃ   s   
{s   } P i    gÍÌÌÌÌÌì?i   i   s   } s   
(   R)   RÄ   Rj   Rk   R   RŽ   R�   RÅ   RÇ   R   R   R   R   R   (   Rs   RÈ   R    Rt   Ru   RÉ   R   RŽ   R�   RÊ   RÌ   RÎ   RÒ   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyR²   Q  s"    

			QƒG‡c         C   s&  | j  d ƒ | j  d ƒ | j  d ƒ |  j ƒ  } | j ƒ  xÕ | D]Í } |  | } | j | k ri qD n  | j } d | j k rD d | j k rD d | j k rD | j d | j d | j d g } | j  d | j ƒ  d d	 | d
 d d	 | d d d	 | d d ƒ qD qD W| j  d ƒ d  S(   Ns   
@kinemage
s"   @group {cablam outliers} dominant
s   @dotlist {cablam outliers}Rd   Re   Rf   t   {s   } s   %.3fi    R,   i   i   s   
(   R)   Rj   Rk   R   R   Rl   R.   (   Rs   RÈ   R    Rt   Ru   RÉ   R   Rv   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   give_pointsn  s    

	-$Rc      
   C   s  | j  d ƒ |  j ƒ  } | j ƒ  xÍ | D]Å } |  | } | j d k  rR d } n6 | j d k  rj d } n | j d k  r‚ d } n d } | j j ƒ  | d	 | j d	 | j | j d	 | j d	 | j	 d	 | j
 g } | j  d
 d j | ƒ ƒ q* W| j  d
 ƒ d  S(   Ns€   residue : outlier_type : contour_level : ca_contour_level : sec struc recommendation : alpha score : beta score : threeten scoreg{®Gázt?s    CA Geo Outlier     g{®Gáz„?s    Peptide Outlier    gš™™™™™©?s    Peptide Disfavored s                       s   %.5fs   
t   :(   R)   Rj   Rk   R   R   R   t   mp_idR   R   R   R   RÇ   (   Rs   R    Rt   Ru   RÉ   t   outlier_typet   outlist(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt	   give_text�  s    

			Mc         C   s   |  j  d ƒ d  S(   NsV   pdbid:residues:peptide_outlier_percent:peptide_bad_outlier_percent:ca_outlier_percent
(   R)   (   R    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   oneline_header¢  s    c         C   sÃ  t  |  | ƒ } t ƒ  } t ƒ  } d }	 d }
 d } d } | j ƒ  } xª| D]¢} | | } d | j k rL d | j k rL d | j k rL d | j k rL |	 d 7}	 | j | j d ƒ d } | j d | j d | j d g } | j d | j d | j d g } | j ƒ  d	 k r?| d
 j | ƒ } | d
 j | ƒ } na | j ƒ  d k rz| d j | ƒ } | d j | ƒ } n& | d j | ƒ } | d j | ƒ } | | k  r¹|
 d 7}
 n  | | k  rÒ| d 7} n  | | k  rî| d 7} qîqL qL Wy |
 |	 d } Wn t	 k
 rd } n Xy | |	 d } Wn t	 k
 rId } n Xy | |	 d } Wn t	 k
 rud } n X| j
 | j ƒ  d t |	 ƒ d d | d d | d d | d ƒ d  S(   Ni    Rd   Re   Rf   Ry   i   R?   Rg   Rh   RI   Ri   RJ   RH   id   RÕ   s   %.1fs   %.2fs   
(   RG   RZ   R[   Rj   Rl   Rm   Rn   Ro   Rp   t   ZeroDivisionErrorR)   t   lowerRÄ   (   RE   R³   R´   R¨   R=   R    RF   R{   R©   t   residue_countt   peptide_outlier_countt   peptide_bad_outlier_countt   ca_outlier_countRt   Ru   R   Rg   R~   t   ca_pointR€   t   ca_percentilet   peptide_outlier_percentt   peptide_bad_outlier_percentt   ca_outlier_percent(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   oneline¦  sT    		
<
$$


c         C   sW   t  |  | ƒ } t ƒ  } t ƒ  } t ƒ  } t | | | ƒ } t | | ƒ t | ƒ | S(   N(   RG   RZ   R[   R^   R‚   RŠ   R”   (   RE   RÈ   R=   RF   R{   R|   R\   R}   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   analyze_pdbã  s    			
c         C   sG   t  |  | ƒ } t ƒ  } t ƒ  } t | | d | ƒ} t | | ƒ | S(   NRr   (   RG   R[   R^   Rz   RŠ   (   RE   RÈ   R=   RF   R©   R\   Rs   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   analyze_pdb_cað  s    		c         C   sé   d } d } t  |  | ƒ } t ƒ  } t ƒ  } t ƒ  } t | | d | ƒ} t | | d | ƒ}	 t | | ƒ t | d |	 ƒxe | j ƒ  D]W }
 |
 j	 j
 j r« d |
 _ qŠ |
 j	 j
 j rÆ d |
 _ qŠ |
 j	 j
 j rŠ d |
 _ qŠ qŠ W| S(   Ngš™™™™™©?g{®Gázt?Rr   R–   s   try Beta Strands   try ThreeTens   try Alpha Helix(   RG   RZ   R[   R^   Rx   Rz   RŠ   Rœ   Rƒ   R   R   R   R   R   R   (   RE   R=   RÈ   t   ca_outlier_cutoffRF   R{   R©   R\   Rs   R–   RÉ   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   analyze_pdb_guiú  s&    			c      	   C   sQ  t  j j j d t ƒ } g  } xÔ |  D]Ì } t j j | ƒ r  t j	 | ƒ } | j
 d k r{ | j | j d d | ƒ ƒ qñ | j
 d k rñ | j | j ƒ qñ q% t j j | ƒ rÒ | j | j d d | ƒ ƒ q% | j d | ƒ } | j | ƒ q% Wt j d | ƒ } | j ƒ  } | j } | j r@t ƒ  t ƒ  t j ƒ  n  | j smt j j d ƒ t ƒ  t j ƒ  n  t j j | j ƒ rÍg  }	 | j }
 x] t j | j ƒ D]" } |	 j t j j |
 | ƒ ƒ q¤Wn$ t j j | j ƒ rñ| j g }	 n  | j d k r
t ƒ  n  x@|	 D]8} t j j | ƒ s/qn  t j	 | ƒ } | j
 d k rgt j j | d	 ƒ qn  t j j t j j  | ƒ ƒ d
 } t! j" | ƒ } | j# ƒ  } | j d k rÆt$ | d | ƒq| j d k rt% | d | j& d | ƒ} t' | | j& ƒ q| j d k r.t( | d | j& d | ƒq| j d k rPt) | d | ƒq| j* rlt* | d | ƒq| j d k r�t+ | d d d d d t j ƒq| j d k rÎt, | d d d d d t j ƒqt- | d | j& d | ƒ} | j d k rt. | | j& ƒ n  | j d k r-t/ | | j& ƒ n  | j d k rt0 | ƒ qqWd  S(   Nt   master_philR   t   args   pdb_infile="%s"t   philt   sourcess.   
Missing input data, please provide .pdb file
Ræ   s    not id'd as readable file
i    R=   t   ca_kinRÈ   t   full_kinR³   t   markup_no_ribbonst   recordsR¨   g{®Gázt?R    t   records_plus_pdbt   kint   pointst   text(1   RN   Rí   t   command_linet   argument_interpreterRë   RQ   RR   RS   R   t   any_filet	   file_typeR�   t   processt   file_objectt   isdirt   fetcht   extractt   cablam_validatet   helpR;   R<   R'   R*   t
   pdb_infileR:   R)   t   listdirRÇ   t   outputRÚ   R(   t   splitextt   basenameR   t   inputt   construct_hierarchyRæ   Rè   RÈ   R²   R¾   R¶   R§   R«   R­   Rç   R±   RÔ   RÙ   (   t   argst   interpreterRî   Rì   t
   input_filet   arg_philt	   work_philt   work_paramst   paramst   filesett   dirpatht   filenameR  t   pdb_inR=   t   pdb_ioRE   R–   Rs   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   run  sˆ      					#
 "	""(9   t
   __future__R    RQ   R'   t   libtbx.phil.command_lineRN   t   iotbxR   R   t   mmtbx.cablamR   R   t   mmtbx.rotamer.n_dim_tableR   R   R   t   libtbx.load_envRí   t   parseR•   Rë   t   objectR	   R   R   R   R;   R<   RG   RZ   R[   R^   Rc   Rx   Rz   R‚   RŠ   R„   R”   Rœ   RŸ   R§   R:   R«   R­   R¶   R¾   R±   R²   RÔ   RÙ   RÚ   Ræ   Rç   Rè   Rê   R  (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/cablam/cablam_validate.pyt   <module>   s^   5			4	2	F								 A	J%
$'!=
