o
    RŀgFe                    @   s<  d 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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mZ dZdZdedefddZ dedefddZ!d edee"e e"e# f fd!d"Z$d#d$ e%d%D Z&d&d$ e%d'd%D Z'd ede(fd(d)Z)d*e d+ksJ d,d$ e%d-D Z*d.d$ e%d'd-D Z+d ede(fd/d0Z,d*e d+ksJ d1d$ e%d'd-D Z-d2d$ e%d-D Z.d ede(fd3d4Z/d5edee0e(e(e(f  fd6d7Z1G d8d9 d9ee( Z2	dd5ed:ddee fd;d<Z3	dd5ed:ddee fd=d>Z4G d?d@ d@eZ5ee6dAksLJ G dBdC dCeZ7d ede(fdDdEZ8G dFdG dGeZ9d ede(fdHdIZ:G dJdK dKeZ;d ede(fdLdMZ<G dNdO dOeZ=d ede(fdPdQZ>	ddRedSed:ddee fdTdUZ?dVedWedXeee( e	e#e
ee(e#f  f f de#fdYdZZ@dVedWedXeee( e	e#e
ee(e#f  f f d[e(d\e(de#fd]d^ZAdVedWede#fd_d`ZBdVedWede#fdadbZCdVedWede#fdcddZDdVedWede#fdedfZEdVedWede#fdgdhZFdVedWede#fdidjZGdVedWede#fdkdlZHdVedWede#fdmdnZIdVedWede#fdodpZJdVedWede#fdqdrZKdVedWede#fdsdtZLdVedWedXe	e(e(f de#fdudvZMdVedWede#fdwdxZNdVedWede#fdydzZOdVedWede#fd{d|ZPeQd}krdd~lRmSZS eSdd dS dS )a2  Bio.SeqIO support for the FASTQ and QUAL file formats.

Note that you are expected to use this code via the Bio.SeqIO interface, as
shown below.

The FASTQ file format is used frequently at the Wellcome Trust Sanger Institute
to bundle a FASTA sequence and its PHRED quality data (integers between 0 and
90).  Rather than using a single FASTQ file, often paired FASTA and QUAL files
are used containing the sequence and the quality information separately.

The PHRED software reads DNA sequencing trace files, calls bases, and
assigns a non-negative quality value to each called base using a logged
transformation of the error probability, Q = -10 log10( Pe ), for example::

    Pe = 1.0,         Q =  0
    Pe = 0.1,         Q = 10
    Pe = 0.01,        Q = 20
    ...
    Pe = 0.00000001,  Q = 80
    Pe = 0.000000001, Q = 90

In typical raw sequence reads, the PHRED quality valuea will be from 0 to 40.
In the QUAL format these quality values are held as space separated text in
a FASTA like file format.  In the FASTQ format, each quality values is encoded
with a single ASCI character using chr(Q+33), meaning zero maps to the
character "!" and for example 80 maps to "q".  For the Sanger FASTQ standard
the allowed range of PHRED scores is 0 to 93 inclusive. The sequences and
quality are then stored in pairs in a FASTA like format.

Unfortunately there is no official document describing the FASTQ file format,
and worse, several related but different variants exist. For more details,
please read this open access publication::

    The Sanger FASTQ file format for sequences with quality scores, and the
    Solexa/Illumina FASTQ variants.
    P.J.A.Cock (Biopython), C.J.Fields (BioPerl), N.Goto (BioRuby),
    M.L.Heuer (BioJava) and P.M. Rice (EMBOSS).
    Nucleic Acids Research 2010 38(6):1767-1771
    https://doi.org/10.1093/nar/gkp1137

The good news is that Roche 454 sequencers can output files in the QUAL format,
and sensibly they use PHREP style scores like Sanger.  Converting a pair of
FASTA and QUAL files into a Sanger style FASTQ file is easy. To extract QUAL
files from a Roche 454 SFF binary file, use the Roche off instrument command
line tool "sffinfo" with the -q or -qual argument.  You can extract a matching
FASTA file using the -s or -seq argument instead.

The bad news is that Solexa/Illumina did things differently - they have their
own scoring system AND their own incompatible versions of the FASTQ format.
Solexa/Illumina quality scores use Q = - 10 log10 ( Pe / (1-Pe) ), which can
be negative.  PHRED scores and Solexa scores are NOT interchangeable (but a
reasonable mapping can be achieved between them, and they are approximately
equal for higher quality reads).

Confusingly early Solexa pipelines produced a FASTQ like file but using their
own score mapping and an ASCII offset of 64. To make things worse, for the
Solexa/Illumina pipeline 1.3 onwards, they introduced a third variant of the
FASTQ file format, this time using PHRED scores (which is more consistent) but
with an ASCII offset of 64.

i.e. There are at least THREE different and INCOMPATIBLE variants of the FASTQ
file format: The original Sanger PHRED standard, and two from Solexa/Illumina.

The good news is that as of CASAVA version 1.8, Illumina sequencers will
produce FASTQ files using the standard Sanger encoding.

You are expected to use this module via the Bio.SeqIO functions, with the
following format names:

    - "qual" means simple quality files using PHRED scores (e.g. from Roche 454)
    - "fastq" means Sanger style FASTQ files using PHRED scores and an ASCII
      offset of 33 (e.g. from the NCBI Short Read Archive and Illumina 1.8+).
      These can potentially hold PHRED scores from 0 to 93.
    - "fastq-sanger" is an alias for "fastq".
    - "fastq-solexa" means old Solexa (and also very early Illumina) style FASTQ
      files, using Solexa scores with an ASCII offset 64. These can hold Solexa
      scores from -5 to 62.
    - "fastq-illumina" means newer Illumina 1.3 to 1.7 style FASTQ files, using
      PHRED scores but with an ASCII offset 64, allowing PHRED scores from 0
      to 62.

We could potentially add support for "qual-solexa" meaning QUAL files which
contain Solexa scores, but thus far there isn't any reason to use such files.

For example, consider the following short FASTQ file::

    @EAS54_6_R1_2_1_413_324
    CCCTTCTTGTCTTCAGCGTTTCTCC
    +
    ;;3;;;;;;;;;;;;7;;;;;;;88
    @EAS54_6_R1_2_1_540_792
    TTGGCAGGCCAAGGCCGATGGATCA
    +
    ;;;;;;;;;;;7;;;;;-;;;3;83
    @EAS54_6_R1_2_1_443_348
    GTTGCTTCTGGCGTGGGTGGGGGGG
    +
    ;;;;;;;;;;;9;7;;.7;393333

This contains three reads of length 25.  From the read length these were
probably originally from an early Solexa/Illumina sequencer but this file
follows the Sanger FASTQ convention (PHRED style qualities with an ASCII
offset of 33).  This means we can parse this file using Bio.SeqIO using
"fastq" as the format name:

>>> from Bio import SeqIO
>>> for record in SeqIO.parse("Quality/example.fastq", "fastq"):
...     print("%s %s" % (record.id, record.seq))
EAS54_6_R1_2_1_413_324 CCCTTCTTGTCTTCAGCGTTTCTCC
EAS54_6_R1_2_1_540_792 TTGGCAGGCCAAGGCCGATGGATCA
EAS54_6_R1_2_1_443_348 GTTGCTTCTGGCGTGGGTGGGGGGG

The qualities are held as a list of integers in each record's annotation:

>>> print(record)
ID: EAS54_6_R1_2_1_443_348
Name: EAS54_6_R1_2_1_443_348
Description: EAS54_6_R1_2_1_443_348
Number of features: 0
Per letter annotation for: phred_quality
Seq('GTTGCTTCTGGCGTGGGTGGGGGGG')
>>> print(record.letter_annotations["phred_quality"])
[26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 24, 26, 22, 26, 26, 13, 22, 26, 18, 24, 18, 18, 18, 18]

You can use the SeqRecord format method to show this in the QUAL format:

>>> print(record.format("qual"))
>EAS54_6_R1_2_1_443_348
26 26 26 26 26 26 26 26 26 26 26 24 26 22 26 26 13 22 26 18
24 18 18 18 18
<BLANKLINE>

Or go back to the FASTQ format, use "fastq" (or "fastq-sanger"):

>>> print(record.format("fastq"))
@EAS54_6_R1_2_1_443_348
GTTGCTTCTGGCGTGGGTGGGGGGG
+
;;;;;;;;;;;9;7;;.7;393333
<BLANKLINE>

Or, using the Illumina 1.3+ FASTQ encoding (PHRED values with an ASCII offset
of 64):

>>> print(record.format("fastq-illumina"))
@EAS54_6_R1_2_1_443_348
GTTGCTTCTGGCGTGGGTGGGGGGG
+
ZZZZZZZZZZZXZVZZMVZRXRRRR
<BLANKLINE>

You can also get Biopython to convert the scores and show a Solexa style
FASTQ file:

>>> print(record.format("fastq-solexa"))
@EAS54_6_R1_2_1_443_348
GTTGCTTCTGGCGTGGGTGGGGGGG
+
ZZZZZZZZZZZXZVZZMVZRXRRRR
<BLANKLINE>

Notice that this is actually the same output as above using "fastq-illumina"
as the format! The reason for this is all these scores are high enough that
the PHRED and Solexa scores are almost equal. The differences become apparent
for poor quality reads. See the functions solexa_quality_from_phred and
phred_quality_from_solexa for more details.

If you wanted to trim your sequences (perhaps to remove low quality regions,
or to remove a primer sequence), try slicing the SeqRecord objects.  e.g.

>>> sub_rec = record[5:15]
>>> print(sub_rec)
ID: EAS54_6_R1_2_1_443_348
Name: EAS54_6_R1_2_1_443_348
Description: EAS54_6_R1_2_1_443_348
Number of features: 0
Per letter annotation for: phred_quality
Seq('TTCTGGCGTG')
>>> print(sub_rec.letter_annotations["phred_quality"])
[26, 26, 26, 26, 26, 26, 24, 26, 22, 26]
>>> print(sub_rec.format("fastq"))
@EAS54_6_R1_2_1_443_348
TTCTGGCGTG
+
;;;;;;9;7;
<BLANKLINE>

If you wanted to, you could read in this FASTQ file, and save it as a QUAL file:

>>> from Bio import SeqIO
>>> record_iterator = SeqIO.parse("Quality/example.fastq", "fastq")
>>> with open("Quality/temp.qual", "w") as out_handle:
...     SeqIO.write(record_iterator, out_handle, "qual")
3

You can of course read in a QUAL file, such as the one we just created:

>>> from Bio import SeqIO
>>> for record in SeqIO.parse("Quality/temp.qual", "qual"):
...     print("%s read of length %d" % (record.id, len(record.seq)))
EAS54_6_R1_2_1_413_324 read of length 25
EAS54_6_R1_2_1_540_792 read of length 25
EAS54_6_R1_2_1_443_348 read of length 25

Notice that QUAL files don't have a proper sequence present!  But the quality
information is there:

>>> print(record)
ID: EAS54_6_R1_2_1_443_348
Name: EAS54_6_R1_2_1_443_348
Description: EAS54_6_R1_2_1_443_348
Number of features: 0
Per letter annotation for: phred_quality
Undefined sequence of length 25
>>> print(record.letter_annotations["phred_quality"])
[26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 24, 26, 22, 26, 26, 13, 22, 26, 18, 24, 18, 18, 18, 18]

Just to keep things tidy, if you are following this example yourself, you can
delete this temporary file now:

>>> import os
>>> os.remove("Quality/temp.qual")

Sometimes you won't have a FASTQ file, but rather just a pair of FASTA and QUAL
files.  Because the Bio.SeqIO system is designed for reading single files, you
would have to read the two in separately and then combine the data.  However,
since this is such a common thing to want to do, there is a helper iterator
defined in this module that does this for you - PairedFastaQualIterator.

Alternatively, if you have enough RAM to hold all the records in memory at once,
then a simple dictionary approach would work:

>>> from Bio import SeqIO
>>> reads = SeqIO.to_dict(SeqIO.parse("Quality/example.fasta", "fasta"))
>>> for rec in SeqIO.parse("Quality/example.qual", "qual"):
...     reads[rec.id].letter_annotations["phred_quality"]=rec.letter_annotations["phred_quality"]

You can then access any record by its key, and get both the sequence and the
quality scores.

>>> print(reads["EAS54_6_R1_2_1_540_792"].format("fastq"))
@EAS54_6_R1_2_1_540_792
TTGGCAGGCCAAGGCCGATGGATCA
+
;;;;;;;;;;;7;;;;;-;;;3;83
<BLANKLINE>

It is important that you explicitly tell Bio.SeqIO which FASTQ variant you are
using ("fastq" or "fastq-sanger" for the Sanger standard using PHRED values,
"fastq-solexa" for the original Solexa/Illumina variant, or "fastq-illumina"
for the more recent variant), as this cannot be detected reliably
automatically.

To illustrate this problem, let's consider an artificial example:

>>> from Bio.Seq import Seq
>>> from Bio.SeqRecord import SeqRecord
>>> test = SeqRecord(Seq("NACGTACGTA"), id="Test", description="Made up!")
>>> print(test.format("fasta"))
>Test Made up!
NACGTACGTA
<BLANKLINE>
>>> print(test.format("fastq"))
Traceback (most recent call last):
 ...
ValueError: No suitable quality scores found in letter_annotations of SeqRecord (id=Test).

We created a sample SeqRecord, and can show it in FASTA format - but for QUAL
or FASTQ format we need to provide some quality scores. These are held as a
list of integers (one for each base) in the letter_annotations dictionary:

>>> test.letter_annotations["phred_quality"] = [0, 1, 2, 3, 4, 5, 10, 20, 30, 40]
>>> print(test.format("qual"))
>Test Made up!
0 1 2 3 4 5 10 20 30 40
<BLANKLINE>
>>> print(test.format("fastq"))
@Test Made up!
NACGTACGTA
+
!"#$%&+5?I
<BLANKLINE>

We can check this FASTQ encoding - the first PHRED quality was zero, and this
mapped to a exclamation mark, while the final score was 40 and this mapped to
the letter "I":

>>> ord('!') - 33
0
>>> ord('I') - 33
40
>>> [ord(letter)-33 for letter in '!"#$%&+5?I']
[0, 1, 2, 3, 4, 5, 10, 20, 30, 40]

Similarly, we could produce an Illumina 1.3 to 1.7 style FASTQ file using PHRED
scores with an offset of 64:

>>> print(test.format("fastq-illumina"))
@Test Made up!
NACGTACGTA
+
@ABCDEJT^h
<BLANKLINE>

And we can check this too - the first PHRED score was zero, and this mapped to
"@", while the final score was 40 and this mapped to "h":

>>> ord("@") - 64
0
>>> ord("h") - 64
40
>>> [ord(letter)-64 for letter in "@ABCDEJT^h"]
[0, 1, 2, 3, 4, 5, 10, 20, 30, 40]

Notice how different the standard Sanger FASTQ and the Illumina 1.3 to 1.7 style
FASTQ files look for the same data! Then we have the older Solexa/Illumina
format to consider which encodes Solexa scores instead of PHRED scores.

First let's see what Biopython says if we convert the PHRED scores into Solexa
scores (rounding to one decimal place):

>>> for q in [0, 1, 2, 3, 4, 5, 10, 20, 30, 40]:
...     print("PHRED %i maps to Solexa %0.1f" % (q, solexa_quality_from_phred(q)))
PHRED 0 maps to Solexa -5.0
PHRED 1 maps to Solexa -5.0
PHRED 2 maps to Solexa -2.3
PHRED 3 maps to Solexa -0.0
PHRED 4 maps to Solexa 1.8
PHRED 5 maps to Solexa 3.3
PHRED 10 maps to Solexa 9.5
PHRED 20 maps to Solexa 20.0
PHRED 30 maps to Solexa 30.0
PHRED 40 maps to Solexa 40.0

Now here is the record using the old Solexa style FASTQ file:

>>> print(test.format("fastq-solexa"))
@Test Made up!
NACGTACGTA
+
;;>@BCJT^h
<BLANKLINE>

Again, this is using an ASCII offset of 64, so we can check the Solexa scores:

>>> [ord(letter)-64 for letter in ";;>@BCJT^h"]
[-5, -5, -2, 0, 2, 3, 10, 20, 30, 40]

This explains why the last few letters of this FASTQ output matched that using
the Illumina 1.3 to 1.7 format - high quality PHRED scores and Solexa scores
are approximately equal.

    N)log)Callable)IO)Iterator)Mapping)Optional)Sequence)Union)BiopythonParserWarning)BiopythonWarning)StreamModeError)	as_handle)Seq)	SeqRecord   )_clean)_get_seq_string)_TextIOSource)SequenceIterator)SequenceWriter!   @   phred_qualityreturnc                 C   sN   | du rdS | dkrt ddtd| d  d d S | dkr dS td| )at  Convert a PHRED quality (range 0 to about 90) to a Solexa quality.

    PHRED and Solexa quality scores are both log transformations of a
    probality of error (high score = low probability of error). This function
    takes a PHRED score, transforms it back to a probability of error, and
    then re-expresses it as a Solexa score. This assumes the error estimates
    are equivalent.

    How does this work exactly? Well the PHRED quality is minus ten times the
    base ten logarithm of the probability of error::

        phred_quality = -10*log(error,10)

    Therefore, turning this round::

        error = 10 ** (- phred_quality / 10)

    Now, Solexa qualities use a different log transformation::

        solexa_quality = -10*log(error/(1-error),10)

    After substitution and a little manipulation we get::

         solexa_quality = 10*log(10**(phred_quality/10.0) - 1, 10)

    However, real Solexa files use a minimum quality of -5. This does have a
    good reason - a random base call would be correct 25% of the time,
    and thus have a probability of error of 0.75, which gives 1.25 as the PHRED
    quality, or -4.77 as the Solexa quality. Thus (after rounding), a random
    nucleotide read would have a PHRED quality of 1, or a Solexa quality of -5.

    Taken literally, this logarithic formula would map a PHRED quality of zero
    to a Solexa quality of minus infinity. Of course, taken literally, a PHRED
    score of zero means a probability of error of one (i.e. the base call is
    definitely wrong), which is worse than random! In practice, a PHRED quality
    of zero usually means a default value, or perhaps random - and therefore
    mapping it to the minimum Solexa score of -5 is reasonable.

    In conclusion, we follow EMBOSS, and take this logarithmic formula but also
    apply a minimum value of -5.0 for the Solexa quality, and also map a PHRED
    quality of zero to -5.0 as well.

    Note this function will return a floating point number, it is up to you to
    round this to the nearest integer if appropriate.  e.g.

    >>> print("%0.2f" % round(solexa_quality_from_phred(80), 2))
    80.00
    >>> print("%0.2f" % round(solexa_quality_from_phred(50), 2))
    50.00
    >>> print("%0.2f" % round(solexa_quality_from_phred(20), 2))
    19.96
    >>> print("%0.2f" % round(solexa_quality_from_phred(10), 2))
    9.54
    >>> print("%0.2f" % round(solexa_quality_from_phred(5), 2))
    3.35
    >>> print("%0.2f" % round(solexa_quality_from_phred(4), 2))
    1.80
    >>> print("%0.2f" % round(solexa_quality_from_phred(3), 2))
    -0.02
    >>> print("%0.2f" % round(solexa_quality_from_phred(2), 2))
    -2.33
    >>> print("%0.2f" % round(solexa_quality_from_phred(1), 2))
    -5.00
    >>> print("%0.2f" % round(solexa_quality_from_phred(0), 2))
    -5.00

    Notice that for high quality reads PHRED and Solexa scores are numerically
    equal. The differences are important for poor quality reads, where PHRED
    has a minimum of zero but Solexa scores can be negative.

    Finally, as a special case where None is used for a "missing value", None
    is returned:

    >>> print(solexa_quality_from_phred(None))
    None
    Nr   g      
         $@r   z0PHRED qualities must be positive (or zero), not )maxr   
