o
    Rŀg                  0   @   s  d Z ddlZddl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d
l	mZ ddlm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ZdZdZ dZ!G dd de"Z#G dd de"Z$G dd deZ%G dd deZ&G dd de'Z(G dd  d eZ)d!d" Z*d#d$ Z+d3d&d'Z,ee ddddd(d)dddd*dddddddddddddddddddd+dd+dddddd%ddddddd,df/d-d.Z-de-_.d/d0 Z/e0d1krdd2lm1Z1 e1  dS dS )4a  Code to parse and store BLAST XML output, and to invoke the NCBI BLAST web server.

This module provides code to parse and store BLAST XML output, following its
definition in the associated BLAST XML DTD file:
https://www.ncbi.nlm.nih.gov/dtd/NCBI_BlastOutput.dtd

This module also provides code to invoke the BLAST web server provided by NCBI.
https://blast.ncbi.nlm.nih.gov/

Variables:

    - email        Set the Blast email parameter (default is None).
    - tool         Set the Blast tool parameter (default is ``biopython``).

    N)UserList)	urlencode)build_opener)HTTPBasicAuthHandler)HTTPPasswordMgrWithDefaultRealm)install_opener)Request)urlopen)expat)BiopythonWarning)StreamModeError)function_with_previous)	Alignment)
Alignments)_writers	biopythonz(https://blast.ncbi.nlm.nih.gov/Blast.cgii   c                   @       e Zd ZdZdd Zdd ZdS )NotXMLErrorzFailed to parse file as XML.c                 C   
   || _ dS zInitialize the class.Nmsgselfmessage r   F/var/www/html/myenv/lib/python3.10/site-packages/Bio/Blast/__init__.py__init__<      
zNotXMLError.__init__c                 C   
   d| j  S ))Return a string summary of the exception.zZFailed to parse the XML data (%s). Please make sure that the input data are in XML format.r   r   r   r   r   __str__@      zNotXMLError.__str__N__name__
