o
    Rŀge                     @   s  U d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	d
ddZi Zi 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 d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPi dQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdri dsdtdudvdwdvdxdvdydvdzd{d|d{d}d{d~d{ddddddddddddddddi dddddddddddddddddddddddddddddddddddddded< ddddddded< i ddddddÓddœddǓddɓdd˓dd͓ddϓddѓddӓddՓddדddٓddۓddݓddߓi ddddddddddddddddddddddddddddddddd di 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 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i dFdGdHdIdJdKdLdMdNdOdPd	dQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`ddadbdcdddei 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ed< ddied< i 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ZdZdZg Zee ed< e D ]	Zee 7 ZqdddZG dd deZdd Zdd Zdd Zdd Zedkr	 dS 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)Seq)	SeqRecord   )SequenceIteratorsample_welldyepolymermachine_model)TUBE1DySN1GTyp1MODL1APFN2z(Sequencing Analysis parameters file nameAPXV1z$Analysis Protocol XML schema versionAPrN1zAnalysis Protocol settings nameAPrV1z"Analysis Protocol settings versionAPrX1zAnalysis Protocol XML stringCMNT1zSample CommentCTID1z*Container Identifier, a.k.a. plate barcodeCTNM1zAContainer name, usually identical to CTID, but not necessarily soCTTL1zComment TitleCpEP1z`Capillary type electrophoresis. 1 for a capillary based machine. 0 for a slab gel based machine.DATA1zChannel 1 raw dataDATA2zChannel 2 raw dataDATA3zChannel 3 raw dataDATA4zChannel 4 raw dataDATA5z=Short Array holding measured volts/10 (EP voltage) during runDATA6zDShort Array holding measured milliAmps trace (EP current) during runDATA7zIShort Array holding measured milliWatts trace (Laser EP Power) during runDATA8zTShort Array holding measured oven Temperature (polymer temperature) trace during runDATA9zChannel 9 processed dataDATA10zChannel 10 processed dataDATA11zChannel 11 processed dataDATA12zChannel 12 processed dataDSam1zDownsampling factorr   zDye set namezDye#1zNumber of dyesDyeN1z
Dye 1 nameDyeN2z
Dye 2 nameDyeN3z
Dye 3 nameDyeN4z
Dye 4 nameDyeW1zDye 1 wavelengthDyeW2zDye 2 wavelengthDyeW3zDye 3 wavelengthDyeW4zDye 4 wavelengthEPVt1z'Electrophoresis voltage setting (volts)EVNT1zStart Run eventEVNT2zStop Run eventEVNT3zStart Collection eventEVNT4zStop Collection eventFWO_1zLBase Order. Sequencing Analysis Filter wheel order. Fixed for 3500 at "GATC"r   zGel or polymer TypeInSc1zInjection time (seconds)InVt1zInjection voltage (volts)LANE1zLane/CapillaryLIMS1zSample tracking IDLNTD1zLength to detectorLsrP1z!Laser Power setting (micro Watts)MCHN1z!Instrument name and serial numberMODF1zData collection module filer   zModel numberNAVG1zPixels averaged per laneNLNE1zNumber of capillariesOfSc1zAList of scans that are marked off scale in Collection. (optional)OvrI1zList 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.OvrI2OvrI3OvrI4OvrV1zList 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.OvrV2OvrV3OvrV4PDMF1z;Sequencing Analysis Mobility file name chosen in collectionRMXV1zRun Module XML schema versionRMdN1zRun Module name (same as MODF)RMdX1zRun Module XML stringRPrN1zRun Protocol nameRPrV1zRun Protocol versionRUND1zRun Started DateRUND2zRun Stopped DateRUND3zData Collection Started DateRUND4zData Collection Stopped dateRUNT1zRun Started TimeRUNT2zRun Stopped TimeRUNT3zData Collection Started TimeRUNT4zData Collection Stopped TimeRate1z&Scanning Rate. Milliseconds per frame.RunN1zRun NameSCAN1zNumber of scansSMED1zPolymer lot expiration dateSMLt1zPolymer lot numberSMPL1zSample nameSVER1z Data collection software versionSVER3z Data collection firmware versionSatd1zyArray of longs representing the scan numbers of data points, which are flagged as saturated by data collection (optional)Scal1z Rescaling divisor for color dataScan1z#Number of scans (legacy - use SCAN)zWell IDzRun temperature settingz-Name of user who created the plate (optional))r   Tmpr1User1generalzContainer ownerzInstrument ClasszInstrument FamilyzOfficial Instrument NamezInstrument ParameterszRun Module version)CTOw1HCFG1HCFG2HCFG3HCFG4RMdVa1zabi_3130/3130xlAAct1zPrimary Analysis Audit Active indication. True if system auditing was enabled during the last write of this file, false if system auditing was disabled.ABED1zAnode buffer expiration date using ISO 8601 format using the patterns YYYY-MM-DDTHH:MM:SS.ss+/-HH:MM. Hundredths of a second are optional.ABID1z&Anode buffer tray first installed dateABLt1zAnode buffer lot numberABRn1zcNumber of runs (injections) processed with the current Anode Buffer (runs allowed - runs remaining)ABTp1zAnode buffer typeAEPt1z?Analysis Ending scan number for basecalling on initial analysisAEPt2z<Analysis Ending scan number for basecalling on last analysisAPCN1zAmplicon nameARTN1z?Analysis Return code. Produced only by 5 Prime basecaller 1.0b3ASPF1z:Flag to indicate whether adaptive processing worked or notASPt1z0Analysis Starting scan number for first analysisASPt2z/Analysis Starting scan number for last analysisAUDT2z.Audit log used across 3500 software (optional)AVld1z%Assay validation flag (true or false)AmbT1z&Record of ambient temperature readingsAsyC1zThe assay contents (xml format)AsyN1zThe assay nameAsyV1zThe assay versionB1Pt1zHReference scan number for mobility and spacing curves for first analysisB1Pt2zGReference scan number for mobility and spacing curves for last analysisBCTS1z@Basecaller timestamp. Time of completion of most recent analysisBcRn1zBasecalling qc codeBcRs1z;Basecalling warnings, a concatenated comma separated stringBcRs2z9Basecalling errors, a concatenated comma separated stringCAED1zCapillary array expirationCALt1zCapillary array lot numberCARn1znNumber of injections processed (including the one of which this sample was a part) through the capillary arrayCASN1zCapillary array serial numberCBED1zCathode buffer expiration dateCBID1z(Cathode buffer tray first installed dateCBLt1zCathode buffer lot numberCBRn1zeNumber of runs (injections) processed with the current Cathode Buffer (runs allowed - runs remaining)CBTp1zCathode buffer typeCLRG1z%Start of the clear range (inclusive).CLRG2zClear range lengthCRLn1zContiguous read lengthCRLn2z!One of "Pass", "Fail", or "Check"rc   z=The name entered as the Owner of a plate, in the plate editorCkSm1zFile checksumDCEv1zrA list of door-close events, separated by semicolon. Door open events are generally paired with door close events.DCHT1zzReserved for backward compatibility. The detection cell heater temperature setting from the Run Module. Not used for 3500.DOEv1zqA list of door-open events, separated by semicolon. Door close events are generally paired with door open events.ESig2z5Electronic signature record used across 3500 softwareFTab1z9Feature table. Can be created by Nibbler for Clear Range.FVoc1zDFeature table vocabulary. Can be created by Nibbler for Clear Range.Feat1z4Features. Can be created by Nibbler for Clear Range.rd   zHThe Instrument Class. All upper case, no spaces. Initial valid value: CEre   zeThe Instrument Family. All upper case, no spaces. Valid values: 31XX or 37XX for UDC, 35XX (for 3500)rf   zThe official instrument name. Mixed case, minus any special formatting. Initial valid values: 3130, 3130xl, 3730, 3730xl, 3500, 3500xl.rg   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#>.InjN1zInjection nameLAST1zParameter settings informationNOIS1zThe 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.P1AM1zkAmplitude of primary peak, which is not necessarily equal to corresponding signal strength at that positionP1RL1zODeviation of primary peak position from (PLoc,2), times 100, rounded to integerP1WD1zFull-width Half-max of primary peak, times 100, rounded to integer. Corresponding signal intensity is not necessarily equal to one half of primary peak amplitudeP2AM1zmAmplitude of secondary peak, which is not necessarily equal to corresponding signal strength at that positionP2BA1zBase of secondary peak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 BasecallerPCON1z1Array of quality Values (0-255) as edited by userPCON2z7Array of quality values (0-255) as called by BasecallerPDMF2zFMobility file name chosen in most recent analysis (identical to PDMF1)PLOC1z&Array of peak locations edited by userPLOC2z/Array of peak locations as called by BasecallerPRJT1z"SeqScape 2.0 project template namePROJ4zSeqScape 2.0 project namePSZE1z]Plate size. The number of sample positions in the container. Current allowed values: 96, 384.PTYP1z6Plate type. Current allowed values: 96-Well, 384-Well.PuSc1zMedian pupscoreQV201zQV20+ valueQV202QcPa1zQC parametersQcRn1zTrimming and QC codeQcRs1z2QC warnings, a concatenated comma separated stringQcRs2z0QC errors, a concatenated comma separated stringRGOw1zThe name entered as the Owner of a Results Group, in the Results Group Editor. Implemented as the user name from the results group.RInj1zpReinjection number. The reinjection number that this sample belongs to. Not present if there was no reinjection.RNmF1zRaman normalization factor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 dyeSMID1zPolymer first installed dateSMRn1z^Number of runs (injections) processed with the current polymer (runs allowed - runs remaining)SPAC1z*Average peak spacing used in last analysisSPAC2z2Basecaller name - corresponds to name of bcp file.SPAC3z7Average peak spacing last calculated by the Basecaller.SPEC1z!Sequencing Analysis Specimen NameSVER2zBasecaller version numberSVER4z!Sample File Format Version StringScPa1z#The parameter string of size callerScSt1z8Raw data start point. Set to 0 for 3500 data collection.SpeN1z Active spectral calibration nameTrPa1zTrimming parametersTrSc1zTrace score.TrSc2phAR1zTrace peak aria ratiophCH1zFChemistry type ("term", "prim", "unknown"), based on DYE_1 informationphDY1z?Dye ("big", "d-rhod", "unknown"), based on mob file informationphQL1zMaximum Quality ValuezSet Trim regionzTrim probability)phTR1phTR2zabi_3530/3530xlBufT1z*Buffer tray heater temperature (degrees C)zabi_3730/3730xlb   s   H   h   i   2i   f   d
   h2B   4B   2i2b   B   2h   4h      4i   )      z	>H4sI2H3Iz>4sI2H4I__global_tag_listingc                 C   s:   | du r|S z|   W S  ty   | j t d Y S w )zReturn 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)decodeUnicodeDecodeErrorsysgetdefaultencoding)opt_bytes_valuedefault r   C/var/www/html/myenv/lib/python3.10/site-packages/Bio/SeqIO/AbiIO.py_get_string_tagL  s   
