ó
­™xTc           @   sð   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 m Z d  d l	 Z	 d  d l
 Z
 d e f d „  ƒ  YZ d e d d „ Z e d	 „ Z e d d
 „ Z d „  Z e j j d ƒ Z d d e
 j d „ Z d „  Z d S(   iÿÿÿÿ(   t   division(   t   easy_runN(   t   Sorry(   t   adopt_init_argst   align_pdb_residuesc        
   B   sž   e  Z d  Z d d d d d d d e d d „	 Z d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d d d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  
  Provides mapping between original residue numbers or IDs and a reference
  numbering, via multiple sequence alignment.  Depending on whether or not
  the insertion code was used in the original PDB files, it can use either
  the resseq or the resid as the lookup key.
  t   reference_seqi    c         C   s0  t  |  t ƒ  ƒ t | ƒ } | d  k	 s: | d  k	 s: t ‚ | d k rX | t | ƒ k s^ t ‚ | d  k	 r» | d  k s| t ‚ t | ƒ t | ƒ k sš t ‚ | d  k s÷ | d  k s÷ t ‚ n< t | ƒ t | ƒ k sÙ t ‚ | d  k s÷ | d  k	 s÷ t ‚ g  |  _ d  |  _ |  j d | ƒ |  j ƒ  d  |  _	 d  S(   Ni   t   out(
   R   t   localst   lent   Nonet   AssertionErrort   fasta_namest   _name_lookupt   run_alignmentt   build_lookup_tableR   (   t   selft   pdb_sequencest	   pdb_namest   pdb_offsetst
   pdb_residst   reference_sequencet   reference_sequence_namet   reference_sequence_offsett   reference_sequence_residst   reference_indext   substitute_namesR   t   n_models(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   __init__   s     $!		
c   
      C   sð  t  } |  j rd i  |  _ xU t |  j ƒ D]5 \ } } t | ƒ |  j | <|  j j t | ƒ ƒ q( Wn |  j |  _ |  j d  k	 r» |  j
 d  k s” t ‚ |  j d  k	 sT|  j d  k	 sTt ‚ n™ |  j
 } t } | d  k ræ d } |  _
 n  |  j | |  _ |  j d  k	 r|  j | |  _ n |  j | |  _ |  j rD|  j | |  _ n |  j | |  _ d j g  t |  j |  j ƒ D] \ } } d | | f ^ qpƒ } | sÂd |  j |  j f }	 |	 d | } n  t | d | ƒ|  _ |  j d  k	 sìt ‚ d  S(   Ni    s   
s   >%s
%sR   (   t   FalseR   R   t	   enumerateR   t   strR   t   appendR   R	   R   R
   R   R   t   TrueR   R   R   R   t   joint   zipt   get_muscle_alignmentt
   muscle_aln(
   R   R   t   use_pdb_sequencet   it   namet   i_reft   nt   st   fastat   ref_seq_fasta(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyR   5   s<    				/	c         C   s¬  i  |  _  i  |  _ i  |  _ |  j j j |  j ƒ } |  j j | |  _ | d  k	 sX t
 ‚ xMt |  j j ƒ D]9\ } } | | k rUd  |  j  | <|  j d  k r¤i  } d  g |  j ƒ  } d } } xp |  j D]e } | d k r'|  j | }	 |	 d  k	 r|	 j ƒ  }	 | | |	 <|	 | | <n  | d 7} n  | d 7} qÌ W| |  j | <| |  j | <q¤qk |  j j | ƒ }
 |  j j | } d } } } |  j d  k	 rJg  } x… t | |  j ƒ D]q \ } } | d k rÒ| d 7} n  | d k räq­q­| d k r | j d  ƒ n | j | |  j ƒ | d 7} q­W| t | ƒ k s:t
 ‚ | |  j  | <qk i  } i  } d  g |  j ƒ  } xØ t | |  j ƒ D]Ä \ } } d  } } | d k rÓ|  j | } | d  k	 rÆ| j ƒ  } n  | d 7} n  | d k r6|  j |
 | } | d  k	 r)| j ƒ  } | | | <| | | <| | | <n  | d 7} n  | d 7} q|Wt | ƒ t |  j |
 ƒ |  j |
 j d  ƒ k s}t
 ‚ | |  j  | <| |  j | <| |  j | <qk Wd  S(   Ni    t   -i   (   t   _lookup_tablet   _indicest   _resids_paddedR$   t   namest   indexR   t
   alignmentst   _reference_alignmentR	   R
   R   R   t   get_alignment_sizeR   t   stripR   R"   R   R   R   R   t   count(   R   R(   R&   R'   t   indicest   resids_paddedt   ht   kt   resit   residt   i_pdbt   alnt   jt   new_resseqst   res1t   res2t
   new_residst   resid1t   resid2(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyR   \   s€    			




 c         C   s   t  t |  d g  ƒ ƒ S(   NR4   (   R   t   getattr(   R   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyR5   ¥   s    c         C   s3   t  | d ƒ } | j t |  j ƒ ƒ | j ƒ  d  S(   Nt   w(   t   opent   writeR   R$   t   close(   R   t	   file_namet   f(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt
   write_file¨   s    c         C   s"   |  j  d  k	 r |  j  | S| Sd  S(   N(   R   R	   (   R   t   pdb_name(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   get_name_index­   s    c         C   sT   | d  k	 r. | d  k s t ‚ |  j | | ƒ S| d  k	 s@ t ‚ |  j | | ƒ Sd  S(   N(   R	   R
   t   convert_residue_numbert   convert_resid(   R   RO   t   resseqR=   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   get_residue_position´   s
    c         C   s6   | d  k	 s t ‚ |  j | ƒ } |  j | } | | S(   N(   R	   R
   RP   R/   (   R   RO   R=   t   seq_nameR8   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   get_resid_array_index¼   s    c         C   s6   g  } x) |  j  D] } | j |  j | | ƒ q W| S(   N(   R   R   R0   (   R   R2   t   residsR'   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   get_all_resids_at_indexÂ   s    c         C   s3   |  j  d  k r' |  j |  j } | | S| d Sd  S(   Ni   (   R   R	   R0   R   (   R   R2   RW   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   get_reference_residÈ   s    c         C   s¢   |  j  d  k	 s t ‚ t | t ƒ s* t ‚ |  j | ƒ } |  j | d  k rP | Sy |  j | | j ƒ  SWn2 t k
 r� } t	 d | t
 | |  j f ƒ ‚ n Xd  S(   NsŒ   Encountered IndexError attempting to convert residue ID!
Values:
  pdb_name = %s
  resid = %s
  seq_name = %s

Dump of full alignment:
  %s
(   R   R	   R
   t
   isinstanceR   RP   R.   R6   t   KeyErrort   RuntimeErrorRS   R$   (   R   RO   R=   RU   t   e(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyRR   Ï   s    	c         C   sÌ   |  j  d  k	 s t ‚ |  j | ƒ } t | t ƒ s9 t ‚ |  j | d  k rP | S|  j j | ƒ } |  j  | } | | d } y |  j | | SWn5 t	 k
 rÇ } t
 d | | | | |  j f ƒ ‚ n Xd  S(   Ni   sž   Encountered IndexError attempting to convert residue number!
Values:
  pdb_name = %s
  resseq = %s
  seq_name = %s
  i_res = %s

Dump of full alignment:
  %s
(   R   R	   R
   RP   RZ   t   intR.   R   R2   t
   IndexErrorR\   R$   (   R   RO   RS   RU   R>   t   offsett   i_resR]   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyRQ   ã   s    
N(   t   __name__t
   __module__t   __doc__R	   R   R   R   R   R5   RN   RP   RT   RV   RX   RY   RR   RQ   (    (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyR      s*   '	I							c         C   s"  | d k r t j } n  g  } d } d } g  } d „  }	 xó t |  | ƒ D]â \ }
 } |
 j ƒ  j d k sp t ‚ |
 j ƒ  j ƒ  } | j	 d t
 ƒ } | j | ƒ | j d t
 ƒ } | j g  | D] } |	 | ƒ ^ qÀ ƒ |
 | k rü | } | d | IJqI | d k rI | d k rI | } | d | IJqI qI W| d k	 rq| } t d | d | d | d	 | d
 | d | ƒ } n­ | j ƒ  j d k sŒt ‚ | j ƒ  j ƒ  } | j	 d t
 ƒ } d } } | j d t
 ƒ } g  | D] } |	 | ƒ ^ qÓ} t d | d | d | d | d | d
 | d | ƒ } | S(   sT  
  Convenience function: takes a collection of *single-model, single-chain* PDB
  hierarchies, plus optional reference hierarchy, extracts sequences and
  resseq offsets or resids, and generates alignment object.  By default, if
  any of the hierarchies contain atoms with insertion codes, the resid mapping
  will be used automatically.
  i    c         S   s   |  d  k r d  S|  j ƒ  S(   N(   R	   R6   (   t   string_or_none(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyR6     s    i   t   skip_insertionss!     Using %s for sequence numberingR   R   R   R   R   R   R   R   N(   R	   t   syst   stdoutR"   t   overall_countst   n_chainsR
   t
   only_modelt
   only_chaint   as_padded_sequenceR   R   t   get_residue_idsR   (   t   hierarchiest   hierarchy_namest   reference_hierarchyR   t   logR   R&   R   R   R6   t	   hierarchyR'   t   chaint	   chain_seqRW   R=   R   t   msa_managerR   R   R   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   align_pdb_hierarchiesû   sX    	&
	c         C   sÍ   | s t  ‚ t j j d d ƒ s0 t d ƒ ‚ n  t j j d ƒ } t j d k r^ | d 7} n  t j j	 | ƒ s t d ƒ ‚ n  t
 |  t ƒ s” t  ‚ d | } | r± | d	 7} n  t j | d
 |  ƒ} | j S(   NR'   t   muscles'   MUSCLE not available or not configured.s   muscle/exe/musclet   nts   .exes#   muscle executable is not available.s   %s -quiet -clws    -groupt   stdin_lines(   R
   t   libtbxt   envt
   has_moduleR\   t   under_buildt   osR'   t   patht   isfileRZ   R   R   t   fully_bufferedt   stdout_lines(   t   fasta_sequencest   group_sequencest   exe_patht   cmdt
   muscle_out(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt
   run_muscle9  s    
	c         C   s`   t  |  | ƒ } d d l m } | d j | ƒ ƒ \ } } | d  k	 r\ | d j | ƒ IJn  | S(   Niÿÿÿÿ(   t   clustal_alignment_parses   
(   R‰   t   iotbx.bioinformaticsRŠ   R!   R	   (   R„   R…   R   Rˆ   RŠ   t	   alignmentt   null(    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyR#   L  s    c      	      s  d d l  m ‰  i  } x@ t |  d d ƒD], \ } } | j | d | ƒ } | | | <q) Wt d d j ‡  f d †  | j ƒ  Dƒ ƒ ƒ } t t | j	 | j
 ƒ ƒ ‰ t ‡ f d	 †  | j ƒ  Dƒ ƒ sÊ t ‚ ˆ  j d
 g  |  D] } | j ^ qÚ d g  |  D] } ˆ | | ^ qö d | j ƒ S(   Niÿÿÿÿ(   t   bioinformaticst   starti   s   Chain_%dR„   s   
c         3   s6   |  ], \ } } t  ˆ  j d  | d | j ƒ ƒ Vq d S(   R'   t   sequenceN(   R   R�   (   t   .0t   seqR'   (   RŽ   (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pys	   <genexpr>`  s   c         3   s   |  ] } | ˆ  k Vq d  S(   N(    (   R‘   R)   (   t   lookup(    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pys	   <genexpr>f  s    R1   R3   t   program(   t   iotbxRŽ   R   t   getR#   R!   t   itemst   dictR"   R1   R3   t   allt   valuesR
   t   clustal_alignmentR'   R”   (   t	   sequencest   name_forR&   R’   R'   RŒ   (    (   RŽ   R“   s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   get_muscle_alignment_orderedT  s    (	!s¥  
muscle
  .caption = PHENIX includes the open-source multiple sequence alignment     program MUSCLE, written by Bob Edgar.  It can produce output identical in     format to CLUSTALW, which is suitable for input to the Sculptor model     preparation program.  You may provide all sequences in a single file,     or in as many different files as desired.  Alternately, you may provide     one or more PDB files from which the chain sequence(s) will be extracted     (note however that this is only useful for homogenous models).
  .style = caption_img:icons/custom/msa64.png caption_width:480
{
  seq_file = None
    .type = path
    .multiple = True
    .short_caption = Sequence or PDB file
    .style = file_type:seq,pdb use_list input_file
  output_file = None
    .type = path
    .style = bold file_type:aln new_file
  load_in_text_editor = True
    .type = bool
    .short_caption = Open alignment in text editor when complete
}c         C   sœ  | d  k	 s t ‚ | j j } | j j } | d  k sB | d k r` t j j t j ƒ  d ƒ } n  d d l	 m
 } d d l m } m } g  } xƒ| D]{}	 |	 j d ƒ sÕ |	 j d ƒ sÕ |	 j d ƒ sÕ |	 j d	 ƒ r¯| j |	 d
 d ƒj }
 |
 j } | j ƒ  d } t } x€ | j ƒ  D]r } | j ƒ  r| j ƒ  } t j j |	 ƒ } d t j j | ƒ d | j f } | j | | | ƒ ƒ t } qqW| st d d |	 ƒ ‚ qq“ y) | |	 d t ƒ\ } } | j | ƒ Wq“ t k
 r} t d d |	 t | ƒ f ƒ ‚ q“ Xq“ Wt  | ƒ d k  r3t d ƒ ‚ n  d j g  | D] } | j! d ƒ ^ q@ƒ } t" | ƒ } t# | d ƒ j$ d j | ƒ ƒ | d j | ƒ f S(   Nt    s
   muscle.alniÿÿÿÿ(   t   file_reader(   t   any_sequence_formatR�   s   .pdbs   .ents   .pdb.gzs   .ent.gzt
   force_typet   pdbi    s   %s_%ss2   The PDB file %s does not contain any recognizable s   protein chains.t   assign_name_if_not_defineds1   Error parsing '%s' - not a recognizable sequence s   format.  (Original message: %s)i   s0   Need at least two valid sequences to run MUSCLE.s   
iP   RH   (%   R	   R
   Rx   t   seq_filet   output_fileR   R€   R!   t   getcwdR•   R    R‹   R¡   R�   t   endswitht   any_filet   file_objectRs   t   modelsR   t   chainst
   is_proteinRm   t   basenamet   splitextt   idR   R    R   t   extendt	   ExceptionR   R   t   formatR‰   RI   RJ   (   t   argst   paramsR   t	   seq_filesR¦   R    R¡   R�   t   seqsRL   t   pdb_inRs   t   first_modelt   found_proteinRt   Ru   t	   base_nameRU   t   seq_objectst   non_compliantR]   R’   t   combinedRˆ   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   runˆ  sL    	#	"+c         C   s+   t  |  j j ƒ d k r' t d ƒ ‚ n  d  S(   Ni    s   No sequence files provided!(   R   Rx   R¥   R   (   Rµ   (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   validate_params±  s    (    (   t
   __future__R    R{   R   t   libtbx.philt   libtbx.load_envt   libtbx.utilsR   R   R   Rg   t   objectR   R	   R    Rw   R‰   R#   Rž   t   philt   parset   master_philRh   R¿   RÀ   (    (    (    s7   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/msa.pyt   <module>   s$   í:			)