__module____qualname____doc__r   r"   r   r   r   r   r   9       r   c                   @   r   )CorruptedXMLErrorzCorrupted XML.c                 C   r   r   r   r   r   r   r   r   K   r   zCorruptedXMLError.__init__c                 C   r   )r    zZFailed to parse the XML data (%s). Please make sure that the input data are not corrupted.r   r!   r   r   r   r"   O   r#   zCorruptedXMLError.__str__Nr$   r   r   r   r   r*   H   r)   r*   c                       s(   e Zd ZdZdd Z fddZ  ZS )HSPag  Stores an alignment of one query sequence against a target sequence.

    An HSP (High-scoring Segment Pair) stores the alignment of one query
    sequence segment against one target (hit) sequence segment. The
    ``Bio.Blast.HSP`` class inherits from the ``Bio.Align.Alignment`` class.

    In addition to the ``target`` and ``query`` attributes of a
    ``Bio.Align.Alignment``, a ``Bio.Blast.HSP`` object has the following
    attributes:

     - score:       score of HSP;
     - annotations: a dictionary that may contain the following keys:
                     - 'bit score': score (in bits) of HSP (float);
                     - 'evalue':    e-value of HSP (float);
                     - 'identity':  number of identities in HSP (integer);
                     - 'positive':  number of positives in HSP (integer);
                     - 'gaps':      number of gaps in HSP (integer);
                     - 'midline':   formatting middle line.

    A ``Bio.Blast.HSP`` object behaves the same as a `Bio.Align.Alignment``
    object and can be used as such. However, when printing a ``Bio.Blast.HSP``
    object, the BLAST e-value and bit score are included in the output (in
    addition to the alignment itself).

    See the documentation of ``Bio.Blast.Record`` for a more detailed
    explanation of how the information in BLAST records is stored in
    Biopython.
    c              	   C   s8   | j }| j}| j\}}d|jd|jd| d| d	S )Nz<Bio.Blast.HSP target.id=
 query.id=; z rows x z	 columns>)querytargetshapeid)r   r.   r/   nmr   r   r   __repr__u   s   
"zHSP.__repr__c                    s  t   }| j}| j}| jd | jd krdnd}| jd | jd kr%dnd}d}tj|jd||d	}tj|jd||d	}| jd
 }	| jd }
| j	}t
| jd}tt|d}g }| jd }td| | }d|||f }|| | jd}|d urtd| | }d|||f }|| z| jd }W n	 ty   Y nw td| | }d|||f }|| d|}d|jt||||jt||||
||	||f S )N   r   r6   PlusMinusr   r   r   r8   z        P   widthinitial_indentsubsequent_indentevalue	bit scorer6   r   identityg      Y@zIdentities:%d/%d(%d%%)positivezPositives:%d/%d(%d%%)gapszGaps:%d.%d(%d%%)z,  zpQuery : %s Length: %d Strand: %s
%s
Target: %s Length: %d Strand: %s
%s

Score:%d bits(%d), Expect:%.1g,
%s

%s
)superr"   r.   r/   coordinatestextwrapfilldescriptionannotationsscorenpdiffsumabsmaxroundappendgetKeyErrorjoinr1   len)r   alignment_textr.   r/   query_strandtarget_strandindentquery_descriptiontarget_descriptionrB   bitscorerM   stepsaln_spantermsrD   identity_percentageidentity_textrE   positive_percentagepositive_textrF   gaps_percentage	gaps_textcounts_line	__class__r   r   r"   {   s   







zHSP.__str__)r%   r&   r'   r(   r4   r"   __classcell__r   r   rj   r   r+   W   s    r+   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )Hita  Stores a single BLAST hit of one single query against one target.

    The ``Bio.Blast.Hit`` class inherits from the ``Bio.Align.Alignments``
    class, which is a subclass of a Python list. The ``Bio.Blast.Hit`` class
    stores ``Bio.Blast.HSP`` objwcts, which inherit from
    ``Bio.Align.Alignment``. A ``Bio.Blast.Hit`` object is therefore
    effectively a list of ``Bio.Align.Alignment`` objects. Most hits consist of
    only 1 or a few Alignment objects.

    Each ``Bio.Blast.Hit`` object has a ``target`` attribute containing the
    following information:

     - target.id:          seqId of subject;
     - target.description: definition line of subject;
     - target.name:        accession of subject;
     - len(target.seq):    sequence length of subject.

    See the documentation of ``Bio.Blast.Record`` for a more detailed
    explanation of the information stored in the alignments contained in the
    ``Bio.Blast.Hit`` object.
    c                    sL   zt  |}W n ty   tdd w t|tr$t|}| j|_|S |S )Nindex out of range)rG   __getitem__
IndexError
isinstanceslicerm   r/   )r   keyvaluehitrj   r   r   ro      s   

zHit.__getitem__c              	   C   sv   | j }z| d }W n ty   d|jd Y S w |j}t| }|dkr(d}nd}d|jd|jd| d	| d
	S )Nr   z<Bio.Blast.Hit target.id=
; no hits>r6   r+   HSPsr,   r-    >)r/   rp   r1   r.   rX   )r   r/   	alignmentr.   nhspsunitr   r   r   r4      s   "zHit.__repr__c              
   C   s  g }| d j }d|j }|| d}tj|jd||d}|| | j}d|jt|f }|| tj|jd||d}|| |dd  d	}||d
  ||d  t	| D ]w\}	}
t
|
jd d}t
|
jd d}t|
jd}tt|d}|
jd }|
jd }d| d| d}t|dkr|dd d n|}|
jd }|
jd }d| d| d}t|dkr|dd d n|}|||	|||||f  q]d|S )z2Return a human readable summary of the Hit object.r   z	Query: %sz       r=   r>   z  Hit: %s (length=%i)z HSPs: %s  %s  %s  %s  %s  %s)----z--------z	---------z------z---------------z---------------------z%11s  %8s  %9s  %6s  %15s  %21s)#zE-valuez	Bit scoreSpanzQuery rangez	Hit rangerB   z.2grC   z.2fr6   r5   r7   [:]   N   z~]r;   r<         
)r.   r1   rT   rI   wraprK   extendr/   rX   	enumerateformatrL   rN   rO   rH   rP   rQ   rR   rW   )r   linesr.   qid_liner\   description_linesr/   hid_linepatternidxhsprB   r_   r`   ra   query_start	query_endquery_range	hit_starthit_end	hit_ranger   r   r   r"      sd   









 
zHit.__str__)r%   r&   r'   r(   ro   r4   r"   rl   r   r   rj   r   rm      s
    rm   c                       sP   e Zd ZdZdd Zdd Zdd Z fdd	Zd
