
:>"^Xh  ã            Ð   @   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 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 d d d d d d i Z i  Z 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/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA d dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ d` d da db dc dd de df dg dh di dj dk dl dm dn do dp dq d dr ds dt du dv dw dx dy dz d{ dz d| dz d} dz 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¨ d© dª d« d¬ d­ d® d¯ d° d± d² d³ d´ d dµ d¶ d· d¸ d¹ iX e dº <d» d¼ d½ d¾ d¿ dÀ dÁ dÂ dÃ dÄ dÅ dÆ i 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ô 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!d"d#d$d½ d%d¿ d&dÁ d'dÃ d(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUddVdWdXdYdZd[d\d]d^d_d`dadbdcddded¡ dfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~dd€dd‚dƒdd„d…d†d‡dˆd‰dŠd‹dŒddŽdih e d<d‘d’i 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Ÿi Z d¶Z d·Z g  Z x$ e j ƒ  D] Z e e j ƒ  7Z qYWd d¸d¹„ Z d dºd»d¼„ Z  d½d¾„  Z! d¿dÀ„  Z" dÁdÂ„  Z# dÃdÄ„  Z$ e% dÅk rÐd S(Æ  a  Bio.SeqIO parser for the ABI format.

ABI is the format used by Applied Biosystem's sequencing machines to store
sequencing results.

For more details on the format specification, visit:
http://www6.appliedbiosystems.com/support/software_community/ABIF_File_Format.pdf

