ó

šxTc           @   sÅ   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l m Z d d d „  ƒ  YZ d e f d „  ƒ  YZ d S(	   iÿÿÿÿN(   t   Thread(   t   CalcCCFromMRsolutionst   ThreadLoggerc           B   s   e  Z d d  „ Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   loggert   mylock(   t   selfR   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyt   __init__   s    	c         C   s}   |  j  d  k r |  j  j ƒ  n  |  j j | | ƒ |  j j d j ƒ  |  j j d j ƒ  |  j  d  k ry |  j  j ƒ  n  d  S(   Ni    i   (   R   t   Nonet   acquireR   t   logt   handlerst   flusht   release(   R   t   loglevelt   mstr(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyR	      s    N(   t   __name__t
   __module__R   R   R	   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyR      s   t   PhaserRunnerc           B   sJ   e  Z d d  d d d d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(	   i0   i    i   c         C   sü   t  j |  ƒ | |  _ | |  _ | |  _ |	 |  _ t | j |	 ƒ |  _ | |  _	 |  j	 d |  _
 t |  _ | |  _ | |  _ | |  _ |  j d |  _ d |  _ | |  _ d |  _ | |  _ | |  _ |  j t |
 ƒ d |  _ t j j d k rø t |  _ n  d  S(   Ns   .txti<   i    s   The s   . threadt   win32(   R    R   t   searchest   baseinputfnamet	   calclabelR   R   R   t   tloggert   modelinputfnameprefixt   modelinputfnamet   Falset	   osdepboolt   rootstrt   nmaxsolst	   fmaxhourst   nmaxminutest   errorcodesumt   minmodelordert   prefixt
   maxthreadst   parentt   strt   namet   ost   syst   platformt   True(   R   R   R   R   R   R#   R   R   R   R   t   threadnumberR"   R    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyR   #   s*    														c      
   C   s–  t  |  j d ƒ } | j ƒ  } | j ƒ  t  t j j | |  j ƒ d ƒ } t  t j j | |  j ƒ d ƒ j ƒ  } | j ƒ  t j	 d d | ƒ } |  j
 j t j d |  j d |  j d |  j ƒ t j d ƒ t |  j ƒ  ƒ } t d |  j | d ƒ } d | }	 | |  j |	 | }
 t j | |  j d	 ƒ } t  | d
 ƒ } | j |
 ƒ | j ƒ  |  j j } t } d } zº y� |  j
 j t j |  j d | d |
 ƒ t j | d |  j d t j d t j d t j ƒ} | j  d |
 ƒ \ } } | j! } Wn2 t" k
 r)} t# |  _$ |  j
 j t j% | ƒ n XWd  | d k r‘|  j
 j t j% d t j j |  j j& | ƒ d t' | ƒ d | ƒ |  j( | 7_( n  | SX(   Nt   rs   SOLU ENSE.*
t    s   Concatenating: s   MRinput.txt = s    + rootstr + i   s   JOBS %d
s   MRinput.txtt   wi    s     is executing Phaser in folder, s   