d Zdd Z	dd Z
  ZS )Recorda  Stores the BLAST results for a single query.

    A ``Bio.Blast.Record`` object is a list of ``Bio.Blast.Hit`` objects, each
    corresponding to one hit for the query in the BLAST output.

    The ``Bio.Blast.Record`` object may have the following attributes:
     - query:   A ``SeqRecord`` object which may contain some or all of the
                following information:
                 - query.id:          SeqId of query;
                 - query.description: Definition line of query;
                 - len(query.seq):    Length of the query sequence.
     - stat:    A dictionary with summary statistics of the BLAST run. It may
                contain the following keys:
                 - 'db-num':    number of sequences in BLAST db (integer);
                 - 'db-len':    length of BLAST db (integer);
                 - 'hsp-len':   effective HSP length (integer);
                 - 'eff-space': effective search space (float);
                 - 'kappa':     Karlin-Altschul parameter K (float);
                 - 'lambda':    Karlin-Altschul parameter Lambda (float);
                 - 'entropy':   Karlin-Altschul parameter H (float).
     - message: Some (error?) information.

    Each ``Bio.Blast.Hit`` object has a ``target`` attribute containing the
    following information:

     - target.id:          seqId of subject;
     - target.description: definition line of subject;
     - target.name:        accession of subject;
     - len(target.seq):    sequence length of subject.

    The ``Bio.Blast.Hit`` class inherits from the ``Bio.Align.Alignments``
    class, which inherits from a Python list. In this list, the
    ``Bio.Blast.Hit`` object stores ``Bio.Blast.HSP`` objects, which inherit
    from the ``Bio.Align.Alignment`` class.  A ``Bio.Blast.Hit`` object is
    therefore effectively a list of alignment objects.

    Each HSP in a ``Bio.Blast.Hit`` object has the attributes ``target`` and
    ``query`` attributes, as usual for of a ``Bio.Align.Alignment`` object
    storing a pairwise alignment, pointing to a ``SeqRecord`` object
    representing the target and query, respectively.  For translated BLAST
    searches, the ``features`` attribute of the target or query may contain a
    ``SeqFeature`` of type CDS that stores the amino acid sequence region.  The
    ``qualifiers`` attribute of such a feature is a dictionary with  a single
    key 'coded_by'; the corresponding value specifies the nucleotide sequence
    region, in a GenBank-style string with 1-based coordinates, that encodes
    the amino acid sequence.

    Each ``Bio.Blast.HSP`` object has the following additional attributes:

     - score:       score of HSP;
     - annotations: a dictionary that may contain the following keys:
                     - 'bit score': score (in bits) of HSP (float);
                     - 'evalue':    e-value of HSP (float);
                     - 'identity':  number of identities in HSP (integer);
                     - 'positive':  number of positives in HSP (integer);
                     - 'gaps':      number of gaps in HSP (integer);
                     - 'midline':   formatting middle line.

    >>> from Bio import Blast
    >>> record = Blast.read("Blast/xml_2212L_blastx_001.xml")
    >>> record.query
    SeqRecord(seq=Seq(None, length=556), id='gi|1347369|gb|G25137.1|G25137', name='<unknown name>', description='human STS EST48004, sequence tagged site', dbxrefs=[])
    >>> record.stat
    {'db-num': 2934173, 'db-len': 1011751523, 'hsp-len': 0, 'eff-space': 0, 'kappa': 0.041, 'lambda': 0.267, 'entropy': 0.14}
    >>> len(record)
    78
    >>> hit = record[0]
    >>> type(hit)
    <class 'Bio.Blast.Hit'>
    >>> from Bio.Align import Alignments
    >>> isinstance(hit, Alignments)
    True
    >>> hit.target
    SeqRecord(seq=Seq(None, length=319), id='gi|12654095|gb|AAH00859.1|', name='AAH00859', description='Unknown (protein for IMAGE:3459481) [Homo sapiens]', dbxrefs=[])

    Most hits consist of only 1 or a few Alignment objects:

    >>> len(hit)
    1
    >>> alignment = hit[0]
    >>> type(alignment)
    <class 'Bio.Blast.HSP'>
    >>> alignment.score
    630.0
    >>> alignment.annotations
    {'bit score': 247.284, 'evalue': 1.69599e-64, 'identity': 122, 'positive': 123, 'gaps': 0, 'midline': 'DLQLLIKAVNLFPAGTNSRWEVIANYMNIHSSSGVKRTAKDVIGKAKSLQKLDPHQKDDINKKAFDKFKKEHGVVPQADNATPSERF GPYTDFTP TTE QKL EQAL TYPVNT ERW  IA AVPGR K+'}

    Target and query information are stored in the respective attributes of the
    alignment:

    >>> alignment.target
    SeqRecord(seq=Seq({155: 'DLQLLIKAVNLFPAGTNSRWEVIANYMNIHSSSGVKRTAKDVIGKAKSLQKLDP...TKK'}, length=319), id='gi|12654095|gb|AAH00859.1|', name='AAH00859', description='Unknown (protein for IMAGE:3459481) [Homo sapiens]', dbxrefs=[])
    >>> alignment.query
    SeqRecord(seq=Seq('DLQLLIKAVNLFPAGTNSRWEVIANYMNIHSSSGVKRTAKDVIGKAKSLQKLDP...XKE'), id='gi|1347369|gb|G25137.1|G25137', name='<unknown name>', description='human STS EST48004, sequence tagged site', dbxrefs=[])

    This was a BLASTX run, so the query sequence was translated:

    >>> len(alignment.target.features)
    0
    >>> len(alignment.query.features)
    1
    >>> feature = alignment.query.features[0]
    >>> feature
    SeqFeature(SimpleLocation(ExactPosition(0), ExactPosition(133)), type='CDS', qualifiers=...)
    >>> feature.qualifiers
    {'coded_by': 'gi|1347369|gb|G25137.1|G25137:1..399'}

    i.e., nucleotides 0:399 (in zero-based coordinates) encode the amino acids
    of the query in the alignment.

    For an alignment against the reverse strand, the location in the qualifier
    is shown as in this example:

    >>> record[72][0].query.features[0].qualifiers
    {'coded_by': 'complement(gi|1347369|gb|G25137.1|G25137:345..530)'}

    c                 C   s
   d| _ dS )zInitialize the Record object.N)r.   r!   r   r   r   r     r   zRecord.__init__c                 C   sj   | j }z|j}W n ty   d}Y nw t| }|dkr"d|dS |dkr,d|dS d|d| dS )	Nunknownr   z<Bio.Blast.Record query.id=rv   r6   z; 1 hit>r-   z hits>)r.   r1   AttributeErrorrX   )r   r.   query_idnhitsr   r   r   r4     s   