é    N)Úbasename)ÚBiopythonParserWarning)ÚAlphabet)Úambiguous_dnaÚunambiguous_dna)Ú	as_handle)ÚSeq)Ú	SeqRecord)Ú_bytes_to_string)ÚzipZTUBE1Zsample_wellZDySN1ZdyeZGTyp1ZpolymerZMODL1Zmachine_modelZAPFN2z(Sequencing Analysis parameters file nameZAPXV1z$Analysis Protocol XML schema versionZAPrN1zAnalysis Protocol settings nameZAPrV1z"Analysis Protocol settings versionZAPrX1zAnalysis Protocol XML stringZCMNT1zSample CommentÚCTID1z*Container Identifier, a.k.a. plate barcodeZCTNM1zAContainer name, usually identical to CTID, but not necessarily soZCTTL1zComment TitleZCpEP1z`Capillary type electrophoresis. 1 for a capillary based machine. 0 for a slab gel based machine.ZDATA1zChannel 1 raw dataZDATA2zChannel 2 raw dataZDATA3zChannel 3 raw dataZDATA4zChannel 4 raw dataZDATA5z=Short Array holding measured volts/10 (EP voltage) during runZDATA6zDShort Array holding measured milliAmps trace (EP current) during runZDATA7zIShort Array holding measured milliWatts trace (Laser EP Power) during runZDATA8zTShort Array holding measured oven Temperature (polymer temperature) trace during runZDATA9zChannel 9 processed dataZDATA10zChannel 10 processed dataZDATA11zChannel 11 processed dataZDATA12zChannel 12 processed dataZDSam1zDownsampling factorzDye set namezDye#1zNumber of dyesZDyeN1z
Dye 1 nameZDyeN2z
Dye 2 nameZDyeN3z
Dye 3 nameZDyeN4z
Dye 4 nameZDyeW1zDye 1 wavelengthZDyeW2zDye 2 wavelengthZDyeW3zDye 3 wavelengthZDyeW4zDye 4 wavelengthZEPVt1z'Electrophoresis voltage setting (volts)ZEVNT1zStart Run eventZEVNT2zStop Run eventZEVNT3zStart Collection eventZEVNT4zStop Collection eventZFWO_1zLBase Order. Sequencing Analysis Filter wheel order. Fixed for 3500 at "GATC"zGel or polymer TypeZInSc1zInjection time (seconds)ZInVt1zInjection voltage (volts)ZLANE1zLane/CapillaryÚLIMS1zSample tracking IDZLNTD1zLength to detectorZLsrP1z!Laser Power setting (micro Watts)ZMCHN1z!Instrument name and serial numberZMODF1zData collection module filezModel numberZNAVG1zPixels averaged per laneZNLNE1zNumber of capillariesZOfSc1zAList of scans that are marked off scale in Collection. (optional)ZOvrI1zýList of scan number indexes that have values greater than 32767 but did not saturate the camera. In Genemapper samples, this can have indexes with values greater than 32000. In sequencing samples, this cannot have indexes with values greater than 32000.ZOvrI2ZOvrI3ZOvrI4ZOvrV1z’List of color data values found at the locations listed in the OvrI tag. There must be exactly as many numbers in this array as in the OvrI array.ZOvrV2ZOvrV3ZOvrV4ZPDMF1z;Sequencing Analysis Mobility file name chosen in collectionZRMXV1zRun Module XML schema versionZRMdN1zRun Module name (same as MODF)ZRMdX1zRun Module XML stringZRPrN1zRun Protocol nameZRPrV1zRun Protocol versionÚRUND1zRun Started DateÚRUND2zRun Stopped DateZRUND3zData Collection Started DateZRUND4zData Collection Stopped dateÚRUNT1zRun Started TimeÚRUNT2zRun Stopped TimeZRUNT3zData Collection Started TimeZRUNT4zData Collection Stopped TimeZRate1z&Scanning Rate. Milliseconds per frame.ZRunN1zRun NameZSCAN1zNumber of scansZSMED1zPolymer lot expiration dateZSMLt1zPolymer lot numberÚSMPL1zSample nameZSVER1z Data collection software versionZSVER3z Data collection firmware versionZSatd1zyArray of longs representing the scan numbers of data points, which are flagged as saturated by data collection (optional)ZScal1z Rescaling divisor for color dataZScan1z#Number of scans (legacy - use SCAN)zWell IDZTmpr1zRun temperature settingZUser1z-Name of user who created the plate (optional)ZgeneralZCTOw1zContainer ownerZHCFG1zInstrument ClassZHCFG2zInstrument FamilyZHCFG3zOfficial Instrument NameZHCFG4zInstrument ParametersZRMdVa1zRun Module versionzabi_3130/3130xlZAAct1z˜Primary Analysis Audit Active indication. True if system auditing was enabled during the last write of this file, false if system auditing was disabled.ZABED1zŠAnode buffer expiration date using ISO 8601 format using the patterns YYYY-MM-DDTHH:MM:SS.ss+/-HH:MM. Hundredths of a second are optional.ZABID1z&Anode buffer tray first installed dateZABLt1zAnode buffer lot numberZABRn1zcNumber of runs (injections) processed with the current Anode Buffer (runs allowed - runs remaining)ZABTp1zAnode buffer typeZAEPt1z?Analysis Ending scan number for basecalling on initial analysisZAEPt2z<Analysis Ending scan number for basecalling on last analysisZAPCN1zAmplicon nameZARTN1z?Analysis Return code. Produced only by 5 Prime basecaller 1.0b3ZASPF1z:Flag to indicate whether adaptive processing worked or notZASPt1z0Analysis Starting scan number for first analysisZASPt2z/Analysis Starting scan number for last analysisZAUDT2z.Audit log used across 3500 software (optional)ZAVld1z%Assay validation flag (true or false)ZAmbT1z&Record of ambient temperature readingsZAsyC1zThe assay contents (xml format)ZAsyN1zThe assay nameZAsyV1zThe assay versionZB1Pt1zHReference scan number for mobility and spacing curves for first analysisZB1Pt2zGReference scan number for mobility and spacing curves for last analysisZBCTS1z@Basecaller timestamp. Time of completion of most recent analysisZBcRn1zBasecalling qc codeZBcRs1z;Basecalling warnings, a concatenated comma separated stringZBcRs2z9Basecalling errors, a concatenated comma separated stringZCAED1zCapillary array expirationZCALt1zCapillary array lot numberZCARn1znNumber of injections processed (including the one of which this sample was a part) through the capillary arrayZCASN1zCapillary array serial numberZCBED1zCathode buffer expiration dateZCBID1z(Cathode buffer tray first installed dateZCBLt1zCathode buffer lot numberZCBRn1zeNumber of runs (injections) processed with the current Cathode Buffer (runs allowed - runs remaining)ZCBTp1zCathode buffer typeZCLRG1z%Start of the clear range (inclusive).ZCLRG2zClear range lengthZCRLn1zContiguous read lengthZCRLn2z!One of "Pass", "Fail", or "Check"z=The name entered as the Owner of a plate, in the plate editorZCkSm1zFile checksumZDCEv1zrA list of door-close events, separated by semicolon. Door open events are generally paired with door close events.ZDCHT1zzReserved for backward compatibility. The detection cell heater temperature setting from the Run Module. Not used for 3500.ZDOEv1zqA list of door-open events, separated by semicolon. Door close events are generally paired with door open events.ZESig2z5Electronic signature record used across 3500 softwareZFTab1z9Feature table. Can be created by Nibbler for Clear Range.ZFVoc1zDFeature table vocabulary. Can be created by Nibbler for Clear Range.ZFeat1z4Features. Can be created by Nibbler for Clear Range.zHThe Instrument Class. All upper case, no spaces. Initial valid value: CEzeThe Instrument Family. All upper case, no spaces. Valid values: 31XX or 37XX for UDC, 35XX (for 3500)z‡The official instrument name. Mixed case, minus any special formatting. Initial valid values: 3130, 3130xl, 3730, 3730xl, 3500, 3500xl.a  Instrument parameters. Contains key-value pairs of instrument configuration information, separated by semicolons. Four parameters are included initially: UnitID=<UNITD number>, CPUBoard=<board type>, ArraySize=<# of capillaries>, SerialNumber=<Instrument Serial#>.ZInjN1zInjection nameZLAST1zParameter settings informationZNOIS1zªThe estimate of rms baseline noise (S/N ratio) for each dye for a successfully analyzed sample. Corresponds in order to the raw data in tags DATA 1-4. KB basecaller only.ZP1AM1zkAmplitude of primary peak, which is not necessarily equal to corresponding signal strength at that positionZP1RL1zODeviation of primary peak position from (PLoc,2), times 100, rounded to integerZP1WD1z¡Full-width Half-max of primary peak, times 100, rounded to integer. Corresponding signal intensity is not necessarily equal to one half of primary peak amplitudeZP2AM1zmAmplitude of secondary peak, which is not necessarily equal to corresponding signal strength at that positionZP2BA1zBase of secondary peakZP2RL1zQDeviation of secondary peak position from (PLoc,2), times 100, rounded to integerÚPBAS1z+Array of sequence characters edited by userÚPBAS2z4Array of sequence characters as called by BasecallerZPCON1z1Array of quality Values (0-255) as edited by userÚPCON2z7Array of quality values (0-255) as called by BasecallerZPDMF2zFMobility file name chosen in most recent analysis (identical to PDMF1)ZPLOC1z&Array of peak locations edited by userZPLOC2z/Array of peak locations as called by BasecallerZPRJT1z"SeqScape 2.0 project template nameZPROJ4zSeqScape 2.0 project nameZPSZE1z]Plate size. The number of sample positions in the container. Current allowed values: 96, 384.ZPTYP1z6Plate type. Current allowed values: 96-Well, 384-Well.ZPuSc1zMedian pupscoreZQV201zQV20+ valueZQV202ZQcPa1zQC parametersZQcRn1zTrimming and QC codeZQcRs1z2QC warnings, a concatenated comma separated stringZQcRs2z0QC errors, a concatenated comma separated stringZRGOw1zƒThe name entered as the Owner of a Results Group, in the Results Group Editor. Implemented as the user name from the results group.ZRInj1zpReinjection number. The reinjection number that this sample belongs to. Not present if there was no reinjection.ZRNmF1zRaman normalization factorZRevC1z.for whether the sequence has been complementedz<Run name (which, for 3500, is different from injection name)zS/N%1zSignal strength for each dyeZSMID1zPolymer first installed dateZSMRn1z^Number of runs (injections) processed with the current polymer (runs allowed - runs remaining)ZSPAC1z*Average peak spacing used in last analysisZSPAC2z2Basecaller name - corresponds to name of bcp file.ZSPAC3z7Average peak spacing last calculated by the Basecaller.ZSPEC1z!Sequencing Analysis Specimen NameZSVER2zBasecaller version numberZSVER4z!Sample File Format Version StringZScPa1z#The parameter string of size callerZScSt1z8Raw data start point. Set to 0 for 3500 data collection.ZSpeN1z Active spectral calibration nameZTrPa1zTimming parametersZTrSc1zTrace score.ZTrSc2ZphAR1zTrace peak aria ratioZphCH1zFChemistry type ("term", "prim", "unknown"), based on DYE_1 informationZphDY1z?Dye ("big", "d-rhod", "unknown"), based on mob file informationZphQL1zMaximum Quality ValueZphTR1zSet Trim regionZphTR2zTrim probabilityzabi_3530/3530xlZBufT1z*Buffer tray heater temperature (degrees C)zabi_3730/3730xlé   Úbé   Úsé   ÚHé   Úhé   Úié   Z2ié   Úfé   Údé
   Zh2Bé   Z4Bé   Z2i2bé   ÚBé   Z2hé   Z4hé   é   Z4ié   é   é   z	>H4sI2H3Iz>4sI2H4Ic             C   sM   |  d k r | Sy t  |  ƒ SWn( t k
 rH |  j d t j ƒ  ƒ SYn Xd S)zŒReturn the string value of the given an optional raw bytes tag value.

    If the bytes value is None, return the given default value.

    NÚencoding)r
   ÚUnicodeDecodeErrorÚdecodeÚsysÚgetdefaultencoding)Zopt_bytes_valueÚdefault© r7   ú4/tmp/pip-build-ww9dw3qa/biopython/Bio/SeqIO/AbiIO.pyÚ_get_string_tagT  s    r9   Fc             #   s  | d k	 rZ t  t j | ƒ t j ƒ r3 t d ƒ ‚ t  t j | ƒ t j ƒ rZ t d ƒ ‚ t |  d ƒ r– t d ƒ t |  j j	 ƒ  ƒ k r– t d ƒ ‚ t
 |  d ƒ \}  |  j d ƒ |  j d ƒ } | sÖ t d	 ƒ ‚ | d
 k rò t d | ƒ ‚ d d d d d d d d i } t t t j ƒ  d g t t ƒ ƒ ƒ } t j t |  j t j t ƒ ƒ ƒ } d } i  ‰  xt | |  ƒ D]ð \ } }	 }
 | t |	 ƒ } |
 ˆ  | <| d k ròt |
 ƒ } d } | d k rht | ƒ j | ƒ rét } qht } qx| d k rd d „  t |
 ƒ Dƒ } qx| d k r5t |
 ƒ } qx| | k rN|
 | | <qx| t k rx|
 | t | <qxWd | d | d f | d <d | d | d f | d <ˆ  | d <t ‡  f d d †  d* Dƒ ƒ } | rjy t |  j  ƒ j! d d ƒ } Wn t" k
 rd } Yn Xt ˆ  j# d  ƒ | ƒ } t ˆ  j# d! ƒ d" ƒ } t$ t% d ƒ d# | d$ | d% | d& | ƒ} ns y t |  j  ƒ j! d' d ƒ } Wn t" k
 r£d } Yn Xt$ t% | | ƒ d# | d$ | d% d d& | d( d) | i ƒ} | sê| rò| Vn t& | ƒ VWd QRXd S)+z+Return an iterator for the Abi file format.Nz1Invalid alphabet, ABI files do not hold proteins.z,Invalid alphabet, ABI files do not hold RNA.ÚmodeÚrbz(ABI files has to be opened in 'rb' mode.r   r   zEmpty file.s   ABIFzFile should start ABIF, not %rr   Ú r   r   r   z<unknown id>r   ZKYWMRSr   c             S   s   g  |  ] } t  | ƒ ‘ q Sr7   )Úord)Ú.0Úvalr7   r7   r8   ú