t   shellt   stdint   stdoutt   stderrt   inputsD   Something went wrong with the phaser calculation using input file: 
s   
Return code is: s   
()   t   openR   t   readt   closeR&   t   patht   joinR   t   ret   subR   R	   t   loggingt   DEBUGR   t   timet   sleept   lent   GetActivethreadst   maxR"   R   t	   posixpatht   writeR#   t	   phaserexet
   subprocesst   INFOR%   t   PopenR   t   PIPEt   communicatet
   returncodet	   ExceptionR)   t   failuret   ERRORt	   topfolderR$   R   (   R   R6   t   baseinputfilet	   baseinputt   modelinputfileR   t   modelinput_no_vrmst   nactivethreadst   nopenmpthreadst   jobstrt   myinputt   inputfilenamet	   inputfilet   commandt   outputt   retvalt   myproct
   stdoutdatat
   stderrdatat   m(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyt	   PhaserRunA   sJ    
!'
$

 	6c         C   s}   t  j j t j |  j j d ƒ ƒ } t  j j t j |  j j d d ƒ ƒ } | sW | ru |  j j t	 j
 d | ƒ t St Sd  S(   Nt   StopThreadsGracefullys   ..s   Aborting calculation on %s.
(   R&   R6   t   existsRA   R7   R#   RM   R   R	   R:   RE   R)   R   (   R   t   foldert   stop1t   stop2(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyt   AbortRequested{   s    $'c         C   s“   g  t  j ƒ  D] } | j ^ q } t | ƒ } |  j | k rP | j |  j ƒ n  g  } x6 | D]. } | j |  j ƒ d k r] | j | ƒ q] q] W| S(   Niÿÿÿÿ(   t	   threadingt	   enumerateR%   t   sett   removet   findR!   t   append(   R   t   tt   activet   setofthreadnamest   namesofproperthreads(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyR?   ˆ   s    "c          C   so  yt  j |  j j |  j |  j j d ƒ } t j |  j j |  j | |  j ƒ } xat |  j	 ƒ D]P\ } } g  | d  D]# } g  | D] } | d ^ q„ ^ qw d } d j | ƒ } t  j |  j j d | ƒ }	 t  j
 |	 |  j j ƒ }	 |  j |	 ƒ rù Pn  |  j j t j d | d t |  j	 ƒ |  j f ƒ |  j j | d  d ƒ xi| d D]]}
 |  j j t j d |  j ƒ t |
 j d ƒ ƒ } |  j | k rœqOn  |  j j t j d	 |  j | f ƒ |  j |	 ƒ rÒPn  t g } d
 g } t } | j | |
 | ƒ t k rq| d t k rB|  j j t j d |  j |
 | d f ƒ n& |  j j t j d |  j |
 | f ƒ t } n  |  j d | d |
 } | d } t  j
 |	 ƒ t  j |  j d | d |
 } d | d |  j |  j f |  _ |  j d |
 d |  _ | r<|  j |	 ƒ } t | d d ƒ } | j  | ƒ | j! ƒ  n  | j" | |
 | ƒ } | j# | |
 | ƒ | d k  r™|  j j t j d |  j |
 | f ƒ n  |  j j t j d |  j |
 | f ƒ t$ d t% |  j& t |  j' ƒ  ƒ d ƒ ƒ } t  j |  j j d | ƒ }	 t  j |	 |  j d | |
 d ƒ } t  j |	 |  j d | d |
 d ƒ } t( j) j* | ƒ t k rÔt+ j, |  j j |	 |  j | |
 d t d |  j d | d t ƒ} t | d ƒ } | j  | ƒ | j! ƒ  n# |  j j t j d |  j | f ƒ t  j |  j j d | ƒ } | } t( j) j* | ƒ t k rzg  } | j- | ƒ | j. ƒ  d j | ƒ } t  j |  j j d | ƒ } n  t  j | |  j d  | d! ƒ } t  j | |  j d" | d# ƒ } t( j) j* | ƒ oÝt( j) j* | ƒ t k rRt+ j, |  j j | |  j d$ | d% d t d |  j d | d t ƒ} t | d ƒ } | j  | ƒ | j! ƒ  n# |  j j t j d |  j | f ƒ | d | k rOt  j |  j j d | d ƒ } | d } t  j | |  j d  | d! ƒ } t  j | |  j d" | d# ƒ } t( j) j* | ƒ ot( j) j* | ƒ t k r†t+ j, |  j j | |  j d$ | d% d t d |  j d | d t ƒ} t | d ƒ } | j  | ƒ | j! ƒ  q¬|  j j t j d |  j | f ƒ qOqOWq` W|  j' ƒ  } d& j | ƒ } | d
 k räd' } n  |  j j t j d( |  j | f ƒ Wn` t/ k
 rj} |  j j t j0 |  j d) t1 | ƒ d* t2 j3 ƒ  ƒ |  j4 d 7_4 t/ | ‚ n Xd  S(+   Ns   _DoneMRJobs.txtiÿÿÿÿi    t   ,t   MRs#   
%d. MR calculation out of %s by %si   s!   %s: Adding one more partial models   %s: modelorder %sR,   s7   %s: A previous job found the partial structure %s in %ss=   %s: A previous job didn't find the partial structure %s in %st   {t   }s   .sols   ROOT s    
KILL TIME %d
PURGE RNP NUMB %d
s   }.txts   .logR-   s@   %s: Phaser didn't even write a solution file, %s, for the %s jobs\   %s: Done placing partial model, %s, for the %s job.
Now calculating correlation coefficientst   CalcCC_s   CalcCC_{s   .txtt   bCalcCCR   t   nthreadst   debugs   %s: %s already donet   TemplateSolutionst
   RNPCalcCC_s   .superposed.logs   RNPCalcCC_{s   }.superposed.txtt   RNPs   .superposeds   , s	   no otherss'   %s is done and waits for %s to finish.
s   : s   
(5   RA   R7   R#   RM   R   t   targetpdbidt   BLASTMRutilsR   Rg   R   t   relpathRe   R   R	   R:   RE   R>   R%   t   PrepareInputFilest   splitR    R   R)   t   PhaserRanPartModelsBeforet   sepR   R   R   R   R   R_   R3   RB   R5   t   IsthereaPartialSolutiont   NoticeCompletedPhaserRunR@   t   intR"   R?   R&   R6   Ra   R   t   CalcCCFromMRsolutiont   extendt   reverseRJ   RL   R$   t	   tracebackt
   format_excR   (    R   t   completedrunfnamet   testsolt   it   modelst   e1t   et	   modelsdirt   workdirt   MRfoldert
   partmodelst
   modelordert   bsolvedt   founddirt
   brunphasert   solfnameprefixt   solfnamet   rootnamet	   stdoutputt   phaserlogfilet   nPartSolWithThisStrategyRv   t   logfnamet   CCfnamet   CCoutputt   mfilet   templfoldert   templworkdirt   revmodelsdirt   namesofproperthreadt   threadnamesR^   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyt   runš   sî    8!			
-,		 #
		*#
		*.	&N(   R   R   R   R   R_   Re   R?   R§   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyR   "   s   	:		(    (   R&   R<   RA   Rf   R    R:   Rˆ   RD   t   scitbxR8   R|   t   phaserR   R   R   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/phaser/BLASTphaserthread.pyt   <module>   s   $