zRecord.__repr__c                 C   s  g }z| j }W n	 ty   Y n	w |d|  z| j}W n	 ty'   Y n	w |d|  | jd urU|d| jjt| jf  d}tj| jj	d||d}|
| t| dkra|d n|d	d
  d}||d  ||d
  t| D ]i\}}t|}	|dk rd|jj|jj	f }
t|
dkr|
d d d }
|||t||
f  q||t| d krd|jj|jj	f }
t|
dkr|
d d d }
|||t||
f  q||dkr|dd  q|d|S )Nz	Program: z	     db: z  Query: %s (length=%d)z	         r=   r>   r   z   Hits: No hits foundz   Hits: %s  %s  %s)r}   z-----z:----------------------------------------------------------z%13s  %5s  %s)r~   z# HSPzID + description   z%s  %s:   7   z...   z%14sz~~~r   )versionr   rT   dbr.   r1   rX   rI   r   rK   r   r   r/   rW   )r   r   r   r   r\   r   r   r   ru   r2   r   r   r   r   r"     sZ   




zRecord.__str__c              	      s   zt  |}W n1 ty   tdd  ty9   t|ts$tdd | D ]}|jj|kr4|   Y S q&t|w t|t	ryt
 }|| z| j}W n	 tyU   Y nw ||_d}|D ]}zt| |}W n	 tyo   Y q]w t||| q]|S |S )Nrn   z%key must be an integer, slice, or strsourceprogramr   	referencer   parammbstat)rG   ro   rp   	TypeErrorrq   strr/   r1   rV   rr   r   r   r.   r   getattrsetattr)r   rs   rt   ru   recordr.   keysrj   r   r   ro     s@   





	zRecord.__getitem__c                 C   s   dd | D S )z+Return a list of the target.id of each hit.c                 S   s   g | ]}|j jqS r   r/   r1   ).0ru   r   r   r   
<listcomp>*  s    zRecord.keys.<locals>.<listcomp>r   r!   r   r   r   r   (  s   zRecord.keysc                 C   s    | D ]}|j j|kr dS qdS )NTFr   )r   rs   ru   r   r   r   __contains__,  s
   zRecord.__contains__c                 C   s6   t | D ]\}}|jj|kr|  S qtd| d)zHReturn the index of the hit for which the target.id is equal to the key.'z' not found)r   r/   r1   
ValueError)r   rs   iru   r   r   r   index2  s
   zRecord.index)r%   r&   r'   r(   r   r4   r"   ro   r   r   r   rl   r   r   rj   r   r   B  s    v0-r   c                       sl   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	 fddZ