ValueError)r    r   G/var/www/html/myenv/lib/python3.10/site-packages/Bio/SeqIO/QualityIO.pysolexa_quality_from_phred  s   M r    solexa_qualityc                 C   s@   | du rdS | dk rt d| t dtd| d  d d S )a  Convert a Solexa quality (which can be negative) to a PHRED quality.

    PHRED and Solexa quality scores are both log transformations of a
    probality of error (high score = low probability of error). This function
    takes a Solexa score, transforms it back to a probability of error, and
    then re-expresses it as a PHRED score. This assumes the error estimates
    are equivalent.

    The underlying formulas are given in the documentation for the sister
    function solexa_quality_from_phred, in this case the operation is::

        phred_quality = 10*log(10**(solexa_quality/10.0) + 1, 10)

    This will return a floating point number, it is up to you to round this to
    the nearest integer if appropriate.  e.g.

    >>> print("%0.2f" % round(phred_quality_from_solexa(80), 2))
    80.00
    >>> print("%0.2f" % round(phred_quality_from_solexa(20), 2))
    20.04
    >>> print("%0.2f" % round(phred_quality_from_solexa(10), 2))
    10.41
    >>> print("%0.2f" % round(phred_quality_from_solexa(0), 2))
    3.01
    >>> print("%0.2f" % round(phred_quality_from_solexa(-5), 2))
    1.19

    Note that a solexa_quality less then -5 is not expected, will trigger a
    warning, but will still be converted as per the logarithmic mapping
    (giving a number between 0 and 1.19 back).

    As a special case where None is used for a "missing value", None is
    returned:

    >>> print(phred_quality_from_solexa(None))
    None
    Nz$Solexa quality less than -5 passed, r   r   r   )warningswarnr   r   )r!   r   r   r   phred_quality_from_solexa  s   &
