ó
šxTc           @   s  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 d  d l	 Z d  d l m
 Z
 d  d l Z d  d l m Z d  d	 l m Z d  d l Z d  d l Z d
 Z e j j d i e j d 6e j d 6ƒ Z d „  Z d „  Z d „  Z d d „ Z d S(   iÿÿÿÿ(   t   division(   t	   tbx_utils(   t   sculptor(   t   ncs(   t   proxyN(   t   bioinformatics(   t   Sorry(   t   flexs   Model preparations%  input
        .help = "Input files"
    {
        homology_search
            .help = "Homology search"
        {
            file_name = None
                .help = "File name"
                .optional = False
                .type = path
                
            max_hits = None
                .help = "Max number of hits"
                .optional = True
                .type = int
        }
        
        remove_alternate_conformations = False
            .help = "Remove alternate conformations"
            .type = bool
            .optional = False
            
        max_model_count = 1
            .help = "Max models to keep when processing an entry with multiple structure"
            .type = int
            .optional = True
    }
    
    output
        .help = "Output options"
    {
        folder = .
            .help = "Output folder; None is current folder"
            .type = path
            .optional = False
    }
    
    sculptor
        .help = "Sculptor keywords"
    {
        %(sculptor)s
        %(other)s
    }
    R   t   otherc      	   C   sŽ  t  j d |  d t ƒ \ } } } | rM t d | ƒ t d d j | ƒ ‚ n  t  j d | d d d	 g ƒ \ } } | r� t d | ƒ t d
 d j | ƒ ‚ n  d | k r?| d } t | ƒ } | j d d j | ƒ t  j	 d d d | ƒ t  j
 d d d | ƒ f ƒ | j g  | D]" } t j j d d | f ƒ ^ qƒ n  t j d | ƒ }	 | j d ƒ | j d ƒ | j |	 j ƒ  ƒ |	 j ƒ  }
 |
 S(   Nt   argst   phil_mastert   loggers   Unknown keyword(s): %ss   , t   filest   homologys   .hhrs   .xmls   Unknown file(s): %ss   Guessing: %s %s %st   verbt   ist   countt   words   homology search files   %s=%ss   input.homology_search.file_namet   sourcest    s   All configuration options:
(   R   t   process_command_linet   PHIL_MASTERt   usageR   t   joint   group_files_on_extensiont   lent   infot	   conjugatet   inflectt   extendt   libtbxt   philt   parset   fetcht   as_strt   extract(   R	   R   t   phil_objectsR   t   unknownst	   files_fort   hssR   t   ft   merged_philt   params(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/mr_model_preparation.pyR   F   s:    		
0c         C   s+   |  j  d ƒ |  j  d ƒ |  j  d ƒ d  S(   Ns!   Prepares MR models for MR_Rosettas   Required input:s9   hhpred_file - results of homology search in hhpred format(   R   (   R   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/mr_model_preparation.pyR   v   s    c         C   s¹  | j  d |  j ƒ t |  j g ƒ } | j ƒ  s6 | St j d |  d t j j ƒ } x^| j ƒ  d j	 ƒ  D]F} | j  d | j ƒ | j
 d | ƒ \ } } t d t | j ƒ  ƒ d ƒ } | j  d | ƒ | j  d	 t | ƒ ƒ t | ƒ | k  r| j  d
 ƒ | j  d ƒ qk n  | j  d | ƒ t j d |  d | d | ƒ }	 d }
 |	 j ƒ  } | j  d | ƒ | |
 k  r”| j  d ƒ | j  d ƒ qk n  | j  d ƒ | j | j ƒ qk W| S(   Ns0   Finding chains identical in sequence to chain %st   chaint   macromolecule_typei    s   Comparing with chain %si
   i   s$   Minimum acceptable HSSP length is %ss    Length of longest HSSP found: %ss   The longest HSSP is too shorts   Discarding this chains-   Performing alignment using numbering shift %st	   referencet   alignedt   offsetgš™™™™™é?s,   Sequence identity between the two chains: %ss   Sequence identity too lows   This chain passes all tests(   R   t   idt   sett   atomsR   t   ReferenceChaint   MacromoleculeTypet   proteint   modelst   chainst   determine_numbering_shiftt   maxR   t   residue_groupst   ChainAlignmentt   get_sequence_identityt   add(   R-   t   othersR   t
   identicalst   refR+   t   overlapR/   t   min_acceptedt	   chain_alit   min_sequence_identityt   seqid(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/mr_model_preparation.pyt   find_chains_identical_to}   sB    			c         C   sü  t  j d t d | ƒ \ } } zµ| j t  j d t d d ƒ ƒ t d |  d | ƒ } | j j j s} t	 d | ƒ t
 d ‚ n  | j d	 | j j j ƒ t  j j d
 | j j j ƒ } | j } | j j j r| j d | j j j ƒ | j d | j j j ƒ n  t  j d | j j d | ƒ | j d ƒ d d t j d | j j j d | j j j d | j j j d | j j j d | j j j d | j j j d | j j j ƒ f g } t j d | j j ƒ } t j d | d | ƒ }	 | j j  d  k	 r| j j  d k rt
 d ‚ qn  | j d ƒ t" j# ƒ  }
 | j d |
 j$ ƒ  ƒ | j d ƒ g  } xbt% | j& ƒ  ƒ D]N\ } } | j d | | j' | j( f ƒ | j d  ƒ | j | j) ƒ |
 j* d! | j' ƒ rä| j d" ƒ | j d# ƒ qfn  |
 j+ d! | j' ƒ rD| j d$ ƒ |
 j, d! | j' ƒ } | s't- ‚ | j d% | ƒ | | _' n  | j d& ƒ t. j/ ƒ  } yL t0 j1 j2 j2 d' | j' d( | ƒ } t0 j1 j d) | j3 ƒ  d* d+ ƒ j4 ƒ  } Wnc t5 k
 r} | j6 d, | ƒ | j7 ƒ  } | rû| j6 d- ƒ | j6 | ƒ n  | j6 d# ƒ qfn X| j j8 r;| j d. ƒ t j8 d/ | ƒ n  | j j  r}| j d0 | j j  ƒ t j9 d/ | d | j j  ƒ n  | j: ƒ  s°| j6 d1 | j' ƒ | j6 d# ƒ qfn  | j d2 | j( ƒ | j; ƒ  } | j< | j= d3 | j( ƒ t> ƒ } | j: ƒ  s%| j6 d4 | j( ƒ | j6 d# ƒ qfn  d5 t? | j@ ƒ  d jA ƒ  ƒ k  rk| j6 d6 | j( ƒ | j6 d7 ƒ n  | j@ ƒ  d jA ƒ  d } tB jC jD jE d8 | d9 d: ƒ sÃ| j6 d; ƒ | j6 d# ƒ qfn  tF d< | d= | j< | j= d> | j( ƒ t> ƒ d | ƒ } | j d? d@ jG | ƒ ƒ g  } x\tH | ƒ D]N} | j dA | ƒ | j< | j= dB | ƒ t> ƒ } | jI } | j dC ƒ | j tJ | ƒ ƒ | j dD ƒ t jK dE t  jL d/ | d dF | j' | f ƒ dG |	 dH t  jM dI | d dJ | ƒ g dK i  dL | dM | dN | j jN dO | j jO dP tP jQ jG | j j dF | j' | f ƒ dQ dR dS t jR d | ƒ } t? | ƒ d5 k sht- ‚ | jS | ƒ q'W| jT | ƒ | j dT | j' ƒ | j dU d@ jG | ƒ ƒ qfW| j dV | j j j ƒ Wd  | jU | ƒ | jV ƒ  | jW ƒ  X| S(W   Nt   namet   outt   programt   versions   0.0.2R	   R   s   No homology search specifieds    Reading homology search file: %st	   file_names"   Considering only the first %s hitst	   max_countt   folders   Setting up sculptor...t   protocolR   t   deletiont	   polishingt   renumbert   pruningt
   completiont   renamet   bfactorR*   t
   processorsi    s)   max_model_count: positive number expecteds$   Reading list of obsoleted entries...s   Read %s entriess   Processing hits...s   Hit #%s: %s chain %ss   Annotation:t
   identifiers   This entry has been retracted.s   Continue with next...s   This entry has been obsoleted.s   Replacement entry: %ss   Retrieving file...R0   t   logt   linest   source_infot   rcsbs   WARNING: download problem (%s)s
   Fetch log:s#   Removing alternate conformations...t   roots/   Discarding models above requested max %s if anys   No atoms in PDB file: %ss   Selecting reference chain %ss   pepnames and chain '%s's   No atoms found for chain %si   s    Multiple chains with chain_id %ss   Using only firstR+   t
   confidencegš™™™™™é?s)   Reference chain not recognized as proteinR-   R>   s   pepnames and not chain '%s's   Identical chains: %ss   , s   Processing chain %ss
   chain '%s's
   Alignment:s   Running sculptor...t   pdbs   %s_%st   factoryt
   alignmentst	   alignments   Hit %st   sequence_fort	   sculptorst	   discardert
   min_lengtht   min_fractiont   prefixt   suffixt   sculptt   output_methods   Finished processing hit %ss   Written models: %ss   Processing complete for %s(X   R   t
   get_loggert   PROGRAMR   t
   get_bannerR   t   inputt   homology_searchRK   R   R   t   HomologySearchObjectt	   from_filet   objectt   max_hitst   restrictt   create_output_foldert   outputRM   R   t   get_sculptor_objectt   macromoleculeRO   RP   RQ   RR   RS   RT   RU   t   get_discarder_objectt   heterot   chain_sample_factoryt   max_model_countt   NoneR   t   get_pdb_redirectionsR   t	   enumeratet   hitsRW   R+   t
   annotationt	   retractedt	   obsoletedt   replacement_fort   AssertionErrort	   cStringIOt   StringIOt   iotbxR^   R!   t   readt   construct_hierarchyt   RuntimeErrort   warningt   getvaluet   remove_alternate_conformationst   restrict_model_countR2   t   atom_selection_cachet   selectt	   selectiont   TrueR   R6   R7   R   R4   R5   t   recognize_chainRF   R   t   sortedRa   t   strt   process_single_pdbt	   PDBObjectt   AlignmentObjectt   min_hssp_lengtht   min_matching_fractiont   ost   patht   write_pdb_outputR   t   appendt   removeHandlert   flusht   close(   R	   RH   R   t   handlerR*   t   hsobjR'   Rc   Rd   R_   t   redirectionst	   processedt   indext   hitt   replt	   logstreamt   streamR\   t   et   fetchlogt   asct   ref_rR-   t   identical_chainst   writtent   ct   croott   alit   ofiles(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/mr_model_preparation.pyt   run¯   s,   			 				"											
(   t
   __future__R    t   phaserR   R   R   R   t	   iotbx.pdbRˆ   t   iotbx.pdb.fetchR   t   libtbx.philR   t   libtbx.utilsR   t   scitbx.array_familyR   Rœ   R†   Rl   R   R    t   PHIL_SCULPTORt
   PHIL_OTHERR   R   R   RF   R}   R¶   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/mr_model_preparation.pyt   <module>   s*   	,
	0		2