edd Zdd Zdd Z  ZS )Recordsu  Stores the BLAST results of a single BLAST run.

    A ``Bio.Blast.Records`` object is an iterator. Iterating over it returns
    returns ``Bio.Blast.Record`` objects, each of which corresponds to one
    BLAST query.

    Common attributes of a ``Bio.Blast.Records`` object are

     - source:     The input data from which the ``Bio.Blast.Records`` object
                   was constructed.
     - program:    The specific BLAST program that was used (e.g., 'blastn').
     - version:    The version of the BLAST program (e.g., 'BLASTN 2.2.27+').
     - reference:  The literature reference to the BLAST publication.
     - db:         The BLAST database against which the query was run
                   (e.g., 'nr').
     - query:      A ``SeqRecord`` object which may contain some or all of the
                   following information:
                    - query.id:          SeqId of the query;
                    - query.description: Definition line of the query;
                    - query.seq:         The query sequence. The query sequence.
                                         The query sequence.
     - param:      A dictionary with the parameters used for the BLAST run.
                   You may find the following keys in this dictionary:
                    - 'matrix':       the scoring matrix used in the BLAST run
                                      (e.g., 'BLOSUM62') (string);
                    - 'expect':       threshold on the expected number of chance
                                      matches (float);
                    - 'include':      e-value threshold for inclusion in
                                      multipass model in psiblast (float);
                    - 'sc-match':     score for matching nucleotides (integer);
                    - 'sc-mismatch':  score for mismatched nucleotides
                                      (integer);
                    - 'gap-open':     gap opening cost (integer);
                    - 'gap-extend':   gap extension cost (integer);
                    - 'filter':       filtering options applied in the BLAST
                                      run (string);
                    - 'pattern':      PHI-BLAST pattern (string);
                    - 'entrez-query': Limit of request to Entrez query (string).
     - mbstat:     A dictionary with Mega BLAST search statistics.  As this
                   information is stored near the end of the XML file, this
                   attribute can only be accessed after the file has been read
                   completely (by iterating over the records until a
                   ``StopIteration`` is issued. This dictionary can contain the
                   same keys as the dictionary stored under the ``stat``
                   attribute of a ``Record`` object.

    >>> from Bio import Blast
    >>> path = "Blast/xml_2218_blastp_002.xml"

    In a script, you would use a ``with`` block, as in

    >>> with Blast.parse(path) as records:
    ...     print(records.source)
    ...
    Blast/xml_2218_blastp_002.xml

    to ensure that the file is closed at the end of the block.
    Here, we will simply do

    >>> records = Blast.parse("Blast/xml_2218_blastp_002.xml")

    so we can see the output of each command right away.

    >>> type(records)
    <class 'Bio.Blast.Records'>
    >>> records.source
    'Blast/xml_2218_blastp_002.xml'
    >>> records.program
    'blastp'
    >>> records.version
    'BLASTP 2.2.18+'
    >>> records.reference
    'Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schäffer, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), "Gapped BLAST and PSI-BLAST: a new generation of protein database search programs", Nucleic Acids Res. 25:3389-3402.'
    >>> records.db
    'gpipe/9606/Previous/protein'
    >>> records.param
    {'matrix': 'BLOSUM62', 'expect': 0.01, 'gap-open': 11, 'gap-extend': 1, 'filter': 'm L; R -d repeat/repeat_9606;'}

    Iterating over the records returns Bio.Blast.Record objects:

    >>> record = next(records)
    >>> type(record)
    <class 'Bio.Blast.Record'>
    >>> record.query.id
    'gi|585505|sp|Q08386|MOPB_RHOCA'
    >>> record = next(records)
    >>> type(record)
    <class 'Bio.Blast.Record'>
    >>> record.query.id
    'gi|129628|sp|P07175.1|PARA_AGRTU'
    >>> record = next(records)  # doctest:+ELLIPSIS
    Traceback (most recent call last):
    ...
    StopIteration

    You can also use the records as a list, for example by extracting a record
    by index, or by calling ``len`` or ``print`` on the records. The parser
    will then automatically iterate over the records and store them:

    >>> records = Blast.parse("Blast/wnts.xml")
    >>> record = records[3]  # this causes all records to be read in and stored
    >>> record.query.id
    'Query_4'
    >>> len(records)
    5

    After the records have been read in, you can still iterate over them:

    >>> for i, record in enumerate(records):
    ...     print(i, record.query.id)
    ...
    0 Query_1
    1 Query_2
    2 Query_3
    3 Query_4
    4 Query_5

    c                 C   s   t |tr|| _d| _d| _dS || _zt|d}W n ty1   |ddkr-t	dd|}Y nw || _
|   g | _d| _dS )zInitialize the Records object.Tr   Nrb    z1BLAST output files must be opened in binary mode.F)rq   list_records_loaded_indexr   openr   readr   _stream_read_header)r   r   streamr   r   r   r     s*   

zRecords.__init__c              
   C   s  ddl m} | j}| j}zhtjdd}|| _||}| |_	 |t	}|dkr?z|j W n	 t
y5   Y n>w td|j dz||d	 W n tjya } z|jrXt|d t|d d }~ww z| j W n	 t
yp   Y nw nqW n ty   ||ur|   w d| _d S )
Nr   )
XMLHandlerrx   )namespace_separatorTr   )premature end of XML file (after reading  bytes)F)Bio.Blast._parserr   r   r   r
   ParserCreate_parserr   r   BLOCKr   r   CurrentByteIndexParse
ExpatErrorStartElementHandlerr*   r   _cache	Exceptioncloser   )r   r   r   r   parserhandlerdataer   r   r   r     sR   






