o
    ॵi                     @   sH   d Z ddlZddlZddlZddlmZ ddlmZ G dd deZ	dS )z@A Python wrapper for hmmbuild - construct HMM profiles from MSA.    N)logging   )utilsc                   @   sj   e Zd ZdZdddedefddZ	dd	ed
efddZded
efddZ	ddeded
efddZ	dS )Hmmbuildz&Python wrapper of the hmmbuild binary.F)singlemxbinary_pathr   c                C   s   || _ || _dS )ai  Initializes the Python hmmbuild wrapper.

        Args:
            binary_path: The path to the hmmbuild executable.
            singlemx: Whether to use --singlemx flag. If True, it forces HMMBuild to
                just use a common substitution score matrix.

        Raises:
            RuntimeError: If hmmbuild binary not found within the path.
        N)r   r   )selfr   r    r	   h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/science/unifold/msa/tools/hmmbuild.py__init__   s   
zHmmbuild.__init__faststoreturnc                 C   s   | j ||dS )a  Builds a HHM for the aligned sequences given as an A3M string.

        Args:
            sto: A string with the aligned sequences in the Stockholm format.
            model_construction: Whether to use reference annotation in the msa to
                determine consensus columns ('hand') or default ('fast').

        Returns:
            A string with the profile in the HMM format.

        Raises:
            RuntimeError: If hmmbuild fails.
        model_construction)_build_profile)r   r   r   r	   r	   r
   build_profile_from_sto*   s   zHmmbuild.build_profile_from_stoa3mc                 C   sP   g }|  D ]}|dstdd|}||d  qd|}| j|ddS )a'  Builds a HHM for the aligned sequences given as an A3M string.

        Args:
            a3m: A string with the aligned sequences in the A3M format.

        Returns:
            A string with the profile in the HMM format.

        Raises:
            RuntimeError: If hmmbuild fails.
        >z[a-z]+ 
r   r   )
splitlines
startswithresubappendjoinr   )r   r   lineslinemsar	   r	   r
   build_profile_from_a3m<   s   

zHmmbuild.build_profile_from_a3mr   r   c              	   C   s  |dvrt d| dt }tj|d}tj|d}t|d}|| W d   n1 s4w   Y  | jg}|dkrI|	d	|  | j
rQ|	d
 |d||g td| tj|tjtjd}td | \}	}
| }td|	d|
d W d   n1 sw   Y  |rtd|	d|
df t|dd}| }W d   n1 sw   Y  W d   |S W d   |S 1 sw   Y  |S )a  Builds a HMM for the aligned sequences given as an MSA string.

        Args:
            msa: A string with the aligned sequences, in A3M or STO format.
            model_construction: Whether to use reference annotation in the msa to
                determine consensus columns ('hand') or default ('fast').

        Returns:
            A string with the profile in the HMM format.

        Raises:
            RuntimeError: If hmmbuild fails.
            ValueError: If unspecified arguments are provided.
        >   r   handzInvalid model_construction z - onlyhand and fast supported.z	query.msaz
output.hmmwNr!   z--z
--singlemxz--aminozLaunching subprocess %s)stdoutstderrzhmmbuild queryz hmmbuild stdout:
%s

stderr:
%s
zutf-8z'hmmbuild failed
stdout:
%s

stderr:
%s
)encoding)
ValueErrorr   tmpdir_managerospathr   openwriter   r   r   extendr   info
subprocessPopenPIPEtimingcommunicatewaitdecodeRuntimeErrorread)r   r   r   query_tmp_dirinput_queryoutput_hmm_pathfcmdprocessr#   r$   retcodehmmr	   r	   r
   r   P   sb   



	

)
))zHmmbuild._build_profileN)r   )
__name__
__module____qualname____doc__strboolr   r   r    r   r	   r	   r	   r
   r      s$    
r   )
rB   r(   r   r.   abslr   r   r   objectr   r	   r	   r	   r
   <module>   s   