r%   recordc                 C   sV   z| j d W S  ty   Y nw zdd | j d D W S  ty*   td| j dw )zExtract PHRED qualities from a SeqRecord's letter_annotations (PRIVATE).

    If there are no PHRED qualities, but there are Solexa qualities, those are
    used instead after conversion.
    r   c                 S      g | ]}t |qS r   )r%   .0qr   r   r   
<listcomp>   s    z&_get_phred_quality.<locals>.<listcomp>r!   LNo suitable quality scores found in letter_annotations of SeqRecord (id=%s).N)letter_annotationsKeyErrorr   id)r&   r   r   r   _get_phred_quality  s$   r0   c                 C       i | ]}|t td |t qS ~   )chrminSANGER_SCORE_OFFSETr)   qpr   r   r   
<dictcomp>,      r9   ^   c              
   C   s,   i | ]}|t td ttt|t qS r2   r4   r5   introundr%   r6   r)   qsr   r   r   r9   0      r"   c                 C     z| j d }W n	 ty   Y n4w zddd |D W S  ty%   Y nw d|v r.tdt|dkr:tdt dd	d |D S z| j d
 }W n tyZ   td| j	 dw zddd |D W S  tyo   Y nw d|v rxtdt|dkrtdt ddd |D S )a	  Return a Sanger FASTQ encoded quality string (PRIVATE).

    >>> from Bio.Seq import Seq
    >>> from Bio.SeqRecord import SeqRecord
    >>> r = SeqRecord(Seq("ACGTAN"), id="Test",
    ...               letter_annotations = {"phred_quality":[50, 40, 30, 20, 10, 0]})
    >>> _get_sanger_quality_str(r)
    'SI?5+!'

    If as in the above example (or indeed a SeqRecord parser with Bio.SeqIO),
    the PHRED qualities are integers, this function is able to use a very fast
    pre-cached mapping. However, if they are floats which differ slightly, then
    it has to do the appropriate rounding - which is slower:

    >>> r2 = SeqRecord(Seq("ACGTAN"), id="Test2",
    ...      letter_annotations = {"phred_quality":[50.0, 40.05, 29.99, 20, 9.55, 0.01]})
    >>> _get_sanger_quality_str(r2)
    'SI?5+!'

    If your scores include a None value, this raises an exception:

    >>> r3 = SeqRecord(Seq("ACGTAN"), id="Test3",
    ...               letter_annotations = {"phred_quality":[50, 40, 30, 20, 10, None]})
    >>> _get_sanger_quality_str(r3)
    Traceback (most recent call last):
       ...
    TypeError: A quality value of None was found

    If (strangely) your record has both PHRED and Solexa scores, then the PHRED
    scores are used in preference:

    >>> r4 = SeqRecord(Seq("ACGTAN"), id="Test4",
    ...               letter_annotations = {"phred_quality":[50, 40, 30, 20, 10, 0],
    ...                                     "solexa_quality":[-5, -4, 0, None, 0, 40]})
    >>> _get_sanger_quality_str(r4)
    'SI?5+!'

    If there are no PHRED scores, but there are Solexa scores, these are used
    instead (after the appropriate conversion):

    >>> r5 = SeqRecord(Seq("ACGTAN"), id="Test5",
    ...      letter_annotations = {"solexa_quality":[40, 30, 20, 10, 0, -5]})
    >>> _get_sanger_quality_str(r5)
    'I?5+$"'

    Again, integer Solexa scores can be looked up in a pre-cached mapping making
    this very fast. You can still use approximate floating point scores:

    >>> r6 = SeqRecord(Seq("ACGTAN"), id="Test6",
    ...      letter_annotations = {"solexa_quality":[40.1, 29.7, 20.01, 10, 0.0, -4.9]})
    >>> _get_sanger_quality_str(r6)
    'I?5+$"'

    Notice that due to the limited range of printable ASCII characters, a
    PHRED quality of 93 is the maximum that can be held in an Illumina FASTQ
    file (using ASCII 126, the tilde). This function will issue a warning
    in this situation.
    r    c                 s       | ]}t | V  qd S N)_phred_to_sanger_quality_strr7   r   r   r   	<genexpr>}      z*_get_sanger_quality_str.<locals>.<genexpr>N!A quality value of None was foundg     `W@z0Data loss - max PHRED quality 93 in Sanger FASTQc                 s   *    | ]}t td tt|t V  qdS r3   N)r4   r5   r=   r>   r6   r7   r   r   r   rG         
r!   r,   c                 s   rD   rE   )_solexa_to_sanger_quality_strr?   r   r   r   rG     rH   c                 s   .    | ]}t td ttt|t V  qdS rK   r<   r?   r   r   r   rG     
    

r-   r.   join	TypeErrorr   r#   r$   r   r   r/   r&   	qualitiesr   r   r   _get_sanger_quality_str6  sX   >

rU   >   r3   c                 C   s   i | ]	}|t |t qS r   r4   SOLEXA_SCORE_OFFSETr7   r   r   r   r9     s    ?   c                 C   s&   i | ]}|t ttt|t qS r   )r4   r=   r>   r%   rX   r?   r   r   r   r9     s    c                 C   rB   )aC  Return an Illumina 1.3 to 1.7 FASTQ encoded quality string (PRIVATE).

    Notice that due to the limited range of printable ASCII characters, a
    PHRED quality of 62 is the maximum that can be held in an Illumina FASTQ
    file (using ASCII 126, the tilde). This function will issue a warning
    in this situation.
    r   rC   c                 s   rD   rE   )_phred_to_illumina_quality_strr7   r   r   r   rG     rH   z,_get_illumina_quality_str.<locals>.<genexpr>NrI        @O@z2Data loss - max PHRED quality 62 in Illumina FASTQc                 s   rJ   rK   r4   r5   r=   r>   rX   r7   r   r   r   rG     rL   r!   r,   c                 s   rD   rE   )_solexa_to_illumina_quality_strr?   r   r   r   rG     rH   c                 s   rN   rK   )r4   r5   r=   r>   r%   rX   r?   r   r   r   rG     rO   rP   rS   r   r   r   _get_illumina_quality_str  sX   

r^   c                 C   r1   r2   )r4   r5   rX   r?   r   r   r   r9     r:   c              
   C   s,   i | ]}|t td ttt|t qS r2   r4   r5   r=   r>   r    rX   r7   r   r   r   r9     rA   c                 C   rB   )a3  Return a Solexa FASTQ encoded quality string (PRIVATE).

    Notice that due to the limited range of printable ASCII characters, a
    Solexa quality of 62 is the maximum that can be held in a Solexa FASTQ
    file (using ASCII 126, the tilde). This function will issue a warning
    in this situation.
    r!   rC   c                 s   rD   rE   )_solexa_to_solexa_quality_strr?   r   r   r   rG     rH   z*_get_solexa_quality_str.<locals>.<genexpr>NrI   r[   1Data loss - max Solexa quality 62 in Solexa FASTQc                 s   rJ   rK   r\   r?   r   r   r   rG     rL   r   r,   c                 s   rD   rE   )_phred_to_solexa_quality_strr7   r   r   r   rG   +  rH   c                 s   rN   rK   r_   r7   r   r   r   rG   8  rO   rP   rS   r   r   r   _get_solexa_quality_str  sX   

rc   sourcec              	   c   s   t | }|ddkrtddzt|}W n ty(   Y W d   dS w 	 |d dkr4td|dd  }d}|D ]}|d d	krJ n|| 7 }q@|rWtd
td|dd  }|rm||krmtdd|v sud|v rytdt|}d}d}|D ]}|d dkrt||kr n|| 7 }q|du rtdd}|t|krtd||t|f |||fV  |du rnq*W d   dS 1 sw   Y  dS )a  Iterate over Fastq records as string tuples (not as SeqRecord objects).

    Arguments:
     - source - input stream opened in text mode, or a path to a file

    This code does not try to interpret the quality string numerically.  It
    just returns tuples of the title, sequence and quality as strings.  For
    the sequence and quality, any whitespace (such as new lines) is removed.

    Our SeqRecord based FASTQ iterators call this function internally, and then
    turn the strings into a SeqRecord objects, mapping the quality string into
    a list of numerical scores.  If you want to do a custom quality mapping,
    then you might consider calling this function directly.

    For parsing FASTQ files, the title string from the "@" line at the start
    of each record can optionally be omitted on the "+" lines.  If it is
    repeated, it must be identical.

    The sequence string and the quality string can optionally be split over
    multiple lines, although several sources discourage this.  In comparison,
    for the FASTA file format line breaks between 60 and 80 characters are
    the norm.

    **WARNING** - Because the "@" character can appear in the quality string,
    this can cause problems as this is also the marker for the start of
    a new sequence.  In fact, the "+" sign can also appear as well.  Some
    sources recommended having no line breaks in the  quality to avoid this,
    but even that is not enough, consider this example::

        @071113_EAS56_0053:1:1:998:236
        TTTCTTGCCCCCATAGACTGAGACCTTCCCTAAATA
        +071113_EAS56_0053:1:1:998:236
        IIIIIIIIIIIIIIIIIIIIIIIIIIIIICII+III
        @071113_EAS56_0053:1:1:182:712
        ACCCAGCTAATTTTTGTATTTTTGTTAGAGACAGTG
        +
        @IIIIIIIIIIIIIIICDIIIII<%<6&-*).(*%+
        @071113_EAS56_0053:1:1:153:10
        TGTTCTGAAGGAAGGTGTGCGTGCGTGTGTGTGTGT
        +
        IIIIIIIIIIIICIIGIIIII>IAIIIE65I=II:6
        @071113_EAS56_0053:1:3:990:501
        TGGGAGGTTTTATGTGGA
        AAGCAGCAATGTACAAGA
        +
        IIIIIII.IIIIII1@44
        @-7.%<&+/$/%4(++(%

    This is four PHRED encoded FASTQ entries originally from an NCBI source
    (given the read length of 36, these are probably Solexa Illumina reads where
    the quality has been mapped onto the PHRED values).

    This example has been edited to illustrate some of the nasty things allowed
    in the FASTQ format.  Firstly, on the "+" lines most but not all of the
    (redundant) identifiers are omitted.  In real files it is likely that all or
    none of these extra identifiers will be present.

    Secondly, while the first three sequences have been shown without line
    breaks, the last has been split over multiple lines.  In real files any line
    breaks are likely to be consistent.

    Thirdly, some of the quality string lines start with an "@" character.  For
    the second record this is unavoidable.  However for the fourth sequence this
    only happens because its quality string is split over two lines.  A naive
    parser could wrongly treat any line starting with an "@" as the beginning of
    a new sequence!  This code copes with this possible ambiguity by keeping
    track of the length of the sequence which gives the expected length of the
    quality string.

    Using this tricky example file as input, this short bit of code demonstrates
    what this parsing function would return:

    >>> with open("Quality/tricky.fastq") as handle:
    ...     for (title, sequence, quality) in FastqGeneralIterator(handle):
    ...         print(title)
    ...         print("%s %s" % (sequence, quality))
    ...
    071113_EAS56_0053:1:1:998:236
    TTTCTTGCCCCCATAGACTGAGACCTTCCCTAAATA IIIIIIIIIIIIIIIIIIIIIIIIIIIIICII+III
    071113_EAS56_0053:1:1:182:712
    ACCCAGCTAATTTTTGTATTTTTGTTAGAGACAGTG @IIIIIIIIIIIIIIICDIIIII<%<6&-*).(*%+
    071113_EAS56_0053:1:1:153:10
    TGTTCTGAAGGAAGGTGTGCGTGCGTGTGTGTGTGT IIIIIIIIIIIICIIGIIIII>IAIIIE65I=II:6
    071113_EAS56_0053:1:3:990:501
    TGGGAGGTTTTATGTGGAAAGCAGCAATGTACAAGA IIIIIII.IIIIII1@44@-7.%<&+/$/%4(++(%

    Finally we note that some sources state that the quality string should
    start with "!" (which using the PHRED mapping means the first letter always
    has a quality score of zero).  This rather restrictive rule is not widely
    observed, so is therefore ignored here.  One plus point about this "!" rule
    is that (provided there are no line breaks in the quality sequence) it
    would prevent the above problem with the "@" character.
    r   rC   z'Fastq files must be opened in text modeNT@z6Records in Fastq files should start with '@' characterr   +z(End of file without quality information.zUnexpected end of filez%Sequence and quality captions differ. 	z*Whitespace is not allowed in the sequence.zBLengths of sequence and quality values differs for %s (%i and %i).)r   readr   nextStopIterationr   rstriplen)rd   handleline
title_line
seq_stringsecond_titleseq_lenquality_stringr   r   r   FastqGeneralIterator?  sh   
^
;"ru   c                       sb   e Zd ZdZ	ddeddf fddZdee dee	 fd	d
Z
dee dee	 fddZ  ZS )FastqPhredIteratorzParser for FASTQ files.Nrd   alphabetc                    &   |durt dt j|ddd dS )a  Iterate over FASTQ records as SeqRecord objects.

        Arguments:
         - source - input stream opened in text mode, or a path to a file
         - alphabet - optional alphabet, no longer used. Leave as None.

        For each sequence in a (Sanger style) FASTQ file there is a matching string
        encoding the PHRED qualities (integers between 0 and about 90) using ASCII
        values with an offset of 33.

        For example, consider a file containing three short reads::

            @EAS54_6_R1_2_1_413_324
            CCCTTCTTGTCTTCAGCGTTTCTCC
            +
            ;;3;;;;;;;;;;;;7;;;;;;;88
            @EAS54_6_R1_2_1_540_792
            TTGGCAGGCCAAGGCCGATGGATCA
            +
            ;;;;;;;;;;;7;;;;;-;;;3;83
            @EAS54_6_R1_2_1_443_348
            GTTGCTTCTGGCGTGGGTGGGGGGG
            +
            ;;;;;;;;;;;9;7;;.7;393333

        For each sequence (e.g. "CCCTTCTTGTCTTCAGCGTTTCTCC") there is a matching
        string encoding the PHRED qualities using a ASCII values with an offset of
        33 (e.g. ";;3;;;;;;;;;;;;7;;;;;;;88").

        Using this module directly you might run:

        >>> with open("Quality/example.fastq") as handle:
        ...     for record in FastqPhredIterator(handle):
        ...         print("%s %s" % (record.id, record.seq))
        EAS54_6_R1_2_1_413_324 CCCTTCTTGTCTTCAGCGTTTCTCC
        EAS54_6_R1_2_1_540_792 TTGGCAGGCCAAGGCCGATGGATCA
        EAS54_6_R1_2_1_443_348 GTTGCTTCTGGCGTGGGTGGGGGGG

        Typically however, you would call this via Bio.SeqIO instead with "fastq"
        (or "fastq-sanger") as the format:

        >>> from Bio import SeqIO
        >>> with open("Quality/example.fastq") as handle:
        ...     for record in SeqIO.parse(handle, "fastq"):
        ...         print("%s %s" % (record.id, record.seq))
        EAS54_6_R1_2_1_413_324 CCCTTCTTGTCTTCAGCGTTTCTCC
        EAS54_6_R1_2_1_540_792 TTGGCAGGCCAAGGCCGATGGATCA
        EAS54_6_R1_2_1_443_348 GTTGCTTCTGGCGTGGGTGGGGGGG

        If you want to look at the qualities, they are record in each record's
        per-letter-annotation dictionary as a simple list of integers:

        >>> print(record.letter_annotations["phred_quality"])
        [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 24, 26, 22, 26, 26, 13, 22, 26, 18, 24, 18, 18, 18, 18]

        To modify the records returned by the parser, you can use a generator
        function. For example, to store the mean PHRED quality in the record
        description, use

        >>> from statistics import mean
        >>> def modify_records(records):
        ...     for record in records:
        ...         record.description = mean(record.letter_annotations['phred_quality'])
        ...         yield record
        ...
        >>> with open('Quality/example.fastq') as handle:
        ...     for record in modify_records(FastqPhredIterator(handle)):
        ...         print(record.id, record.description)
        ...
        EAS54_6_R1_2_1_413_324 25.28
        EAS54_6_R1_2_1_540_792 24.52
        EAS54_6_R1_2_1_443_348 23.4

        N,The alphabet argument is no longer supportedtFastqmodefmtr   super__init__selfrd   rw   	__class__r   r   r     s   OzFastqPhredIterator.__init__rn   r   c                 C      |  |}|S z8Start parsing the file, and return a SeqRecord iterator.iterater   rn   recordsr   r   r   parse9     
zFastqPhredIterator.parsec           
   	   #   s    t tdks	J dd tt dt  D  t|D ]<\}}}|}| d }|}tt||||d}z fdd|D }	W n tyI   td	d
w t	
|jd|	 |V  qd
S ).Parse the file and generate SeqRecord objects.!c                 S      i | ]	}t ||t qS r   )r4   r6   r)   letterr   r   r   r9   F      z.FastqPhredIterator.iterate.<locals>.<dictcomp>r;   r   r/   namedescriptionc                       g | ]} | qS r   r   r)   letter2	q_mappingr   r   r+   Q      z.FastqPhredIterator.iterate.<locals>.<listcomp>#Invalid character in quality stringNr   )r6   ordrangeru   splitr   r   r.   r   dict__setitem___per_letter_annotations)
r   rn   rp   rq   rt   descrr/   r   r&   rT   r   r   r   r   >  s$   
zFastqPhredIterator.iteraterE   )__name__
__module____qualname____doc__r   r   r   strr   r   r   r   __classcell__r   r   r   r   rv     s    S"rv   rw   c           
   	   #   s    |dur	t ddd ttd dt D  t| D ]<\}}}|}| d }|}tt||||d}z fd	d
|D }	W n tyK   t ddw t	|j
d|	 |V  qdS )aF  Parse old Solexa/Illumina FASTQ like files (which differ in the quality mapping).

    The optional arguments are the same as those for the FastqPhredIterator.

    For each sequence in Solexa/Illumina FASTQ files there is a matching string
    encoding the Solexa integer qualities using ASCII values with an offset
    of 64.  Solexa scores are scaled differently to PHRED scores, and Biopython
    will NOT perform any automatic conversion when loading.

    NOTE - This file format is used by the OLD versions of the Solexa/Illumina
    pipeline. See also the FastqIlluminaIterator function for the NEW version.

    For example, consider a file containing these five records::

        @SLXA-B3_649_FC8437_R1_1_1_610_79
        GATGTGCAATACCTTTGTAGAGGAA
        +SLXA-B3_649_FC8437_R1_1_1_610_79
        YYYYYYYYYYYYYYYYYYWYWYYSU
        @SLXA-B3_649_FC8437_R1_1_1_397_389
        GGTTTGAGAAAGAGAAATGAGATAA
        +SLXA-B3_649_FC8437_R1_1_1_397_389
        YYYYYYYYYWYYYYWWYYYWYWYWW
        @SLXA-B3_649_FC8437_R1_1_1_850_123
        GAGGGTGTTGATCATGATGATGGCG
        +SLXA-B3_649_FC8437_R1_1_1_850_123
        YYYYYYYYYYYYYWYYWYYSYYYSY
        @SLXA-B3_649_FC8437_R1_1_1_362_549
        GGAAACAAAGTTTTTCTCAACATAG
        +SLXA-B3_649_FC8437_R1_1_1_362_549
        YYYYYYYYYYYYYYYYYYWWWWYWY
        @SLXA-B3_649_FC8437_R1_1_1_183_714
        GTATTATTTAATGGCATACACTCAA
        +SLXA-B3_649_FC8437_R1_1_1_183_714
        YYYYYYYYYYWYYYYWYWWUWWWQQ

    Using this module directly you might run:

    >>> with open("Quality/solexa_example.fastq") as handle:
    ...     for record in FastqSolexaIterator(handle):
    ...         print("%s %s" % (record.id, record.seq))
    SLXA-B3_649_FC8437_R1_1_1_610_79 GATGTGCAATACCTTTGTAGAGGAA
    SLXA-B3_649_FC8437_R1_1_1_397_389 GGTTTGAGAAAGAGAAATGAGATAA
    SLXA-B3_649_FC8437_R1_1_1_850_123 GAGGGTGTTGATCATGATGATGGCG
    SLXA-B3_649_FC8437_R1_1_1_362_549 GGAAACAAAGTTTTTCTCAACATAG
    SLXA-B3_649_FC8437_R1_1_1_183_714 GTATTATTTAATGGCATACACTCAA

    Typically however, you would call this via Bio.SeqIO instead with
    "fastq-solexa" as the format:

    >>> from Bio import SeqIO
    >>> with open("Quality/solexa_example.fastq") as handle:
    ...     for record in SeqIO.parse(handle, "fastq-solexa"):
    ...         print("%s %s" % (record.id, record.seq))
    SLXA-B3_649_FC8437_R1_1_1_610_79 GATGTGCAATACCTTTGTAGAGGAA
    SLXA-B3_649_FC8437_R1_1_1_397_389 GGTTTGAGAAAGAGAAATGAGATAA
    SLXA-B3_649_FC8437_R1_1_1_850_123 GAGGGTGTTGATCATGATGATGGCG
    SLXA-B3_649_FC8437_R1_1_1_362_549 GGAAACAAAGTTTTTCTCAACATAG
    SLXA-B3_649_FC8437_R1_1_1_183_714 GTATTATTTAATGGCATACACTCAA

    If you want to look at the qualities, they are recorded in each record's
    per-letter-annotation dictionary as a simple list of integers:

    >>> print(record.letter_annotations["solexa_quality"])
    [25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 23, 25, 25, 25, 25, 23, 25, 23, 23, 21, 23, 23, 23, 17, 17]

    These scores aren't very good, but they are high enough that they map
    almost exactly onto PHRED scores:

    >>> print("%0.2f" % phred_quality_from_solexa(25))
    25.01

    Let's look at faked example read which is even worse, where there are
    more noticeable differences between the Solexa and PHRED scores::

         @slxa_0001_1_0001_01
         ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTNNNNNN
         +slxa_0001_1_0001_01
         hgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;

    Again, you would typically use Bio.SeqIO to read this file in (rather than
    calling the Bio.SeqIO.QualtityIO module directly).  Most FASTQ files will
    contain thousands of reads, so you would normally use Bio.SeqIO.parse()
    as shown above.  This example has only as one entry, so instead we can
    use the Bio.SeqIO.read() function:

    >>> from Bio import SeqIO
    >>> with open("Quality/solexa_faked.fastq") as handle:
    ...     record = SeqIO.read(handle, "fastq-solexa")
    >>> print("%s %s" % (record.id, record.seq))
    slxa_0001_1_0001_01 ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTNNNNNN
    >>> print(record.letter_annotations["solexa_quality"])
    [40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]

    These quality scores are so low that when converted from the Solexa scheme
    into PHRED scores they look quite different:

    >>> print("%0.2f" % phred_quality_from_solexa(-1))
    2.54
    >>> print("%0.2f" % phred_quality_from_solexa(-5))
    1.19

    Note you can use the Bio.SeqIO.write() function or the SeqRecord's format
    method to output the record(s):

    >>> print(record.format("fastq-solexa"))
    @slxa_0001_1_0001_01
    ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTNNNNNN
    +
    hgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;
    <BLANKLINE>

    Note this output is slightly different from the input file as Biopython
    has left out the optional repetition of the sequence identifier on the "+"
    line.  If you want the to use PHRED scores, use "fastq" or "qual" as the
    output format instead, and Biopython will do the conversion for you:

    >>> print(record.format("fastq"))
    @slxa_0001_1_0001_01
    ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTNNNNNN
    +
    IHGFEDCBA@?>=<;:9876543210/.-,++*)('&&%%$$##""
    <BLANKLINE>

    >>> print(record.format("qual"))
    >slxa_0001_1_0001_01
    40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
    20 19 18 17 16 15 14 13 12 11 10 10 9 8 7 6 5 5 4 4 3 3 2 2
    1 1
    <BLANKLINE>

    As shown above, the poor quality Solexa reads have been mapped to the
    equivalent PHRED score (e.g. -5 to 1 as shown earlier).
    Nry   c                 S   r   r   rW   r   r   r   r   r9     r   z'FastqSolexaIterator.<locals>.<dictcomp>   rY   r   r   c                    r   r   r   r   r   r   r   r+     r   z'FastqSolexaIterator.<locals>.<listcomp>r   r!   r   r   rX   ru   r   r   r   r.   r   r   r   
rd   rw   rp   rq   rt   r   r/   r   r&   rT   r   r   r   FastqSolexaIterator]  s(    

r   c           
   	   #   s    |dur	t ddd ttdt D  t| D ]<\}}}|}| d }|}tt||||d}z fdd	|D }	W n tyI   t d
dw t	|j
d|	 |V  qdS )a  Parse Illumina 1.3 to 1.7 FASTQ like files (which differ in the quality mapping).

    The optional arguments are the same as those for the FastqPhredIterator.

    For each sequence in Illumina 1.3+ FASTQ files there is a matching string
    encoding PHRED integer qualities using ASCII values with an offset of 64.

    >>> from Bio import SeqIO
    >>> record = SeqIO.read("Quality/illumina_faked.fastq", "fastq-illumina")
    >>> print("%s %s" % (record.id, record.seq))
    Test ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTN
    >>> max(record.letter_annotations["phred_quality"])
    40
    >>> min(record.letter_annotations["phred_quality"])
    0

    NOTE - Older versions of the Solexa/Illumina pipeline encoded Solexa scores
    with an ASCII offset of 64. They are approximately equal but only for high
    quality reads. If you have an old Solexa/Illumina file with negative
    Solexa scores, and try and read this as an Illumina 1.3+ file it will fail:

    >>> record2 = SeqIO.read("Quality/solexa_faked.fastq", "fastq-illumina")
    Traceback (most recent call last):
       ...
    ValueError: Invalid character in quality string

    NOTE - True Sanger style FASTQ files use PHRED scores with an offset of 33.
    Nry   c                 S   r   r   rW   r   r   r   r   r9   !  r   z)FastqIlluminaIterator.<locals>.<dictcomp>rY   r   r   c                    r   r   r   r   r   r   r   r+   ,  r   z)FastqIlluminaIterator.<locals>.<listcomp>r   r   r   r   r   r   r   FastqIlluminaIterator  s&    
r   c                       s^   e Zd ZdZ	ddeddddf fddZdedee fd	d
Z	dedee fddZ
  ZS )QualPhredIteratorz@Parser for QUAL files with PHRED quality scores but no sequence.Nrd   rw   r   c                    rx   )a	  For QUAL files which include PHRED quality scores, but no sequence.

        For example, consider this short QUAL file::

            >EAS54_6_R1_2_1_413_324
            26 26 18 26 26 26 26 26 26 26 26 26 26 26 26 22 26 26 26 26
            26 26 26 23 23
            >EAS54_6_R1_2_1_540_792
            26 26 26 26 26 26 26 26 26 26 26 22 26 26 26 26 26 12 26 26
            26 18 26 23 18
            >EAS54_6_R1_2_1_443_348
            26 26 26 26 26 26 26 26 26 26 26 24 26 22 26 26 13 22 26 18
            24 18 18 18 18

        Using this module directly you might run:

        >>> with open("Quality/example.qual") as handle:
        ...     for record in QualPhredIterator(handle):
        ...         print("%s read of length %d" % (record.id, len(record.seq)))
        EAS54_6_R1_2_1_413_324 read of length 25
        EAS54_6_R1_2_1_540_792 read of length 25
        EAS54_6_R1_2_1_443_348 read of length 25

        Typically however, you would call this via Bio.SeqIO instead with "qual"
        as the format:

        >>> from Bio import SeqIO
        >>> with open("Quality/example.qual") as handle:
        ...     for record in SeqIO.parse(handle, "qual"):
        ...         print("%s read of length %d" % (record.id, len(record.seq)))
        EAS54_6_R1_2_1_413_324 read of length 25
        EAS54_6_R1_2_1_540_792 read of length 25
        EAS54_6_R1_2_1_443_348 read of length 25

        Only the sequence length is known, as the QUAL file does not contain
        the sequence string itself.

        The quality scores themselves are available as a list of integers
        in each record's per-letter-annotation:

        >>> print(record.letter_annotations["phred_quality"])
        [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 24, 26, 22, 26, 26, 13, 22, 26, 18, 24, 18, 18, 18, 18]

        You can still slice one of these SeqRecord objects:

        >>> sub_record = record[5:10]
        >>> print("%s %s" % (sub_record.id, sub_record.letter_annotations["phred_quality"]))
        EAS54_6_R1_2_1_443_348 [26, 26, 26, 26, 26]

        As of Biopython 1.59, this parser will accept files with negatives quality
        scores but will replace them with the lowest possible PHRED score of zero.
        This will trigger a warning, previously it raised a ValueError exception.
        Nry   rz   QUALr|   r   r   r   r   r   r   8  s   :zQualPhredIterator.__init__rn   c                 C   r   r   r   r   r   r   r   r   v  r   zQualPhredIterator.parsec           	      c   s   |D ]
}|d dkr nqdS 	 |d dkrt d|dd  }| d }|}g }|D ]}|d dkr9 n|dd | D  q/d}|rat|dk ratd	t| t d
d |D }tdt	|d}t
||||d}t|jd| |V  |du rdS q)r   r   >NTz6Records in Fasta files should start with '>' characterr   c                 s   s    | ]}t |V  qd S rE   )r=   )r)   wordr   r   r   rG     rH   z,QualPhredIterator.iterate.<locals>.<genexpr>zANegative quality score %i found, substituting PHRED zero instead.c                 S   s   g | ]}t d |qS r   )r   r(   r   r   r   r+     s    z-QualPhredIterator.iterate.<locals>.<listcomp>)lengthr   r   )r   rl   r   extendr5   r#   r$   r
   r   rm   r   r   r   r   )	r   rn   ro   r   r/   r   rT   sequencer&   r   r   r   r   {  sF   zQualPhredIterator.iteraterE   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   5  s    >r   r   c                   @   "   e Zd ZdZdeddfddZdS )FastqPhredWritera  Class to write standard FASTQ format files (using PHRED quality scores) (OBSOLETE).

    Although you can use this class directly, you are strongly encouraged
    to use the ``as_fastq`` function, or top level ``Bio.SeqIO.write()``
    function instead via the format name "fastq" or the alias "fastq-sanger".

    For example, this code reads in a standard Sanger style FASTQ file
    (using PHRED scores) and re-saves it as another Sanger style FASTQ file:

    >>> from Bio import SeqIO
    >>> record_iterator = SeqIO.parse("Quality/example.fastq", "fastq")
    >>> with open("Quality/temp.fastq", "w") as out_handle:
    ...     SeqIO.write(record_iterator, out_handle, "fastq")
    3

    You might want to do this if the original file included extra line breaks,
    which while valid may not be supported by all tools.  The output file from
    Biopython will have each sequence on a single line, and each quality
    string on a single line (which is considered desirable for maximum
    compatibility).

    In this next example, an old style Solexa/Illumina FASTQ file (using Solexa
    quality scores) is converted into a standard Sanger style FASTQ file using
    PHRED qualities:

    >>> from Bio import SeqIO
    >>> record_iterator = SeqIO.parse("Quality/solexa_example.fastq", "fastq-solexa")
    >>> with open("Quality/temp.fastq", "w") as out_handle:
    ...     SeqIO.write(record_iterator, out_handle, "fastq")
    5

    This code is also called if you use the .format("fastq") method of a
    SeqRecord, or .format("fastq-sanger") if you prefer that alias.

    Note that Sanger FASTQ files have an upper limit of PHRED quality 93, which is
    encoded as ASCII 126, the tilde. If your quality scores are truncated to fit, a
    warning is issued.

    P.S. To avoid cluttering up your working directory, you can delete this
    temporary file now:

    >>> import os
    >>> os.remove("Quality/temp.fastq")
    r&   r   Nc              	   C      d| _ |j}|du rtd|j t|}t|t|kr,td|jt|t|f |jr5| |jnd}| |j}|rL|ddd |krL|}n|rV| d| }n|}| j	
d	| d
| d| d
 dS z(Write a single FASTQ record to the file.TNzNo sequence for record 6Record %s has sequence length %i but %i quality scoresrC   r   r   rg   re   

+
)_record_writtenseqr   r/   rU   rm   cleanr   r   rn   writer   r&   r   qualities_strid_r   titler   r   r   write_record  s&   $zFastqPhredWriter.write_recordr   r   r   r   r   r   r   r   r   r   r     s    -r   c                 C      t | }t| }t|t|krtd| jt|t|f | jr&t| jnd}t| j}|r<|ddd |kr<|}n|rF| d| }n|}d| d| d	| dS )
zTurn a SeqRecord into a Sanger FASTQ formatted string.

    This is used internally by the SeqRecord's .format("fastq")
    method and by the SeqIO.write(..., ..., "fastq") function,
    and under the format alias "fastq-sanger" as well.
    r   rC   Nr   r   rg   re   r   r   )r   rU   rm   r   r/   r   r   r   r&   seq_strr   r   r   r   r   r   r   as_fastq  s    
r   c                
       sX   e Zd ZdZ		ddededeeege	f  ddf fdd	Z
d
eddfddZ  ZS )QualPhredWritera"  Class to write QUAL format files (using PHRED quality scores) (OBSOLETE).

    Although you can use this class directly, you are strongly encouraged
    to use the ``as_qual`` function, or top level ``Bio.SeqIO.write()``
    function instead.

    For example, this code reads in a FASTQ file and saves the quality scores
    into a QUAL file:

    >>> from Bio import SeqIO
    >>> record_iterator = SeqIO.parse("Quality/example.fastq", "fastq")
    >>> with open("Quality/temp.qual", "w") as out_handle:
    ...     SeqIO.write(record_iterator, out_handle, "qual")
    3

    This code is also called if you use the .format("qual") method of a
    SeqRecord.

    P.S. Don't forget to clean up the temp file if you don't need it anymore:

    >>> import os
    >>> os.remove("Quality/temp.qual")
    <   Nrn   wraprecord2titler   c                    s2   t  | d| _|r|dk rt|| _|| _dS )a1  Create a QUAL writer.

        Arguments:
         - handle - Handle to an output file, e.g. as returned
           by open(filename, "w")
         - wrap   - Optional line length used to wrap sequence lines.
           Defaults to wrapping the sequence at 60 characters. Use
           zero (or None) for no wrapping, giving a single long line
           for the sequence.
         - record2title - Optional function to return the text to be
           used for the title line of each record.  By default a
           combination of the record.id and record.description is
           used.  If the record.description starts with the record.id,
           then just the record.description is used.

        The record2title argument is present for consistency with the
        Bio.SeqIO.FastaIO writer class.
        Nr   )r   r   r   r   r   )r   rn   r   r   r   r   r   r   *  s   
zQualPhredWriter.__init__r&   c                 C   s  d| _ | j}| j}| jr| | |}n,|jr| |jnd}| |j}|r5|ddd |kr5|}n|r?t d| }n|}|d| d t	|}z	d	d
 |D }W n t
yh   d|v rgt
dd w |dur|dkrd|}		 t|	|kr| j|	d  dS |	dd|}
||	d|
 d  |	|
d d }	qw|r|r|d}|rt|d t|d  |k r|d|d 7 }|rt|d t|d  |k s||d  |sdS dS d|}	||	d  dS )z'Write a single QUAL record to the file.TrC   Nr   r   rg   r   r   c                 S      g | ]	}d t |d qS z%ir   r>   r(   r   r   r   r+   d      z0QualPhredWriter.write_record.<locals>.<listcomp>rI   r   )r   rn   r   r   r   r/   r   r   r   r0   rR   rQ   rm   rfindpop)r   r&   rn   r   r   r   r   rT   qualities_strsdatairo   r   r   r   r   K  sR   



  
zQualPhredWriter.write_record)r   N)r   r   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r   r     s    !r   c                 C   s  | j rt| j nd}t| j}|r|ddd |kr|}n|r(| d| }n|}d| dg}t| }z	dd	 |D }W n tyO   d|v rNtd
d w |r|d}|rt|d t|d  dk r|d|d 7 }|rt|d t|d  dk sg||d  |sRd	|S )zTurn a SeqRecord into a QUAL formatted string.

    This is used internally by the SeqRecord's .format("qual")
    method and by the SeqIO.write(..., ..., "qual") function.
    rC   Nr   r   rg   r   r   c                 S   r   r   r   r(   r   r   r   r+     r   zas_qual.<locals>.<listcomp>rI   r   )
r/   r   r   r   r0   rR   r   rm   appendrQ   )r&   r   r   r   linesrT   r   ro   r   r   r   as_qual  s0   


  
r   c                   @   r   )FastqSolexaWriteraq  Write old style Solexa/Illumina FASTQ format files (with Solexa qualities) (OBSOLETE).

    This outputs FASTQ files like those from the early Solexa/Illumina
    pipeline, using Solexa scores and an ASCII offset of 64. These are
    NOT compatible with the standard Sanger style PHRED FASTQ files.

    If your records contain a "solexa_quality" entry under letter_annotations,
    this is used, otherwise any "phred_quality" entry will be used after
    conversion using the solexa_quality_from_phred function. If neither style
    of quality scores are present, an exception is raised.

    Although you can use this class directly, you are strongly encouraged
    to use the ``as_fastq_solexa`` function, or top-level ``Bio.SeqIO.write()``
    function instead.  For example, this code reads in a FASTQ file and re-saves
    it as another FASTQ file:

    >>> from Bio import SeqIO
    >>> record_iterator = SeqIO.parse("Quality/solexa_example.fastq", "fastq-solexa")
    >>> with open("Quality/temp.fastq", "w") as out_handle:
    ...     SeqIO.write(record_iterator, out_handle, "fastq-solexa")
    5

    You might want to do this if the original file included extra line breaks,
    which (while valid) may not be supported by all tools.  The output file
    from Biopython will have each sequence on a single line, and each quality
    string on a single line (which is considered desirable for maximum
    compatibility).

    This code is also called if you use the .format("fastq-solexa") method of
    a SeqRecord. For example,

    >>> record = SeqIO.read("Quality/sanger_faked.fastq", "fastq-sanger")
    >>> print(record.format("fastq-solexa"))
    @Test PHRED qualities from 40 to 0 inclusive
    ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTN
    +
    hgfedcba`_^]\[ZYXWVUTSRQPONMLKJHGFECB@>;;
    <BLANKLINE>

    Note that Solexa FASTQ files have an upper limit of Solexa quality 62, which is
    encoded as ASCII 126, the tilde.  If your quality scores must be truncated to fit,
    a warning is issued.

    P.S. Don't forget to delete the temp file if you don't need it anymore:

    >>> import os
    >>> os.remove("Quality/temp.fastq")
    r&   r   Nc              	   C   r   r   )r   r   r   r/   rc   rm   r   r   r   rn   r   r   r   r   r   r     &   $zFastqSolexaWriter.write_recordr   r   r   r   r   r     s    1r   c                 C   r   )
