o
    Rŀg                     @   s  d Z ddlZddlZ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mZ dd	lmZ d
edee fddZ	ddedeeeeef  defddZ	ddedeeeeef  defddZ		dd
edee deeeeef  dee fddZdS )zA module for interacting with the AlphaFold Protein Structure Database.

See the `database website <https://alphafold.com/>`_ and the `API docs <https://alphafold.com/api-docs/>`_.
    N)PathLike)Iterator)Optional)Union)urlopen)MMCIFParser)	Structure	qualifierreturnc                 c   sR    d|  }t |}t|  E dH  W d   dS 1 s"w   Y  dS )zGet all AlphaFold predictions for a UniProt accession.

    :param qualifier: A UniProt accession, e.g. P00520
    :type qualifier: str
    :return: The AlphaFold predictions
    :rtype: Iterator[dict]
    z%https://alphafold.com/api/prediction/N)r   jsonloadsreaddecode)r	   urlresponse r   H/var/www/html/myenv/lib/python3.10/site-packages/Bio/PDB/alphafold_db.pyget_predictions   s
   

"r   
prediction	directoryc                 C   s8   |du rt  }| d }|dd }tt j||S )am  Get the path to the mmCIF file for an AlphaFold prediction.

    :param prediction: An AlphaFold prediction
    :type prediction: dict
    :param directory: The directory that stores the mmCIF file, defaults to the current working directory
    :type directory: Union[int, str, bytes, PathLike], optional
    :return: The path to the mmCIF file
    :rtype: str
    NcifUrl/)osgetcwdsplitstrpathjoin)r   r   cif_url	file_namer   r   r   _get_mmcif_file_path_for    s
   r!   c                 C   s   |du rt  }| d }t j|dd t| |}t j|r(td| d |S t|}| }W d   n1 s;w   Y  t	|d}|
| W d   |S 1 sVw   Y  |S )a  Download the mmCIF file for an AlphaFold prediction.

    Downloads the file to the current working directory if no destination is specified.

    :param prediction: An AlphaFold prediction
    :type prediction: dict
    :param directory: The directory to write the mmCIF data to, defaults to the current working directory
    :type directory: Union[int, str, bytes, PathLike], optional
    :return: The path to the mmCIF file
    :rtype: str
    Nr   T)exist_okzFile z# already exists, skipping download.wb)r   r   makedirsr!   r   existsprintr   r   openwrite)r   r   r   	file_pathr   datafiler   r   r   download_cif_for5   s"   



r,   mmcif_parserc                 c   s`    |du rt  }|du rt }t| D ]}t||}tj|s&t||}|| |V  qdS )aG  Get the PDB structures for a UniProt accession.

    Downloads the mmCIF files to the directory if they are not present.

    :param qualifier: A UniProt accession, e.g. P00520
    :type qualifier: str
    :param mmcif_parser: The mmCIF parser to use, defaults to ``MMCIFParser()``
    :type mmcif_parser: MMCIFParser, optional
    :param directory: The directory to store the mmCIF data, defaults to the current working directory
    :type directory: Union[int, str, bytes, PathLike], optional
    :return: An iterator over the PDB structures
    :rtype: Iterator[PDBStructure]
    N)	r   r   r   r   r!   r   r%   r,   get_structure)r	   r-   r   r   
mmcif_pathr   r   r   get_structural_models_forW   s   

r0   )N)NN)__doc__r   r   r   collections.abcr   typingr   r   urllib.requestr   Bio.PDB.MMCIFParserr   Bio.PDB.Structurer   StructuralModelr   dictr   bytesr!   r,   r0   r   r   r   r   <module>   sL    

$