<listcomp>  s   	 zAbiIterator.<locals>.<listcomp>r   z%s %sZ	run_startZ
run_finishZabif_rawc             3   s   |  ] } | ˆ  k Vq d  S)Nr7   )r>   Útn)Úrawr7   r8   ú	<genexpr>°  s    zAbiIterator.<locals>.<genexpr>r   z.fsar   r   z<unknown description>ÚidÚnameÚdescriptionÚannotationsz.ab1Úletter_annotationsÚphred_quality)r   r   )'Ú
isinstancer   Z_get_base_alphabetZProteinAlphabetÚ
ValueErrorZRNAAlphabetÚhasattrÚsetr:   Úlowerr   ÚseekÚreadÚIOErrorÚdictr   Ú_EXTRACTÚvaluesÚlenÚstructÚunpackÚ_HEADFMTÚcalcsizeÚ_abi_parse_headerÚstrr
   Úintersectionr   r   r9   Úallr   rE   ÚreplaceÚAttributeErrorÚgetr	   r   Ú	_abi_trim)ÚhandleZalphabetÚtrimÚmarkerÚtimesZannotÚheaderZ	sample_idÚtag_nameÚ
tag_numberZtag_dataÚkeyÚseqZambigsÚqualZis_fsa_fileÚ	file_namerF   Úrecordr7   )rB   r8   ÚAbiIteratore  sŠ    !($
		
rn   c             C   s   t  |  d d ƒS)z[Return an iterator for the Abi file format that yields trimmed SeqRecord objects (PRIVATE).rc   T)rn   )rb   r7   r7   r8   Ú_AbiTrimIterator×  s    ro   c             c   sG  |  d } |  d } |  d } d } x| | k  rB| | | } | j  | ƒ t j t | j t j t ƒ ƒ ƒ | f } | d 7} t | d ƒ } | t | d ƒ 7} t | d ƒ }	 | d }
 | d } | d } | d } | d } | d } | d k r	| d	 } | j  | ƒ | j | ƒ } |	 |
 t | | | ƒ f Vq' Wd
 S)z$Return directory contents (PRIVATE).r   r   r!   r   r   r   r    r#   r0   N)	rO   rV   rW   Ú_DIRFMTrP   rY   r
   r[   Ú_parse_tag_data)rf   rb   Zhead_elem_sizeZhead_elem_numZhead_offsetÚindexÚstartZ	dir_entryri   rg   rh   Ú	elem_codeÚelem_numZ	data_sizeZdata_offsetZ