zTurn a SeqRecord into a Solexa FASTQ formatted string.

    This is used internally by the SeqRecord's .format("fastq-solexa")
    method and by the SeqIO.write(..., ..., "fastq-solexa") function.
    r   rC   Nr   r   rg   re   r   r   )r   rc   rm   r   r/   r   r   r   r   r   r   r   as_fastq_solexa  s    
r   c                   @   r   )FastqIlluminaWritera  Write Illumina 1.3+ FASTQ format files (with PHRED quality scores) (OBSOLETE).

    This outputs FASTQ files like those from the Solexa/Illumina 1.3+ pipeline,
    using PHRED scores and an ASCII offset of 64. Note these files are NOT
    compatible with the standard Sanger style PHRED FASTQ files which use an
    ASCII offset of 32.

    Although you can use this class directly, you are strongly encouraged to
    use the ``as_fastq_illumina`` or top-level ``Bio.SeqIO.write()`` function
    with format name "fastq-illumina" instead. This code is also called if you
    use the .format("fastq-illumina") method of a SeqRecord. For example,

    >>> from Bio import SeqIO
    >>> record = SeqIO.read("Quality/sanger_faked.fastq", "fastq-sanger")
    >>> print(record.format("fastq-illumina"))
    @Test PHRED qualities from 40 to 0 inclusive
    ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTN
    +
    hgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@
    <BLANKLINE>

    Note that Illumina FASTQ files have an upper limit of PHRED quality 62, which is
    encoded as ASCII 126, the tilde. If your quality scores are truncated to fit, a
    warning is issued.
    r&   r   Nc              	   C   r   r   )r   r   r   r/   r^   rm   r   r   r   rn   r   r   r   r   r   r   -  r   z FastqIlluminaWriter.write_recordr   r   r   r   r   r     s    r   c                 C   r   )