r   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
AbiIteratorzParser for Abi files.Fc                    s   || _ t j|ddd dS )z+Return an iterator for the Abi file format.r   ABI)modefmtN)trimsuper__init__)selfsourcer   	__class__r   r   r   ]  s   zAbiIterator.__init__c                 C   s:   | d}|std|dkrtd|| |}|S )z9Start parsing the file, and return a SeqRecord generator.r   zEmpty file.s   ABIFzFile should start ABIF, not )read
ValueErrorOSErroriterate)r   handlemarkerrecordsr   r   r   parseb  s   

zAbiIterator.parsec                 #   s:   ddddd}t tt dgtt }tt|t	t}d}i  d }}t
||D ]B\}}	}
|t|	 }|
 |< |dkrG|
 }q/|dkrUdd |
 D }q/|d	kr^t|
}q/||v rg|
||< q/|tv rq|
|t| < q/|d
  d|d  |d< |d  d|d  |d<  |d< t fdddD }|rzt|jdd}W n ty   d}Y nw t d|}t dd}ttd||||d}n"zt|jdd}W n ty   d}Y nw tt|||d|d}|r||jd< n|s|s| jrtd| jr|st|}d|jd< |V  dS )z.Parse the file and generate SeqRecord objects. )rM   rN   rQ   rR   Nz<unknown id>r   r   c                 S   s   g | ]}t |qS r   )ord).0valr   r   r   