tag_offsetÚdatar7   r7   r8   rZ   Ü  s0    


!








rZ   c       	         së   d } d } d } d ‰  t  |  ƒ | k r. |  S‡  f d d †  |  j d Dƒ } d g } xj t d t  | ƒ ƒ D]S } | d | | } | d k  r¡ | j d ƒ qm | j | ƒ | sm | } d	 } qm W| j t | ƒ ƒ } |  | | … Sd
 S)a   Trims the sequence using Richard Mott's modified trimming algorithm (PRIVATE).

    Arguments:
        - seq_record - SeqRecord object to be trimmed.

    Trimmed bases are determined from their segment score, which is a
    cumulative sum of each base's score. Base scores are calculated from
    their quality values.

    More about the trimming algorithm:
    http://www.phrap.org/phredphrap/phred.html
    http://resources.qiagenbioinformatics.com/manuals/clcgenomicsworkbench/650/Quality_trimming.html
    Fr0   r   gš™™™™™©?c                s"   g  |  ] } ˆ  d  | d ‘ q S)r%   g      $@g      $Àr7   )r>   rk   )Úcutoffr7   r8   r@     s   	z_abi_trim.<locals>.<listcomp>rI   r   TNéÿÿÿÿ)rU   rH   ÚrangeÚappendrr   Úmax)	Z