zTurn a SeqRecord into an Illumina FASTQ formatted string.

    This is used internally by the SeqRecord's .format("fastq-illumina")
    method and by the SeqIO.write(..., ..., "fastq-illumina") function.
    r   rC   Nr   r   rg   re   r   r   )r   r^   rm   r   r/   r   r   r   r   r   r   r   as_fastq_illuminaK  s    
r   fasta_sourcequal_sourcec                 c   s   |dur	t dddlm} || }t|}	 zt|}W n ty)   d}Y nw zt|}W n ty;   d}Y nw |du rF|du rFdS |du rNt d|du rVt d|j|jkrit d|j d	|j d
t|t|jd kr|t d|j |jd |jd< |V  q)a	  Iterate over matched FASTA and QUAL files as SeqRecord objects.

    For example, consider this short QUAL file with PHRED quality scores::

        >EAS54_6_R1_2_1_413_324
        26 26 18 26 26 26 26 26 26 26 26 26 26 26 26 22 26 26 26 26
        26 26 26 23 23
        >EAS54_6_R1_2_1_540_792
        26 26 26 26 26 26 26 26 26 26 26 22 26 26 26 26 26 12 26 26
        26 18 26 23 18
        >EAS54_6_R1_2_1_443_348
        26 26 26 26 26 26 26 26 26 26 26 24 26 22 26 26 13 22 26 18
        24 18 18 18 18

    And a matching FASTA file::

        >EAS54_6_R1_2_1_413_324
        CCCTTCTTGTCTTCAGCGTTTCTCC
        >EAS54_6_R1_2_1_540_792
        TTGGCAGGCCAAGGCCGATGGATCA
        >EAS54_6_R1_2_1_443_348
        GTTGCTTCTGGCGTGGGTGGGGGGG

    You can parse these separately using Bio.SeqIO with the "qual" and
    "fasta" formats, but then you'll get a group of SeqRecord objects with
    no sequence, and a matching group with the sequence but not the
    qualities.  Because it only deals with one input file handle, Bio.SeqIO
    can't be used to read the two files together - but this function can!
    For example,

    >>> with open("Quality/example.fasta") as f:
    ...     with open("Quality/example.qual") as q:
    ...         for record in PairedFastaQualIterator(f, q):
    ...             print("%s %s" % (record.id, record.seq))
    ...
    EAS54_6_R1_2_1_413_324 CCCTTCTTGTCTTCAGCGTTTCTCC
    EAS54_6_R1_2_1_540_792 TTGGCAGGCCAAGGCCGATGGATCA
    EAS54_6_R1_2_1_443_348 GTTGCTTCTGGCGTGGGTGGGGGGG

    As with the FASTQ or QUAL parsers, if you want to look at the qualities,
    they are in each record's per-letter-annotation dictionary as a simple
    list of integers:

    >>> print(record.letter_annotations["phred_quality"])
    [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 24, 26, 22, 26, 26, 13, 22, 26, 18, 24, 18, 18, 18, 18]

    If you have access to data as a FASTQ format file, using that directly
    would be simpler and more straight forward.  Note that you can easily use
    this function to convert paired FASTA and QUAL files into FASTQ files:

    >>> from Bio import SeqIO
    >>> with open("Quality/example.fasta") as f:
    ...     with open("Quality/example.qual") as q:
    ...         SeqIO.write(PairedFastaQualIterator(f, q), "Quality/temp.fastq", "fastq")
    ...
    3

    And don't forget to clean up the temp file if you don't need it anymore:

    >>> import os
    >>> os.remove("Quality/temp.fastq")
    Nry   r   )FastaIteratorTz/FASTA file has more entries than the QUAL file.z/QUAL file has more entries than the FASTA file.z%FASTA and QUAL entries do not match (z vs z).r   z:Sequence length and number of quality scores disagree for )	r   Bio.SeqIO.FastaIOr   r   rj   rk   r/   rm   r-   )r   r   rw   r   