<listcomp>  s    z'AbiIterator.iterate.<locals>.<listcomp>rZ   rM    rQ   	run_startrN   rR   
run_finishabif_rawc                 3   s    | ]}| vV  qd S Nr   )r  tnrawr   r   	<genexpr>  s    z&AbiIterator.iterate.<locals>.<genexpr>)r   r   z.fsar7   r   z<unknown description>)idnamedescriptionannotationsz.ab1phred_qualityzGThe 'abi-trim' format can not be used for files without quality values.DNAmolecule_type)dictzip_EXTRACTvalueslenstructunpack_HEADFMTr  calcsize_abi_parse_headerstrr   r   allr   r  replaceAttributeErrorgetr   r   letter_annotationsr   r  	_abi_trimr  )r   r  timesannotheader	sample_idseqqualtag_name
tag_numbertag_datakeyis_fsa_file	file_namer  recordr   r  r   r  o  s|   





zAbiIterator.iterate)F)__name__
__module____qualname____doc__r   r	  r  __classcell__r   r   r   r   r   Z  s
    r   c                 C   s   t | ddS )z[Return an iterator for the Abi file format that yields trimmed SeqRecord objects (PRIVATE).T)r   )r   )r  r   r   r   _AbiTrimIterator  s   rB  c                 c   s   | d }| d }| d }d}||k r|||  }| | tt|tt|f }|d7 }|d  }|t|d 7 }|d  }	|d }
