ó
£™xTc           @   s
  d  d l  m Z d  d l m Z d  d l m Z d  d l Z d  d l m Z m Z d  d l m	 Z	 m
 Z
 d  d l Z e j j d ƒ Z d Z d	 Z d
 Z d Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e e e
 d e e j d	 d „ Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   division(   t   easy_mp(   t	   str_utilsN(   t   Sorryt   null_out(   t   adopt_init_argst   Autosx   
  similarity_matrix =  blosum50  dayhoff *identity
    .type = choice
  min_allowable_identity = 0.5
    .type = float
i    i   t   Ut   Kc         C   s]   t  |  ƒ d k r d  St  |  ƒ d k r0 |  d Sd d l m } | j |  ƒ } | j ƒ  Sd  S(   Ni    i   iÿÿÿÿ(   t   flex(   t   lent   Nonet   scitbx.array_familyR	   t   vec3_doublet   mean(   t   sitesR	   t   v(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_mean_coordinate   s    t   chainc           B   s�   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z e d „ Z e d „ Z RS(   sN  
  Stores information on a protein or nucleic acid chain, its alignment to
  the target sequence, and the coordinates of each residue.  For command-line
  use, much of this is irrelevant.  In the PHENIX GUI, much of this data is
  fed to the sequence/alignment viewer (wxtbx.sequence_view), which controls
  the graphics window(s).
  c         C   sÑ   t  |  t ƒ  ƒ | t t g k s( t ‚ t | ƒ t | ƒ k sF t ‚ d  |  _ d  |  _ d  |  _	 d |  _
 d |  _ d |  _ d |  _ d |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ d  |  _ g  |  _ d  S(   Ng        i    (   R   t   localst   PROTEINt   NUCLEIC_ACIDt   AssertionErrorR
   R   t	   alignmentt   sequence_namet   sequence_idt   identityt	   n_missingt   n_missing_startt   n_missing_endt   n_gapst   extrat   unknownt   mismatcht   actual_codet   _xyzt   _tablet   _flag_indices(   t   selft   chain_idt   sequencet   residst
   chain_typet   sec_strt   resnames(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   __init__'   s$    														c         C   sz  t  | j ƒ t  | j ƒ k r3 t  | j ƒ d k s9 t ‚ | |  _ | |  _ | |  _ |  j d  k	 ru t	 |  j ƒ } n t	 d t  |  j
 ƒ ƒ } d |  _ |  j d	 k r² d |  _ n  | d  k	 rÙ | j d d g ƒ |  _ n  d } } t } d  } t  | j ƒ }	 x¢| |	 k  r¢| j | }
 | j | } d  } |
 d k rf|  j | } |  j | | 7_ | d 7} n |  j d 7_ | |  j } |
 d k r| d
 k r·|  j d 7_ | rä|  j d  k sÒ|  j | d  k rä|  j d 7_ q| d k r|  j d 7_ qq·n¯ |
 d k r·| r‡| t  |  j ƒ k  r´|  j j | d  ƒ | j | d ƒ |  j d  k	 rw|  j j | d  ƒ n  | d 7} q´q·| d k r·|  j d 7_ |  j d 7_ q·n  |
 d k r�t } | d k rø|  j j | ƒ |  j j | ƒ q�| d k r�| |
 k r�|
 t k r+|  j t k sF|
 t k rY|  j t k rY|  j j | ƒ n  |  j j | ƒ |  j j | ƒ |  j j | ƒ q�n  |
 } | d 7} qW|	 d } | j  ƒ  } x– | j | d k rQ| |  j } | | d k rD|  j d  k s#| t  |  j ƒ k s#|  j | d  k rD|  j! d 7_! |  j d 7_ n  | d 8} q¼Wt  |  j ƒ t  | j ƒ k svt ‚ d  S(   Ni    t   -t    s	   (unnamed)t
   skip_charst   Xi   t    (   NR/   (   R1   R.   (   R.   (   R1   R.   (   R1   R.   (   R1   R.   ("   R
   t   at   bR   R   R   R   R+   R   t   listR(   t   calculate_sequence_identityR   t   FalseR)   R   R   R,   R   t   insertt   TrueR   t   appendR%   t   UNK_AAR*   R   t   UNK_NAR   R    R!   R"   t   matchesR   (   R&   R   R   R   t   raw_sec_strt   i_alnt   i_residt   chain_startedt	   prev_chart   alignment_lengtht
   symbol_pdbt
   symbol_seqt   residt   j_residR=   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   set_alignment<   sŽ    9				
"
c         C   sæ  |  j  | j k s t ‚ g  |  _ d } x^| j ƒ  D]P} | j ƒ  } x1 |  j | | k ry |  j j d
 ƒ | d 7} qI Wd
 } | |  j
 k r˜ d } n0 | |  j k r° d } n | |  j k rÈ d } n  d
 } x[ | j ƒ  D]1 } | j d k sÿ | j d k rÛ | j } PqÛ qÛ Wd | GH| j ƒ  d j } |  j j | ƒ | d
 k	 rz|  j j |  j  | | d	 |  j  | f | g ƒ n  | d 7} q4 Wx3 | t |  j ƒ k  r½|  j j d
 ƒ | d 7} q‹Wt |  j ƒ t |  j ƒ k sât ‚ d
 S(   sœ   
    Collect the coordinate of the central atom (CA or P) in each residue,
    padding the array with None so it matches the sequence and resid arrays.
    i    i   s   not in sequences   mismatch to sequences   special residues    CA s    P  s(   WARNING: can't find center of residue %ss   chain '%s' and resid %sN(   R'   t   idR   R$   t   residue_groupsRF   R)   R#   R:   R   R   R!   R    t   atomst   namet   xyzR
   (   R&   t	   pdb_chaint   kt   residue_groupRF   t	   res_classRM   t   atom(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   extract_coordinatesˆ   s>    						c         c   s¨   |  j  | j k s t ‚ d } xX | j ƒ  D]J } | j ƒ  } x& |  j | | k re | d 7} d  Vq@ W| V| d 7} q+ Wx( | t |  j ƒ k  r£ | d 7} d  Vq| Wd  S(   Ni    i   (   R'   RI   R   RJ   RF   R)   R   R
   (   R&   RN   RO   RP   RF   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   iter_residue_groups®   s    
	
c         C   s   t  |  j | ƒ ƒ |  _ d  S(   N(   R5   RT   RJ   (   R&   RN   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   extract_residue_groups¼   s    c         C   s   |  j  S(   s   Used in PHENIX validation GUI(   R$   (   R&   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_outliers_table¿   s    c         C   s   |  j  S(   s:   Used for wxtbx.sequence_view to highlight mismatches, etc.(   R%   (   R&   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_highlighted_residuesÃ   s    c         C   sš   t  |  j ƒ d k s t ‚ d } g  } xl t |  j j ƒ D]X \ } } | | k rV Pn# | | k ry | j |  j | ƒ n  | d k r: | d 7} q: q: W| S(   Ni    R.   i   (   R
   R#   R   t	   enumerateR   R3   R:   (   R&   t   i1t   i2RO   R   t   jR3   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt#   get_coordinates_for_alignment_rangeÇ   s    c         O   s   |  j  | | Ž  } t | ƒ S(   N(   R\   R   (   R&   t   argst   kwdsR   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt'   get_mean_coordinate_for_alignment_rangeÔ   s    c         C   s:   g  } x- | D]% \ } } | j  |  j | | ƒ ƒ q W| S(   N(   t   extendR\   (   R&   t   rangesR   RY   RZ   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt$   get_coordinates_for_alignment_rangesØ   s    c         O   s   |  j  | | Ž  } t | ƒ S(   N(   Rb   R   (   R&   R]   R^   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt(   get_mean_coordinate_for_alignment_rangesÞ   s    c         C   s<   | r" |  j  j |  j  j |  j g S|  j  j |  j  j g Sd  S(   N(   R   R3   R4   R+   (   R&   t   include_sec_str(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_alignmentâ   s    c            s£  ‡  f d †  } ˆ  d |  j  IJ|  j d  k r< ˆ  d IJncˆ  d |  j IJˆ  d |  j d IJ|  j d k r” ˆ  d |  j |  j |  j f IJn  |  j d k r· ˆ  d	 |  j IJn  t	 |  j
 ƒ d k ró ˆ  d
 t	 |  j
 ƒ IJ| |  j
 ƒ n  t	 |  j ƒ d k r/ˆ  d t	 |  j ƒ IJ| |  j ƒ n  t	 |  j ƒ d k rkˆ  d t	 |  j ƒ IJ| |  j ƒ n  | rŸ|  j j d ˆ  d d d d d d d t ƒ n  d  S(   Nc            sš   t  |  ƒ d k s t ‚ d } d j g  |  D] } | j ƒ  ^ q+ ƒ } t j | d ƒ j d ƒ } ˆ  d | d IJx  | d D] } ˆ  d | IJq~ Wd  S(   Ni    R2   i<   s   
s       residue IDs: %si   s                    %s(   R
   R   t   joint   stripR   t   wordwrapt   split(   R)   t   wRF   t   linet   lines(   t   out(    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   print_residsé   s    (s   Chain '%s':s&     No appropriate sequence match found!s     best matching sequence: %ss     sequence identity: %.2f%%id   i    s?     %d residue(s) missing from PDB chain (%d at start, %d at end)s     %d gap(s) in chains     %d mismatches to sequences#     %d residues not found in sequences     %d residues of unknown typeRm   t
   block_sizei<   t   top_names   PDB filet   bottom_nameR(   t
   show_ruler(   R'   R   R   R   R   R   R   R   R   R
   R!   R   R    t   pretty_printR7   (   R&   Rm   t   verboseRn   (    (   Rm   sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   show_summaryè   s2    #N(   t   __name__t
   __module__t   __doc__R   R-   RH   RS   RT   RU   RV   RW   R\   R_   Rb   Rc   R7   Re   R9   Ru   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyR      s   	L	&								t
   validationc           B   sb   e  Z d d e e e e d  d „ Z d „  Z d „  Z d „  Z d d „ Z	 d „  Z
 d d „ Z RS(	   i    c
   '      C   s^  t  | ƒ d k s t ‚ x# | D] }
 |
 j d k s t ‚ q W| d  k rV t j } n  | d  k rq t j ƒ  } n  d |  _ d |  _	 d |  _
 g  |  _ |	 |  _ | |  _ d  g t  | ƒ |  _ x„ t d t  | ƒ ƒ D]m } | | } xZ t d t  | ƒ ƒ D]C } | | k rPqù | | } | j | j k rù | |  j | <Pqù qù WqÓ Wt  | j ƒ  ƒ d k rkt d ƒ ‚ n  d  } } | rÖd d  l } | j j d | d d  d d  ƒ } | j d	 t ƒ  ƒ | j ƒ  } | j ƒ  } n  g  } xà| j ƒ  d j ƒ  D]È} t } | j } | j ƒ  d } | j ƒ  rB|  j	 d 7_	 t } t } nD | j ƒ  rf|  j d 7_ t  } n  |  j
 d 7_
 | d
 | IJqót! } t" } | j# d | d | d | ƒ } | j$ d | d | ƒ } | j% d | d | ƒ } t  | ƒ t  | ƒ k o	t  | ƒ k n st ‚ d  } | d  k	 rq| j ƒ  rq| j& | | d | d | ƒ} t  | ƒ t  | ƒ k sqt ‚ n  t' d | d | d | d | d | d | ƒ } |  j j( | ƒ | j( | ƒ qóWt  |  j ƒ d k rãt d ƒ ‚ n  t" }  |  r.g  }! xf t t  |  j ƒ ƒ D] }" |! j( |  j) |" ƒ ƒ qWn0 t* j+ d |  j) d t t  |  j ƒ ƒ d | ƒ }! t  |! ƒ t  |  j ƒ k oŠt  | ƒ k n s•t ‚ x¹ t, |  j ƒ D]¨ \ }" } |! |" \ }# }$ }% |# d  k rÖq¥n  | |" } y | j- |# |$ |% ƒ Wn' t. k
 r }& d | j/ GH‚  |& GHq¥X| r7| j0 | ƒ n  | r¥| j1 | ƒ q¥q¥Wd  |  _ d  S(   Ni    R/   i   s$   Multi-model PDB files not supported.iÿÿÿÿt   pdb_hierarchyt   sec_str_from_pdb_filet   assume_hydrogens_all_missingt   logs   Skipping non-polymer chain '%s't   substitute_unknownt   padt   pad_at_startR'   R(   R)   R*   R+   R,   s+   Could not find any polymer chains to align.t
   fixed_funcR]   t	   processess   Error processing chain %s(2   R
   R   R(   R   t   syst   stdoutt   master_philt   extractt	   n_proteint	   n_rna_dnat   n_othert   chainst   minimum_identityt	   sequencest   sequence_mappingst   ranget   modelsR   t   mmtbx.secondary_structuret   secondary_structuret   managert   find_automaticallyR   t   alpha_selectiont   beta_selectionR;   RI   t
   conformerst   is_naR<   R   t
   is_proteinR   R9   R7   t   as_padded_sequencet   get_residue_idst   get_residue_names_paddedt   as_sec_str_sequenceR   R:   t   align_chainR   t   pool_mapRX   RH   t	   ExceptionR'   RS   RU   ('   R&   Rz   RŒ   t   paramsR}   t   nproct   include_secondary_structureRS   RU   R‹   t
   seq_objectt   i_seqt   seq_obj1t   j_seqt   seq_obj2t   helix_selectiont   sheet_selectiont   mmtbxt   ssmt
   pdb_chainsRN   t   unkR'   t	   main_confR*   R   R€   t   seqR)   R,   R+   t   ct   debugt   alignments_and_namest   iR   t   seq_namet   seq_idt   e(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyR-     sÆ    						


						4	!				7 
	c         C   s  d d  l  } |  j | } d  } d  } d  } |  j } t j } xÃ t |  j ƒ D]² \ }	 }
 | j j	 d | j
 d |
 j
 ƒ j ƒ  } | j d d g ƒ } | j j d ƒ | j j d ƒ } | | k sÛ | | k rM | | k  rM | } | } |
 j } |	 } | } qM qM W| | | f S(   Niÿÿÿÿt   seq_at   seq_bR0   R1   t   m(   t   mmtbx.alignmentRŠ   R   R‹   Rƒ   t   maxintRX   RŒ   R   t   alignR(   t   extract_alignmentR6   t   match_codest   rfindt   findRL   (   R&   R³   Rª   R   t   best_alignmentt   best_sequencet   best_seq_idt   best_identityt
   best_widthR¤   R£   R   R   t   width(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyR�   w  s*    			"	c         C   sF   g  } x9 |  j  D]. } | j ƒ  } | d  k	 r | j | ƒ q q W| S(   N(   RŠ   RV   R   R`   (   R&   t   tableR°   t   outliers(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_table_data�  s    c         C   sI   g  } x< |  j  D]1 } | j d  k r | j | j | j f ƒ q q W| S(   N(   RŠ   R   R   R:   R'   R(   (   R&   t   missingR°   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_missing_chains˜  s
     c         C   s=   | d  k r t j } n  x |  j D] } | j | ƒ q" Wd  S(   N(   R   Rƒ   R„   RŠ   Ru   (   R&   Rm   R   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   showŸ  s    c         C   s
  t  |  j ƒ } d g | } x6 |  j D]+ } | j d k	 r& | | j c d 7<q& q& Wd g | } xJ t | ƒ D]< } |  j | } | d k	 ro | | c d 7<d | | <qo qo Wd g | } xG t | ƒ D]9 } | | d k rì d | | <qÉ | | | | | | <qÉ W| S(   sÁ  
    Count the number of copies of each sequence within the model, used for
    adjusting the input settings for Phaser-MR in Phenix.  This should
    automatically account for redundancy: only the first matching sequence is
    considered, and sequences which are non-unique will have a copy number of
    -1.  Thus if we have 4 copies of a sequence, and the PDB hierarchy has
    2 matching chains, the copy numbers will be [0.5, -1, -1, -1].
    i    i   iÿÿÿÿN(   R
   R�   RŠ   R   R   RŽ   (   R&   t   n_seqt   countsR°   t   redundanciesR¤   R¦   t   counts_relative(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt!   get_relative_sequence_copy_number¥  s"    	c            sy  d d  l  } | d  k r- | j j j ƒ  } n  | j j j d d ƒ } d } d } d } xS t |  j ƒ D]B \ } } | j | d | | | d | j	 j
 | j d d f ƒ qg W| j j j d d  ƒ }	 d d  l }
 |
 j d ƒ ‰  ‡  f d †  } d } xct |  j ƒ D]R\ } } | j	 j ƒ  } d } x.t rV| j d | d ƒ } | d k rUPn  | j d | ƒ } | d k r‚t | ƒ } n  | d 8} | j	 j | } | j	 j | } | j	 j | } | j	 j | } | | j | ƒ \ } } | | j | ƒ \ } } |	 j | | d d | j | d | | d | d | d | d | | f ƒ | d 7} q)WqW| j | ƒ | j |	 ƒ | S(!   Niÿÿÿÿt   headers   _struct_ref.ids   _struct_ref.db_names   _struct_ref.db_codes   _struct_ref.pdbx_db_accessions   _struct_ref.entity_ids$   _struct_ref.pdbx_seq_one_letter_codes   _struct_ref.pdbx_align_begins   _struct_ref.biol_idR/   i   t   ?s   _struct_ref_seq.align_ids   _struct_ref_seq.ref_ids    _struct_ref_seq.pdbx_PDB_id_codes   _struct_ref_seq.pdbx_strand_ids   _struct_ref_seq.seq_align_begs+   _struct_ref_seq.pdbx_seq_align_beg_ins_codes   _struct_ref_seq.seq_align_ends+   _struct_ref_seq.pdbx_seq_align_end_ins_codes!   _struct_ref_seq.pdbx_db_accessions   _struct_ref_seq.db_align_begs   _struct_ref_seq.db_align_ends'   _struct_ref_seq.pdbx_auth_seq_align_begs'   _struct_ref_seq.pdbx_auth_seq_align_ends   \d+c            s{   |  j  ƒ  }  ˆ  j |  ƒ } | d  k	 s- t ‚ |  | j ƒ  | j ƒ  !} |  | j ƒ  } t | ƒ d k rq d } n  | | f S(   Ni    RÓ   (   Rg   t   searchR   R   t   startt   endR
   (   RF   t   st   resseqt   ins_code(   t   prog(    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   decode_residð  s     	t   |R2   (   s   _struct_ref.ids   _struct_ref.db_names   _struct_ref.db_codes   _struct_ref.pdbx_db_accessions   _struct_ref.entity_ids$   _struct_ref.pdbx_seq_one_letter_codes   _struct_ref.pdbx_align_begins   _struct_ref.biol_id(   s   _struct_ref_seq.align_ids   _struct_ref_seq.ref_ids    _struct_ref_seq.pdbx_PDB_id_codes   _struct_ref_seq.pdbx_strand_ids   _struct_ref_seq.seq_align_begs+   _struct_ref_seq.pdbx_seq_align_beg_ins_codes   _struct_ref_seq.seq_align_ends+   _struct_ref_seq.pdbx_seq_align_end_ins_codes!   _struct_ref_seq.pdbx_db_accessions   _struct_ref_seq.db_align_begs   _struct_ref_seq.db_align_ends'   _struct_ref_seq.pdbx_auth_seq_align_begs'   _struct_ref_seq.pdbx_auth_seq_align_end(   t   iotbx.cif.modelR   t   cift   modelt   blockt   loopRX   RŠ   t   add_rowR   R4   R   t   ret   compileR=   R9   RÀ   R
   t   i_seqs_at   i_seqs_bR)   R'   t   add_loop(   R&   t	   cif_blockt   iotbxt   struct_ref_loopt   db_namet   db_codet   db_accessiont   i_chainR   t   struct_ref_seq_loopRã   RÛ   t   align_idR=   t   i_range_endt   i_range_begint   i_a_range_begint   i_a_range_endt   i_b_range_begint   i_b_range_endt   resseq_begint   ins_code_begint
   resseq_endt   ins_code_end(    (   RÚ   sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   as_cif_blockÁ  s|           	!            			 
N(   Rv   Rw   R   R   R7   R-   R�   RÉ   RË   RÌ   RÑ   Rû   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyRy     s   h				g333333Ó?c	         C   s¡  | d IJt  d | d |  d | d d d t d t d	 d
 ƒ }	 |	 j ƒ  }
 d „  } t |
 ƒ d k r¡ d t |
 ƒ d d g } | r” | d | IJq¡ | | ƒ n  |	 j ƒ  } t | ƒ } d | k rÕ | j d ƒ n  d | k r| j d ƒ | d | j d ƒ IJ| d IJn  d4 } } | } | t
 k r7| } n  | d4 k rVd } | d IJn  | d k  rqt d ƒ ‚ n  t | ƒ d k r�d d } n¼t | ƒ d k rÁd d d d t | ƒ } n‹d „  } t | ƒ d } | d k sìt ‚ | d k r