seq_recordrs   ZsegmentZ
trim_startZ
score_listZcummul_scorer   ÚscoreZtrim_finishr7   )rw   r8   ra     s&    	
ra   c             C   sK  |  t  k rC| d k r! d } n t | ƒ } d | t  |  } t | ƒ t j | ƒ k s` t ‚ t j | | ƒ } |  d k rš t | ƒ d k rš | d } |  d k rª | S|  d k rÉ t t j | Œ  ƒ S|  d k rò t t j	 | d d	 … Œ  ƒ S|  d
 k rt
 | ƒ S|  d k r"| d d … S|  d k r<| d d … S| Sn d Sd S)zÍReturn single data value (PRIVATE).

    Arguments:
     - elem_code - What kind of data
     - elem_num - How many data points
     - raw_data - abi file object from which the tags would be unpacked

    r   r<   ú>r%   r&   r   r   Nr   r(   r.   r/   )r%   r&   rx   )Ú_BYTEFMTr[   rU   rV   rY   ÚAssertionErrorrW   ÚdatetimeÚdateÚtimeÚbool)rt   ru   Zraw_dataÚnumÚfmtrv   r7   r7   r8   rq   7  s.    		!

rq   Ú__main__)&Ú__doc__r€   rV   r4   ÚwarningsÚos.pathr   ZBior   r   ZBio.Alphabet.IUPACr   r   ZBio.Filer   ZBio.Seqr   ZBio.SeqRecordr	   Z	Bio._py3kr
   r   rS   Z_INSTRUMENT_SPECIFIC_TAGSr~   rX   rp   Z__global_tag_listingrT   ÚtagÚkeysr9   rn   ro   rZ   ra   rq   Ú__name__r7   r7   r7   r8   Ú<module>   sò  	r(3,