zRecords._read_headerc                 C      | S Nr   r!   r   r   r   	__enter__     zRecords.__enter__c                 C   s:   z| j }W n
 ty   Y d S w || jur|  | ` d S r   )r   r   r   r   )r   exc_type	exc_valueexc_tracebackr   r   r   r   __exit__  s   

zRecords.__exit__c                 C   r   r   r   r!   r   r   r   __iter__  r   zRecords.__iter__c              
   C   s  | j du r!z| j| j }W n
 ty   td w |  jd7  _|S z| j}W n
 ty0   td w | j}| j}	 z| j	 }W n	 tyH   Y n
w |  jd7  _|S |
t}|dkro| `| `|jd urmtd|j dtz||d W n tjy } zt|d d }~ww q8)NTr6   r   r   r   F)r   r   r   rp   StopIterationr   r   r   r   popleftr   r   r   r   r   r   r
   r   r*   )r   r   cacher   r   r   r   r   r   r   __next__  sN   




zRecords.__next__c                    sF   t  |}|td d d kr!| j D ]\}}|dvr ||j|< q|S )N)r   r   r   )rG   ro   rr   __dict__items)r   r   itemrs   rt   rj   r   r   ro   ,  s   
zRecords.__getitem__c                 C   s   | j du rG| j}|dkr(z| jd W n tjy    tdw |   d| _| D ]}| j	| q*| j}|| j
ur?|  | `d| _ || _| jS )z)Overrides the data attribute of UserList.Fr   zQlist-like access after iterating is supported only if the input data is seekable.T)r   r   r   seekioUnsupportedOperationr   r   r   rT   r   r   )r   r   r   r   r   r   r   r   4  s*   

zRecords.datac              	   C   s&   d| j d| jd| jd| jd	S )Nz<Bio.Blast.Records source=z	 program=z	 version=z db=ry   )r   r   r   r   r!   r   r   r   r4   M  s   &zRecords.__repr__c                 C   s<   d| j | jf }| d d  }| jD ]
}|dt| 7 }q|S )NzProgram: %s
     db: %sz

)r   r   r   r   )r   textrecordsr   r   r   r   r"   P  s   
zRecords.__str__)r%   r&   r'   r(   r   r   r   r   r   r   ro   propertyr   r4   r"   rl   r   r   rj   r   r   :  s    w0	%
r   c                 C   s   t | S )a  Parse an XML file containing BLAST output and return a Bio.Blast.Records object.

    This returns an iterator object; iterating over it returns Bio.Blast.Record
    objects one by one.

    The source can be a file stream or the path to an XML file containing the
    BLAST output. If a file stream, source  must be in binary mode. This allows
    the parser to detect the encoding from the XML file,and to use it to convert
    any text in the XML to the correct Unicode string. The qblast function in
    Bio.Blast returns a file stream in binary mode. For files, please use mode
    "rb" when opening the file, as in

    >>> from Bio import Blast
    >>> stream = open("Blast/wnts.xml", "rb")  # opened in binary mode
    >>> records = Blast.parse(stream)
    >>> for record in records:
    ...     print(record.query.id, record.query.description)
    ...
    Query_1 gi|195230749:301-1383 Homo sapiens wingless-type MMTV integration site family member 2 (WNT2), transcript variant 1, mRNA
    Query_2 gi|325053704:108-1166 Homo sapiens wingless-type MMTV integration site family, member 3A (WNT3A), mRNA
    Query_3 gi|156630997:105-1160 Homo sapiens wingless-type MMTV integration site family, member 4 (WNT4), mRNA
    Query_4 gi|371502086:108-1205 Homo sapiens wingless-type MMTV integration site family, member 5A (WNT5A), transcript variant 2, mRNA
    Query_5 gi|53729353:216-1313 Homo sapiens wingless-type MMTV integration site family, member 6 (WNT6), mRNA
    >>> stream.close()

    )r   )r   r   r   r   parse]  s   r   c              	   C   s   t | -}zt|}W n ty   tddw zt| td ty)   Y nw W d   n1 s4w   Y  dD ]}zt||}W n	 tyM   Y q;w t||| q;|S )a*  Parse an XML file containing BLAST output for a single query and return it.

    Internally, this function uses Bio.Blast.parse to obtain an iterator over
    BLAST records.  The function then reads one record from the iterator,
    ensures that there are no further records, and returns the record it found
    as a Bio.Blast.Record object. An exception is raised if no records are
    found, or more than one record is found.

    The source can be a file stream or the path to an XML file containing the
    BLAST output. If a file stream, source  must be in binary mode. This allows
    the parser to detect the encoding from the XML file,and to use it to convert
    any text in the XML to the correct Unicode string. The qblast function in
    Bio.Blast returns a file stream in binary mode. For files, please use mode
    "rb" when opening the file, as in

    >>> from Bio import Blast
    >>> stream = open("Blast/xml_21500_blastn_001.xml", "rb")  # opened in binary mode
    >>> record = Blast.read(stream)
    >>> record.query.id
    'Query_78041'
    >>> record.query.description
    'G26684.1 human STS STS_D11570, sequence tagged site'
    >>> len(record)
    11
    >>> stream.close()

    Use the Bio.Blast.parse function if you want to read a file containing
    BLAST output for more than one query.
    zNo BLAST output found.Nz+BLAST output for more than one query found.r   )r   nextr   r   r   r   r   )r   r   r   rs   rt   r   r   r   r   {  s,   


r   XMLc                 C   s   |  }|dkrtj}n|dkrtj}ntd| dzt|d}W n tyB   z|d W n ty=   tddw |}Y nw ||}z|| }W ||urV|	  |S ||ura|	  w w )	a  Write BLAST records as an XML file, and return the number of records.

    Arguments:
     - records     - A ``Bio.Blast.Records`` object.
     - destination - File or file-like object to write to, or filename as
                     string.
                     The File object must have been opened for writing in
                     binary mode, and must be closed (or flushed) by the caller
                     after this function returns to ensure that all records are
                     written.
     - fmt         - string describing the file format to write
                     (case-insensitive).
                     Currently, only "XML" and "XML2" are accepted.

    Returns the number of records written (as an integer).
    r   XML2zUnknown format z; expected 'XML' or 'XML2'wbr   z/File must be opened in binary mode for writing.N)