fasta_iter	qual_iterf_recq_recr   r   r   PairedFastaQualIteratorc  sH   C

r   in_fileout_filemappingc           
      C   s   d}t d}t|d3}t| D ]%\}}}|d7 }||}	||	v r&td|d| d| d|	 d qW d   |S 1 sAw   Y  |S )	zIFASTQ helper function where can't have data loss by truncation (PRIVATE).r   wr   r   re   r   r   N)r4   r   ru   	translater   r   )
r   r   r   countnull
out_handler   r   old_qualqualr   r   r   _fastq_generic  s   
 
r   truncate_chartruncate_msgc                 C   s   d}t d}t|dE}t| D ]7\}}	}
|d7 }|
|}||v r&td||v r8||t d}t|t |	d| d|	 d| d qW d	   |S 1 sSw   Y  |S )
zMFASTQ helper function where there could be data loss by truncation (PRIVATE).r   r   r   r   r3   re   r   r   N)
r4   r   ru   r   r   replacer#   r$   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   _fastq_generic2  s"   	
 
r   c                 C   \   d dd tdD dd tddD  dd tddD  }t|dks(J t| ||S )	a*  Fast Sanger FASTQ to Sanger FASTQ conversion (PRIVATE).

    Useful for removing line wrapping and the redundant second identifier
    on the plus lines. Will check also check the quality string is valid.

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S      g | ]}t d qS r   r4   r)   asciir   r   r   r+   
  r   z6_fastq_sanger_convert_fastq_sanger.<locals>.<listcomp>r   c                 S   r'   r   r   r   r   r   r   r+     r      c                 S   r   r   r   r   r   r   r   r+     r      rQ   r   rm   r   r   r   r   r   r   r   "_fastq_sanger_convert_fastq_sanger  s   r  c                 C   r   )	a)  Fast Solexa FASTQ to Solexa FASTQ conversion (PRIVATE).

    Useful for removing line wrapping and the redundant second identifier
    on the plus lines. Will check also check the quality string is valid.
    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S   r   r   r   r   r   r   r   r+     r   z6_fastq_solexa_convert_fastq_solexa.<locals>.<listcomp>;   c                 S   r'   r   r   r   r   r   r   r+     r   r   c                 S   r   r   r   r   r   r   r   r+      r   r   r  r  r   r   r   "_fastq_solexa_convert_fastq_solexa     r  c                 C   r   )	a7  Fast Illumina 1.3+ FASTQ to Illumina 1.3+ FASTQ conversion (PRIVATE).

    Useful for removing line wrapping and the redundant second identifier
    on the plus lines. Will check also check the quality string is valid.
    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S   r   r   r   r   r   r   r   r+   2  r   z:_fastq_illumina_convert_fastq_illumina.<locals>.<listcomp>r   c                 S   r'   r   r   r   r   r   r   r+   3  r   r   c                 S   r   r   r   r   r   r   r   r+   4  r   r   r  r  r   r   r   &_fastq_illumina_convert_fastq_illumina&  r  r  c                 C   Z   d dd tdD dd tdD  dd tdd	D  }t|d	ks'J t| ||S )