| d | IJ| S| d k r]| | ƒ rDt | ƒ | } | d | IJ| Sd  d! d" d# | } nï d | } | | ƒ r>| | k rüd$ d% | t | ƒ f d& d' } | t
 k r;| d4 k	 r;| r;| | IJ| d( IJ| d) IJ| d* IJ| d+ | IJ| SqL| | k  r-d, d- | t | ƒ f d& d' } qL| d. IJd Sn d/ d0 | } | d4 k	 r‘| r„| d1 | IJ| d( IJ| d2 IJd S| | ƒ n t d3 ƒ ‚ d4 S(5   s  
  Utility function for reconciling the contents of the sequence file, the
  chains in the model, and the ASU.  Returns the number of copies of the
  sequence file to tell Phaser are present, or raises an error if this is
  either ambiguous or in conflict with the search model multiplicity.
  This is intended to allow the user to specify any combination of inputs -
  for instance, given a tetrameric search model, 2 copies in the ASU, and a
  monomer sequence file, the ASU contains 8 copies of the sequence(s).
  s=   Guessing the number of copies of the sequence file in the ASURz   RŒ   R}   R¡   i   R¢   RS   R‹   g333333Ó?c         S   s   t  |  d d d ƒ ‚ d  S(   Ns5    (Add the parameter force_accept_composition=True to sD   disable this error message and continue assuming 1 copy of sequence s   file.)(   R   (   t   msg(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   raise_sorry5  s    i    s>   %d chain(s) not found in sequence file.  If the sequence does sE   not accurately represent the contents of the crystal after adjusting s=   for copy number, molecular replacement and building may fail.s   WARNING: %siÿÿÿÿs8   WARNING: %d sequence(s) not found in model.  This is nots;   usually a problem, but it may indicate an incomplete model.s!   WARNING: assuming 1 copy of models%   Must have at least one copy of model.s8   The sequence file provided does not appear to match the s   search model.s6   The sequence file provided does not map evenly to the sC   search model; this usually means an error in the sequence file (or s>   model) such as accidental duplication or deletion of a chain. s   [counts: %s]c         S   s   t  |  ƒ |  k S(   N(   t   int(   t   x(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt
   is_integer^  s    g      ð?s6   Assuming %d copies of sequence file (as well as model)s+   Assuming %d copies of sequence file presents4   The search model does not appear to contain a round s5   number of copies of the sequence; this usually means s<   that your sequence file has too many or too few sequences.  s	   [freq=%g]s8   The number of copies of the model expected is less than s;   the number indicated by the sequence file (%d versus %d).  s=   This may mean that either the sequence file or the predicted s   number of copies is wrong.R/   s1   Since the number of copies was guessed by Xtriages-   based on the sequence file, it will be scaleds-   by %g to be appropriate for the search model.s7   The number of copies of the model expected exceeds the s5   number indicated by the sequence file (%d versus %d).s1   Assuming only one copy of sequence file contents.s5   The sequence file does not appear to contain a round s#   number of copies of the model (%g).s	   WARNING: s6   Ambiguous input, defaulting to 1 copy of sequence files%   No error but frequency not determinedN(   Ry   R9   R7   RË   R
   RÑ   t   sett   removet   countR   R   R   R5   R   Rþ   t   RuntimeError(   RŒ   Rz   t   force_accept_compositiont   copies_from_xtriaget   copies_from_userR‹   t(   assume_xtriage_copies_from_sequence_fileRm   R¡   R   RÊ   Rý   t   errorRÎ   t   unique_countst   freqt   model_copiesR   t   seq_freqt   inverse_freq(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   get_sequence_n_copies  s®    
		
	 
	
	








c         K   s©   d d l  m } | j |  d t d t ƒ} | j d k rI t d ƒ ‚ n  | j | d t d t ƒ} | j d k r‚ t d ƒ ‚ n  | j j | d	 <| j | d
 <t | �  S(   Niÿÿÿÿ(   t   file_readert   raise_sorry_if_errorst"   raise_sorry_if_not_expected_formatR¯   s"   Can't parse %s as a sequence file.t   pdbs&   Can't parse %s as a PDB or mmCIF file.Rz   RŒ   (	   Ré   R  t   any_fileR9   t	   file_typeR   t   file_objectt	   hierarchyR  (   t   seq_filet   pdb_fileR^   R  t   seq_int   pdb_in(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt    get_sequence_n_copies_from_files¡  s    		c         K   s  d d l  m } | j |  d t d t ƒ} | j d k rI t d ƒ ‚ n  | j | d t d t ƒ} | j d k r‚ t d ƒ ‚ n  | j j | d	 <| j | d
 <t | �  } i  } i  } xÁ | j	 D]¶ }	 |	 j
 }
 |	 j } |
 d  k rþ t d | |  f ƒ ‚ n  | j |
 j } | | k r@| | | k rJt d | ƒ ‚ qJn
 | | | <|	 j | k rfg  | | <n  | | j | ƒ qÁ W| S(   Niÿÿÿÿ(   R  R  R  R¯   s"   Can't parse %s as a sequence file.R  s&   Can't parse %s as a PDB or mmCIF file.Rz   RŒ   s'   Can't map chain %s to a sequence in %s.s!   Multiple unique chains named '%s'(   Ré   R  R  R9   R  R   R  R  Ry   RŠ   R   R'   R   R(   R:   (   R  R  R^   R  R  R  R   t   chain_to_sequence_mappingst   sequence_to_chain_mappingsR   Rµ   R'   R(   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   group_chains_and_sequences²  s>    					
(   t
   __future__R    t   libtbxR   R   t   libtbx.philt   libtbx.utilsR   R   R   R   Rƒ   t   philt   parseR…   R   R   R;   R<   R   t   objectR   Ry   R7   R   R„   R  R  R  (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/validation/sequence.pyt   <module>   s2   			
ìÿ €	