upperr   	XMLWriter
XML2Writerr   r   r   writer   r   )r   destinationfmtWriterr   writercountr   r   r   r     s:   	
r   z(none)g      $@2   i  blastc2           B      C   sj  g d}2| |2vrt d|  dd|2 | r3| dkr3d} t|dk r3d}	d	}d
}d}
d}tdt i d|d|d|d|d|d|d|	d|
d|d|d|d|d|d|d|-d|d|i d|d|d|d |d!| d"|d#|d$|d%|d&|d'|d(|d)| d*|.d+|/d,|d-||d.d/}3|1durt }4|4d||0|1 t|4}5t	|5}6t
|6 |tkr|3ttd0 d1d2 |3 D }3t|3 }7t||7d3d4i}8t|8}9t|9\}:};|!|"|#|$|%|&|'|(|)|*|:|+||,d5d6}3d7d2 |3 D }3t|3 }7d8}<t }=	 t }>tj|< |> }?|?d:kr-t|? |>|? t_n|>t_|<d;k r<|tkr<d;}<t |= }@|@d<krQtd=|: d>t t||7d3d4i}8t|8}9|9 }A|)d?krmd@|Av rmq|AdAruq	 |)dBkr|AdCsJ n)|)d?kr|AdDsJ n|)dEv r|AdFsJ n|)dGv r|AdHsJ |:|9_|;|9_|9S )Ia
  BLAST search using NCBI's QBLAST server or a cloud service provider.

    Supports all parameters of the old qblast API for Put and Get.

    Please note that NCBI uses the new Common URL API for BLAST searches
    on the internet (http://ncbi.github.io/blast-cloud/dev/api.html). Thus,
    some of the parameters used by this function are not (or are no longer)
    officially supported by NCBI. Although they are still functioning, this
    may change in the future.

    The Common URL API (http://ncbi.github.io/blast-cloud/dev/api.html) allows
    doing BLAST searches on cloud servers. To use this feature, please set
    ``url_base='http://host.my.cloud.service.provider.com/cgi-bin/blast.cgi'``
    and ``format_object='Alignment'``. For more details, please see
    https://blast.ncbi.nlm.nih.gov/Blast.cgi?PAGE_TYPE=BlastDocs&DOC_TYPE=CloudBlast

    Some useful parameters:

     - program        blastn, blastp, blastx, tblastn, or tblastx (lower case)
     - database       Which database to search against (e.g. "nr").
     - sequence       The sequence to search.
     - ncbi_gi        TRUE/FALSE whether to give 'gi' identifier.
     - descriptions   Number of descriptions to show.  Def 500.
     - alignments     Number of alignments to show.  Def 500.
     - expect         An expect value cutoff.  Def 10.0.
     - matrix_name    Specify an alt. matrix (PAM30, PAM70, BLOSUM80, BLOSUM45).
     - filter         "none" turns off filtering.  Default no filtering
     - format_type    "XML" (default), "HTML", "Text", "XML2", "JSON2",
                      or "Tabular".
     - entrez_query   Entrez query to limit Blast search
     - hitlist_size   Number of hits to return. Default 50
     - megablast      TRUE/FALSE whether to use MEga BLAST algorithm (blastn only)
     - short_query    TRUE/FALSE whether to adjust the search parameters for a
                      short query sequence. Note that this will override
                      manually set parameters like word size and e value. Turns
                      off when sequence length is > 30 residues. Default: None.
     - service        plain, psi, phi, rpsblast, megablast (lower case)

    This function does no checking of the validity of the parameters
    and passes the values to the server as is.  More help is available at:
    https://ncbi.github.io/blast-cloud/dev/api.html

    The http.client.HTTPResponse object returned by this function has the
    additional attributes rid and rtoe with the Request ID and Request Time Of
    Execution for this BLAST search.

    )blastnblastpblastxtblastntblastxzProgram specified is z. Expected one of z, r   N   i     r6   z"SHORT_QUERY_ADJUST" is incorrectly implemented (by NCBI) for blastn. We bypass the problem by manually adjusting the search parameters. Thus, results may slightly differ from web page searches.AUTO_FORMATCOMPOSITION_BASED_STATISTICSDATABASEDB_GENETIC_CODE	ENDPOINTSENTREZ_QUERYEXPECTFILTERGAPCOSTSGENETIC_CODEHITLIST_SIZEI_THRESHLAYOUT
LCASE_MASK	MEGABLASTMATRIX_NAMENUCL_PENALTYNUCL_REWARDOTHER_ADVANCED
PERC_IDENTPHI_PATTERNPROGRAMQUERY
QUERY_FILEQUERY_BELIEVE_DEFLINE
QUERY_FROMQUERY_TOSEARCHSP_EFFSERVICESHORT_QUERY_ADJUSTTEMPLATE_TYPETEMPLATE_LENGTH	THRESHOLDUNGAPPED_ALIGNMENTPut)	WORD_SIZECMD)emailtoolc                 S      i | ]\}}|d ur||qS r   r   r   rs   rt   r   r   r   
<dictcomp>      zqblast.<locals>.<dictcomp>z
User-AgentBiopythonClientGet)
ALIGNMENTSALIGNMENT_VIEWDESCRIPTIONSENTREZ_LINKS_NEW_WINDOW
EXPECT_LOWEXPECT_HIGHFORMAT_ENTREZ_QUERYFORMAT_OBJECTFORMAT_TYPENCBI_GIRIDRESULTS_FILEr   SHOW_OVERVIEWr(  c                 S   r+  r   r   r,  r   r   r   r-    r.     Tr   <   iX  zBLAST request z9 is taking longer than 10 minutes, consider re-issuing itHTMLs   <title>NCBI Blast:</title>s   <!DOCTYPE htmlr   s   <?xml s   <!DOCTYPE html )TextTabulars   <p><!--
QBlastInfoBegin)r   JSON2s   PK)r   rW   rX   warningswarnr   r   add_passwordr   r   r   NCBI_BLAST_URLupdater)  r*  r   r   encoder   r	   _parse_qblast_ref_pagetimeqblastprevioussleeppeek
startswithridrtoe)Br   databasesequenceurl_baseauto_formatcomposition_based_statisticsdb_genetic_code	endpointsentrez_queryexpectfiltergapcostsgenetic_codehitlist_sizei_threshlayout
lcase_maskmatrix_namenucl_penaltynucl_rewardother_advanced
perc_identphi_pattern
query_filequery_believe_defline
query_fromquery_tosearchsp_effservice	thresholdungapped_alignment	word_sizeshort_query
alignmentsalignment_viewdescriptionsentrez_links_new_window
expect_lowexpect_highformat_entrez_queryformat_objectformat_typencbi_giresults_fileshow_overview	megablasttemplate_typetemplate_lengthusernamepasswordprograms
parameterspassword_mgrr   openerr   requestr   rQ  rR  delay
start_timecurrentwaitelapsedr   r   r   r   rL    s8  d	
 !"#$%)







