o
    RŀgX                     @   s   d 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d
ee dee dee fddZG dd de	ZedkrXddlmZ edd d	S d	S )ai  Bio.AlignIO support for the "nexus" file format.

You are expected to use this module via the Bio.AlignIO functions (or the
Bio.SeqIO functions if you want to work directly with the gapped sequences).

See also the Bio.Nexus module (which this code calls internally),
as this offers more than just accessing the alignment or its
sequences as SeqRecord objects.
    )IO)Iterator)Optional)MultipleSeqAlignment)AlignmentWriter)Nexus)	SeqRecordNhandle	seq_countreturnc                 #   s    t  | jsdS tjtjksJ |r+|tjkr+tdtj|f jdv r5ddi njdkr?ddi njdkrIddi nd  fd	d
tjjD }t|V  dS )aU  Return SeqRecord objects from a Nexus file.

    Thus uses the Bio.Nexus module to do the hard work.

    You are expected to call this function via Bio.SeqIO or Bio.AlignIO
    (and not use it directly).

    NOTE - We only expect ONE alignment matrix per Nexus file,
    meaning this iterator will only yield one MultipleSeqAlignment.
    Nz$Found %i sequences, but seq_count=%i)dna
nucleotidemolecule_typeDNArnaRNAproteinc                 3   s,    | ]\}}t j| ||d  dV  qdS ) )idnamedescriptionannotationsN)r   matrix).0old_namenew_namer   n G/var/www/html/myenv/lib/python3.10/site-packages/Bio/AlignIO/NexusIO.py	<genexpr>E   s    
z NexusIterator.<locals>.<genexpr>)	r   r   lenunaltered_taxlabels	taxlabels
ValueErrordatatypezipr   )r	   r
   recordsr   r   r   NexusIterator   s,   






r(   c                   @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
NexusWriterzNexus alignment writer.

    Note that Nexus files are only expected to hold ONE alignment
    matrix.

    You are expected to call this class via the Bio.AlignIO.write() or
    Bio.SeqIO.write() functions.
    c                 C   s\   t |}zt|}W n
 ty   Y dS w zt| td ty&   Y nw | | dS )zUse this to write an entire file containing the given alignments.

        Arguments:
         - alignments - A list or iterator returning MultipleSeqAlignment objects.
           This should hold ONE and only one alignment.

        r   z0We can only write one Alignment to a Nexus file.   )iternextStopIterationr$   write_alignment)self
alignments
align_iter	alignmentr   r   r   
write_file]   s   
zNexusWriter.write_fileNc                 C   s   t |dkr
td| }|dkrtd| |}d| }t|}|D ].}|dkr9d|jv r9t|j d|dkrJd	|jv rJt|j d
||jt|j q&|du r]|dk}|j	| j
|d dS )a  Write an alignment to file.

        Creates an empty Nexus object, adds the sequences
        and then gets Nexus to prepare the output.
        Default interleave behaviour: Interleave if columns > 1000
        --> Override with interleave=[True/False]
        r   zMust have at least one sequencez Non-empty sequences are requiredzF#NEXUS
begin data; dimensions ntax=0 nchar=0; format datatype=%s; end;r   Uz contains U, but DNA alignmentr   Tz contains T, but RNA alignmentNi  )
interleave)r!   r$   get_alignment_length_classify_mol_type_for_nexusr   seqr   add_sequencestrwrite_nexus_datar	   )r/   r2   r6   columnsr%   minimal_recordr   recordr   r   r   r.   w   s(   

zNexusWriter.write_alignmentc                 C   sX   dd |D }t dd |D rdS t dd |D rdS t dd |D r(d	S td
)zReturn 'protein', 'dna', or 'rna' based on records' molecule type (PRIVATE).

        All the records must have a molecule_type annotation, and they must
        agree.

        Raises an exception if this is not possible.
        c                 S   s   h | ]	}|j d dqS )r   N)r   getr   _r   r   r   	<setcomp>   s    z;NexusWriter._classify_mol_type_for_nexus.<locals>.<setcomp>c                 s       | ]	}|o	d |v V  qdS )r   Nr   rA   r   r   r   r           z;NexusWriter._classify_mol_type_for_nexus.<locals>.<genexpr>r   c                 s   rD   )r   Nr   rA   r   r   r   r       rE   r   c                 s   rD   )r   Nr   rA   r   r   r   r       rE   r   z$Need the molecule type to be defined)allr$   )r/   r2   valuesr   r   r   r8      s   z(NexusWriter._classify_mol_type_for_nexusN)__name__
__module____qualname____doc__r3   r.   r8   r   r   r   r   r)   S   s
    	
 r)   __main__)run_doctest)verboserH   )rL   typingr   collections.abcr   r   	Bio.Alignr   Bio.AlignIO.Interfacesr   	Bio.Nexusr   Bio.SeqRecordr   r;   intr(   r)   rI   
Bio._utilsrN   r   r   r   r   <module>   s*   

4W