o
    ॵiJ                     @   sj   d Z ddlZddlZddlZddlmZmZmZmZm	Z	 ddl
mZ ddlmZ dZdZG d	d
 d
ZdS )z#Library to run HHblits from Python.    N)AnyListMappingOptionalSequence)logging   )utils   i  c                   @   s   e Zd ZdZddddddddd	eed
dedee dedede	dedededede
dee dedefddZdedeeeef  fddZd	S )HHBlitsz%Python wrapper of the HHblits binary.      gMbP?i@B i i  FN)n_cpun_itere_valuemaxseqrealign_maxmaxfiltmin_prefilter_hitsall_seqsaltpzbinary_path	databasesr   r   r   r   r   r   r   r   r   r   r   c                C   s   || _ || _| jD ]}t|d std| td| q	|| _|| _|| _|| _	|| _
|| _|	| _|
| _|| _|| _|| _dS )a  Initializes the Python HHblits wrapper.

        Args:
            binary_path: The path to the HHblits executable.
            databases: A sequence of HHblits database paths. This should be the
                common prefix for the database files (i.e. up to but not including
                _hhm.ffindex etc.)
            n_cpu: The number of CPUs to give HHblits.
            n_iter: The number of HHblits iterations.
            e_value: The E-value, see HHblits docs for more details.
            maxseq: The maximum number of rows in an input alignment. Note that this
                parameter is only supported in HHBlits version 3.1 and higher.
            realign_max: Max number of HMM-HMM hits to realign. HHblits default: 500.
            maxfilt: Max number of hits allowed to pass the 2nd prefilter.
                HHblits default: 20000.
            min_prefilter_hits: Min number of hits to pass prefilter.
                HHblits default: 100.
            all_seqs: Return all sequences in the MSA / Do not filter the result MSA.
                HHblits default: False.
            alt: Show up to this many alternative alignments.
            p: Minimum Prob for a hit to be included in the output hhr file.
                HHblits default: 20.
            z: Hard cap on number of hits reported in the hhr file.
                HHblits default: 500. NB: The relevant HHblits flag is -Z not -z.

        Raises:
            RuntimeError: If HHblits binary not found within the path.
        z_*z"Could not find HHBlits database %sz Could not find HHBlits database N)r   r   globr   error
ValueErrorr   r   r   r   r   r   r   r   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r   r   r   database_path r    g/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/science/unifold/msa/tools/hhblits.py__init__    s,   -

zHHBlits.__init__input_fasta_pathreturnc                 C   s4  t  }tj|d}g }| jD ]}|d || q| jd|dt| j	d|dddt| j
d	t| jd
t| jdt| jdt| jdt| jg}| jrS|dg7 }| jr_|dt| jg7 }| jtkrm|dt| jg7 }| jtkr{|dt| jg7 }||7 }tdd| tj|tjtjd}t d | \}}	| }
W d   n1 sw   Y  |
rtd |	d  D ]}|! rt|!  qtd t"d|d|	dd df t#|}|$ }W d   n1 sw   Y  W d   n	1 sw   Y  t%|||	| j
| jd}|gS )z#Queries the database using HHblits.z
output.a3mz-dz-iz-cpuz-oa3mz-oz	/dev/nullz-nz-ez-maxseqz-realign_maxz-maxfiltz-min_prefilter_hitsz-allz-altz-pz-ZzLaunching subprocess "%s" )stdoutstderrzHHblits queryNz%HHblits failed. HHblits stderr begin:zutf-8zHHblits stderr endz&HHblits failed
stdout:
%s

stderr:
%s
i  )a3moutputr'   r   r   )&r	   tmpdir_managerospathjoinr   appendr   strr   r   r   r   r   r   r   r   r   r   _HHBLITS_DEFAULT_Pr   _HHBLITS_DEFAULT_Zr   info
subprocessPopenPIPEtimingcommunicatewaitr   decode
splitlinesstripRuntimeErroropenreaddict)r   r#   query_tmp_dira3m_pathdb_cmddb_pathcmdprocessr&   r'   retcode
error_linefr(   
raw_outputr    r    r!   queryc   s   











>zHHBlits.query)__name__
__module____qualname____doc__r0   r1   r/   r   intfloatboolr   r"   r   r   r   rJ   r    r    r    r!   r      sR    	

"Cr   )rN   r   r+   r3   typingr   r   r   r   r   abslr    r	   r0   r1   r   r    r    r    r!   <module>   s   