|d }|d }|d }|d }|d }|dkre|d	 }| | ||}|	|
t|||fV  ||k sd
S d
S )z$Return directory contents (PRIVATE).r   r   r   r   r   r   r   r   r   N)	seekr$  r%  _DIRFMTr  r'  r   r)  _parse_tag_data)r2  r  head_elem_sizehead_elem_numhead_offsetindexstart	dir_entryr9  r6  r7  	elem_codeelem_num	data_sizedata_offset
tag_offsetdatar   r   r   r(    s6   


r(  c           	         s   d}d}d}d t | |kr| S  fdd| jd D }dg}tdt |D ]}|d	 ||  }|dk r:|d q&|| |sE|}d
}q&|t|}| || 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
    Fr   r   g?c                    s   g | ]
} d |d   qS )r   g      $r   )r  r5  cutoffr   r   r    s    z_abi_trim.<locals>.<listcomp>r  r   T)r#  r.  rangeappendrI  max)	
seq_recordrJ  segment
trim_start
score_listcummul_scorer   scoretrim_finishr   rR  r   r/    s*   
	
r/  c                 C   s   | t v rt|dkrd}nt|}d| t |   }t|t|ks"J t||}| dvr6t|dkr6|d }| dkr<|S | dkrGttj| S | dkrVttj|d	d
  S | dkr^t	|S | dkrh|dd	 S | dkrr|d	d S |S d	S )zReturn 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   r   r   Nr   r   r   r   rT  )
_BYTEFMTr)  r#  r$  r'  r%  datetimedatetimebool)rL  rM  raw_datanumr   rQ  r   r   r   rE  ,  s.   	rE  __main__r  )r@  ra  r$  r   os.pathr   Bio.Seqr   Bio.SeqRecordr   
Interfacesr   r!  _INSTRUMENT_SPECIFIC_TAGSr`  r&  rD  r   listr)  __annotations__r"  tagkeysr   r   rB  r(  r/  rE  r=  r   r   r   r   <module>   s  
		
 !"#$'()*+,-./0123456789<@DHLNPRTUVWXYZ[\]^_`abcdefghijklmw
		
 !"#$%&'()*+,-./01234789;<=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklq
	

r(3,