zFast Illumina 1.3+ FASTQ to Sanger FASTQ conversion (PRIVATE).

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S   r   r   r   r   r   r   r   r+   D  r   z8_fastq_illumina_convert_fastq_sanger.<locals>.<listcomp>r   c                 S      g | ]}t d | qS r   r   r(   r   r   r   r+   E      rY   c                 S   r   r   r   r   r   r   r   r+   F  r   r   r   r  r  r   r   r   $_fastq_illumina_convert_fastq_sanger:     	r  c                    ~   t d ddd tdD dd tdD   fddtd	d
D  dd td
dD  }t|dks7J t| || dS )a  Fast Sanger FASTQ to Illumina 1.3+ FASTQ conversion (PRIVATE).

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion. Will issue a warning if the scores had to be truncated at 62
    (maximum possible in the Illumina 1.3+ FASTQ format)
    r   rC   c                 S   r   r   r   r   r   r   r   r+   X  r   z8_fastq_sanger_convert_fastq_illumina.<locals>.<listcomp>r   c                 S   r	  r   r   r(   r   r   r   r+   Y  r  rY   c                       g | ]} qS r   r   r   
trunc_charr   r   r+   Z      `   r   c                 S   r   r   r   r   r   r   r   r+   [  r   r   z7Data loss - max PHRED quality 62 in Illumina 1.3+ FASTQr4   rQ   r   rm   r   r  r   r  r   $_fastq_sanger_convert_fastq_illuminaL  $   
r  c                 C   \   d dd tdD dd tddD  dd td	d
D  }t|d
ks(J t| ||S )zFast Solexa FASTQ to Sanger FASTQ conversion (PRIVATE).

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S   r   r   r   r   r   r   r   r+   q  r   z6_fastq_solexa_convert_fastq_sanger.<locals>.<listcomp>r  c                 S   $   g | ]}t d ttt| qS r
  r4   r=   r>   r%   r(   r   r   r   r+   r      r"   rY   c                 S   r   r   r   r   r   r   r   r+   v  r   r   r   r  r  r   r   r   "_fastq_solexa_convert_fastq_sangerg     	r  c                    r  )a  Fast Sanger FASTQ to Solexa FASTQ conversion (PRIVATE).

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion. Will issue a warning if the scores had to be truncated at 62
    (maximum possible in the Solexa FASTQ format)
    r   rC   c                 S   r   r   r   r   r   r   r   r+     r   z6_fastq_sanger_convert_fastq_solexa.<locals>.<listcomp>r   c                 S   r  r  r4   r=   r>   r    r(   r   r   r   r+        $ rY   c                    r  r   r   r   r  r   r   r+     r  r  r   c                 S   r   r   r   r   r   r   r   r+     r   r   ra   r  r  r   r  r   "_fastq_sanger_convert_fastq_solexa|  r  r   c                 C   r  )zFast Solexa FASTQ to Illumina 1.3+ FASTQ conversion (PRIVATE).

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S   r   r   r   r   r   r   r   r+     r   z8_fastq_solexa_convert_fastq_illumina.<locals>.<listcomp>r  c                 S   r  r  r  r(   r   r   r   r+     r  r"   rY   c                 S   r   r   r   r   r   r   r   r+     r   r   r   r  r  r   r   r   $_fastq_solexa_convert_fastq_illumina  r  r!  c                 C   r  )
zFast Illumina 1.3+ FASTQ to Solexa FASTQ conversion (PRIVATE).

    Avoids creating SeqRecord and Seq objects in order to speed up this
    conversion.
    rC   c                 S   r   r   r   r   r   r   r   r+     r   z8_fastq_illumina_convert_fastq_solexa.<locals>.<listcomp>r   c                 S   r  r  r  r(   r   r   r   r+     r  rY   c                 S   r   r   r   r   r   r   r   r+     r   r   r   r  r  r   r   r   $_fastq_illumina_convert_fastq_solexa  r  r"  c              	   C   s   d}t |d8}t| D ]*\}}}|d7 }|d| d tdt|dD ]}||||d  d  q&qW d   |S 1 sBw   Y  |S )a  Fast FASTQ to FASTA conversion (PRIVATE).

    Avoids dealing with the FASTQ quality encoding, and creating SeqRecord and
    Seq objects in order to speed up this conversion.

    NOTE - This does NOT check the characters used in the FASTQ quality string
    are valid!
    r   r   r   r   r   r   N)r   ru   r   r   rm   )r   r   r   r   r   r   r   r   r   r   r   _fastq_convert_fasta  s   

r#  c                 C   sr   d}t |d(}t| D ]\}}}|d7 }||ddd  d| d qW d   |S 1 s2w   Y  |S )a  Fast FASTQ to simple tabbed conversion (PRIVATE).

    Avoids dealing with the FASTQ quality encoding, and creating SeqRecord and
    Seq objects in order to speed up this conversion.

    NOTE - This does NOT check the characters used in the FASTQ quality string
    are valid!
    r   r   r   Nrh   r   )r   ru   r   r   )r   r   r   r   r   r   r   r   r   r   _fastq_convert_tab  s   
$
r$  c           
   
      sX  d}t |d}t| D ]\}}}|d7 }|d| d z fdd|D }W n ty5   tdd	w d
|}	t|	dkr|	d d
krY||	d	d d  |	dd	 }	n3|	d d
krq||	d	d d  |	dd	 }	n|	d d
ks{J d||	d	d d  |	dd	 }	t|	dksA||	d  qW d	   |S 1 sw   Y  |S )zFASTQ helper function for QUAL output (PRIVATE).

    Mapping should be a dictionary mapping expected ASCII characters from the
    FASTQ quality string to PHRED quality scores (as strings).
    r   r   r   r   r   c                    r   r   r   )r)   ascii_r   r   r   r+     r   z'_fastq_convert_qual.<locals>.<listcomp>r   Nrg   r   =   r  :   z"Internal logic failure in wrapping)r   ru   r   r.   r   rQ   rm   )
r   r   r   r   r   r   r   r   r   r   r   r&  r   _fastq_convert_qual  s8   


r)  c                 C      dd t dD }t| ||S )z/Fast Sanger FASTQ to QUAL conversion (PRIVATE).c                 S      i | ]}t |d  t|qS r
  r4   r   r(   r   r   r   r9   	      z._fastq_sanger_convert_qual.<locals>.<dictcomp>r;   r   r)  r  r   r   r   _fastq_sanger_convert_qual	  s   r/  c                 C   s    dd t ddD }t| ||S )z/Fast Solexa FASTQ to QUAL conversion (PRIVATE).c                 S   s*   i | ]}t |d  tttt|qS r  )r4   r   r=   r>   r%   r(   r   r   r   r9   	  s    z._fastq_solexa_convert_qual.<locals>.<dictcomp>r"   rY   r.  r  r   r   r   _fastq_solexa_convert_qual	  s   r0  c                 C   r*  )z6Fast Illumina 1.3+ FASTQ to QUAL conversion (PRIVATE).c                 S   r+  r  r,  r(   r   r   r   r9   	  r-  z0_fastq_illumina_convert_qual.<locals>.<dictcomp>rY   r.  r  r   r   r   _fastq_illumina_convert_qual	  s   r1  __main__)run_doctest)verboserE   )Tr   r#   mathr   typingr   r   collections.abcr   r   r   r   r	   Bior
   r   r   Bio.Filer   Bio.Seqr   Bio.SeqRecordr   
Interfacesr   r   r   r   r   r6   rX   floatr    r%   listr=   r0   r   rF   rM   r   rU   rZ   r]   r^   r`   rb   rc   tupleru   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r   r!  r"  r#  r$  r)  r/  r0  r1  r   
Bio._utilsr3  r   r   r   r   <module>   s    d^"0s@ @ %|
 $
7uKt$P9
m"
"











'	