rL  c                 C   s  |    }|d}|dkrd}n|d|}||td |  }|d}|dkr0d}n|d|}||td |  }|s|s|d}|dkrt||td d  }|ddd	 ddd	  }|rttd
| |d}|dkr||td d  }|ddd	 ddd	  }|rtd
| |d}|dkr||d ddd	 ddd	  }td
| td|std|d|std|dz|t|fW S  ty   td|dw )zExtract a tuple of RID, RTOE from the 'please wait' page (PRIVATE).

    The NCBI FAQ pages use TOE for 'Time of Execution', so RTOE is probably
    'Request Time of Execution' and RID would be 'Request Identifier'.
    zRID =r8   Nr   zRTOE =z<div class="error msInf">z</div>r6   r   zError message from NCBI: z<p class="error">z</p>zMessage ID#<zNo RID and no RTOE found in the 'please wait' page, there was probably an error in your request but we could not extract a helpful error message.z9No RID found in the 'please wait' page. (although RTOE = )z9No RTOE found in the 'please wait' page. (although RID = z4A non-integer RTOE found in the 'please wait' page, )r   decodefindrX   stripsplitr   int)handlesr   rQ  jrR  r   r   r   r   rJ    s`   


 
 
(

rJ  __main__)run_doctest)r   )2r(   r   rI   rK  rD  collectionsr   urllib.parser   urllib.requestr   r   r   r   r   r	   xml.parsersr
   numpyrN   Bior   r   
Bio._utilsr   	Bio.Alignr   r   	Bio.Blastr   r)  r*  rG  r   r   r   r*   r+   rm   r   r   r   r   r   r   rL  rM  rJ  r%   r  r   r   r   r   <module>   s   vu y  %
2,  
J
